1 22 package org.objectweb.petals.kernel.admin; 23 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 import javax.management.remote.JMXConnector ; 29 import javax.management.remote.JMXConnectorFactory ; 30 import javax.management.remote.JMXServiceURL ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.naming.NotContextException ; 35 36 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 37 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 38 import org.objectweb.fractal.fraclet.annotation.Interface; 39 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 40 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 41 import org.objectweb.fractal.fraclet.annotation.Provides; 42 import org.objectweb.petals.PetalsException; 43 import org.objectweb.petals.util.JNDIUtil; 44 import org.objectweb.petals.util.LoggingUtil; 45 import org.objectweb.petals.util.PropertyUtil; 46 47 53 @FractalComponent 54 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.kernel.admin.DistributedJMXServerFactory.class)) 55 public class DistributedJMXServerFactoryImpl 56 implements DistributedJMXServerFactory { 57 58 61 private static final String CONTAINERS_REF = "containers"; 62 63 66 protected ContainerInformation containerInformation; 67 68 71 protected Context containersContext; 72 73 76 protected LoggingUtil log; 77 78 81 protected InitialContext rootContext; 82 83 90 public DistributedJMXServer createDistributedJMXServer(String containerName) 91 throws PetalsException { 92 log.start(); 93 DistributedJMXServer distributedJMXServer = null; 94 try { 95 ContainerInformation containerInformation = (ContainerInformation) containersContext 96 .lookup(containerName); 97 if (containerInformation != null) { 98 JMXConnector connector = null; 99 Properties env = System.getProperties(); 100 env.put("java.naming.factory.initial", 101 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 102 Map <String , Object > args = new HashMap <String , Object >(); 103 String username = containerInformation.getJmxLogin(); 104 String password = containerInformation.getJmxPassword(); 105 if (!"".equals(username)) { 106 if (password == null) { 107 password = ""; 108 } 109 String [] credentials = new String [] {username, password}; 110 args.put("jmx.remote.credentials", credentials); 111 } 112 String urlStr = "service:jmx:rmi:///jndi/rmi://" 113 + containerInformation.getHost() + ":" 114 + containerInformation.getJmxPort() 115 + "/management/rmi-jmx-connector"; 116 JMXServiceURL url; 117 118 url = new JMXServiceURL (urlStr); 119 connector = JMXConnectorFactory.newJMXConnector(url, args); 120 121 distributedJMXServer = new DistributedJMXServer(connector); 122 } 123 } catch (Exception e) { 124 throw new PetalsException( 125 "Error while trying to connect to JMX server", e); 126 } 127 log.end(); 128 return distributedJMXServer; 129 } 130 131 134 public boolean isContainerStarted(String containerName) { 135 log.start(); 136 if (containerName == null) { 137 throw new IllegalArgumentException ( 138 "The container name must be non null"); 139 } 140 boolean isStarted = false; 141 if (JNDIUtil.isBound(rootContext, CONTAINERS_REF, containerName)) { 142 try { 143 ContainerInformation containerInformation = (ContainerInformation) containersContext 144 .lookup(containerName); 145 isStarted = containerInformation.isStarted(); 146 } catch (NamingException e) { 147 } 149 } 150 log.end(); 151 return isStarted; 152 } 153 154 @LifeCycle(on=LifeCycleType.START) 155 public void start() throws IllegalLifeCycleException { 156 log=new LoggingUtil(null); 157 log.start(); 158 try { 159 initContext(); 160 161 } catch (PetalsException e) { 162 log.error("Error starting JMX server", e); 163 } 164 log.end(); 165 } 166 167 @LifeCycle(on=LifeCycleType.STOP) 168 public void stop() throws IllegalLifeCycleException { 169 log.call(); 170 } 171 172 177 protected void initContext() throws PetalsException { 178 try { 179 rootContext = new InitialContext (PropertyUtil 180 .retrieveJNDIProperties()); 181 if (!JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) { 182 containersContext = rootContext 183 .createSubcontext(CONTAINERS_REF); 184 } else { 185 containersContext = (Context ) rootContext 186 .lookup(CONTAINERS_REF); 187 } 188 if (containersContext == null) { 189 throw new NotContextException (); 190 } 191 } catch (Exception e1) { 192 throw new PetalsException( 193 "Problem while trying to initialize the container context", e1); 194 } 195 } 196 197 } 198 | Popular Tags |