Package AppletSupport

Source Code of AppletSupport.LaunchMgr

/*
For Proof of Concept and evaluation purposes only:
This code is not to be used in production systems
without express written permission from ITerative Consulting Pty Ltd.

*/
package AppletSupport;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.prefs.Preferences;

import org.apache.log4j.Logger;

import AppletSupport.interfaces.ILaunchMgr;
import DisplayProject.events.ClientEventManager;
import Framework.Array_Of_Object;
import Framework.Array_Of_TextData;
import Framework.ErrorMgr;
import Framework.EventManager;
import Framework.FrameworkUtils;
import Framework.IListAppletService;
import Framework.NamedElement;
import Framework.ParameterHolder;
import Framework.RemoteEvent;
import Framework.RuntimeProperties;
import Framework.ServiceObject;
import Framework.ServiceObjectContext;
import Framework.ServiceObjectRegistry;
import Framework.TextData;
import Framework.UsageException;

/**
* LaunchMgr<p>
* <p>
* The LaunchMgr class is a partial implementation of the Forte/UDS the class that defines the LaunchService service object,
* which provides functionality supporting client applications as Forte/UDS applets.
* <p>
* <b>NOT ALL METHODS ARE IMPLEMENTED</b>
* @since  05-Sep-2008
*/
@RuntimeProperties(isDistributed=true, isAnchored=false, isShared=false, isTransactional=false)
@ServiceObject(name="LaunchService", visibility=ServiceObject.Visibility.USER, dialogDuration=ServiceObject.DialogDuration.SESSION, failover=false, loadBalanced=false)
@SuppressWarnings("serial")
public class LaunchMgr
        implements Serializable, ILaunchMgr
{
  static Logger _log = Logger.getLogger(LaunchMgr.class);
    /**
     * Do not call this method, used only to satisfy the interface
     */
    public String registerInterest(String pHostName, RemoteEvent pAnchoredObject, String pEvent) throws RemoteException {
        return EventManager.registerInterest(pHostName, pAnchoredObject, this, pEvent);
    }

    /**
     * Do not call this method, used only to satisfy the interface
     */
    public void postEvent(String pEventName, Hashtable<String, Object> pParameters) throws RemoteException {
        EventManager.postEvent(this, pEventName, pParameters);
    }

    /**
     * Do not call this method, used only to satisfy the interface
     */
    public void deregisterInterest(String pHostName, RemoteEvent pAnchoredObject, String pEvent) throws RemoteException {
        EventManager.deregisterInterest(pHostName, pAnchoredObject, this, pEvent);
    }


    // ---------
    // Constants
    // ---------
    // -----------------
    // Event definitions
    // -----------------
    /**
     * Occurs when an applet or application completes.
     */
    public static final String cEVENT_RUN_STARTED = "RunStarted";
    /**
     * Occurs when an applet or application starts running.
     */
    public static final String cEVENT_RUN_COMPLETED = "RunCompleted";

    // ----------
    // Attributes
    // ----------
    Preferences userPrefs;
    Hashtable<String, AppletRunInfo> whatsRunning;
    ServerSocket socket = null;
    int nextAppletID = 1;

    // ------------
    // Constructors
    // ------------
    public LaunchMgr() {
        // Explicitly call the superclass constructor to prevent the implicit call
        super();
        userPrefs = Preferences.systemRoot();
        whatsRunning = new Hashtable<String, AppletRunInfo>();
        final String appletPortString = System.getProperty("qq_APPLETPort");
        try {
         
          if (appletPortString != null){
            int port = Integer.parseInt(appletPortString);
      socket = new ServerSocket(port);
          }
    } catch (BindException e) {
      // just terminate
      return;
    } catch (IOException e) {
      _log.error("Launch manager socket exception", e);
    }
    Runnable listener = new Runnable(){

      public void run() {
        _log.debug("Launch manager listener started on port: " + appletPortString);
        while (true){
          try {
            Socket client = socket.accept();
            processAppletConnection(client);
          } catch (BindException e) {
            // just terminate
            break;
          } catch (IOException e) {
            _log.error("Launch manager listener exception", e);
            break;
          }
        }
        _log.debug("Launch manager listener terminated");
      }
     
    };
    Thread listenerThread = new Thread(listener, "LaunchManagerListener");
        listenerThread.start();

    }

    private void processAppletConnection(final Socket appletConnected){
      Runnable connection = new Runnable(){
        ObjectInputStream in = null;
        ObjectOutputStream out = null;
        String message = null;
        String parts[] = null;
        AppletRunInfo appletInfo = null;
        public void run() {
          try {
            in = new ObjectInputStream(appletConnected.getInputStream());
            out = new ObjectOutputStream(appletConnected.getOutputStream());
            out.flush();
            boolean closeConnection = false;
            do {
             
              try{
                message = (String)in.readObject();
                _log.debug("Processing message: " + message);
                parts = message.split(":");
                String appletName = parts[0];
                String appletEvent = parts[1];
               
                  Hashtable<String, ParameterHolder> params = new Hashtable<String, ParameterHolder>(1);
                  if (appletEvent.equals("stopped")){
                    appletInfo = whatsRunning.get(appletName);
                    params.put("appletId", new ParameterHolder(appletInfo.getAppletId()));
                    _log.debug("Posting RunComplete event");
                    ClientEventManager.postEvent(LaunchMgr.this, "RunCompleted", params);
                    whatsRunning.remove(appletName);
                    closeConnection = true;
                  } else if (appletEvent.equals("started")){
                    AppletRunInfo info = new AppletRunInfo();
                      info.setAppletName(appletName);
                      info.setAppletId(LaunchMgr.this.nextAppletID++);
                      this.appletInfo = info;
                      whatsRunning.put(appletName, info);
                    params.put("applet", new ParameterHolder(appletInfo));
                    _log.debug("Posting RunStarted event");
                    ClientEventManager.postEvent(LaunchMgr.this, "RunStarted", params);
                  }
              } catch (ClassNotFoundException classnot){
                System.err.println("Data received in unknown format");
              }
            } while(!closeConnection);
        } catch (java.io.EOFException e){
          Hashtable<String, ParameterHolder> params = new Hashtable<String, ParameterHolder>(1);
          params.put("appletId", new ParameterHolder(appletInfo.getAppletId()));
          _log.debug("Posting RunComplete event after connection closed");
          ClientEventManager.postEvent(LaunchMgr.this, "RunCompleted", params);
          } catch (IOException e) {
            _log.error("Launch manager listener exception", e);
          } finally {
            try {
              in.close();
              appletConnected.close();
            } catch (IOException e) {
              _log.error("Launch manager listener exception", e);
            }
          }
        }
      };
      Thread connectionThread = new Thread(connection, "AppletEventPoster");
      connectionThread.start();
    }

    /**
     * A dummy constructor used to instantiate a "blank" object. This is used for anchored objects, Spring beans and their
     * superclasses, but should not be expected to create a valid LaunchMgr.
     */
    public LaunchMgr(ServiceObjectContext pContext) {
    }

    // -------
    // Methods
    // -------

    /**
     * convertList<p>
     * <p>
     * @param namesSeen Type: Array_Of_Object<Object>
     * @param dataCache Type: Array_Of_Object<Object>
     * @param nameList Type: Array_Of_TextData<TextData>
     * @param type Type: int
     * @param appletList Type: Array_Of_AppletData<AppletData>
     */
    public void convertList(Array_Of_Object<Object> namesSeen, Array_Of_Object<Object> dataCache, Array_Of_TextData<TextData> nameList, int type, Array_Of_AppletData<AppletData> appletList) {
        throw new UsageException("This is a stub method only from a Forte library, please implement the library");
    }

    /**
     * Returns a list of the available applets or applications.<p>
     * <p>
     * @param type Type: int
     * @param category Type: int (Input) (default in Forte: 3)
     * @return Array_Of_AppletData<AppletData>
     */
    public Array_Of_AppletData<AppletData> listApplets(int type, int category) {
      Array_Of_AppletData<AppletData> appletData = new Array_Of_AppletData<AppletData>();
//    /*
//         * get the server list
//         */
      IListAppletService ls = ServiceObjectRegistry.getService("listAppletService", IListAppletService.class);
        ArrayList<NamedElement> apps = ls.listApplications();
        for (NamedElement app : apps){
        String name = app.getName().asString();
       
        String url = (String)app.getObject();
        AppletData ad = new AppletData(name, url);
        appletData.add(ad);
         
        }
        return appletData;
    }
    public Array_Of_AppletData<AppletData> listApplets(int type) {
      return listApplets(type, Constants.APPLET_CATEGORY_BOTH);
    }

    /**
     * Starts the named applet or application.<p>
     * <p>
     * @param name Type: TextData
     * @param release Type: TextData (Input) (default in Forte: NIL)
     * @param arguments Type: TextData (Input) (default in Forte: NIL)
     * @param update Type: boolean (Input) (default in Forte: TRUE)
     * @return AppletRunInfo
     */
    public AppletRunInfo runApplet(TextData name, TextData release, TextData arguments, boolean update) {
        IListAppletService lister = ServiceObjectRegistry.getService("listAppletService", IListAppletService.class);
        NamedElement ne = lister.findApplicationJNLP(name.toString());
        if (ne == null){
          UsageException ue = new UsageException("Unable to launch application: " + name);
          ErrorMgr.addError(ue);
          throw ue;
        }
        String startClass  = (String)ne.getObject();
        String cmdString = "javaws \"" + startClass + "\"";
      if (_log.isDebugEnabled()) {
        _log.debug("Launching application with: " + cmdString);
      }
     
      FrameworkUtils.runCommand(cmdString, false);
    return null;
    }
    public AppletRunInfo runApplet(TextData name) {
      return this.runApplet(name, null, null, true);
    }

    /**
     * runApplet<p>
     * <p>
     * @param name Type: TextData
     * @param options Type: short
     * @param release Type: TextData (Input) (default in Forte: NIL)
     * @param arguments Type: TextData (Input) (default in Forte: NIL)
     * @param update Type: boolean (Input) (default in Forte: TRUE)
     * @return AppletRunInfo
     */
    public AppletRunInfo runApplet(TextData name, short options, TextData release, TextData arguments, boolean update) {
        throw new UsageException("This is a stub method only from a Forte library, please implement the library");
    }

    /**
     * Returns a list of the currently running applets or applications.<p>
     * <p>
     * @return Array_Of_AppletRunInfo<AppletRunInfo>
     */
    public Array_Of_AppletRunInfo<AppletRunInfo> runningApplets() {
      Enumeration<AppletRunInfo> running = this.whatsRunning.elements();
      Array_Of_AppletRunInfo<AppletRunInfo> runningInfo = new Array_Of_AppletRunInfo<AppletRunInfo>();
      while (running.hasMoreElements()){
        runningInfo.add(running.nextElement());
      }
      return runningInfo;
    }

    /**
     * Shuts down one or more applets or applications started by the LaunchService service object.<p>
     * <p>
     * @param appletId Type: int
     */
    public void shutdown(int appletId) {
        throw new UsageException("This is a stub method only from a Forte library, please implement the library");
    }

    /**
     * Updates the specified assigned applet or application.<p>
     * <p>
     * @param name Type: TextData
     * @param release Type: TextData (Input) (default in Forte: NIL)
     */
    public void updateApplet(TextData name, TextData release) {
        throw new UsageException("This is a stub method only from a Forte library, please implement the library");
    }
   
//    private void loadAppList(){
//      if (this.appList == null){
//        LaunchService ls = ServiceObjectRegistry.getService("launchService", LaunchService.class);
//        this.appList = ls.listApplications();
//      }
//    }
    public static void main (String[] args){
      LaunchMgr lm = new LaunchMgr();
      Array_Of_AppletData<AppletData> ad = lm.listApplets(3);
      for (AppletData a : ad){
        System.out.println(a);
      }
    }
// end class LaunchMgr
// c Pass 2 Conversion Time: 188 milliseconds
TOP

Related Classes of AppletSupport.LaunchMgr

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.