KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > sample > SamplePlugin


1 package com.sslexplorer.sample;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import com.sslexplorer.core.CoreEvent;
7 import com.sslexplorer.core.CoreListener;
8 import com.sslexplorer.core.CoreMenuTree;
9 import com.sslexplorer.core.CoreServlet;
10 import com.sslexplorer.core.MenuItem;
11 import com.sslexplorer.core.PageTaskMenuTree;
12 import com.sslexplorer.navigation.MenuTree;
13 import com.sslexplorer.navigation.NavigationManager;
14 import com.sslexplorer.plugin.Plugin;
15 import com.sslexplorer.plugin.PluginDefinition;
16 import com.sslexplorer.plugin.PluginException;
17 import com.sslexplorer.policyframework.Permission;
18 import com.sslexplorer.policyframework.PolicyConstants;
19 import com.sslexplorer.security.SessionInfo;
20 import com.sslexplorer.security.UserAttributeDefinition;
21
22 /**
23  * Sample implementation of a {@link Plugin} that amongst other things provides
24  * a new {@link com.sslexplorer.sample.Sample} resource that can be managed in
25  * SSL-Explorer.
26  * <p>
27  * The resources are held in memory for the life of the server.
28  *
29  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
30  */

31 public class SamplePlugin implements Plugin {
32
33     // Event code constants
34
static SampleDatabase database;
35
36     /**
37      * New sample record created
38      */

39     public final static int EVT_SAMPLE_CREATED = 50000;
40     public final static int EVT_SAMPLE_UPDATED = 50001;
41     public final static int EVT_SAMPLE_DELETED = 50002;
42
43     final static Log log = LogFactory.getLog(SamplePlugin.class);
44
45     /**
46      * Constructor
47      */

48     public SamplePlugin() {
49         super();
50     }
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see com.sslexplorer.plugin.Plugin#startPlugin()
56      */

57     public void startPlugin() throws PluginException {
58         log.info("Starting Sample Plugin");
59
60         // Register the resource type and add the permissions
61
try {
62             CoreServlet.getServlet().getPolicyDatabase().registerResourceType(Sample.SAMPLE_RESOURCE_TYPE);
63             Sample.SAMPLE_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN);
64             Sample.SAMPLE_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_EDIT_AND_ASSIGN);
65             Sample.SAMPLE_RESOURCE_TYPE.addPermission(PolicyConstants.PERM_DELETE);
66         } catch (Exception JavaDoc e) {
67             throw new PluginException("Failed to register resource type.", e);
68         }
69
70         // Add the new items to the main menu
71
MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
72         tree.addMenuItem("resources", new MenuItem("userSamples", "sample", "/showUserSamples.do", 50, true, null,
73                         SessionInfo.USER_CONSOLE_CONTEXT, null, null, Sample.SAMPLE_RESOURCE_TYPE));
74         tree.addMenuItem("globalResources", new MenuItem("managementSamples", "sample", "/showSamples.do", 50, true, null,
75                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, Sample.SAMPLE_RESOURCE_TYPE, new Permission[] {
76                                         PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_EDIT_AND_ASSIGN,
77                                         PolicyConstants.PERM_DELETE }, Sample.SAMPLE_RESOURCE_TYPE));
78
79         // Add the new page tasks
80
MenuTree pageTasks = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
81         pageTasks.addMenuItem(null, new MenuItem("showSamples", null, null, 100, false, SessionInfo.MANAGEMENT_CONSOLE_CONTEXT));
82         pageTasks.addMenuItem("showSamples", new MenuItem("createSample", "sample", "/sampleDefaultDetails.do", 100, true, "_self",
83                         SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, Sample.SAMPLE_RESOURCE_TYPE,
84                         new Permission[] { PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN }));
85
86         // This demonstrates listening for events. In this case we will look for
87
// 'sample created' events and display a message on the console
88
CoreServlet.getServlet().addCoreListener(new CoreListener() {
89             public void coreEvent(CoreEvent evt) {
90                 if (evt.getId() == SamplePlugin.EVT_SAMPLE_CREATED) {
91                     System.err.println("****************************************");
92                     System.err.println("* Someone just created a sample!!! *");
93                     System.err.println("****************************************");
94                 }
95             }
96
97         });
98
99         /*
100          * The following code demonstrates the registering user attribute
101          * definitions. See the message resources for how to provide the
102          * titles and descriptions for attributes and categories.
103          */

104         try {
105             CoreServlet.getServlet().getUserDatabase().registerUserAttributeDefinition(
106                             new UserAttributeDefinition(
107                                   UserAttributeDefinition.TYPE_STRING,
108                                   "sample1", "", 20000, "", "",
109                                   UserAttributeDefinition.USER_OVERRIDABLE_ATTRIBUTE,
110                                   10, "sample", false, "", "", true, true));
111             CoreServlet.getServlet().getUserDatabase().registerUserAttributeDefinition(
112                             new UserAttributeDefinition(
113                                   UserAttributeDefinition.TYPE_STRING,
114                                   "sample2", "", 20000, "", "",
115                                   UserAttributeDefinition.USER_VIEWABLE_ATTRIBUTE,
116                                   20, "sample", false, "Default Sample 2 Value", "", true, true));
117             CoreServlet.getServlet().getUserDatabase().registerUserAttributeDefinition(
118                             new UserAttributeDefinition(
119                                   UserAttributeDefinition.TYPE_STRING,
120                                   "sample3", "", 20001, "", "",
121                                   UserAttributeDefinition.USER_OVERRIDABLE_ATTRIBUTE,
122                                   20, "sample", false, "Default Sample 2 Value", "", true, true));
123         }
124         catch(Exception JavaDoc e) {
125             throw new PluginException("Failed to register user attribute definitions.", e);
126         }
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.sslexplorer.plugin.Plugin#canStopPlugin()
133      */

134     public boolean canStopPlugin() {
135         return false;
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see com.sslexplorer.plugin.Plugin#stopPlugin()
142      */

143     public void stopPlugin() throws PluginException {
144         try {
145             CoreServlet.getServlet().getPolicyDatabase().deregisterResourceType(Sample.SAMPLE_RESOURCE_TYPE);
146         } catch (Exception JavaDoc e) {
147             log.warn("Failed to deregister resource type.", e);
148         }
149
150         // Remove the new items from the main menu
151
MenuTree tree = NavigationManager.getMenuTree(CoreMenuTree.MENU_ITEM_MENU_TREE);
152         tree.removeMenuItem("resources", "userSamples");
153         tree.removeMenuItem("globalResources", "managementSamples");
154
155         // Remove the new page tasks
156
MenuTree pageTasks = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
157         pageTasks.removeMenuItem(null, "showSamples");
158     }
159
160     /**
161      * Get a static instance of the database used to store the samples
162      *
163      * @return samples database
164      */

165     public static SampleDatabase getDatabase() {
166         return database;
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see com.sslexplorer.plugin.Plugin#getTilesConfigFile()
173      */

174     public String JavaDoc getTilesConfigFile() {
175         return "/WEB-INF/sslexplorer-sample-tiles-defs.xml";
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see com.sslexplorer.plugin.Plugin#initPlugin(com.sslexplorer.plugin.PluginDefinition)
182      */

183     public void initPlugin(PluginDefinition definition) throws PluginException {
184         database = new SampleDatabase();
185         try {
186             database.open(CoreServlet.getServlet());
187         } catch (Exception JavaDoc e) {
188             throw new PluginException("Failed to open samples database.");
189         }
190     }
191
192
193
194     /* (non-Javadoc)
195      * @see com.sslexplorer.plugin.Plugin#installPlugin()
196      */

197     public void installPlugin() throws PluginException {
198     }
199
200     /* (non-Javadoc)
201      * @see com.sslexplorer.plugin.Plugin#uninstallPlugin()
202      */

203     public void uninstallPlugin() throws PluginException {
204     }
205
206 }
207
Popular Tags