KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.jdom.Element;
29
30 import com.sslexplorer.agent.DefaultAgentManager;
31 import com.sslexplorer.applications.types.ExecutableType;
32 import com.sslexplorer.applications.types.HtmlType;
33 import com.sslexplorer.applications.types.JavaType;
34 import com.sslexplorer.applications.types.JavasType;
35 import com.sslexplorer.applications.types.ServerType;
36 import com.sslexplorer.core.CoreEvent;
37 import com.sslexplorer.core.CoreEventConstants;
38 import com.sslexplorer.core.CoreListener;
39 import com.sslexplorer.core.CoreMenuTree;
40 import com.sslexplorer.core.CoreServlet;
41 import com.sslexplorer.core.CoreUtil;
42 import com.sslexplorer.core.MenuItem;
43 import com.sslexplorer.core.PageTaskMenuTree;
44 import com.sslexplorer.extensions.ExtensionBundle;
45 import com.sslexplorer.extensions.ExtensionDescriptor;
46 import com.sslexplorer.extensions.ExtensionException;
47 import com.sslexplorer.extensions.ExtensionTypeManager;
48 import com.sslexplorer.extensions.types.DefaultPlugin;
49 import com.sslexplorer.extensions.types.PluginDefinition;
50 import com.sslexplorer.navigation.MenuTree;
51 import com.sslexplorer.navigation.NavigationManager;
52 import com.sslexplorer.policyframework.Permission;
53 import com.sslexplorer.policyframework.PolicyConstants;
54 import com.sslexplorer.policyframework.PolicyDatabase;
55 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
56 import com.sslexplorer.policyframework.PolicyUtil;
57 import com.sslexplorer.policyframework.ResourceType;
58 import com.sslexplorer.policyframework.itemactions.AddToFavoritesAction;
59 import com.sslexplorer.policyframework.itemactions.CloneResourceAction;
60 import com.sslexplorer.policyframework.itemactions.EditResourceAction;
61 import com.sslexplorer.policyframework.itemactions.RemoveFromFavoritesAction;
62 import com.sslexplorer.policyframework.itemactions.RemoveResourceAction;
63 import com.sslexplorer.security.SessionInfo;
64 import com.sslexplorer.table.TableItemActionMenuTree;
65
66 /**
67  * Plugin implementation that provides the <i>Applications</i> feature.
68  * <p>
69  * By itself, this extension does not do much, the <i>Application Extension</i>
70  * for the application to be used must be downloaded from the 3SP Extension
71  * Store.
72  * <p>
73  * Once an Application Extension is installed, <i>Application Shortcuts</i> may
74  * then be created. Application Shortcuts are used to configure and launch
75  * instances of any installed and supported Application.
76  *
77  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
78  */

79 public class ApplicationsPlugin extends DefaultPlugin implements CoreListener {
80
81     /**
82      * Application shortcut resource type ID
83      */

84     public final static int APPLICATION_SHORTCUT_RESOURCE_TYPE_ID = 3;
85
86     /**
87      * Application shortcut resource type
88      */

89     public static final ResourceType APPLICATION_SHORTCUT_RESOURCE_TYPE = new ApplicationShortcutResourceType();
90
91     /**
92      * Extension bundle ID
93      */

94     public static final String JavaDoc BUNDLE_ID = "sslexplorer-community-applications";
95
96     final static Log log = LogFactory.getLog(ApplicationsPlugin.class);
97
98     /**
99      * Message resources key (resource bundle id)
100      */

101     public static final String JavaDoc MESSAGE_RESOURCES_KEY = "applications";
102
103     /**
104      * Constructor.
105      */

106     public ApplicationsPlugin() {
107         super("/WEB-INF/sslexplorer-community-applications-tiles-defs.xml", true);
108     }
109
110     /* (non-Javadoc)
111      * @see com.sslexplorer.extensions.types.DefaultPlugin#startPlugin(com.sslexplorer.extensions.types.PluginDefinition, com.sslexplorer.extensions.ExtensionDescriptor, org.jdom.Element)
112      */

113     public void startPlugin(PluginDefinition definition, ExtensionDescriptor descriptor, Element element) throws ExtensionException {
114         super.startPlugin(definition, descriptor, element);
115         try {
116             initExtensionTypes();
117         } catch (Exception JavaDoc e) {
118             throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e);
119         }
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see com.sslexplorer.plugin.DefaultPlugin#startPlugin()
126      */

127     public void activatePlugin() throws ExtensionException {
128         super.activatePlugin();
129         try {
130             initDatabase();
131             initAgentService();
132             initPolicyFramework();
133             initTableItemActions();
134             initMainMenu();
135             initPageTasks();
136             CoreUtil.updateEventsTable(ApplicationsPlugin.MESSAGE_RESOURCES_KEY, ApplicationShortcutEventConstants.class);
137             CoreServlet.getServlet().addCoreListener(this);
138         } catch (Exception JavaDoc e) {
139             e.printStackTrace();
140             throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e);
141         }
142     }
143
144     void initDatabase() throws Exception JavaDoc {
145         ApplicationShortcutDatabaseFactory.getInstance().open(CoreServlet.getServlet(), this.getPluginDefinition());
146     }
147
148     void initAgentService() throws Exception JavaDoc {
149         DefaultAgentManager.getInstance().registerService(ApplicationService.class);
150     }
151
152     void initExtensionTypes() throws Exception JavaDoc {
153         ExtensionTypeManager.getInstance().registerExtensionType("server", ServerType.class);
154         ExtensionTypeManager.getInstance().registerExtensionType("javas", JavasType.class);
155         ExtensionTypeManager.getInstance().registerExtensionType("java", JavaType.class);
156         ExtensionTypeManager.getInstance().registerExtensionType("executable", ExecutableType.class);
157         ExtensionTypeManager.getInstance().registerExtensionType("html", HtmlType.class);
158     }
159
160     void initPolicyFramework() throws Exception JavaDoc {
161
162         PolicyDatabase pdb = PolicyDatabaseFactory.getInstance();
163
164         // Application Shortcut
165
pdb.registerResourceType(APPLICATION_SHORTCUT_RESOURCE_TYPE);
166         APPLICATION_SHORTCUT_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN);
167         APPLICATION_SHORTCUT_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_EDIT_AND_ASSIGN);
168         APPLICATION_SHORTCUT_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_ASSIGN);
169         APPLICATION_SHORTCUT_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_DELETE);
170     }
171
172     void initTableItemActions() throws Exception JavaDoc {
173         MenuTree tree = NavigationManager.getMenuTree(TableItemActionMenuTree.MENU_TABLE_ITEM_ACTION_MENU_TREE);
174
175         // Application shortcuts
176
tree.addMenuItem(null, new MenuItem("applicationShortcuts",
177                         ApplicationsPlugin.MESSAGE_RESOURCES_KEY,
178                         null,
179                         100,
180                         false,
181                         SessionInfo.ALL_CONTEXTS));
182         tree.addMenuItem("applicationShortcuts", new AddToFavoritesAction(ApplicationsPlugin.MESSAGE_RESOURCES_KEY));
183         tree.addMenuItem("applicationShortcuts", new RemoveFromFavoritesAction(ApplicationsPlugin.MESSAGE_RESOURCES_KEY));
184         tree.addMenuItem("applicationShortcuts", new RemoveResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT,
185                         ApplicationsPlugin.MESSAGE_RESOURCES_KEY));
186         tree.addMenuItem("applicationShortcuts", new EditResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT,
187                         ApplicationsPlugin.MESSAGE_RESOURCES_KEY));
188         tree.addMenuItem("applicationShortcuts", new CloneResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT,
189             ApplicationsPlugin.MESSAGE_RESOURCES_KEY));
190     }
191
192     void initMainMenu() throws Exception JavaDoc {
193         MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
194
195         tree.addMenuItem("resources", new MenuItem("userApplicationShortcuts",
196                         MESSAGE_RESOURCES_KEY,
197                         "/showUserApplicationShortcuts.do",
198                         300,
199                         true,
200                         null,
201                         SessionInfo.USER_CONSOLE_CONTEXT,
202                         null,
203                         null,
204                         APPLICATION_SHORTCUT_RESOURCE_TYPE));
205
206         tree.addMenuItem("globalResources", new MenuItem("applicationShortcuts",
207                         MESSAGE_RESOURCES_KEY,
208                         "/showApplicationShortcuts.do",
209                         300,
210                         true,
211                         null,
212                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT,
213                         APPLICATION_SHORTCUT_RESOURCE_TYPE,
214                         new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN,
215                             PolicyConstants.PERM_EDIT_AND_ASSIGN,
216                             PolicyConstants.PERM_DELETE,
217                             PolicyConstants.PERM_ASSIGN }) {
218             public boolean isAvailable(int checkNavigationContext, SessionInfo info, HttpServletRequest JavaDoc request) {
219                 boolean available = super.isAvailable(checkNavigationContext, info, request);
220                 if (available) {
221                     try {
222
223                         PolicyUtil.checkPermissions(APPLICATION_SHORTCUT_RESOURCE_TYPE,
224                             new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN,
225                                 PolicyConstants.PERM_EDIT_AND_ASSIGN,
226                                 PolicyConstants.PERM_DELETE,
227                                 PolicyConstants.PERM_ASSIGN },
228                             request);
229                         available = true;
230                     } catch (Exception JavaDoc e1) {
231                         available = false;
232                     }
233                 }
234                 return available;
235             }
236         });
237     }
238
239     void initPageTasks() throws Exception JavaDoc {
240         MenuTree tree = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
241
242         tree.addMenuItem(null, new MenuItem("showApplicationShortcuts",
243                         null,
244                         null,
245                         100,
246                         false,
247                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT));
248         tree.addMenuItem("showApplicationShortcuts", new MenuItem("createApplicationShortcut",
249                         ApplicationsPlugin.MESSAGE_RESOURCES_KEY,
250                         "/applicationShortcutApplication.do",
251                         100,
252                         true,
253                         "_self",
254                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT,
255                         APPLICATION_SHORTCUT_RESOURCE_TYPE,
256                         new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN }));
257     }
258
259     public void coreEvent(CoreEvent evt) {
260         if (evt.getId() == CoreEventConstants.REMOVING_EXTENSION) {
261             ExtensionBundle bundle = (ExtensionBundle) evt.getParameter();
262             for (Iterator JavaDoc itr = bundle.iterator(); itr.hasNext();) {
263                 ExtensionDescriptor app = (ExtensionDescriptor) itr.next();
264                 try {
265                     ApplicationShortcutDatabaseFactory.getInstance().removeApplicationShortcuts(app.getId());
266                 } catch (Exception JavaDoc e) {
267                     log.error("Failed to remove application shortcuts for removed extension " + bundle.getId());
268                 }
269             }
270         }
271
272     }
273     
274     public void stopPlugin() throws ExtensionException {
275         super.stopPlugin();
276         try {
277             removeExtensionTypes();
278             stopDatabase();
279             removeAgentService();
280             removePolicyFramework();
281             removeTableItemActions();
282             removeMainMenu();
283             removePageTasks();
284             CoreServlet.getServlet().removeCoreListener(this);
285         } catch (Exception JavaDoc e) {
286             throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e, "Failed to start.");
287         }
288     }
289
290     private void removePageTasks() {
291         MenuTree tree = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
292         tree.removeMenuItem("showApplicationShortcuts", "createApplicationShortcut");
293     }
294
295     private void removeMainMenu() {
296         MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
297         tree.removeMenuItem("resources", "userApplicationShortcuts");
298         tree.removeMenuItem("globalResources", "applicationShortcuts");
299     }
300
301     private void removeTableItemActions() {
302         MenuTree tree = NavigationManager.getMenuTree(TableItemActionMenuTree.MENU_TABLE_ITEM_ACTION_MENU_TREE);
303         tree.removeMenuItem("applicationShortcuts", AddToFavoritesAction.TABLE_ITEM_ACTION_ID);
304         tree.removeMenuItem("applicationShortcuts", RemoveFromFavoritesAction.TABLE_ITEM_ACTION_ID);
305         tree.removeMenuItem("applicationShortcuts", RemoveResourceAction.TABLE_ITEM_ACTION_ID);
306         tree.removeMenuItem("applicationShortcuts", EditResourceAction.TABLE_ITEM_ACTION_ID);
307     }
308
309     private void removePolicyFramework() throws Exception JavaDoc {
310
311         PolicyDatabase pdb = PolicyDatabaseFactory.getInstance();
312         pdb.deregisterResourceType(APPLICATION_SHORTCUT_RESOURCE_TYPE);
313     }
314
315     private void removeAgentService() {
316          DefaultAgentManager.getInstance().unregisterService(ApplicationService.class);
317     }
318
319     private void stopDatabase() throws Exception JavaDoc {
320         ApplicationShortcutDatabaseFactory.getInstance().close();
321     }
322
323     private void removeExtensionTypes() {
324         ExtensionTypeManager.getInstance().unregisterExtensionType("server");
325         ExtensionTypeManager.getInstance().unregisterExtensionType("javas");
326         ExtensionTypeManager.getInstance().unregisterExtensionType("java");
327         ExtensionTypeManager.getInstance().unregisterExtensionType("executable");
328         ExtensionTypeManager.getInstance().unregisterExtensionType("html");
329     }
330
331 }
332
Popular Tags