KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > configmanager > ConfigManagerPortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.configmanager;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.portlet.ActionRequest;
28 import javax.portlet.ActionResponse;
29 import javax.portlet.PortletConfig;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletRequestDispatcher;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.portlet.WindowState;
35 import org.apache.geronimo.console.BasePortlet;
36 import org.apache.geronimo.console.util.PortletManager;
37 import org.apache.geronimo.gbean.AbstractName;
38 import org.apache.geronimo.kernel.DependencyManager;
39 import org.apache.geronimo.kernel.Kernel;
40 import org.apache.geronimo.kernel.KernelRegistry;
41 import org.apache.geronimo.kernel.config.Configuration;
42 import org.apache.geronimo.kernel.config.ConfigurationInfo;
43 import org.apache.geronimo.kernel.config.ConfigurationManager;
44 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
45 import org.apache.geronimo.kernel.config.ConfigurationUtil;
46 import org.apache.geronimo.kernel.config.InvalidConfigException;
47 import org.apache.geronimo.kernel.config.LifecycleException;
48 import org.apache.geronimo.kernel.config.NoSuchConfigException;
49 import org.apache.geronimo.kernel.management.State;
50 import org.apache.geronimo.kernel.repository.Artifact;
51 import org.apache.geronimo.management.geronimo.WebModule;
52
53 public class ConfigManagerPortlet extends BasePortlet {
54
55     private static final String JavaDoc START_ACTION = "start";
56
57     private static final String JavaDoc STOP_ACTION = "stop";
58
59     private static final String JavaDoc RESTART_ACTION = "restart";
60
61     private static final String JavaDoc UNINSTALL_ACTION = "uninstall";
62
63     private static final String JavaDoc CONFIG_INIT_PARAM = "config-type";
64
65     private String JavaDoc messageStatus = "";
66
67     private Kernel kernel;
68
69     private PortletRequestDispatcher normalView;
70
71     private PortletRequestDispatcher maximizedView;
72
73     private PortletRequestDispatcher helpView;
74
75     public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException JavaDoc {
76         String JavaDoc action = actionRequest.getParameter("action");
77         actionResponse.setRenderParameter("message", ""); // set to blank first
78
try {
79             ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
80             String JavaDoc config = getConfigID(actionRequest);
81             Artifact configId = Artifact.create(config);
82
83             if (START_ACTION.equals(action)) {
84                 if(!configurationManager.isLoaded(configId)) {
85                     configurationManager.loadConfiguration(configId);
86                 }
87                 if(!configurationManager.isRunning(configId)) {
88                     configurationManager.startConfiguration(configId);
89                     messageStatus = "Started application<br /><br />";
90                 }
91             } else if (STOP_ACTION.equals(action)) {
92                 if(configurationManager.isRunning(configId)) {
93                     configurationManager.stopConfiguration(configId);
94                 }
95                 if(configurationManager.isLoaded(configId)) {
96                     configurationManager.unloadConfiguration(configId);
97                     messageStatus = "Stopped application<br /><br />";
98                 }
99             } else if (UNINSTALL_ACTION.equals(action)) {
100                 configurationManager.uninstallConfiguration(configId);
101                 messageStatus = "Uninstalled application<br /><br />";
102             } else if (RESTART_ACTION.equals(action)) {
103                 configurationManager.restartConfiguration(configId);
104                 messageStatus = "Restarted application<br /><br />";
105             } else {
106                 messageStatus = "Invalid value for changeState: " + action + "<br /><br />";
107                 throw new PortletException("Invalid value for changeState: " + action);
108             }
109         } catch (NoSuchConfigException e) {
110             // ignore this for now
111
messageStatus = "Configuration not found<br /><br />";
112             throw new PortletException("Configuration not found", e);
113         } catch (LifecycleException e) {
114             // todo we have a much more detailed report now
115
messageStatus = "Lifecycle operation failed<br /><br />";
116             throw new PortletException("Exception", e);
117         } catch (Exception JavaDoc e) {
118             messageStatus = "Encountered an unhandled exception<br /><br />";
119             throw new PortletException("Exception", e);
120         }
121     }
122
123     /**
124      * Check if a configuration should be listed here. This method depends on the "config-type" portlet parameter
125      * which is set in portle.xml.
126      */

127     private boolean shouldListConfig(ConfigurationInfo info) {
128         String JavaDoc configType = getInitParameter(CONFIG_INIT_PARAM);
129         return configType == null || info.getType().getName().equalsIgnoreCase(configType);
130     }
131
132     /*
133      * private URI getConfigID(ActionRequest actionRequest) throws
134      * PortletException { URI configID; try { configID = new
135      * URI(actionRequest.getParameter("configId")); } catch (URISyntaxException
136      * e) { throw new PortletException("Invalid configId parameter: " +
137      * actionRequest.getParameter("configId")); } return configID; }
138      */

139
140     private String JavaDoc getConfigID(ActionRequest actionRequest) {
141         return actionRequest.getParameter("configId");
142     }
143
144     protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException JavaDoc, PortletException {
145         if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
146             return;
147         }
148
149         List JavaDoc moduleDetails = new ArrayList JavaDoc();
150         ConfigurationManager configManager = ConfigurationUtil.getConfigurationManager(kernel);
151         DependencyManager depMgr = kernel.getDependencyManager();
152         List JavaDoc infos = configManager.listConfigurations();
153         for (Iterator JavaDoc j = infos.iterator(); j.hasNext();) {
154             ConfigurationInfo info = (ConfigurationInfo) j.next();
155             if (shouldListConfig(info)) {
156                 ModuleDetails details = new ModuleDetails(info.getConfigID(), info.getType(), info.getState());
157
158                 if (info.getType().getValue()== ConfigurationModuleType.WAR.getValue()){
159                     WebModule webModule = (WebModule) PortletManager.getModule(renderRequest, info.getConfigID());
160                     if (webModule != null) {
161                         details.setContextPath(webModule.getContextPath());
162                         details.setUrlFor(webModule.getURLFor());
163                     }
164                 }
165                 try {
166                     AbstractName configObjName = Configuration.getConfigurationAbstractName(info.getConfigID());
167                     boolean flag = false;
168                     // Check if the configuration is loaded. If not, load it to get information.
169
if(!kernel.isLoaded(configObjName)) {
170                         flag = true;
171                         try {
172                             configManager.loadConfiguration(configObjName.getArtifact());
173                         } catch (NoSuchConfigException e) {
174                             // Should not occur
175
e.printStackTrace();
176                         } catch (LifecycleException e) {
177                             // TODO Auto-generated catch block
178
e.printStackTrace();
179                         }
180                     }
181
182                     java.util.Set JavaDoc parents = depMgr.getParents(configObjName);
183                     for(Iterator JavaDoc itr = parents.iterator(); itr.hasNext(); ) {
184                         AbstractName parent = (AbstractName)itr.next();
185                         details.getParents().add(parent.getArtifact());
186                     }
187                     java.util.Set JavaDoc children = depMgr.getChildren(configObjName);
188                     for(Iterator JavaDoc itr = children.iterator(); itr.hasNext(); ) {
189                         AbstractName child = (AbstractName)itr.next();
190                         //if(configManager.isConfiguration(child.getArtifact()))
191
if(child.getNameProperty("configurationName") != null) {
192                             details.getChildren().add(child.getArtifact());
193                         }
194                     }
195                     Collections.sort(details.getParents());
196                     Collections.sort(details.getChildren());
197
198                     // Unload the configuration if it has been loaded earlier for the sake of getting information
199
if(flag) {
200                         try {
201                             configManager.unloadConfiguration(configObjName.getArtifact());
202                         } catch (NoSuchConfigException e) {
203                             // Should not occur
204
e.printStackTrace();
205                         }
206                     }
207                 } catch(InvalidConfigException ice) {
208                     // Should not occur
209
ice.printStackTrace();
210                 }
211                 moduleDetails.add(details);
212             }
213         }
214         Collections.sort(moduleDetails);
215         renderRequest.setAttribute("configurations", moduleDetails);
216         renderRequest.setAttribute("showWebInfo", Boolean.valueOf(getInitParameter(CONFIG_INIT_PARAM).equalsIgnoreCase(ConfigurationModuleType.WAR.getName())));
217         if (moduleDetails.size() == 0) {
218             renderRequest.setAttribute("messageInstalled", "No modules found of this type<br /><br />");
219         } else {
220             renderRequest.setAttribute("messageInstalled", "");
221         }
222         renderRequest.setAttribute("messageStatus", messageStatus);
223         messageStatus = "";
224         if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
225             normalView.include(renderRequest, renderResponse);
226         } else {
227             maximizedView.include(renderRequest, renderResponse);
228         }
229     }
230
231     protected void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException JavaDoc {
232         helpView.include(renderRequest, renderResponse);
233     }
234
235     public void init(PortletConfig portletConfig) throws PortletException {
236         super.init(portletConfig);
237         kernel = KernelRegistry.getSingleKernel();
238         normalView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/normal.jsp");
239         maximizedView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/maximized.jsp");
240         helpView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/help.jsp");
241     }
242
243     public void destroy() {
244         normalView = null;
245         maximizedView = null;
246         kernel = null;
247         super.destroy();
248     }
249
250     /**
251      * Convenience data holder for portlet that displays deployed modules.
252      * Includes context path information for web modules.
253      */

254     public static class ModuleDetails implements Comparable JavaDoc, Serializable JavaDoc {
255         private final Artifact configId;
256         private final ConfigurationModuleType type;
257         private final State state;
258         private URL JavaDoc urlFor; // only relevant for webapps
259
private String JavaDoc contextPath; // only relevant for webapps
260
private List JavaDoc parents = new ArrayList JavaDoc();
261         private List JavaDoc children = new ArrayList JavaDoc();
262
263         public ModuleDetails(Artifact configId, ConfigurationModuleType type, State state) {
264             this.configId = configId;
265             this.type = type;
266             this.state = state;
267         }
268
269         public ModuleDetails(Artifact configId, ConfigurationModuleType type, State state, List JavaDoc parents, List JavaDoc children) {
270             this.configId = configId;
271             this.type = type;
272             this.state = state;
273             this.parents = parents;
274             this.children = children;
275         }
276
277         public int compareTo(Object JavaDoc o) {
278             if (o != null && o instanceof ModuleDetails){
279                 return configId.compareTo(((ModuleDetails)o).configId);
280             } else {
281                 return -1;
282             }
283         }
284
285         public Artifact getConfigId() {
286             return configId;
287         }
288
289         public State getState() {
290             return state;
291         }
292
293         public URL JavaDoc getUrlFor() {
294             return urlFor;
295         }
296
297         public String JavaDoc getContextPath() {
298             return contextPath;
299         }
300
301         public void setUrlFor(URL JavaDoc urlFor) {
302             this.urlFor = urlFor;
303         }
304
305         public void setContextPath(String JavaDoc contextPath) {
306             this.contextPath = contextPath;
307         }
308
309         public ConfigurationModuleType getType() {
310             return type;
311         }
312
313         public List JavaDoc getParents() {
314             return parents;
315         }
316
317         public List JavaDoc getChildren() {
318             return children;
319         }
320     }
321 }
322
Popular Tags