1 17 18 package org.sape.carbon.services.jmx.server.mx4j; 19 20 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import javax.management.Attribute ; 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectInstance ; 28 import javax.management.ObjectName ; 29 30 import org.sape.carbon.core.component.Component; 31 import org.sape.carbon.core.component.ComponentConfiguration; 32 import org.sape.carbon.core.component.lifecycle.Configurable; 33 import org.sape.carbon.core.component.lifecycle.Destroyable; 34 import org.sape.carbon.core.component.lifecycle.Initializable; 35 import org.sape.carbon.core.component.lifecycle.Startable; 36 import org.sape.carbon.core.config.InvalidConfigurationException; 37 import org.sape.carbon.services.jmx.server.MBeanServerService; 38 39 import mx4j.adaptor.http.HttpAdaptor; 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 44 45 54 public class DefaultHttpAdaptorServiceImpl 55 implements Configurable, Initializable, 56 Startable, Destroyable, MBeanServerService { 57 58 61 private Log log = LogFactory.getLog(this.getClass()); 62 63 64 private MBeanServer server; 65 66 67 private HttpAdaptor adaptor; 68 69 70 private int port; 71 72 73 private String host; 74 75 76 private int retries; 77 78 85 public void configure(ComponentConfiguration configuration) { 86 try { 87 HttpAdaptorConfiguration config = 88 (HttpAdaptorConfiguration) configuration; 89 90 this.server = config.getMBeanServerService().getMBeanServer(); 91 92 this.port = config.getPort(); 93 this.host = config.getHostname(); 94 this.retries = config.getPortIncrementals(); 95 96 ObjectName adaptorName = new ObjectName ("Server:name=HttpAdaptor"); 97 this.adaptor = new HttpAdaptor(); 98 this.adaptor.setPort(config.getPort()); 99 this.adaptor.setHost(config.getHostname()); 100 this.server.registerMBean(adaptor, adaptorName); 101 102 ObjectName processorName = 103 new ObjectName ("Server:name=XSLTProcessor"); 104 105 this.server.createMBean( 106 "mx4j.adaptor.http.XSLTProcessor", processorName, null); 107 108 this.server.setAttribute( 109 adaptorName, new Attribute ("ProcessorName", processorName)); 110 111 this.server.invoke( 112 processorName, 113 "addMimeType", 114 new Object [] {".pdf", "application/pdf"}, 115 new String [] {"java.lang.String", "java.lang.String"}); 116 } catch (Exception e) { 117 throw new InvalidConfigurationException( 118 this.getClass(), 119 configuration.getConfigurationName(), "", 120 "Couldn't configure MBeanServer.", 121 e); 122 } 123 } 124 125 131 public void initialize(Component thisComponent) { 132 133 136 } 137 138 145 public void start() { 146 147 149 int retriesLeft = this.retries; 150 int currentPort = this.port; 151 boolean success = false; 152 while ((retriesLeft > 0) && (success == false)) { 153 try { 154 this.adaptor.setPort(currentPort); 155 this.adaptor.start(); 156 success = true; 157 } catch (IOException ioe) { 158 retriesLeft--; 159 currentPort++; 160 } 161 } 162 if (success) { 163 this.port = currentPort; 164 if (log.isDebugEnabled()) { 165 log.debug("Able to start JMX Web Console listener on port [" 166 + this.port + "]"); 167 } 168 } else { 169 if (log.isDebugEnabled()) { 170 log.debug("Unable to start JMX Web Console listener on port [" 171 + this.port + "] through [" 172 + (this.port + this.retries) + "]"); 173 } 174 } 175 } 176 177 185 public void stop() { 186 adaptor.stop(); 187 } 188 189 190 191 196 public MBeanServer getMBeanServer() { 197 return this.server; 198 } 199 200 205 public void destroy() { 206 this.adaptor.stop(); 207 Set mbeanSet = this.server.queryMBeans(null, null); 208 Iterator mbeanIterator = mbeanSet.iterator(); 209 while (mbeanIterator.hasNext()) { 210 ObjectInstance inst = (ObjectInstance ) mbeanIterator.next(); 211 try { 212 ObjectName objName = inst.getObjectName(); 213 this.server.unregisterMBean(objName); 214 if (log.isTraceEnabled()) { 215 log.trace("Successfully deregistered object [" 216 + inst.getObjectName() + "]"); 217 } 218 } catch (Exception e) { 219 if (log.isTraceEnabled()) { 220 log.trace("Could not deregistered object [" 221 + inst.getObjectName() + "]"); 222 } 223 } 224 } 225 226 } 227 228 } 229 | Popular Tags |