KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > manager > MX4JSystemManager


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.manager;
9
10 import java.io.File JavaDoc;
11 import javax.management.Attribute JavaDoc;
12 import javax.management.AttributeNotFoundException JavaDoc;
13 import javax.management.InstanceNotFoundException JavaDoc;
14 import javax.management.InvalidAttributeValueException JavaDoc;
15 import javax.management.MBeanException JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.management.ReflectionException JavaDoc;
20 import mx4j.adaptor.rmi.jrmp.JRMPAdaptorMBean;
21 import mx4j.log.Log;
22 import mx4j.util.StandardMBeanProxy;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.excalibur.i18n.Resources;
30 import org.apache.avalon.excalibur.i18n.ResourceManager;
31
32 /**
33  * This component is responsible for managing phoenix instance.
34  *
35  * @author <a HREF="mail@leosimons.com">Leo Simons</a>
36  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
37  * @author <a HREF="mailto:Huw@mmlive.com">Huw Roberts</a>
38  */

39 public class MX4JSystemManager
40     extends AbstractJMXManager
41     implements Contextualizable, Configurable
42 {
43     private static final Resources REZ =
44                ResourceManager.getPackageResources( MX4JSystemManager.class );
45     private static final String JavaDoc DEFAULT_NAMING_FACTORY =
46         "com.sun.jndi.rmi.registry.RegistryContextFactory";
47     private static final String JavaDoc DEFAULT_HTTPADAPTER_HOST = "localhost";
48     private static final int DEFAULT_HTTPADAPTER_PORT =
49         Integer.getInteger( "phoenix.adapter.http", 8082 ).intValue();
50
51     private String JavaDoc m_host;
52     private int m_port;
53     private boolean m_rmi;
54     private File JavaDoc m_homeDir;
55     private String JavaDoc m_stylesheetDir;
56     private String JavaDoc m_namingFactory;
57     private String JavaDoc m_password;
58     private String JavaDoc m_username;
59
60     public void contextualize( Context context )
61         throws ContextException
62     {
63         m_homeDir = (File JavaDoc)context.get( "phoenix.home" );
64     }
65
66     public void configure( final Configuration configuration )
67         throws ConfigurationException
68     {
69         m_host = configuration.getChild( "manager-adaptor-host" ).
70                        getValue( DEFAULT_HTTPADAPTER_HOST );
71
72         m_port = configuration.getChild( "manager-adaptor-port" ).
73             getValueAsInteger( DEFAULT_HTTPADAPTER_PORT );
74
75         //This is for backwards compatability with old-style
76
//RI JMX implementation
77
m_port = configuration.getChild( "port" ).
78             getValueAsInteger( m_port );
79
80         getLogger().debug( "MX4J HTTP listener port: " + m_port );
81
82         m_rmi = configuration.getChild( "enable-rmi-adaptor" ).getValueAsBoolean( false );
83
84         m_namingFactory =
85             configuration.getChild( "rmi-naming-factory" ).getValue( DEFAULT_NAMING_FACTORY );
86
87         final String JavaDoc stylesheets =
88             configuration.getChild( "stylesheets-dir" ).getValue( null );
89         if( null != stylesheets )
90         {
91             m_stylesheetDir = new File JavaDoc( m_homeDir, stylesheets ).getAbsolutePath();
92         }
93
94         /*<user>
95           <name>user</name>
96           <password>passwd</password>
97         </user>*/

98         final Configuration userConfig = configuration.getChild( "user" );
99         m_username = userConfig.getChild( "name" ).getValue( null );
100         m_password = userConfig.getChild( "password" ).getValue( null );
101     }
102
103     public void initialize()
104         throws Exception JavaDoc
105     {
106         super.initialize();
107
108         final MBeanServer JavaDoc mBeanServer = getMBeanServer();
109
110         startHttpAdaptor( mBeanServer );
111
112         if( m_rmi )
113         {
114             startRMIAdaptor( mBeanServer );
115         }
116     }
117
118     public void dispose()
119        {
120            final MBeanServer JavaDoc mBeanServer = getMBeanServer();
121
122            stopHttpAdaptor( mBeanServer );
123
124            if( m_rmi )
125            {
126                stopRMIAdaptor( mBeanServer );
127            }
128
129            super.dispose();
130        }
131
132     private void startHttpAdaptor( final MBeanServer JavaDoc mBeanServer )
133         throws Exception JavaDoc
134     {
135         final ObjectName JavaDoc adaptorName = new ObjectName JavaDoc( "Http:name=HttpAdaptor" );
136         mBeanServer.createMBean( "mx4j.adaptor.http.HttpAdaptor", adaptorName, null );
137         mBeanServer.setAttribute( adaptorName, new Attribute JavaDoc( "Host", m_host ) );
138         mBeanServer.setAttribute( adaptorName, new Attribute JavaDoc( "Port", new Integer JavaDoc( m_port ) ) );
139
140         if( null != m_username )
141         {
142             configureAuthentication( mBeanServer, adaptorName );
143         }
144
145         configureProcessor( mBeanServer, adaptorName );
146
147         // starts the server
148
mBeanServer.invoke( adaptorName, "start", null, null );
149     }
150
151     private void configureProcessor( final MBeanServer JavaDoc mBeanServer,
152                                      final ObjectName JavaDoc adaptorName )
153         throws Exception JavaDoc
154     {
155         final ObjectName JavaDoc processorName = new ObjectName JavaDoc( "Http:name=XSLTProcessor" );
156         mBeanServer.createMBean( "mx4j.adaptor.http.XSLTProcessor", processorName, null );
157         mBeanServer.setAttribute( adaptorName, new Attribute JavaDoc( "ProcessorName", processorName ) );
158
159         if( null != m_stylesheetDir )
160         {
161             final Attribute JavaDoc stylesheetDir = new Attribute JavaDoc( "File", m_stylesheetDir );
162             mBeanServer.setAttribute( processorName, stylesheetDir );
163         }
164
165         final Attribute JavaDoc useCache =
166             new Attribute JavaDoc( "UseCache", Boolean.FALSE );
167         mBeanServer.setAttribute( processorName, useCache );
168     }
169
170     private void configureAuthentication( final MBeanServer JavaDoc mBeanServer, final ObjectName JavaDoc adaptorName ) throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
171     {
172         // add user names
173
mBeanServer.invoke( adaptorName,
174                             "addAuthorization",
175                             new Object JavaDoc[]{m_username, m_password},
176                             new String JavaDoc[]{"java.lang.String", "java.lang.String"} );
177
178         // use basic authentication
179
mBeanServer.setAttribute( adaptorName,
180                                   new Attribute JavaDoc( "AuthenticationMethod", "basic" ) );
181     }
182
183     private void startRMIAdaptor( final MBeanServer JavaDoc server )
184         throws Exception JavaDoc
185     {
186         // Create and start the naming service
187
final ObjectName JavaDoc naming = new ObjectName JavaDoc( "Naming:type=rmiregistry" );
188         server.createMBean( "mx4j.tools.naming.NamingService", naming, null );
189         server.invoke( naming, "start", null, null );
190
191         // Create the JRMP adaptor
192
final ObjectName JavaDoc adaptor = new ObjectName JavaDoc( "Adaptor:protocol=JRMP" );
193         server.createMBean( "mx4j.adaptor.rmi.jrmp.JRMPAdaptor", adaptor, null );
194         JRMPAdaptorMBean mbean =
195             (JRMPAdaptorMBean)StandardMBeanProxy.create( JRMPAdaptorMBean.class,
196                                                          server,
197                                                          adaptor );
198         // Set the JNDI name with which will be registered
199
mbean.setJNDIName( "jrmp" );
200         mbean.putJNDIProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
201                                m_namingFactory );
202         // Register the JRMP adaptor in JNDI and start it
203
mbean.start();
204     }
205
206     private void stopHttpAdaptor( final MBeanServer JavaDoc server )
207     {
208         stopJMXMBean( server, "Http:name=HttpAdaptor" );
209     }
210
211     private void stopRMIAdaptor( final MBeanServer JavaDoc server )
212     {
213            // stop the JRMP adaptor
214
stopJMXMBean( server, "Adaptor:protocol=JRMP" );
215            // stop the naming service
216
stopJMXMBean( server, "Naming:type=rmiregistry" );
217     }
218
219     protected MBeanServer JavaDoc createMBeanServer()
220         throws Exception JavaDoc
221     {
222         MX4JLoggerAdapter.setLogger( getLogger() );
223         Log.redirectTo( new MX4JLoggerAdapter() );
224         return MBeanServerFactory.createMBeanServer( "Phoenix" );
225     }
226
227     private void stopJMXMBean( final MBeanServer JavaDoc mBeanServer, final String JavaDoc name )
228     {
229         try
230         {
231             final ObjectName JavaDoc objectName = new ObjectName JavaDoc( name );
232             mBeanServer.invoke( objectName, "stop", null, null );
233         }
234         catch ( final Exception JavaDoc e )
235         {
236             final String JavaDoc message =
237                 REZ.getString( "jmxmanager.error.jmxmbean.dispose", name );
238             getLogger().error( message, e );
239         }
240     }
241 }
242
Popular Tags