1 37 package net.sourceforge.cruisecontrol.jmx; 38 39 import java.io.IOException ; 40 import java.rmi.NoSuchObjectException ; 41 import java.rmi.RemoteException ; 42 import java.util.HashMap ; 43 import java.util.Map ; 44 import java.util.Iterator ; 45 46 import javax.management.Attribute ; 47 import javax.management.MBeanServer ; 48 import javax.management.MBeanServerFactory ; 49 import javax.management.ObjectName ; 50 import javax.management.remote.JMXConnectorServer ; 51 import javax.management.remote.JMXConnectorServerFactory ; 52 import javax.management.remote.JMXServiceURL ; 53 import javax.naming.Context ; 54 55 import mx4j.tools.adaptor.http.HttpAdaptor; 56 import mx4j.tools.naming.NamingService; 57 import mx4j.tools.naming.NamingServiceMBean; 58 import net.sourceforge.cruisecontrol.CruiseControlController; 59 import net.sourceforge.cruisecontrol.util.MainArgs; 60 61 import org.apache.log4j.Logger; 62 63 68 public class CruiseControlControllerAgent { 69 private static final Logger LOG = Logger.getLogger(CruiseControlControllerAgent.class); 70 private static final String JNDI_NAME = "/jndi/jrmp"; 71 72 private HttpAdaptor httpAdaptor = new HttpAdaptor(); 73 private int httpPort; 74 private NamingServiceMBean rmiRegistry; 75 private JMXConnectorServer connectorServer; 76 private int connectorServerPort; 77 private String path; 78 private String user; 79 private String password; 80 81 public CruiseControlControllerAgent(CruiseControlController controller, int httpPort, 82 int connectorServerPort, String user, String password, String xslPath) { 83 this.httpPort = httpPort; 84 this.connectorServerPort = connectorServerPort; 85 path = xslPath; 86 CruiseControlControllerJMXAdaptor controllerAdaptor = new CruiseControlControllerJMXAdaptor(controller); 87 this.user = user; 88 this.password = password; 89 90 Iterator i = MBeanServerFactory.findMBeanServer(null).iterator(); 91 MBeanServer server = i.hasNext() ? (MBeanServer ) i.next() : MBeanServerFactory.createMBeanServer(); 92 try { 93 controllerAdaptor.register(server); 94 } catch (Exception e) { 95 LOG.error("Problem registering CruiseControlController HttpAdaptor", e); 96 } 97 try { 98 registerHttpAdaptor(server); 99 } catch (Exception e) { 100 LOG.error("Problem registering HttpAdaptor", e); 101 } 102 try { 103 registerConnectorServer(server); 104 } catch (Exception e) { 105 LOG.error("Problem registering ConnectorServer", e); 106 } 107 try { 108 ObjectName name = new ObjectName ("Logger:name=root"); 109 server.registerMBean(new LoggerController(Logger.getRootLogger()), name); 110 } catch (Exception e) { 111 LOG.error("Problem registering LoggerController for root-Logger", e); 112 } 113 114 } 115 116 public void start() { 117 if (useHttpAdaptor()) { 118 try { 119 LOG.info("starting httpAdaptor"); 120 httpAdaptor.start(); 121 } catch (IOException e) { 122 LOG.error("Exception starting httpAdaptor", e); 123 } 124 } 125 if (useConnectorServer()) { 126 try { 127 LOG.info("starting rmiRegistry"); 128 rmiRegistry.start(); 129 } catch (RemoteException e) { 130 if (e.getMessage().startsWith("Port already in use")) { 131 LOG.warn("Port " + connectorServerPort + " is already in use, so no new rmiRegistry is started"); 132 } else { 133 LOG.error("Exception starting rmiRegistry", e); 134 } 135 } 136 try { 137 LOG.info("starting connectorServer"); 138 connectorServer.start(); 139 } catch (Exception e) { 140 if (e.getMessage().startsWith("javax.naming.NameAlreadyBoundException")) { 141 LOG.warn("Couldn't start connectorServer since its name (" + JNDI_NAME 142 + ") is already bound; you might need to restart your rmi registry"); 143 } else { 144 LOG.error("Exception starting connectorServer", e); 145 } 146 } 147 } 148 } 149 150 public void stop() { 151 if (useHttpAdaptor() && httpAdaptor.isActive()) { 152 httpAdaptor.stop(); 153 } 154 if (useConnectorServer()) { 155 if (connectorServer.isActive()) { 156 try { 157 LOG.info("stopping connectorServer"); 158 connectorServer.stop(); 159 } catch (IOException e) { 160 LOG.error("IOException stopping connectorServer", e); 161 } 162 } 163 if (rmiRegistry.isRunning()) { 164 try { 165 LOG.info("stopping rmiRegistry"); 166 rmiRegistry.stop(); 167 } catch (NoSuchObjectException e) { 168 LOG.error("NoSuchObjectException stopping rmiRegistry", e); 169 } 170 } 171 } 172 } 173 174 private void registerHttpAdaptor(MBeanServer server) throws Exception { 175 if (useHttpAdaptor()) { 176 httpAdaptor.setPort(httpPort); 177 System.setProperty("cruisecontrol.jmxport", String.valueOf(httpPort)); 178 httpAdaptor.setHost("0.0.0.0"); 179 ObjectName adaptorName = new ObjectName ("Adapter:name=HttpAdaptor,httpPort=" + httpPort); 180 server.registerMBean(httpAdaptor, adaptorName); 181 ObjectName processorName = new ObjectName ("Http:name=XSLTProcessor"); 182 server.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null); 183 String pathInJar = "net/sourceforge/cruisecontrol/jmx/xsl"; 184 if (path != null && !path.equals("")) { 185 LOG.info("Starting HttpAdaptor with customized Stylesheets"); 186 server.setAttribute(processorName, new Attribute ("File", path)); 187 } else { 188 LOG.info("Starting HttpAdaptor with CC-Stylesheets"); 189 server.setAttribute(processorName, new Attribute ("PathInJar", pathInJar)); 190 } 191 server.setAttribute(adaptorName, new Attribute ("ProcessorName", processorName)); 192 if (user != null && password != null) { 193 LOG.info("This CruiseControl instance is password protected"); 194 httpAdaptor.setAuthenticationMethod("basic"); 195 httpAdaptor.addAuthorization(user, password); 196 } 197 } 198 } 199 200 private boolean useHttpAdaptor() { 201 return httpPort != MainArgs.NOT_FOUND; 202 } 203 204 private void registerConnectorServer(MBeanServer server) throws Exception { 205 if (useConnectorServer()) { 206 ObjectName naming = new ObjectName ("Naming:type=rmiregistry"); 208 rmiRegistry = new NamingService(connectorServerPort); 209 server.registerMBean(rmiRegistry, naming); 210 System.setProperty("cruisecontrol.rmiport", String.valueOf(connectorServerPort)); 211 212 JMXServiceURL address = new JMXServiceURL ("rmi", "localhost", 0, JNDI_NAME); 213 214 Map environment = new HashMap (); 215 final String registryContextFactory = "com.sun.jndi.rmi.registry.RegistryContextFactory"; 216 environment.put(Context.INITIAL_CONTEXT_FACTORY, registryContextFactory); 217 environment.put(Context.PROVIDER_URL, "rmi://localhost:" + connectorServerPort); 218 219 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, environment, server); 220 ObjectName connServerName = new ObjectName ("ConnectorServer:name=" + JNDI_NAME); 221 server.registerMBean(connectorServer, connServerName); 222 } 223 } 224 225 private boolean useConnectorServer() { 226 return connectorServerPort != MainArgs.NOT_FOUND; 227 } 228 } 229 | Popular Tags |