Listening for 3270 Print Jobs

The following example shows how you can use the service manager to enable your plugin to listen for 3270 print job events. These changes are made to the RCP application plugin class:
  1. Implement ServiceManagerListener, ISessionServiceListener, and IPrintJobManagerChangeListener
  2. Implement the ServiceManagerListener method public void serviceChanged(ServiceManagerEvent event). This method is called when the service manager changes. In this example, it is used to determine when a new session has been created or destroyed. When a new session is created, add yourself as a listener. When a session is destroyed, remove yourself as a listener.
  3. Implement the ISessionServiceListener methods
    • public void sessionStateChanged(StateChangeEvent event) - This method is called when the state of the session changes. Use this method to find the sessions print job manager and add yourself as a listener to it.
    • public void aboutToProcessCommand(CommandEvent event) - This method is left empty for this example.
    • public void afterProcessCommand(CommandEvent event) - This method is left empty for this example.
  4. Implement the IPrintJobManagerChangeListeners
    • public void addPrintJob(PrintJobManager printJobManager, PrintJob printJob) This method is called when a print job has been added to the print job manager.
    • public void removePrintJob(PrintJobManager printJobManager, PrintJob printJob) This method is called when a print job is removed from the print job manger (deleted).
    • public void updatePrintJob(PrintJobManager printJobManager, PrintJob printJob) This method is called when a print job is updated.
  5. Add yourself as a service manager listener in the start() method of your application plug-in.
  6. Remove yourself as a service manager listener in the stop() method of your application plug-in.
package printExample;

import java.util.HashSet;

import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;

import com.ibm.hats.rcp.runtime.RcpRuntimePlugin;
import com.ibm.hats.rcp.ui.AbstractRcpApplicationPlugin;
import com.ibm.hats.runtime.ApplicationSpecificInfo;
import com.ibm.hats.runtime.ClientSpecificInfo;
import com.ibm.hats.runtime.IPrintJobManagerChangeListener;
import com.ibm.hats.runtime.PrintJob;
import com.ibm.hats.runtime.PrintJobManager;
import com.ibm.hats.runtime.PrintResourceHandler;
import com.ibm.hats.runtime.PrintSpecificInfo;
import com.ibm.hats.runtime.events.CommandEvent;
import com.ibm.hats.runtime.events.ISessionServiceListener;
import com.ibm.hats.runtime.events.ServiceManagerEvent;
import com.ibm.hats.runtime.events.ServiceManagerListener;
import com.ibm.hats.runtime.events.StateChangeEvent;
import com.ibm.hats.runtime.services.IService;
import com.ibm.hats.runtime.services.IServiceManager;
import com.ibm.hats.runtime.services.ISessionService;
import com.ibm.hats.util.StateType;

/**
 * The activator class controls the plug-in life cycle
 */
public class PrintExamplePlugin extends AbstractRcpApplicationPlugin implements 
                               ServiceManagerListener, ISessionServiceListener, 
                               IPrintJobManagerChangeListener
{
    // The plug-in ID
    public static final String PLUGIN_ID = "PrintExample";

    // The shared instance
    private static PrintExamplePlugin plugin;
    
    // The service manager
    private IServiceManager serviceManager = null;
    
    // The print managers we are listeners for
    private HashSet printJobManagers = new HashSet();

    /**
     * The constructor
     */
    public PrintExamplePlugin()
    {
        plugin = this;
        
        System.out.println("PrintExamplePlugin: ctor");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractRcpApplicationPlugin#
     *                  start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception
    {
        super.start(context);
        
        // Add the service manager listener
        serviceManager = RcpRuntimePlugin.getDefault().getServiceManager();
        serviceManager.addServiceManagerListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractRcpApplicationPlugin#stop(org.osgi.
     *                                               framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception
    {
        plugin = null;
        
        // Remove the service manager listener
        if (serviceManager != null)
            serviceManager.removeServiceManagerListener(this);
        
        super.stop(context);
        
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static PrintExamplePlugin getDefault()
    {
        return plugin;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path
     * 
     * @param path
     *            the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path)
    {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *         addPrintJob(com.ibm.hats.runtime.PrintJobManager,
     *         com.ibm.hats.runtime.PrintJob)
     */
    public void addPrintJob(PrintJobManager printJobManager, 
                                           PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: addPrintJob: 
                                     " + printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *      removePrintJob(com.ibm.hats.runtime.PrintJobManager,
     *      com.ibm.hats.runtime.PrintJob)
     */
    public void removePrintJob(PrintJobManager printJobManager, 
                                              PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: removePrintJob: " 
+ printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *      updatePrintJob(com.ibm.hats.runtime.PrintJobManager,
     *      com.ibm.hats.runtime.PrintJob)
     */
    public void updatePrintJob(PrintJobManager printJobManager, 
                                             PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: updatePrintJob: " 
                                           + printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ServiceManagerListener#
     *      serviceChanged(com.ibm.hats.runtime.events.ServiceManagerEvent)
     */
    public void serviceChanged(ServiceManagerEvent event)
    {
        IService theService = event.getService();

        if (theService instanceof ISessionService)
        {
            ISessionService sessionService = (ISessionService) theService;

            int type = event.getType();
            if (type == ServiceManagerEvent.TYPE_SERVICE_CREATED)
            {
                sessionService.addSessionServiceListener(this);
            }
            else if (type == ServiceManagerEvent.TYPE_SERVICE_DESTROYED)
            {
                sessionService.removeSessionServiceListener(this);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.SessionStateListener#
     *      sessionStateChanged(com.ibm.hats.runtime.events.SessionStateEvent)
     */
    public void sessionStateChanged(StateChangeEvent event)
    {
        // If the session has change to operational then add a listener to
        // the PrintJobManager (if there is one and we haven't already done so).
        StateType state = event.getNewState();
        if (state == StateType.OPERATIONAL)
        {
            ISessionService sessionService = event.getSessionService();
            ClientSpecificInfo csi = sessionService.getRuntimeService()
                 .getClientContainer().accessClient(sessionService.getClientId());

            if (csi != null)
            {
                String asiId = ApplicationSpecificInfo.
                                            createCompositeAsiId(sessionService.
                                            getApplicationId(), 
                                                      sessionService.getViewId());
                ApplicationSpecificInfo asi = csi.peekAll(asiId);
                if (asi != null)
                {
                    PrintSpecificInfo psi = asi.getPrint();
                    if (psi != null)
                    {
                        PrintResourceHandler prh = psi.getResourceHandler();
                        if (prh != null)
                        {
                            PrintJobManager pjm = prh.getPrintJobManager();
                            
                            // Add this PrintJobManager to our list of 
                            // PrintJobManagers.  
                            // This will return true if we haven't encountered 
                            // this PrintJobManager before.                            
                            // In this case, add ourselves as a 
                            // listener to this PrintJobManager.
                            if (printJobManagers.add(pjm))
                            {
                                pjm.addChangeListener(this);
                            }
                        }
                    }
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ISessionServiceListener#
     *      aboutToProcessCommand(com.ibm.hats.runtime.events.CommandEvent)
     */
    public void aboutToProcessCommand(CommandEvent event)
    {
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ISessionServiceListener#
     *      afterProcessCommand(com.ibm.hats.runtime.events.CommandEvent)
     */
    public void afterProcessCommand(CommandEvent event)
    {
    }
}