1 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.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 START_ACTION = "start"; 56 57 private static final String STOP_ACTION = "stop"; 58 59 private static final String RESTART_ACTION = "restart"; 60 61 private static final String UNINSTALL_ACTION = "uninstall"; 62 63 private static final String CONFIG_INIT_PARAM = "config-type"; 64 65 private String 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 { 76 String action = actionRequest.getParameter("action"); 77 actionResponse.setRenderParameter("message", ""); try { 79 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 80 String 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 messageStatus = "Configuration not found<br /><br />"; 112 throw new PortletException("Configuration not found", e); 113 } catch (LifecycleException e) { 114 messageStatus = "Lifecycle operation failed<br /><br />"; 116 throw new PortletException("Exception", e); 117 } catch (Exception e) { 118 messageStatus = "Encountered an unhandled exception<br /><br />"; 119 throw new PortletException("Exception", e); 120 } 121 } 122 123 127 private boolean shouldListConfig(ConfigurationInfo info) { 128 String configType = getInitParameter(CONFIG_INIT_PARAM); 129 return configType == null || info.getType().getName().equalsIgnoreCase(configType); 130 } 131 132 139 140 private String getConfigID(ActionRequest actionRequest) { 141 return actionRequest.getParameter("configId"); 142 } 143 144 protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException , PortletException { 145 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) { 146 return; 147 } 148 149 List moduleDetails = new ArrayList (); 150 ConfigurationManager configManager = ConfigurationUtil.getConfigurationManager(kernel); 151 DependencyManager depMgr = kernel.getDependencyManager(); 152 List infos = configManager.listConfigurations(); 153 for (Iterator 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 if(!kernel.isLoaded(configObjName)) { 170 flag = true; 171 try { 172 configManager.loadConfiguration(configObjName.getArtifact()); 173 } catch (NoSuchConfigException e) { 174 e.printStackTrace(); 176 } catch (LifecycleException e) { 177 e.printStackTrace(); 179 } 180 } 181 182 java.util.Set parents = depMgr.getParents(configObjName); 183 for(Iterator itr = parents.iterator(); itr.hasNext(); ) { 184 AbstractName parent = (AbstractName)itr.next(); 185 details.getParents().add(parent.getArtifact()); 186 } 187 java.util.Set children = depMgr.getChildren(configObjName); 188 for(Iterator itr = children.iterator(); itr.hasNext(); ) { 189 AbstractName child = (AbstractName)itr.next(); 190 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 if(flag) { 200 try { 201 configManager.unloadConfiguration(configObjName.getArtifact()); 202 } catch (NoSuchConfigException e) { 203 e.printStackTrace(); 205 } 206 } 207 } catch(InvalidConfigException ice) { 208 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 { 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 254 public static class ModuleDetails implements Comparable , Serializable { 255 private final Artifact configId; 256 private final ConfigurationModuleType type; 257 private final State state; 258 private URL urlFor; private String contextPath; private List parents = new ArrayList (); 261 private List children = new ArrayList (); 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 parents, List 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 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 getUrlFor() { 294 return urlFor; 295 } 296 297 public String getContextPath() { 298 return contextPath; 299 } 300 301 public void setUrlFor(URL urlFor) { 302 this.urlFor = urlFor; 303 } 304 305 public void setContextPath(String contextPath) { 306 this.contextPath = contextPath; 307 } 308 309 public ConfigurationModuleType getType() { 310 return type; 311 } 312 313 public List getParents() { 314 return parents; 315 } 316 317 public List getChildren() { 318 return children; 319 } 320 } 321 } 322 | Popular Tags |