KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > applications > ApplicationManager


1 package com.sslexplorer.agent.client.applications;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import com.maverick.multiplex.MultiplexedConnection;
9 import com.maverick.multiplex.Request;
10 import com.maverick.multiplex.RequestHandler;
11 import com.maverick.util.ByteArrayReader;
12 import com.maverick.util.ByteArrayWriter;
13 import com.sslexplorer.agent.client.AbstractResourceManager;
14 import com.sslexplorer.agent.client.Agent;
15 import com.sslexplorer.agent.client.util.TunnelConfiguration;
16
17 public class ApplicationManager extends AbstractResourceManager implements RequestHandler {
18
19     public static final String JavaDoc LAUNCH_APPLICATION_REQUEST = "launchApplication@3sp.com";
20
21     // #ifdef DEBUG
22
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ApplicationManager.class);
23     // #endif
24

25     /**
26      * Application resource type ID
27      */

28     public final static int APPLICATION_SHORTCUT_RESOURCE_TYPE_ID = 3;
29
30     Hashtable JavaDoc applicationResources = new Hashtable JavaDoc();
31
32     static ApplicationManager instance;
33
34     public ApplicationManager(Agent agent) {
35         super(agent);
36         agent.getConnection().registerRequestHandler(LAUNCH_APPLICATION_REQUEST, this);
37     }
38     
39     public void getApplicationResources() {
40         super.getResources(APPLICATION_SHORTCUT_RESOURCE_TYPE_ID, "Applications");
41     }
42
43     public void launchResource(int resourceId) {
44         try {
45             ByteArrayWriter baw = new ByteArrayWriter();
46             baw.writeInt(resourceId);
47             Request request = new Request("setupAndLaunchApplication@3sp.com", baw.toByteArray());
48             if (agent.getConnection().sendRequest(request, true)) {
49                 // #ifdef DEBUG
50
log.debug("Application launch setup");
51                 // #endif
52
processLaunchRequest(request);
53             } else {
54                 // #ifdef DEBUG
55
log.error("Failed to setup and launch application launch");
56                 // #endif
57
}
58         } catch (IOException JavaDoc e) {
59             // #ifdef DEBUG
60
log.error("Failed to setup and launch application launch", e);
61             // #endif
62
}
63     }
64
65     public boolean processRequest(Request request, MultiplexedConnection con) {
66
67         if (request.getRequestName().equals(LAUNCH_APPLICATION_REQUEST)) {
68
69             try {
70                 return processLaunchRequest(request);
71             } catch (IOException JavaDoc e) {
72                 // #ifdef DEBUG
73
log.error("Failed to process launch application request", e); //$NON-NLS-1$
74
// #endif
75
return false;
76             }
77         } else
78             return false;
79     }
80
81     public void postReply(MultiplexedConnection connection) {
82     }
83
84     boolean processLaunchRequest(Request request) throws IOException JavaDoc {
85
86         if(request.getRequestData()==null)
87             return true;
88         
89         ByteArrayReader msg = new ByteArrayReader(request.getRequestData());
90         
91         // If this was a server side the no further processing is required
92
boolean serverSide = msg.readBoolean();
93         if(serverSide) {
94             // #ifdef DEBUG
95
log.info("Server side launch. No further processing required.");
96             // #endif
97
return false;
98         }
99
100         // Get the application name
101
String JavaDoc name = msg.readString();
102         msg.readInt(); // shortcut id
103
String JavaDoc launchId = msg.readString();
104         String JavaDoc descriptor = msg.readString();
105         Hashtable JavaDoc parameters = new Hashtable JavaDoc();
106         parameters.put("launchId", launchId);
107         parameters.put("ticket", launchId);
108
109         Application app = launchApplication(name, descriptor, parameters);
110         app.start();
111         Vector JavaDoc tunnels = ((AgentApplicationLauncher)app.getLauncher()).getTunnels();
112         ByteArrayWriter baw = new ByteArrayWriter();
113         for(Enumeration JavaDoc e = tunnels.elements(); e.hasMoreElements(); ) {
114             TunnelConfiguration listeningSocketConfiguration = (TunnelConfiguration) e.nextElement();
115             baw.writeString(listeningSocketConfiguration.getName());
116             baw.writeString(listeningSocketConfiguration.getSourceInterface());
117             baw.writeInt(listeningSocketConfiguration.getSourcePort());
118         }
119         request.setRequestData(baw.toByteArray());
120         
121         return true;
122     }
123
124     public Application launchApplication(String JavaDoc name, String JavaDoc descriptor, Hashtable JavaDoc parameters) throws IOException JavaDoc {
125         if (agent.getConfiguration().isDisplayInformationPopups()) {
126             agent.getGUI().popup(null, "Launching " + name, "SSL-Explorer Agent", "popup-application", -1); //$NON-NLS-1$
127
}
128         Application app = new Application(agent, parameters, name, descriptor);
129         return app;
130     }
131 }
132
Popular Tags