1 19 20 package com.sslexplorer.applications; 21 22 import java.util.Iterator ; 23 24 import javax.servlet.http.HttpServletRequest ; 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 79 public class ApplicationsPlugin extends DefaultPlugin implements CoreListener { 80 81 84 public final static int APPLICATION_SHORTCUT_RESOURCE_TYPE_ID = 3; 85 86 89 public static final ResourceType APPLICATION_SHORTCUT_RESOURCE_TYPE = new ApplicationShortcutResourceType(); 90 91 94 public static final String BUNDLE_ID = "sslexplorer-community-applications"; 95 96 final static Log log = LogFactory.getLog(ApplicationsPlugin.class); 97 98 101 public static final String MESSAGE_RESOURCES_KEY = "applications"; 102 103 106 public ApplicationsPlugin() { 107 super("/WEB-INF/sslexplorer-community-applications-tiles-defs.xml", true); 108 } 109 110 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 e) { 118 throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e); 119 } 120 } 121 122 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 e) { 139 e.printStackTrace(); 140 throw new ExtensionException(ExtensionException.INTERNAL_ERROR, e); 141 } 142 } 143 144 void initDatabase() throws Exception { 145 ApplicationShortcutDatabaseFactory.getInstance().open(CoreServlet.getServlet(), this.getPluginDefinition()); 146 } 147 148 void initAgentService() throws Exception { 149 DefaultAgentManager.getInstance().registerService(ApplicationService.class); 150 } 151 152 void initExtensionTypes() throws Exception { 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 { 161 162 PolicyDatabase pdb = PolicyDatabaseFactory.getInstance(); 163 164 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 { 173 MenuTree tree = NavigationManager.getMenuTree(TableItemActionMenuTree.MENU_TABLE_ITEM_ACTION_MENU_TREE); 174 175 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 { 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 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 e1) { 231 available = false; 232 } 233 } 234 return available; 235 } 236 }); 237 } 238 239 void initPageTasks() throws Exception { 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 itr = bundle.iterator(); itr.hasNext();) { 263 ExtensionDescriptor app = (ExtensionDescriptor) itr.next(); 264 try { 265 ApplicationShortcutDatabaseFactory.getInstance().removeApplicationShortcuts(app.getId()); 266 } catch (Exception 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 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 { 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 { 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 |