View Javadoc

1   /**
2    *
3    * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
21  import java.io.Serializable;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
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.kernel.Kernel;
38  import org.apache.geronimo.kernel.KernelRegistry;
39  import org.apache.geronimo.kernel.config.ConfigurationInfo;
40  import org.apache.geronimo.kernel.config.ConfigurationManager;
41  import org.apache.geronimo.kernel.config.ConfigurationModuleType;
42  import org.apache.geronimo.kernel.config.ConfigurationUtil;
43  import org.apache.geronimo.kernel.config.LifecycleException;
44  import org.apache.geronimo.kernel.config.NoSuchConfigException;
45  import org.apache.geronimo.kernel.management.State;
46  import org.apache.geronimo.kernel.repository.Artifact;
47  import org.apache.geronimo.management.geronimo.WebModule;
48  
49  public class ConfigManagerPortlet extends BasePortlet {
50  
51      private static final String START_ACTION = "start";
52  
53      private static final String STOP_ACTION = "stop";
54  
55      private static final String RESTART_ACTION = "restart";
56  
57      private static final String UNINSTALL_ACTION = "uninstall";
58  
59      private static final String CONFIG_INIT_PARAM = "config-type";
60  
61      private String messageStatus = "";
62  
63      private Kernel kernel;
64  
65      private PortletRequestDispatcher normalView;
66  
67      private PortletRequestDispatcher maximizedView;
68  
69      private PortletRequestDispatcher helpView;
70  
71      public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
72          String action = actionRequest.getParameter("action");
73          actionResponse.setRenderParameter("message", ""); // set to blank first
74          try {
75              ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
76              String config = getConfigID(actionRequest);
77              Artifact configId = Artifact.create(config);
78  
79              if (START_ACTION.equals(action)) {
80                  if(!configurationManager.isLoaded(configId)) {
81                      configurationManager.loadConfiguration(configId);
82                  }
83                  if(!configurationManager.isRunning(configId)) {
84                      configurationManager.startConfiguration(configId);
85                      messageStatus = "Started application<br /><br />";
86                  }
87              } else if (STOP_ACTION.equals(action)) {
88                  if(configurationManager.isRunning(configId)) {
89                      configurationManager.stopConfiguration(configId);
90                  }
91                  if(configurationManager.isLoaded(configId)) {
92                      configurationManager.unloadConfiguration(configId);
93                      messageStatus = "Stopped application<br /><br />";
94                  }
95              } else if (UNINSTALL_ACTION.equals(action)) {
96                  configurationManager.uninstallConfiguration(configId);
97                  messageStatus = "Uninstalled application<br /><br />";
98              } else if (RESTART_ACTION.equals(action)) {
99                  configurationManager.restartConfiguration(configId);
100                 messageStatus = "Restarted application<br /><br />";
101             } else {
102                 messageStatus = "Invalid value for changeState: " + action + "<br /><br />";
103                 throw new PortletException("Invalid value for changeState: " + action);
104             }
105         } catch (NoSuchConfigException e) {
106             // ignore this for now
107             messageStatus = "Configuration not found<br /><br />";
108             throw new PortletException("Configuration not found", e);
109         } catch (LifecycleException e) {
110             // todo we have a much more detailed report now
111             messageStatus = "Lifecycle operation failed<br /><br />";
112             throw new PortletException("Exception", e);
113         } catch (Exception e) {
114             messageStatus = "Encountered an unhandled exception<br /><br />";
115             throw new PortletException("Exception", e);
116         }
117     }
118 
119     /**
120      * Check if a configuration should be listed here. This method depends on the "config-type" portlet parameter
121      * which is set in portle.xml.
122      */
123     private boolean shouldListConfig(ConfigurationInfo info) {
124         String configType = getInitParameter(CONFIG_INIT_PARAM);
125         return configType == null || info.getType().getName().equalsIgnoreCase(configType);
126     }
127 
128     /*
129      * private URI getConfigID(ActionRequest actionRequest) throws
130      * PortletException { URI configID; try { configID = new
131      * URI(actionRequest.getParameter("configId")); } catch (URISyntaxException
132      * e) { throw new PortletException("Invalid configId parameter: " +
133      * actionRequest.getParameter("configId")); } return configID; }
134      */
135 
136     private String getConfigID(ActionRequest actionRequest) {
137         return actionRequest.getParameter("configId");
138     }
139 
140     protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
141         if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
142             return;
143         }
144 
145         List moduleDetails = new ArrayList();
146         ConfigurationManager configManager = ConfigurationUtil.getConfigurationManager(kernel);
147         List infos = configManager.listConfigurations();
148         for (Iterator j = infos.iterator(); j.hasNext();) {
149             ConfigurationInfo info = (ConfigurationInfo) j.next();
150             if (shouldListConfig(info)) {
151                 ModuleDetails details = new ModuleDetails(info.getConfigID(), info.getType(), info.getState());
152 
153                 if (info.getType().getValue()== ConfigurationModuleType.WAR.getValue()){
154                     WebModule webModule = (WebModule) PortletManager.getModule(renderRequest, info.getConfigID());
155                     if (webModule != null) {
156                         details.setContextPath(webModule.getContextPath());
157                         details.setUrlFor(webModule.getURLFor());
158                     }
159                 }
160                 moduleDetails.add(details);
161             }
162         }
163         Collections.sort(moduleDetails);
164         renderRequest.setAttribute("configurations", moduleDetails);
165         renderRequest.setAttribute("showWebInfo", Boolean.valueOf(getInitParameter(CONFIG_INIT_PARAM).equalsIgnoreCase(ConfigurationModuleType.WAR.getName())));
166         if (moduleDetails.size() == 0) {
167             renderRequest.setAttribute("messageInstalled", "No modules found of this type<br /><br />");
168         } else {
169             renderRequest.setAttribute("messageInstalled", "");
170         }
171         renderRequest.setAttribute("messageStatus", messageStatus);
172         messageStatus = "";
173         if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
174             normalView.include(renderRequest, renderResponse);
175         } else {
176             maximizedView.include(renderRequest, renderResponse);
177         }
178     }
179 
180     protected void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException {
181         helpView.include(renderRequest, renderResponse);
182     }
183 
184     public void init(PortletConfig portletConfig) throws PortletException {
185         super.init(portletConfig);
186         kernel = KernelRegistry.getSingleKernel();
187         normalView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/normal.jsp");
188         maximizedView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/maximized.jsp");
189         helpView = portletConfig.getPortletContext().getRequestDispatcher("/WEB-INF/view/configmanager/help.jsp");
190     }
191 
192     public void destroy() {
193         normalView = null;
194         maximizedView = null;
195         kernel = null;
196         super.destroy();
197     }
198 
199     /**
200      * Convenience data holder for portlet that displays deployed modules.
201      * Includes context path information for web modules.
202      */
203     public static class ModuleDetails implements Comparable, Serializable {
204         private final Artifact configId;
205         private final ConfigurationModuleType type;
206         private final State state;
207         private URL urlFor;             // only relevant for webapps
208         private String contextPath;     // only relevant for webapps
209 
210         public ModuleDetails(Artifact configId, ConfigurationModuleType type, State state) {
211             this.configId = configId;
212             this.type = type;
213             this.state = state;
214         }
215 
216         public int compareTo(Object o) {
217             if (o != null && o instanceof ModuleDetails){
218                 return configId.compareTo(((ModuleDetails)o).configId);
219             } else {
220                 return -1;
221             }
222         }
223 
224         public Artifact getConfigId() {
225             return configId;
226         }
227 
228         public State getState() {
229             return state;
230         }
231 
232         public URL getUrlFor() {
233             return urlFor;
234         }
235 
236         public String getContextPath() {
237             return contextPath;
238         }
239 
240         public void setUrlFor(URL urlFor) {
241             this.urlFor = urlFor;
242         }
243 
244         public void setContextPath(String contextPath) {
245             this.contextPath = contextPath;
246         }
247 
248         public ConfigurationModuleType getType() {
249             return type;
250         }
251     }
252 }