1 17 package org.apache.servicemix.console; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.audit.AuditorMBean; 22 import org.apache.servicemix.jbi.audit.jdbc.JdbcAuditor; 23 import org.apache.servicemix.jbi.container.JBIContainer; 24 import org.apache.servicemix.jbi.framework.DeploymentService; 25 import org.apache.servicemix.jbi.framework.InstallationService; 26 import org.apache.servicemix.jbi.management.ManagementContext; 27 import org.apache.servicemix.jbi.management.ManagementContextMBean; 28 29 import javax.jbi.management.DeploymentServiceMBean; 30 import javax.jbi.management.InstallationServiceMBean; 31 import javax.jbi.management.LifeCycleMBean; 32 import javax.management.MBeanServerConnection ; 33 import javax.management.MBeanServerInvocationHandler ; 34 import javax.management.ObjectName ; 35 import javax.management.remote.JMXConnector ; 36 import javax.management.remote.JMXConnectorFactory ; 37 import javax.management.remote.JMXServiceURL ; 38 import javax.portlet.ActionRequest; 39 import javax.portlet.ActionResponse; 40 import javax.portlet.GenericPortlet; 41 import javax.portlet.PortletConfig; 42 import javax.portlet.PortletContext; 43 import javax.portlet.PortletException; 44 import javax.portlet.PortletRequestDispatcher; 45 import javax.portlet.RenderRequest; 46 import javax.portlet.RenderResponse; 47 import javax.portlet.WindowState; 48 49 import java.io.IOException ; 50 import java.net.MalformedURLException ; 51 52 public abstract class ServiceMixPortlet extends GenericPortlet { 53 54 protected final Log log = LogFactory.getLog(getClass()); 55 56 protected PortletRequestDispatcher normalView; 57 protected PortletRequestDispatcher helpView; 58 protected PortletRequestDispatcher errorView; 59 60 private JMXConnector jmxConnector; 61 private String namingHost = "localhost"; 62 private String containerName = JBIContainer.DEFAULT_NAME; 63 private String jmxDomainName = ManagementContext.DEFAULT_DOMAIN; 64 private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT; 65 private String jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH; 66 67 71 public JMXServiceURL getServiceURL(){ 72 JMXServiceURL url = null; 73 try { 74 url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://" + namingHost + ":" + namingPort + jndiPath); 75 } 76 catch (MalformedURLException e) { 77 log.error("error creating serviceURL: ",e); 78 } 79 return url; 80 } 81 82 88 public JMXConnector getJMXConnector (JMXServiceURL url) throws IOException { 89 log.info("Connecting to JBI Container at: " + url); 90 return JMXConnectorFactory.connect(url); 91 } 92 93 protected void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException { 94 log.debug("doHelp"); 95 helpView.include(renderRequest, renderResponse); 96 } 97 98 protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException { 99 log.debug("doView"); 100 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) { 101 return; 102 } 103 try { 104 if (this.jmxConnector == null) { 106 this.jmxConnector = getJMXConnector(getServiceURL()); 107 } 108 renderView(renderRequest, renderResponse); 109 } catch (PortletException e) { 110 log.error("Error rendering portlet", e); 111 closeConnector(); 112 throw e; 113 } catch (IOException e) { 114 log.error("Error rendering portlet", e); 115 closeConnector(); 116 throw e; 117 } catch (Exception e) { 118 try { 119 renderRequest.setAttribute("exception", e); 120 errorView.include(renderRequest, renderResponse); 121 } finally { 122 closeConnector(); 123 } 124 } 127 } 128 129 protected void renderView(RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { 130 fillViewRequest(renderRequest); 132 normalView.include(renderRequest, renderResponse); 134 } 135 136 141 protected ObjectName getObjectName (Class systemClass){ 142 return ManagementContext.getSystemObjectName(jmxDomainName, containerName, systemClass); 143 } 144 145 146 protected void fillViewRequest(RenderRequest request) throws Exception { 147 } 148 149 public void init(PortletConfig portletConfig) throws PortletException { 150 log.debug("init"); 151 super.init(portletConfig); 152 PortletContext pc = portletConfig.getPortletContext(); 153 normalView = pc.getRequestDispatcher("/WEB-INF/view/" + getPortletName() + "/view.jsp"); 154 helpView = pc.getRequestDispatcher("/WEB-INF/view/" + getPortletName() + "/help.jsp"); 155 errorView = pc.getRequestDispatcher("/WEB-INF/view/error.jsp"); 156 } 157 158 public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException { 159 log.debug("processAction: " + actionRequest); 160 try { 161 if (this.jmxConnector == null) { 163 this.jmxConnector = getJMXConnector(getServiceURL()); 164 } 165 doProcessAction(actionRequest, actionResponse); 167 } catch (PortletException e) { 168 log.error("Error processing action", e); 169 closeConnector(); 170 throw e; 171 } catch (IOException e) { 172 log.error("Error processing action", e); 173 closeConnector(); 174 throw e; 175 } catch (Exception e) { 176 log.error("Error processing action", e); 177 closeConnector(); 178 throw new PortletException("Error processing action", e); 179 } 180 } 181 182 protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { 183 } 184 185 public void destroy() { 186 closeConnector(); 187 super.destroy(); 188 } 189 190 protected void closeConnector() { 191 if (this.jmxConnector != null){ 192 try { 193 jmxConnector.close(); 194 } catch (Exception e) { 195 log.warn("caught an error closing the jmxConnector", e); 196 } finally { 197 jmxConnector = null; 198 } 199 } 200 } 201 202 207 public InstallationServiceMBean getInstallationService() throws IOException { 208 ObjectName objectName = getObjectName(InstallationService.class); 209 return (InstallationServiceMBean) getProxy(objectName, InstallationServiceMBean.class); 210 } 211 212 217 public DeploymentServiceMBean getDeploymentService() throws IOException { 218 ObjectName objectName = getObjectName(DeploymentService.class); 219 return (DeploymentServiceMBean) getProxy(objectName, DeploymentServiceMBean.class); 220 } 221 222 223 228 public ManagementContextMBean getManagementContext() throws IOException { 229 ObjectName objectName = getObjectName(ManagementContext.class); 230 return (ManagementContextMBean) getProxy(objectName, ManagementContextMBean.class); 231 } 232 233 public AuditorMBean getJdbcAuditor() throws IOException { 234 ObjectName objectName = getObjectName(JdbcAuditor.class); 235 return (AuditorMBean) getProxy(objectName, AuditorMBean.class); 236 } 237 238 public LifeCycleMBean getJBIContainer() throws IOException { 239 ObjectName objectName = ManagementContext.getContainerObjectName(jmxDomainName, containerName); 240 return (LifeCycleMBean) getProxy(objectName, LifeCycleMBean.class); 241 } 242 243 public Object getProxy(ObjectName name, Class type) throws IOException { 244 return MBeanServerInvocationHandler.newProxyInstance(getServerConnection(), name, type, true); 245 } 246 247 public MBeanServerConnection getServerConnection() throws IOException { 248 return jmxConnector.getMBeanServerConnection(); 249 } 250 251 public String getContainerName() { 252 return containerName; 253 } 254 255 public void setContainerName(String containerName) { 256 this.containerName = containerName; 257 } 258 259 } 260 | Popular Tags |