KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > ApplicationService


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.applications;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.maverick.multiplex.Channel;
29 import com.maverick.multiplex.MultiplexedConnection;
30 import com.maverick.multiplex.Request;
31 import com.maverick.multiplex.RequestHandler;
32 import com.maverick.util.ByteArrayReader;
33 import com.maverick.util.ByteArrayWriter;
34 import com.sslexplorer.agent.AbstractResourceService;
35 import com.sslexplorer.agent.AgentService;
36 import com.sslexplorer.agent.AgentTunnel;
37 import com.sslexplorer.agent.channels.ApplicationFileChannel;
38 import com.sslexplorer.core.CoreAttributeConstants;
39 import com.sslexplorer.core.CoreEvent;
40 import com.sslexplorer.core.CoreServlet;
41 import com.sslexplorer.extensions.ExtensionDescriptor;
42 import com.sslexplorer.extensions.ExtensionParser;
43 import com.sslexplorer.extensions.ExtensionBundle.ExtensionBundleStatus;
44 import com.sslexplorer.extensions.store.ExtensionStore;
45 import com.sslexplorer.policyframework.LaunchSession;
46 import com.sslexplorer.policyframework.LaunchSessionFactory;
47 import com.sslexplorer.policyframework.LaunchSessionManager;
48 import com.sslexplorer.policyframework.Policy;
49 import com.sslexplorer.policyframework.Resource;
50 import com.sslexplorer.policyframework.ResourceAccessEvent;
51
52 /**
53  * {@link AgentService} implementation for dealing with
54  * the <i>Application Shortcuts</i> resource.
55  *
56  * @author brett
57  */

58 public class ApplicationService extends AbstractResourceService implements RequestHandler {
59
60     final static Log log = LogFactory.getLog(ApplicationService.class);
61     
62     /**
63      * Constructor
64      */

65     public ApplicationService() {
66         super(ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE, new int[] {
67                 ApplicationShortcutEventConstants.CREATE_APPLICATION_SHORTCUT,
68                 ApplicationShortcutEventConstants.REMOVE_APPLICATION_SHORTCUT,
69                 ApplicationShortcutEventConstants.UPDATE_APPLICATION_SHORTCUT
70         });
71     }
72
73     /* (non-Javadoc)
74      * @see com.sslexplorer.agent.AgentService#performStartup(com.sslexplorer.agent.AgentTunnel)
75      */

76     public void performStartup(AgentTunnel agent) {
77     }
78
79     /**
80      * Create the request to instruct the agent to launch an application
81      *
82      * @param httpRequest request
83      * @param launchSession launch session
84      * @return boolean indicating if the agent reported the application was
85      * launched
86      * @throws Exception
87      */

88     public Request launchApplication(LaunchSession launchSession) throws Exception JavaDoc {
89         ApplicationShortcut shortcut = (ApplicationShortcut) launchSession.getResource();
90         ByteArrayWriter msg = new ByteArrayWriter();
91         
92         // If this is a service side application launcher then launch now and inform the agent not to go any further
93
ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication());
94         if(((ApplicationLauncherType)descriptor.getExtensionType()).isServerSide()) {
95             msg.writeBoolean(true);
96
97
98             // Do the launch
99
try {
100                 if(descriptor.getApplicationBundle().getStatus() != ExtensionBundleStatus.ACTIVATED) {
101                     throw new Exception JavaDoc("Extension bundle " + descriptor.getApplicationBundle().getId() +" is not activated, cannot launch applicaiton.");
102                 }
103                 
104                 ((ApplicationLauncherType) descriptor.getExtensionType()).launch(new HashMap JavaDoc<String JavaDoc, String JavaDoc>(),
105                     descriptor,
106                     shortcut,
107                     null,
108                     launchSession,
109                     null,
110                     null);
111
112                 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this,
113                                 ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED,
114                                 launchSession.getResource(),
115                                 launchSession.getPolicy(),
116                                 launchSession.getSession(),
117                                 CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME,
118                     descriptor.getName()).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId()));
119
120             } catch (Exception JavaDoc ex) {
121                 CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this,
122                         ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED,
123                                 launchSession.getSession(),
124                                 ex).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, descriptor.getName())
125                                 .addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId()));
126                 throw ex;
127
128             }
129         }
130         else {
131             msg.writeBoolean(false);
132             msg.writeString(shortcut.getApplication());
133             msg.writeInt(shortcut.getResourceId());
134             msg.writeString(launchSession.getId());
135             msg.writeString(ExtensionParser.processApplicationParameters(launchSession,
136                 new Properties JavaDoc(),
137                 shortcut.getParameters(),
138                 shortcut.getApplication()));
139         }
140         
141         return new Request("launchApplication@3sp.com", msg.toByteArray());
142     }
143
144     /* (non-Javadoc)
145      * @see com.sslexplorer.agent.AgentService#createChannel(com.maverick.multiplex.MultiplexedConnection, java.lang.String)
146      */

147     public Channel createChannel(MultiplexedConnection connection, String JavaDoc type) {
148         if (type.equals(ApplicationFileChannel.CHANNEL_TYPE)) {
149             return new ApplicationFileChannel((AgentTunnel)connection);
150         } else
151             return null;
152     }
153
154     /* (non-Javadoc)
155      * @see com.maverick.multiplex.RequestHandler#processRequest(com.maverick.multiplex.Request, com.maverick.multiplex.MultiplexedConnection)
156      */

157     public boolean processRequest(Request request, MultiplexedConnection connection) {
158
159         AgentTunnel agent = (AgentTunnel) connection;
160
161         if (request.getRequestName().equals("setupAndLaunchApplication@3sp.com") && request.getRequestData()!=null) {
162
163             try {
164                 ByteArrayReader reader = new ByteArrayReader(request.getRequestData());
165                 int id = (int)reader.readInt();
166                 Resource resource = ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE.getResourceById(id);
167                 if (resource == null) {
168                     throw new Exception JavaDoc("No resource with ID " + id);
169                 }
170                 Policy policy = LaunchSessionManager.getLaunchRequestPolicy(null, agent.getSession(), resource);
171                 if (resource.sessionPasswordRequired(agent.getSession())) {
172                     // TODO: prompt user for credentials through agent!
173
return true;
174                 } else {
175                     LaunchSession launchSession = LaunchSessionFactory.getInstance().createLaunchSession(agent.getSession(),
176                         resource,
177                         policy);
178                     launchSession.checkAccessRights(null, agent.getSession());
179
180                     ApplicationShortcut shortcut = (ApplicationShortcut) launchSession.getResource();
181                     ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication());
182
183                     Request newRequest = launchApplication(launchSession);
184                     request.setRequestData(newRequest.getRequestData());
185
186                     CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this,
187                             ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED,
188                                     launchSession.getResource(),
189                                     launchSession.getPolicy(),
190                                     launchSession.getSession(),
191                                     CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME,
192                         descriptor.getName()).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId()));
193
194                 }
195                 return true;
196             } catch (Exception JavaDoc e) {
197                 e.printStackTrace();
198                 return false;
199             }
200         }
201         return false;
202     }
203
204     /* (non-Javadoc)
205      * @see com.maverick.multiplex.RequestHandler#postReply(com.maverick.multiplex.MultiplexedConnection)
206      */

207     public void postReply(MultiplexedConnection connection) {
208     }
209
210     /* (non-Javadoc)
211      * @see com.sslexplorer.agent.AgentService#initializeTunnel(com.sslexplorer.agent.AgentTunnel)
212      */

213     public void initializeTunnel(AgentTunnel tunnel) {
214         tunnel.registerRequestHandler("setupAndLaunchApplication@3sp.com", this);
215     }
216 }
217
Popular Tags