KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tunnels > TunnelPlugin


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.tunnels;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import com.sslexplorer.agent.DefaultAgentManager;
28 import com.sslexplorer.core.CoreMenuTree;
29 import com.sslexplorer.core.CoreServlet;
30 import com.sslexplorer.core.CoreUtil;
31 import com.sslexplorer.core.MenuItem;
32 import com.sslexplorer.core.PageTaskMenuTree;
33 import com.sslexplorer.extensions.ExtensionException;
34 import com.sslexplorer.extensions.types.DefaultPlugin;
35 import com.sslexplorer.navigation.MenuTree;
36 import com.sslexplorer.navigation.NavigationManager;
37 import com.sslexplorer.policyframework.Permission;
38 import com.sslexplorer.policyframework.PolicyConstants;
39 import com.sslexplorer.policyframework.PolicyDatabase;
40 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
41 import com.sslexplorer.policyframework.PolicyUtil;
42 import com.sslexplorer.policyframework.ResourceType;
43 import com.sslexplorer.policyframework.itemactions.AddToFavoritesAction;
44 import com.sslexplorer.policyframework.itemactions.CloneResourceAction;
45 import com.sslexplorer.policyframework.itemactions.EditResourceAction;
46 import com.sslexplorer.policyframework.itemactions.RemoveFromFavoritesAction;
47 import com.sslexplorer.policyframework.itemactions.RemoveResourceAction;
48 import com.sslexplorer.security.SessionInfo;
49 import com.sslexplorer.table.TableItemActionMenuTree;
50 import com.sslexplorer.tunnels.itemactions.SwitchOffAction;
51 import com.sslexplorer.tunnels.itemactions.SwitchOnAction;
52
53 /**
54  * Plugin implementation thats the <i>Network Places</i> feature.
55  *
56  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
57  */

58 public class TunnelPlugin extends DefaultPlugin {
59
60     /**
61      * Tunnel resource type ID
62      */

63     public final static int SSL_TUNNEL_RESOURCE_TYPE_ID = 4;
64
65     /**
66      * Tunnel resource type
67      */

68     public static final ResourceType SSL_TUNNEL_RESOURCE_TYPE = new TunnelResourceType();
69
70     /**
71      * Extension bundle ID
72      */

73     public static final String JavaDoc BUNDLE_ID = "sslexplorer-community-tunnels";
74
75     final static Log log = LogFactory.getLog(TunnelPlugin.class);
76
77     /**
78      * Message resources key (resource bundle id)
79      */

80     public static final String JavaDoc MESSAGE_RESOURCES_KEY = "tunnels";
81
82     /**
83      * Constructor.
84      */

85     public TunnelPlugin() {
86         super("/WEB-INF/sslexplorer-community-tunnels-tiles-defs.xml", true);
87     }
88
89     /* (non-Javadoc)
90      * @see com.sslexplorer.extensions.types.DefaultPlugin#activatePlugin()
91      */

92     public void activatePlugin() throws ExtensionException {
93         super.activatePlugin();
94         try {
95             initDatabase();
96             initPolicyFramework();
97             initTableItemActions();
98             initMainMenu();
99             initPageTasks();
100             initService();
101             CoreUtil.updateEventsTable(TunnelPlugin.MESSAGE_RESOURCES_KEY, TunnelsEventConstants.class);
102         } catch (Exception JavaDoc e) {
103             throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e, e.getLocalizedMessage());
104         }
105     }
106
107     void initDatabase() throws Exception JavaDoc {
108         TunnelDatabaseFactory.getInstance().open(CoreServlet.getServlet(), this.getPluginDefinition());
109     }
110
111     void initPolicyFramework() throws Exception JavaDoc {
112         PolicyDatabase pdb = PolicyDatabaseFactory.getInstance();
113         // SSL Tunnel
114
pdb.registerResourceType(SSL_TUNNEL_RESOURCE_TYPE);
115         SSL_TUNNEL_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN);
116         SSL_TUNNEL_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_EDIT_AND_ASSIGN);
117         SSL_TUNNEL_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_DELETE);
118         SSL_TUNNEL_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_ASSIGN);
119     }
120
121     void initTableItemActions() throws Exception JavaDoc {
122         MenuTree tree = NavigationManager.getMenuTree(TableItemActionMenuTree.MENU_TABLE_ITEM_ACTION_MENU_TREE);
123         // Tunnels
124
tree.addMenuItem(null, new MenuItem("tunnel", "tunnels", null, 100, false, SessionInfo.ALL_CONTEXTS));
125         tree.addMenuItem("tunnel", new AddToFavoritesAction("tunnels"));
126         tree.addMenuItem("tunnel", new RemoveFromFavoritesAction("tunnels"));
127         tree.addMenuItem("tunnel", new RemoveResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, "tunnels"));
128         tree.addMenuItem("tunnel", new EditResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, "tunnels"));
129         //tree.addMenuItem("tunnel", new SwitchOnAction());
130
tree.addMenuItem("tunnel", new CloneResourceAction(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, "tunnels"));
131         tree.addMenuItem("tunnel", new SwitchOffAction());
132     }
133
134     void initMainMenu() throws Exception JavaDoc {
135         MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
136
137         tree.addMenuItem("resources", new MenuItem("userTunnels", "tunnels", "/showUserTunnels.do", 500, true, null,
138                         SessionInfo.USER_CONSOLE_CONTEXT, null, null, SSL_TUNNEL_RESOURCE_TYPE));
139
140         tree.addMenuItem("globalResources", new MenuItem("tunnels", "tunnels", "/showTunnels.do", 400, true, null,
141                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, SSL_TUNNEL_RESOURCE_TYPE, new Permission[] {
142                                         PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN,
143                                         PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN }) {
144             public boolean isAvailable(int checkNavigationContext, SessionInfo info, HttpServletRequest JavaDoc request) {
145                 boolean available = super.isAvailable(checkNavigationContext, info, request);
146                 if (available) {
147                     try {
148                         PolicyUtil.checkPermissions(SSL_TUNNEL_RESOURCE_TYPE, new Permission[] {
149                                         PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN,
150                                         PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN }, request);
151                         available = true;
152                     } catch (Exception JavaDoc e1) {
153                         available = false;
154                     }
155                 }
156                 return available;
157             }
158         });
159     }
160
161     void initPageTasks() throws Exception JavaDoc {
162         MenuTree tree = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
163
164         // Tunnels showTunnels
165
tree.addMenuItem(null, new MenuItem("showTunnels", null, null, 100, false, SessionInfo.MANAGEMENT_CONSOLE_CONTEXT));
166         tree.addMenuItem("showTunnels", new MenuItem("createTunnel", "tunnels", "/defaultTunnelDetails.do", 100, true, "_self",
167                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, SSL_TUNNEL_RESOURCE_TYPE,
168                         new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN }));
169     }
170
171     void initService() throws Exception JavaDoc{
172         DefaultAgentManager.getInstance().registerService(TunnelingService.class);
173     }
174     
175     public void stopPlugin() throws ExtensionException {
176         super.stopPlugin();
177         try {
178             stopDatabase();
179             stopPolicyFramework();
180             removeTableItemActions();
181             removeMainMenu();
182             removePageTasks();
183             stopService();
184         } catch (Exception JavaDoc e) {
185             throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e);
186         }
187     }
188
189     private void stopService() {
190         DefaultAgentManager.getInstance().unregisterService(TunnelingService.class);
191     }
192
193     private void removePageTasks() {
194         MenuTree tree = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
195         tree.removeMenuItem("showTunnels", "createTunnel");
196     }
197
198     private void removeMainMenu() {
199         MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
200         tree.removeMenuItem("resources", "userTunnels");
201         tree.removeMenuItem("globalResources", "tunnels");
202     }
203
204     private void removeTableItemActions() {
205         MenuTree tree = NavigationManager.getMenuTree(TableItemActionMenuTree.MENU_TABLE_ITEM_ACTION_MENU_TREE);
206         tree.removeMenuItem("tunnel", AddToFavoritesAction.TABLE_ITEM_ACTION_ID);
207         tree.removeMenuItem("tunnel", RemoveFromFavoritesAction.TABLE_ITEM_ACTION_ID);
208         tree.removeMenuItem("tunnel", RemoveResourceAction.TABLE_ITEM_ACTION_ID);
209         tree.removeMenuItem("tunnel", EditResourceAction.TABLE_ITEM_ACTION_ID);
210         tree.removeMenuItem("tunnel", SwitchOnAction.TABLE_ITEM_ACTION_ID);
211         tree.removeMenuItem("tunnel", SwitchOffAction.TABLE_ITEM_ACTION_ID);
212     }
213
214     private void stopPolicyFramework() throws Exception JavaDoc {
215         PolicyDatabase pdb = PolicyDatabaseFactory.getInstance();
216         pdb.deregisterResourceType(SSL_TUNNEL_RESOURCE_TYPE);
217     }
218
219     private void stopDatabase() throws Exception JavaDoc {
220         TunnelDatabaseFactory.getInstance().close();
221     }
222
223 }
224
Popular Tags