1 8 package org.apache.avalon.phoenix.components.manager; 9 10 import java.io.File ; 11 import javax.management.Attribute ; 12 import javax.management.AttributeNotFoundException ; 13 import javax.management.InstanceNotFoundException ; 14 import javax.management.InvalidAttributeValueException ; 15 import javax.management.MBeanException ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.ObjectName ; 19 import javax.management.ReflectionException ; 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 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 DEFAULT_NAMING_FACTORY = 46 "com.sun.jndi.rmi.registry.RegistryContextFactory"; 47 private static final String DEFAULT_HTTPADAPTER_HOST = "localhost"; 48 private static final int DEFAULT_HTTPADAPTER_PORT = 49 Integer.getInteger( "phoenix.adapter.http", 8082 ).intValue(); 50 51 private String m_host; 52 private int m_port; 53 private boolean m_rmi; 54 private File m_homeDir; 55 private String m_stylesheetDir; 56 private String m_namingFactory; 57 private String m_password; 58 private String m_username; 59 60 public void contextualize( Context context ) 61 throws ContextException 62 { 63 m_homeDir = (File )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 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 stylesheets = 88 configuration.getChild( "stylesheets-dir" ).getValue( null ); 89 if( null != stylesheets ) 90 { 91 m_stylesheetDir = new File ( m_homeDir, stylesheets ).getAbsolutePath(); 92 } 93 94 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 105 { 106 super.initialize(); 107 108 final MBeanServer 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 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 mBeanServer ) 133 throws Exception 134 { 135 final ObjectName adaptorName = new ObjectName ( "Http:name=HttpAdaptor" ); 136 mBeanServer.createMBean( "mx4j.adaptor.http.HttpAdaptor", adaptorName, null ); 137 mBeanServer.setAttribute( adaptorName, new Attribute ( "Host", m_host ) ); 138 mBeanServer.setAttribute( adaptorName, new Attribute ( "Port", new Integer ( m_port ) ) ); 139 140 if( null != m_username ) 141 { 142 configureAuthentication( mBeanServer, adaptorName ); 143 } 144 145 configureProcessor( mBeanServer, adaptorName ); 146 147 mBeanServer.invoke( adaptorName, "start", null, null ); 149 } 150 151 private void configureProcessor( final MBeanServer mBeanServer, 152 final ObjectName adaptorName ) 153 throws Exception 154 { 155 final ObjectName processorName = new ObjectName ( "Http:name=XSLTProcessor" ); 156 mBeanServer.createMBean( "mx4j.adaptor.http.XSLTProcessor", processorName, null ); 157 mBeanServer.setAttribute( adaptorName, new Attribute ( "ProcessorName", processorName ) ); 158 159 if( null != m_stylesheetDir ) 160 { 161 final Attribute stylesheetDir = new Attribute ( "File", m_stylesheetDir ); 162 mBeanServer.setAttribute( processorName, stylesheetDir ); 163 } 164 165 final Attribute useCache = 166 new Attribute ( "UseCache", Boolean.FALSE ); 167 mBeanServer.setAttribute( processorName, useCache ); 168 } 169 170 private void configureAuthentication( final MBeanServer mBeanServer, final ObjectName adaptorName ) throws InstanceNotFoundException , MBeanException , ReflectionException , AttributeNotFoundException , InvalidAttributeValueException 171 { 172 mBeanServer.invoke( adaptorName, 174 "addAuthorization", 175 new Object []{m_username, m_password}, 176 new String []{"java.lang.String", "java.lang.String"} ); 177 178 mBeanServer.setAttribute( adaptorName, 180 new Attribute ( "AuthenticationMethod", "basic" ) ); 181 } 182 183 private void startRMIAdaptor( final MBeanServer server ) 184 throws Exception 185 { 186 final ObjectName naming = new ObjectName ( "Naming:type=rmiregistry" ); 188 server.createMBean( "mx4j.tools.naming.NamingService", naming, null ); 189 server.invoke( naming, "start", null, null ); 190 191 final ObjectName adaptor = new ObjectName ( "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 mbean.setJNDIName( "jrmp" ); 200 mbean.putJNDIProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY, 201 m_namingFactory ); 202 mbean.start(); 204 } 205 206 private void stopHttpAdaptor( final MBeanServer server ) 207 { 208 stopJMXMBean( server, "Http:name=HttpAdaptor" ); 209 } 210 211 private void stopRMIAdaptor( final MBeanServer server ) 212 { 213 stopJMXMBean( server, "Adaptor:protocol=JRMP" ); 215 stopJMXMBean( server, "Naming:type=rmiregistry" ); 217 } 218 219 protected MBeanServer createMBeanServer() 220 throws Exception 221 { 222 MX4JLoggerAdapter.setLogger( getLogger() ); 223 Log.redirectTo( new MX4JLoggerAdapter() ); 224 return MBeanServerFactory.createMBeanServer( "Phoenix" ); 225 } 226 227 private void stopJMXMBean( final MBeanServer mBeanServer, final String name ) 228 { 229 try 230 { 231 final ObjectName objectName = new ObjectName ( name ); 232 mBeanServer.invoke( objectName, "stop", null, null ); 233 } 234 catch ( final Exception e ) 235 { 236 final String message = 237 REZ.getString( "jmxmanager.error.jmxmbean.dispose", name ); 238 getLogger().error( message, e ); 239 } 240 } 241 } 242 | Popular Tags |