KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > manager > MX4JSystemManager


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.manager;
88
89 import java.io.File JavaDoc;
90 import javax.management.Attribute JavaDoc;
91 import javax.management.AttributeNotFoundException JavaDoc;
92 import javax.management.InstanceNotFoundException JavaDoc;
93 import javax.management.InvalidAttributeValueException JavaDoc;
94 import javax.management.MBeanException JavaDoc;
95 import javax.management.MBeanServer JavaDoc;
96 import javax.management.MBeanServerFactory JavaDoc;
97 import javax.management.ObjectName JavaDoc;
98 import javax.management.ReflectionException JavaDoc;
99 import mx4j.adaptor.rmi.jrmp.JRMPAdaptorMBean;
100 import mx4j.log.Log;
101 import mx4j.util.StandardMBeanProxy;
102
103 import org.codehaus.loom.interfaces.ContainerConstants;
104 import org.codehaus.spice.salt.i18n.ResourceManager;
105 import org.codehaus.spice.salt.i18n.Resources;
106 import org.codehaus.dna.Configurable;
107 import org.codehaus.dna.Configuration;
108 import org.codehaus.dna.ConfigurationException;
109
110 /**
111  * This component is responsible for managing loom instance.
112  *
113  * @author <a HREF="mail@leosimons.com">Leo Simons</a>
114  * @author Peter Donald
115  * @author <a HREF="mailto:Huw@mmlive.com">Huw Roberts</a>
116  */

117 public class MX4JSystemManager
118     extends AbstractJMXManager
119     implements Configurable
120 {
121     private static final Resources REZ =
122         ResourceManager.getPackageResources( MX4JSystemManager.class );
123
124     private static final String JavaDoc DEFAULT_NAMING_FACTORY =
125         "com.sun.jndi.rmi.profile.RegistryContextFactory";
126     private static final String JavaDoc DEFAULT_HTTPADAPTER_HOST = "localhost";
127     private static final int DEFAULT_HTTPADAPTER_PORT =
128         Integer.getInteger( "loom.adapter.http", 8082 ).intValue();
129     private static final int DEFAULT_RMIREGISTRY_PORT =
130         Integer.getInteger( "loom.rmiregistry.port", 1099 ).intValue();
131
132     private String JavaDoc m_host;
133     private int m_port;
134     private boolean m_rmi;
135     private int m_rmi_registry_port;
136     private String JavaDoc m_stylesheetDir;
137     private String JavaDoc m_namingFactory;
138     private String JavaDoc m_password;
139     private String JavaDoc m_username;
140     private boolean m_http;
141
142     public void configure( final Configuration configuration )
143         throws ConfigurationException
144     {
145         m_host = configuration.getChild( "manager-adaptor-host" ).
146             getValue( DEFAULT_HTTPADAPTER_HOST );
147
148         m_port = configuration.getChild( "manager-adaptor-port" ).
149             getValueAsInteger( DEFAULT_HTTPADAPTER_PORT );
150
151         //This is for backwards compatability with old-style
152
//RI JMX implementation
153
m_port = configuration.getChild( "port" ).
154             getValueAsInteger( m_port );
155
156         getLogger().debug( "MX4J HTTP listener port: " + m_port );
157
158         m_rmi =
159         configuration.getChild( "enable-rmi-adaptor" ).getValueAsBoolean(
160             false );
161         m_rmi_registry_port =
162         configuration.getChild( "rmi-registry-port" ).getValueAsInteger(
163             DEFAULT_RMIREGISTRY_PORT );
164         m_http =
165         configuration.getChild( "enable-http-adaptor" ).getValueAsBoolean(
166             false );
167
168         m_namingFactory =
169         configuration.getChild( "rmi-naming-factory" ).getValue(
170             DEFAULT_NAMING_FACTORY );
171
172         final String JavaDoc stylesheets =
173             configuration.getChild( "stylesheets-dir" ).getValue( null );
174         if( null != stylesheets )
175         {
176             m_stylesheetDir = new File JavaDoc( stylesheets ).getAbsolutePath();
177             getLogger().debug( "MX4J XSL Stylesheet Dir: " + m_stylesheetDir );
178         }
179
180         /*<user>
181            <name>user</name>
182            <password>passwd</password>
183         </user>*/

184         final Configuration userConfig = configuration.getChild( "user" );
185         m_username = userConfig.getChild( "name" ).getValue( null );
186         m_password = userConfig.getChild( "password" ).getValue( null );
187     }
188
189     public void initialize()
190         throws Exception JavaDoc
191     {
192         super.initialize();
193
194         final MBeanServer JavaDoc mBeanServer = getMBeanServer();
195
196         if( m_http )
197         {
198             startHttpAdaptor( mBeanServer );
199         }
200
201         if( m_rmi )
202         {
203             startRMIAdaptor( mBeanServer );
204         }
205     }
206
207     public void dispose()
208     {
209         final MBeanServer JavaDoc mBeanServer = getMBeanServer();
210
211         if( m_http )
212         {
213             stopHttpAdaptor( mBeanServer );
214         }
215         if( m_rmi )
216         {
217             stopRMIAdaptor( mBeanServer );
218         }
219
220         super.dispose();
221     }
222
223     private void startHttpAdaptor( final MBeanServer JavaDoc mBeanServer )
224         throws Exception JavaDoc
225     {
226         final ObjectName JavaDoc adaptorName = new ObjectName JavaDoc( "Http:name=HttpAdaptor" );
227         mBeanServer.createMBean( "mx4j.adaptor.http.HttpAdaptor",
228                                  adaptorName,
229                                  null );
230         mBeanServer.setAttribute( adaptorName,
231                                   new Attribute JavaDoc( "Host", m_host ) );
232         mBeanServer.setAttribute( adaptorName,
233                                   new Attribute JavaDoc( "Port",
234                                                  new Integer JavaDoc( m_port ) ) );
235
236         if( null != m_username )
237         {
238             configureAuthentication( mBeanServer, adaptorName );
239         }
240
241         configureProcessor( mBeanServer, adaptorName );
242
243         // starts the server
244
mBeanServer.invoke( adaptorName, "start", null, null );
245     }
246
247     private void configureProcessor( final MBeanServer JavaDoc mBeanServer,
248                                      final ObjectName JavaDoc adaptorName )
249         throws Exception JavaDoc
250     {
251         final ObjectName JavaDoc processorName = new ObjectName JavaDoc(
252             "Http:name=XSLTProcessor" );
253         mBeanServer.createMBean( "mx4j.adaptor.http.XSLTProcessor",
254                                  processorName,
255                                  null );
256         mBeanServer.setAttribute( adaptorName,
257                                   new Attribute JavaDoc( "ProcessorName",
258                                                  processorName ) );
259
260         if( null != m_stylesheetDir )
261         {
262             final Attribute JavaDoc stylesheetDir = new Attribute JavaDoc( "File",
263                                                            m_stylesheetDir );
264             mBeanServer.setAttribute( processorName, stylesheetDir );
265         }
266
267         final Attribute JavaDoc useCache =
268             new Attribute JavaDoc( "UseCache", Boolean.FALSE );
269         mBeanServer.setAttribute( processorName, useCache );
270     }
271
272     private void configureAuthentication( final MBeanServer JavaDoc mBeanServer,
273                                           final ObjectName JavaDoc adaptorName )
274         throws InstanceNotFoundException JavaDoc,
275                MBeanException JavaDoc,
276                ReflectionException JavaDoc,
277                AttributeNotFoundException JavaDoc,
278                InvalidAttributeValueException JavaDoc
279     {
280         // add user names
281
mBeanServer.invoke( adaptorName,
282                             "addAuthorization",
283                             new Object JavaDoc[]{m_username, m_password},
284                             new String JavaDoc[]{"java.lang.String",
285                                          "java.lang.String"} );
286
287         // use basic authentication
288
mBeanServer.setAttribute( adaptorName,
289                                   new Attribute JavaDoc( "AuthenticationMethod",
290                                                  "basic" ) );
291     }
292
293     private void stopHttpAdaptor( final MBeanServer JavaDoc server )
294     {
295         stopJMXMBean( server, "Http:name=HttpAdaptor" );
296     }
297
298     private void startRMIAdaptor( final MBeanServer JavaDoc server )
299         throws Exception JavaDoc
300     {
301         // Create and start the naming service
302
final ObjectName JavaDoc naming = new ObjectName JavaDoc( "Naming:type=rmiregistry" );
303         server.createMBean( "mx4j.tools.naming.NamingService",
304                             naming,
305                             null,
306                             new Object JavaDoc[]{new Integer JavaDoc( m_rmi_registry_port )},
307                             new String JavaDoc[]{"int"}
308         );
309         server.invoke( naming, "start", null, null );
310
311         // Create the JRMP adaptor
312
final ObjectName JavaDoc adaptor = new ObjectName JavaDoc( "Adaptor:protocol=JRMP" );
313         server.createMBean( "mx4j.adaptor.rmi.jrmp.JRMPAdaptor",
314                             adaptor,
315                             null );
316         final JRMPAdaptorMBean mbean =
317             (JRMPAdaptorMBean)StandardMBeanProxy.create(
318                 JRMPAdaptorMBean.class,
319                 server,
320                 adaptor );
321         // Set the JNDI name with which will be registered
322
mbean.setJNDIName( "jrmp" );
323
324         mbean.putNamingProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
325                                  m_namingFactory );
326         mbean.putNamingProperty( javax.naming.Context.PROVIDER_URL,
327                                  "rmi://localhost:" + m_rmi_registry_port );
328
329         // Register the JRMP adaptor in JNDI and start it
330
mbean.start();
331     }
332
333     private void stopRMIAdaptor( final MBeanServer JavaDoc server )
334     {
335         // stop the JRMP adaptor
336
stopJMXMBean( server, "Adaptor:protocol=JRMP" );
337         // stop the naming service
338
stopJMXMBean( server, "Naming:type=rmiregistry" );
339     }
340
341     protected MBeanServer JavaDoc createMBeanServer()
342         throws Exception JavaDoc
343     {
344         MX4JLoggerAdapter.setLogger( getLogger() );
345         Log.redirectTo( new MX4JLoggerAdapter() );
346         return MBeanServerFactory.createMBeanServer(
347             ContainerConstants.SOFTWARE );
348     }
349
350     private void stopJMXMBean( final MBeanServer JavaDoc mBeanServer,
351                                final String JavaDoc name )
352     {
353         try
354         {
355             final ObjectName JavaDoc objectName = new ObjectName JavaDoc( name );
356             mBeanServer.invoke( objectName, "stop", null, null );
357         }
358         catch( final Exception JavaDoc e )
359         {
360             final String JavaDoc message =
361                 REZ.format( "jmxmanager.error.jmxmbean.dispose", name );
362             getLogger().error( message, e );
363         }
364     }
365 }
366
Popular Tags