KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > actions > LaunchApplicationAction


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.actions;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessages;
33
34 import com.sslexplorer.applications.ApplicationLauncherType;
35 import com.sslexplorer.applications.ApplicationShortcut;
36 import com.sslexplorer.applications.ApplicationShortcutEventConstants;
37 import com.sslexplorer.applications.ApplicationsPlugin;
38 import com.sslexplorer.core.BundleActionMessage;
39 import com.sslexplorer.core.CoreAttributeConstants;
40 import com.sslexplorer.core.CoreEvent;
41 import com.sslexplorer.core.CoreServlet;
42 import com.sslexplorer.core.RedirectWithMessages;
43 import com.sslexplorer.extensions.ExtensionDescriptor;
44 import com.sslexplorer.extensions.ExtensionBundle.ExtensionBundleStatus;
45 import com.sslexplorer.extensions.store.ExtensionStore;
46 import com.sslexplorer.policyframework.LaunchSession;
47 import com.sslexplorer.policyframework.Resource;
48 import com.sslexplorer.policyframework.ResourceAccessEvent;
49 import com.sslexplorer.policyframework.actions.AbstractLaunchAction;
50 import com.sslexplorer.security.SessionInfo;
51
52 /**
53  * Implementation of {@link com.sslexplorer.core.actions.AuthenticatedAction}
54  * that launches an <i>Application Shortcut</i>.
55  * <p>
56  * The actual launch is delegated to the extension type (which must be a
57  * {@link ApplicationLauncherType}.
58  *
59  *
60  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
61  */

62 public class LaunchApplicationAction extends AbstractLaunchAction {
63
64     final static Log log = LogFactory.getLog(LaunchApplicationAction.class);
65
66     /**
67      * Constructor.
68      *
69      */

70     public LaunchApplicationAction() {
71         super(ApplicationsPlugin.APPLICATION_SHORTCUT_RESOURCE_TYPE,
72                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT);
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see com.sslexplorer.policyframework.actions.AbstractLaunchAction#isAgentRequired(com.sslexplorer.policyframework.Resource)
79      */

80     protected boolean isAgentRequired(Resource resource) {
81         ApplicationShortcut shortcut = (ApplicationShortcut) resource;
82         try {
83             ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication());
84             return ((ApplicationLauncherType) descriptor.getExtensionType()).isAgentRequired(shortcut, descriptor);
85         } catch (Exception JavaDoc e) {
86             log.error("Failed to determine if agent is required. Assuming not.");
87             return false;
88         }
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see com.sslexplorer.policyframework.actions.AbstractLaunchAction#launch(org.apache.struts.action.ActionMapping,
95      * com.sslexplorer.policyframework.LaunchSession,
96      * javax.servlet.http.HttpServletRequest, java.lang.String)
97      */

98     protected ActionForward launch(ActionMapping mapping, LaunchSession launchSession, HttpServletRequest JavaDoc request, String JavaDoc returnTo)
99                     throws Exception JavaDoc {
100         ApplicationShortcut shortcut = (ApplicationShortcut) launchSession.getResource();
101         ExtensionDescriptor descriptor = ExtensionStore.getInstance().getExtensionDescriptor(shortcut.getApplication());
102         HashMap JavaDoc<String JavaDoc, String JavaDoc> parameters = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
103         Enumeration JavaDoc e = request.getParameterNames();
104         while (e.hasMoreElements()) {
105             String JavaDoc nameParam = (String JavaDoc) e.nextElement();
106             parameters.put(nameParam, request.getParameter(nameParam));
107         }
108
109         // Do the launch
110
try {
111             if(descriptor.getApplicationBundle().getStatus() != ExtensionBundleStatus.ACTIVATED) {
112                 throw new Exception JavaDoc("Extension bundle " + descriptor.getApplicationBundle().getId() +" is not activated, cannot launch applicaiton.");
113             }
114             ActionForward fwd = ((ApplicationLauncherType) descriptor.getExtensionType()).launch(parameters,
115                 descriptor,
116                 shortcut,
117                 mapping,
118                 launchSession,
119                 returnTo,
120                 request);
121
122             CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this,
123                     ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED,
124                             launchSession.getResource(),
125                             launchSession.getPolicy(),
126                             launchSession.getSession(),
127                             CoreEvent.STATE_SUCCESSFUL).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME,
128                 descriptor.getName()).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId()));
129
130             /*
131              * If the launch implementation returns its own forward, it is
132              * reponsible for setting up its 'launched' message
133              */

134             if (fwd == null) {
135                 ActionMessages msgs = new ActionMessages();
136                 msgs.add(Globals.MESSAGE_KEY, new BundleActionMessage(ApplicationsPlugin.MESSAGE_RESOURCES_KEY,
137                                 "launchApplication.launched",
138                                 shortcut.getResourceName()));
139                 saveMessages(request, msgs);
140                 return new RedirectWithMessages(returnTo, request);
141             }
142             return fwd;
143         } catch (Exception JavaDoc ex) {
144             CoreServlet.getServlet().fireCoreEvent(new ResourceAccessEvent(this,
145                     ApplicationShortcutEventConstants.APPLICATION_SHORTCUT_LAUNCHED,
146                             launchSession.getSession(),
147                             ex).addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_NAME, descriptor.getName())
148                             .addAttribute(CoreAttributeConstants.EVENT_ATTR_APPLICATION_ID, descriptor.getId()));
149             throw ex;
150
151         }
152     }
153 }
154
Popular Tags