1 23 24 package com.sun.enterprise.admin.server.core; 25 26 import java.util.ArrayList ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Collections ; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 import java.net.InetAddress ; 33 import java.net.MalformedURLException ; 34 import java.net.UnknownHostException ; 35 import java.io.IOException ; 36 import javax.management.MBeanServer ; 37 import javax.management.MBeanServerConnection ; 38 import com.sun.enterprise.admin.common.MBeanServerFactory; 40 import javax.management.remote.JMXConnectorServer ; 41 import javax.management.remote.JMXServiceURL ; 42 import javax.management.remote.JMXConnectorServerFactory ; 43 import javax.management.remote.JMXAuthenticator ; 44 45 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 46 import com.sun.enterprise.admin.server.core.jmx.auth.ASJMXAuthenticator; 47 import com.sun.enterprise.admin.server.core.jmx.auth.ASLoginDriverImpl; 48 49 import java.rmi.server.RMIServerSocketFactory ; 50 import java.rmi.server.RMIClientSocketFactory ; 51 import javax.rmi.ssl.SslRMIClientSocketFactory ; 52 53 import com.sun.enterprise.admin.common.constant.AdminConstants; 54 import com.sun.appserv.server.ServerLifecycle; 55 import com.sun.appserv.server.ServerLifecycleException; 56 import com.sun.enterprise.server.ServerContext; 57 import com.sun.enterprise.config.serverbeans.JmxConnector; 58 import com.sun.enterprise.config.serverbeans.AdminService; 59 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 60 import com.sun.enterprise.config.ConfigException; 61 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory; 62 import com.sun.enterprise.util.i18n.StringManager; 63 64 import javax.rmi.ssl.SslRMIClientSocketFactory ; 65 import com.sun.enterprise.util.SystemPropertyConstants; 66 68 69 import com.sun.enterprise.admin.jmx.remote.server.rmi.JmxConnectorServerDriver; 70 import com.sun.enterprise.admin.jmx.remote.server.rmi.RemoteJmxProtocol; 71 72 import com.sun.enterprise.admin.server.core.jmx.nonssl.RMIMultiHomedServerSocketFactory; 73 import com.sun.enterprise.admin.server.core.jmx.ssl.AdminSslServerSocketFactory; 74 import com.sun.enterprise.admin.server.core.jmx.ssl.AsTlsClientEnvSetter; 75 import com.sun.appserv.management.client.AdminRMISSLClientSocketFactory; 76 import javax.rmi.ssl.SslRMIServerSocketFactory ; 77 81 public class JmxConnectorLifecycle implements ServerLifecycle { 82 83 public static final Logger sLogger = 84 Logger.getLogger(AdminConstants.kLoggerName); 85 private static final StringManager sm = StringManager.getManager(JmxConnectorLifecycle.class); 86 private ServerContext initContext = null; 87 private JmxConnector connectorConfig = null; 88 private JMXConnectorServer cs = null; 89 private JMXConnectorServer jconsolecs = null; 90 private JmxConnectorServerDriver driver; 91 private boolean isEnabled = false; 92 94 public JmxConnectorLifecycle() { 95 sLogger.log(Level.FINE, "rjmx.lc.init"); 96 } 97 98 public void onInitialization(ServerContext sc) throws ServerLifecycleException { 99 try { 100 initContext = sc; 101 initConnectorConfig(); 102 handleIsEnabled(connectorConfig.getPort()); 103 handleSupportedProtocol(); 104 if (isEnabled) { 105 driver = new JmxConnectorServerDriver(); 106 configureJmxConnectorServerDriver(); 107 } 108 } 109 catch(Exception e) { 110 throw new ServerLifecycleException(e.getMessage(), e); 111 } 112 } 113 114 public void onStartup(ServerContext sc) throws ServerLifecycleException { 115 setupClientSide(); 116 try { 117 if (isEnabled) { 118 this.cs = driver.startConnectorServer(); 119 this.jconsolecs = driver.startJconsoleConnectorServer(); 120 } 121 else { 122 final String msg = "JmxConnectorLifeCycle.onStartup: Connector Server not enabled at port: " + connectorConfig.getPort(); 123 sLogger.fine(msg); 124 } 125 } 126 catch (Exception e) { 127 throw new ServerLifecycleException(e.getMessage(), e); 128 } 129 } 130 131 public void onReady(ServerContext sc) throws ServerLifecycleException { 132 } 133 public void onShutdown() throws ServerLifecycleException { 134 try { 135 if (isEnabled) { 136 driver.stopConnectorServer(cs); 137 driver.stopConnectorServer(jconsolecs); 138 } 139 else { 140 final String msg = "JmxConnectorLifeCycle.onShutdown: Connector Server not enabled at port: " + connectorConfig.getPort() + ", its shutdown is not required"; 141 sLogger.fine(msg); 142 } 143 } 144 catch (final Exception e) { 145 throw new ServerLifecycleException(e.getMessage()); 146 } 147 } 148 149 private MBeanServer getAssociatedMBS() { 150 157 return ( MBeanServerFactory.getMBeanServer() ); 158 } 159 160 private Map getEnvironment() { 161 final Map env = new HashMap (); 162 return ( env ); 163 } 164 165 public void onTermination() throws ServerLifecycleException { 166 } 167 168 private void initConnectorConfig() throws ConfigException { 169 AdminService as = ServerBeansFactory.getConfigBean(initContext.getConfigContext()). 171 getAdminService(); 172 connectorConfig = as.getJmxConnectorByName(as.getSystemJmxConnectorName()); 173 if (connectorConfig.isEnabled()) { 174 this.isEnabled = true; 175 } 176 } 177 178 private JMXAuthenticator createJMXAuthenticator() { 179 final ASJMXAuthenticator authenticator = new ASJMXAuthenticator(); 180 authenticator.setRealmName(connectorConfig.getAuthRealmName()); 182 authenticator.setLoginDriver(new ASLoginDriverImpl()); 183 return authenticator; 184 } 185 186 private void configureJmxConnectorServerDriver() throws ServerLifecycleException { 187 driver.setAuthentication(true); 188 driver.setAuthenticator(createJMXAuthenticator()); 189 driver.setLogger(this.sLogger); 190 driver.setMBeanServer(this.getAssociatedMBS()); 191 driver.setRmiRegistrySecureFlag(new Boolean (System.getProperty(RmiTweaks.SECURE_RMI_REGISTRY)).booleanValue()); 192 try { 193 driver.setProtocol(RemoteJmxProtocol.instance(connectorConfig.getProtocol())); 194 driver.setPort(Integer.parseInt(connectorConfig.getPort())); 195 handleSsl(); 196 } 197 catch (final Exception e) { 198 throw new ServerLifecycleException(e.getMessage()); 199 } 200 } 201 202 206 private void handleIsEnabled(final String port) throws ServerLifecycleException { 207 211 if (isEE() && !isEnabled) { 212 final String msg = sm.getString("rjmx.lc.disabled_ee_na", port); 214 throw new ServerLifecycleException(msg); 215 } 216 if (!isEE() && !isEnabled) { 217 sLogger.log(Level.INFO, "rjmx.lc.not_enabled", port); 219 } 220 } 222 223 private boolean isEE() { 224 boolean isEE = false; 225 final String eepffc = "com.sun.enterprise.ee.server.pluggable.EEPluggableFeatureImpl"; 226 final String pn = PluggableFeatureFactory.PLUGGABLE_FEATURES_PROPERTY_NAME; 227 final String pv = System.getProperty(pn); 228 if (eepffc.equals(pv)) { 229 isEE = true; 230 } 231 return ( isEE ); 232 } 233 private void handleSupportedProtocol() throws ServerLifecycleException { 234 final String pfc = connectorConfig.getProtocol(); 235 if (RemoteJmxProtocol.RMIJRMP != RemoteJmxProtocol.instance(pfc)) { 236 final String port = connectorConfig.getPort(); 237 final String setP = connectorConfig.getProtocol(); 238 final String supportedP = RemoteJmxProtocol.RMIJRMP.getName(); 239 final String msg = sm.getString("rjmx.lc.unsupported_protocol", port, setP, supportedP); 240 throw new ServerLifecycleException (msg); 241 } 242 } 243 private void handleSsl() { 244 final boolean ssl = connectorConfig.isSecurityEnabled(); 245 246 RMIServerSocketFactory sf = null; 247 if (ssl) { 248 driver.setSsl(ssl); 249 final com.sun.enterprise.config.serverbeans.Ssl sslc = connectorConfig.getSsl(); 250 sf = new AdminSslServerSocketFactory(sslc, connectorConfig.getAddress()); 251 RMIClientSocketFactory cf = new AdminRMISSLClientSocketFactory(); 252 driver.setRmiClientSocketFactory(cf); 253 } else sf = new RMIMultiHomedServerSocketFactory(connectorConfig.getAddress()); 254 driver.setRmiServerSocketFactory(sf); 255 } 256 257 266 private void setupClientSide() { 267 new AsTlsClientEnvSetter().setup(); 268 } 269 270 private static class RmiTweaks { 271 final static String SECURE_RMI_REGISTRY = "com.sun.aas.jsr160.SecureRmiRegistry"; 272 } 273 } 274 | Popular Tags |