KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > console > ServiceMixPortlet


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 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 JavaDoc;
33 import javax.management.MBeanServerInvocationHandler JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.remote.JMXConnector JavaDoc;
36 import javax.management.remote.JMXConnectorFactory JavaDoc;
37 import javax.management.remote.JMXServiceURL JavaDoc;
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 JavaDoc;
50 import java.net.MalformedURLException JavaDoc;
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 JavaDoc jmxConnector;
61     private String JavaDoc namingHost = "localhost";
62     private String JavaDoc containerName = JBIContainer.DEFAULT_NAME;
63     private String JavaDoc jmxDomainName = ManagementContext.DEFAULT_DOMAIN;
64     private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT;
65     private String JavaDoc jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH;
66
67     /**
68      * Get the JMXServiceURL - built from the protocol used and host names
69      * @return the url
70      */

71     public JMXServiceURL JavaDoc getServiceURL(){
72         JMXServiceURL JavaDoc url = null;
73         try {
74             url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://" + namingHost + ":" + namingPort + jndiPath);
75         }
76         catch (MalformedURLException JavaDoc e) {
77             log.error("error creating serviceURL: ",e);
78         }
79         return url;
80     }
81     
82     /**
83      * Get a JMXConnector from a url
84      * @param url
85      * @return the JMXConnector
86      * @throws IOException
87      */

88     public JMXConnector JavaDoc getJMXConnector (JMXServiceURL JavaDoc url) throws IOException JavaDoc {
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 JavaDoc {
94         log.debug("doHelp");
95         helpView.include(renderRequest, renderResponse);
96     }
97
98     protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException JavaDoc {
99         log.debug("doView");
100         if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
101             return;
102         }
103         try {
104             // Retrieve the jmx connector
105
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 JavaDoc e) {
114             log.error("Error rendering portlet", e);
115             closeConnector();
116             throw e;
117         } catch (Exception JavaDoc e) {
118             try {
119                 renderRequest.setAttribute("exception", e);
120                 errorView.include(renderRequest, renderResponse);
121             } finally {
122                 closeConnector();
123             }
124             //log.error("Error rendering portlet", e);
125
//throw new PortletException("Error rendering portlet", e);
126
}
127     }
128     
129     protected void renderView(RenderRequest renderRequest, RenderResponse renderResponse) throws Exception JavaDoc {
130         // Fill request
131
fillViewRequest(renderRequest);
132         // Render view
133
normalView.include(renderRequest, renderResponse);
134     }
135     
136     /**
137      * Get a servicemix internal system management instance, from it's class name
138      * @param systemClass
139      * @return the object name
140      */

141     protected ObjectName JavaDoc getObjectName (Class JavaDoc systemClass){
142         return ManagementContext.getSystemObjectName(jmxDomainName, containerName, systemClass);
143     }
144     
145     
146     protected void fillViewRequest(RenderRequest request) throws Exception JavaDoc {
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 JavaDoc {
159         log.debug("processAction: " + actionRequest);
160         try {
161             // Retrieve the jmx connector
162
if (this.jmxConnector == null) {
163                 this.jmxConnector = getJMXConnector(getServiceURL());
164             }
165             // Fill request
166
doProcessAction(actionRequest, actionResponse);
167         } catch (PortletException e) {
168             log.error("Error processing action", e);
169             closeConnector();
170             throw e;
171         } catch (IOException JavaDoc e) {
172             log.error("Error processing action", e);
173             closeConnector();
174             throw e;
175         } catch (Exception JavaDoc 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 JavaDoc {
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 JavaDoc e) {
195                 log.warn("caught an error closing the jmxConnector", e);
196             } finally {
197                 jmxConnector = null;
198             }
199         }
200     }
201     
202     /**
203      * Get the InstallationServiceMBean
204      * @return the installation service MBean
205      * @throws IOException
206      */

207     public InstallationServiceMBean getInstallationService() throws IOException JavaDoc {
208         ObjectName JavaDoc objectName = getObjectName(InstallationService.class);
209         return (InstallationServiceMBean) getProxy(objectName, InstallationServiceMBean.class);
210     }
211     
212     /**
213      * Get the DeploymentServiceMBean
214      * @return the deployment service mbean
215      * @throws IOException
216      */

217     public DeploymentServiceMBean getDeploymentService() throws IOException JavaDoc {
218         ObjectName JavaDoc objectName = getObjectName(DeploymentService.class);
219         return (DeploymentServiceMBean) getProxy(objectName, DeploymentServiceMBean.class);
220     }
221     
222     
223     /**
224      * Get the ManagementContextMBean
225      * @return the management service mbean
226      * @throws IOException
227      */

228     public ManagementContextMBean getManagementContext() throws IOException JavaDoc {
229         ObjectName JavaDoc objectName = getObjectName(ManagementContext.class);
230         return (ManagementContextMBean) getProxy(objectName, ManagementContextMBean.class);
231     }
232     
233     public AuditorMBean getJdbcAuditor() throws IOException JavaDoc {
234         ObjectName JavaDoc objectName = getObjectName(JdbcAuditor.class);
235         return (AuditorMBean) getProxy(objectName, AuditorMBean.class);
236     }
237     
238     public LifeCycleMBean getJBIContainer() throws IOException JavaDoc {
239         ObjectName JavaDoc objectName = ManagementContext.getContainerObjectName(jmxDomainName, containerName);
240         return (LifeCycleMBean) getProxy(objectName, LifeCycleMBean.class);
241     }
242     
243     public Object JavaDoc getProxy(ObjectName JavaDoc name, Class JavaDoc type) throws IOException JavaDoc {
244         return MBeanServerInvocationHandler.newProxyInstance(getServerConnection(), name, type, true);
245     }
246     
247     public MBeanServerConnection JavaDoc getServerConnection() throws IOException JavaDoc {
248         return jmxConnector.getMBeanServerConnection();
249     }
250
251     public String JavaDoc getContainerName() {
252         return containerName;
253     }
254
255     public void setContainerName(String JavaDoc containerName) {
256         this.containerName = containerName;
257     }
258     
259 }
260
Popular Tags