KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > jmx > server > mx4j > DefaultJrmpRemotingImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.jmx.server.mx4j;
19
20 import java.rmi.server.ExportException JavaDoc;
21
22 import javax.management.Attribute JavaDoc;
23 import javax.management.InstanceAlreadyExistsException JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.ObjectInstance JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import org.sape.carbon.core.component.ComponentConfiguration;
31 import org.sape.carbon.core.component.lifecycle.Configurable;
32 import org.sape.carbon.core.component.lifecycle.Startable;
33 import org.sape.carbon.services.jmx.server.MBeanServerRetreiveException;
34 import org.sape.carbon.services.jmx.server.MBeanServerService;
35
36 import mx4j.adaptor.rmi.jrmp.JRMPAdaptorMBean;
37 import mx4j.util.StandardMBeanProxy;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41
42 /**
43  * <p>This is the default and very simple implementation of a JRMP admin
44  * server built via the functionality in the MX4J JMX implementation. It
45  * opens a basic RMI over JRMP administration listener to the MX4J client
46  * adaptor.</p>
47  *
48  *
49  * Copyright 2002 Sapient
50  * @since carbon 1.0
51  * @author Greg Hinkle, May 2002
52  * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/05/05 21:21:31 $)
53  */

54 public class DefaultJrmpRemotingImpl
55         implements MBeanServerService, Configurable, Startable {
56
57     /**
58      * Provides a handle to Apache-commons logger
59      */

60     private Log log = LogFactory.getLog(this.getClass());
61
62     /** Object name for the registry. */
63     private ObjectName JavaDoc registryObjectName;
64
65     /** Object instance for the registry. */
66     private ObjectInstance JavaDoc registryObjectInstance;
67
68     /** The Jmx type string. */
69     private static final String JavaDoc REGISTRY_OBJECT_NAME_STRING =
70         "Naming:type=rmiregistry";
71
72     /** Class to use for the naming service. */
73     private static final String JavaDoc NAMING_SERVICE_CLASS_NAME =
74         "mx4j.tools.naming.NamingService";
75
76     /** The Jmx object name for the adapter. */
77     private ObjectName JavaDoc adaptorObjectName;
78
79     /** The Adapter MBean. */
80     private JRMPAdaptorMBean adaptorMBean;
81
82     /** JMX Adapter object name string. */
83     private static final String JavaDoc ADAPTOR_OBJECT_NAME_STRING =
84         "Adaptor:protocol=JRMP";
85
86     /** Name of adapter class. */
87     private static final String JavaDoc ADAPTOR_CLASS_NAME =
88         "mx4j.adaptor.rmi.jrmp.JRMPAdaptor";
89
90     /** Protocol prefix to the URL. */
91     private static final String JavaDoc URL_ID = "rmi://";
92
93     /** Class for the initial context factory. */
94     private Class JavaDoc initialContextFactoryClass;
95
96     /** Hostname. */
97     private String JavaDoc hostname;
98
99     /** Port to expose on. */
100     private int port;
101
102     /** MBeanServerService being exposed remotely. */
103     private MBeanServerService mbeanServerService;
104
105     /**
106      * Configures the component.
107      *
108      * @param componentConfiguration <code>JrmpRemotingConfiguration</code>
109      * to configure the component
110      * @throws Exception indicates an error configuring the component
111      */

112     public void configure(ComponentConfiguration componentConfiguration)
113             throws Exception JavaDoc {
114
115         JrmpRemotingConfiguration config =
116             (JrmpRemotingConfiguration) componentConfiguration;
117
118         this.initialContextFactoryClass =
119             config.getInitialContextFactoryClass();
120
121         this.hostname = config.getHostname();
122         this.port = config.getPort();
123         this.mbeanServerService = config.getMBeanServerService();
124
125
126         MBeanServer JavaDoc server = getMBeanServer();
127
128         // Create the naming service
129
this.registryObjectName = new ObjectName JavaDoc(REGISTRY_OBJECT_NAME_STRING);
130
131         try {
132             this.registryObjectInstance =
133                 server.createMBean(
134                 NAMING_SERVICE_CLASS_NAME,
135                 this.registryObjectName,
136                 null);
137         } catch (InstanceAlreadyExistsException JavaDoc iaee) {
138             // Completely expected second+ time through
139
}
140
141         boolean started = false;
142         while (!started) {
143             started = true;
144             try {
145                 // Start the registry server
146
server.invoke(this.registryObjectName, "start", null, null);
147             } catch (MBeanException JavaDoc mbe) {
148                 if (this.port > (config.getPort() + 5)) {
149                     throw mbe;
150                 }
151
152                 started = false;
153                 if (mbe.getTargetException() instanceof ExportException JavaDoc) {
154                     server.setAttribute(
155                         this.registryObjectName,
156                         new Attribute JavaDoc("Port",
157                         new Integer JavaDoc(++this.port)));
158                 }
159             }
160
161         }
162         if (log.isDebugEnabled()) {
163             log.debug("Able to start naming server on port ["
164                 + this.port + "]");
165         }
166
167         // Create the JRMP adaptor
168
this.adaptorObjectName = new ObjectName JavaDoc(ADAPTOR_OBJECT_NAME_STRING);
169
170
171         try {
172             server.createMBean(ADAPTOR_CLASS_NAME,
173                     this.adaptorObjectName, null);
174
175         } catch (InstanceAlreadyExistsException JavaDoc iaee) {
176             // Completely expected second+ time through
177
}
178
179         this.adaptorMBean =
180             (JRMPAdaptorMBean)
181             StandardMBeanProxy.create(
182                 JRMPAdaptorMBean.class,
183                 server,
184                 adaptorObjectName);
185
186
187         // Set the JNDI name with which will be registered
188
String JavaDoc jndiName = "jrmp";
189         this.adaptorMBean.putNamingProperty(
190             InitialContext.INITIAL_CONTEXT_FACTORY,
191             this.initialContextFactoryClass.getName());
192         this.adaptorMBean.putNamingProperty(
193             InitialContext.PROVIDER_URL,
194             this.URL_ID + this.hostname + ":" + this.port);
195
196         this.adaptorMBean.setJNDIName(jndiName);
197
198     }
199
200     /**
201      * Starts the service.
202      *
203      * @throws Exception indicates an error starting the service
204      */

205     public void start() throws Exception JavaDoc {
206         // Get a MBeanServer
207
MBeanServer JavaDoc server = getMBeanServer();
208
209
210
211
212         // Register the JRMP adaptor in JNDI and start it
213
this.adaptorMBean.start();
214     }
215
216     /**
217      * Stops the service.
218      *
219      * @throws Exception indicates an error stopping the service
220      */

221     public void stop() throws Exception JavaDoc {
222         // Get a MBeanServer
223
MBeanServer JavaDoc server = getMBeanServer();
224
225         this.adaptorMBean.stop();
226
227         // Stop the registry server
228
server.invoke(this.registryObjectName, "stop", null, null);
229     }
230
231     /**
232      * Gets the MBeanServer.
233      *
234      * @return the MBean Server
235      * @throws MBeanServerRetreiveException indicates and error
236      * retreiving the MBean server
237      */

238     public MBeanServer JavaDoc getMBeanServer() throws MBeanServerRetreiveException {
239         return this.mbeanServerService.getMBeanServer();
240     }
241
242 }
243
Popular Tags