1 17 package org.apache.geronimo.jetty6; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.geronimo.gbean.AbstractName; 27 import org.apache.geronimo.gbean.AbstractNameQuery; 28 import org.apache.geronimo.gbean.GBeanData; 29 import org.apache.geronimo.gbean.GBeanInfo; 30 import org.apache.geronimo.gbean.GBeanInfoBuilder; 31 import org.apache.geronimo.gbean.ReferencePatterns; 32 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 33 import org.apache.geronimo.jetty6.connector.AJP13Connector; 34 import org.apache.geronimo.jetty6.connector.HTTPConnector; 35 import org.apache.geronimo.jetty6.connector.HTTPSConnector; 36 import org.apache.geronimo.jetty6.connector.JettyConnector; 37 import org.apache.geronimo.jetty6.requestlog.JettyLogManager; 38 import org.apache.geronimo.kernel.GBeanNotFoundException; 39 import org.apache.geronimo.kernel.Kernel; 40 import org.apache.geronimo.kernel.config.ConfigurationUtil; 41 import org.apache.geronimo.kernel.config.EditableConfigurationManager; 42 import org.apache.geronimo.kernel.config.InvalidConfigException; 43 import org.apache.geronimo.kernel.proxy.ProxyManager; 44 import org.apache.geronimo.management.geronimo.KeystoreManager; 45 import org.apache.geronimo.management.geronimo.NetworkConnector; 46 import org.apache.geronimo.management.geronimo.WebAccessLog; 47 import org.apache.geronimo.management.geronimo.WebConnector; 48 import org.apache.geronimo.management.geronimo.WebContainer; 49 import org.apache.geronimo.management.geronimo.WebManager; 50 51 57 public class JettyManagerImpl implements WebManager { 58 private final static Log log = LogFactory.getLog(JettyManagerImpl.class); 59 private final Kernel kernel; 60 61 public JettyManagerImpl(Kernel kernel) { 62 this.kernel = kernel; 63 } 64 65 public String getProductName() { 66 return "Jetty"; 67 } 68 69 74 public WebConnector addConnector(WebContainer container, String uniqueName, String protocol, String host, int port) { 75 AbstractName containerName = kernel.getAbstractNameFor(container); 76 AbstractName name = kernel.getNaming().createSiblingName(containerName, uniqueName, NameFactory.GERONIMO_SERVICE); 77 GBeanData connector; 78 if (protocol.equals(PROTOCOL_HTTP)) { 79 connector = new GBeanData(name, HTTPConnector.GBEAN_INFO); 80 } else if (protocol.equals(PROTOCOL_HTTPS)) { 81 connector = new GBeanData(name, HTTPSConnector.GBEAN_INFO); 82 AbstractNameQuery query = new AbstractNameQuery(KeystoreManager.class.getName()); 83 connector.setReferencePattern("KeystoreManager", query); 84 } else if (protocol.equals(PROTOCOL_AJP)) { 86 connector = new GBeanData(name, AJP13Connector.GBEAN_INFO); 87 } else { 88 throw new IllegalArgumentException ("Invalid protocol '" + protocol + "'"); 89 } 90 connector.setAttribute("host", host); 91 connector.setAttribute("port", new Integer (port)); 92 connector.setAttribute("minThreads", new Integer (10)); 93 connector.setAttribute("maxThreads", new Integer (50)); 94 connector.setReferencePattern(JettyConnector.CONNECTOR_CONTAINER_REFERENCE, containerName); 95 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel); 96 if(mgr != null) { 97 try { 98 mgr.addGBeanToConfiguration(containerName.getArtifact(), connector, false); 99 return (WebConnector) kernel.getProxyManager().createProxy(name, JettyWebConnector.class.getClassLoader()); 100 } catch (InvalidConfigException e) { 101 log.error("Unable to add GBean", e); 102 return null; 103 } finally { 104 ConfigurationUtil.releaseConfigurationManager(kernel, mgr); 105 } 106 } else { 107 log.warn("The ConfigurationManager in the kernel does not allow editing"); 108 return null; 109 } 110 } 111 112 115 public Object [] getContainers() { 116 ProxyManager proxyManager = kernel.getProxyManager(); 117 AbstractNameQuery query = new AbstractNameQuery(JettyContainer.class.getName()); 118 Set names = kernel.listGBeans(query); 119 JettyContainer[] results = new JettyContainer[names.size()]; 120 int i=0; 121 for (Iterator it = names.iterator(); it.hasNext(); i++) { 122 AbstractName name = (AbstractName) it.next(); 123 results[i] = (JettyContainer) proxyManager.createProxy(name, JettyContainer.class.getClassLoader()); 124 } 125 return results; 126 } 127 128 132 public String [] getSupportedProtocols() { 133 return new String []{PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_AJP}; 134 } 135 136 142 public void removeConnector(AbstractName connectorName) { 143 try { 144 GBeanInfo info = kernel.getGBeanInfo(connectorName); 145 boolean found = false; 146 Set intfs = info.getInterfaces(); 147 for (Iterator it = intfs.iterator(); it.hasNext();) { 148 String intf = (String ) it.next(); 149 if (intf.equals(JettyWebConnector.class.getName())) { 150 found = true; 151 } 152 } 153 if (!found) { 154 throw new GBeanNotFoundException(connectorName); 155 } 156 EditableConfigurationManager mgr = ConfigurationUtil.getEditableConfigurationManager(kernel); 157 if(mgr != null) { 158 try { 159 mgr.removeGBeanFromConfiguration(connectorName.getArtifact(), connectorName); 160 } catch (InvalidConfigException e) { 161 log.error("Unable to add GBean", e); 162 } finally { 163 ConfigurationUtil.releaseConfigurationManager(kernel, mgr); 164 } 165 } else { 166 log.warn("The ConfigurationManager in the kernel does not allow editing"); 167 } 168 } catch (GBeanNotFoundException e) { 169 log.warn("No such GBean '" + connectorName + "'"); } catch (Exception e) { 171 log.error(e); 172 } 173 } 174 175 181 public NetworkConnector[] getConnectors(String protocol) { 182 if(protocol == null) { 183 return getConnectors(); 184 } 185 List result = new ArrayList (); 186 ProxyManager proxyManager = kernel.getProxyManager(); 187 AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName()); 188 Set names = kernel.listGBeans(query); 189 for (Iterator it = names.iterator(); it.hasNext();) { 190 AbstractName name = (AbstractName) it.next(); 191 try { 192 if (kernel.getAttribute(name, "protocol").equals(protocol)) { 193 result.add(proxyManager.createProxy(name, JettyWebConnector.class.getClassLoader())); 194 } 195 } catch (Exception e) { 196 log.error("Unable to check the protocol for a connector", e); 197 } 198 } 199 return (JettyWebConnector[]) result.toArray(new JettyWebConnector[names.size()]); 200 } 201 202 public WebAccessLog getAccessLog(WebContainer container) { 203 AbstractNameQuery query = new AbstractNameQuery(JettyLogManager.class.getName()); 204 Set names = kernel.listGBeans(query); 205 if(names.size() == 0) { 206 return null; 207 } else if(names.size() > 1) { 208 throw new IllegalStateException ("Should not be more than one Jetty access log manager"); 209 } 210 return (WebAccessLog) kernel.getProxyManager().createProxy((AbstractName)names.iterator().next(), JettyLogManager.class.getClassLoader()); 211 } 212 213 216 public NetworkConnector[] getConnectors() { 217 ProxyManager proxyManager = kernel.getProxyManager(); 218 AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName()); 219 Set names = kernel.listGBeans(query); 220 JettyWebConnector[] results = new JettyWebConnector[names.size()]; 221 int i=0; 222 for (Iterator it = names.iterator(); it.hasNext(); i++) { 223 AbstractName name = (AbstractName) it.next(); 224 results[i] = (JettyWebConnector) proxyManager.createProxy(name, JettyWebConnector.class.getClassLoader()); 225 } 226 return results; 227 } 228 229 public NetworkConnector[] getConnectorsForContainer(Object container, String protocol) { 230 if(protocol == null) { 231 return getConnectorsForContainer(container); 232 } 233 AbstractName containerName = kernel.getAbstractNameFor(container); 234 ProxyManager mgr = kernel.getProxyManager(); 235 try { 236 List results = new ArrayList (); 237 AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName()); 238 Set set = kernel.listGBeans(query); for (Iterator it = set.iterator(); it.hasNext();) { 240 AbstractName name = (AbstractName) it.next(); GBeanData data = kernel.getGBeanData(name); 242 ReferencePatterns refs = data.getReferencePatterns(JettyConnector.CONNECTOR_CONTAINER_REFERENCE); 243 if(containerName.equals(refs.getAbstractName())) { 244 try { 245 String testProtocol = (String ) kernel.getAttribute(name, "protocol"); 246 if(testProtocol != null && testProtocol.equals(protocol)) { 247 results.add(mgr.createProxy(name, JettyWebConnector.class.getClassLoader())); 248 } 249 } catch (Exception e) { 250 log.error("Unable to look up protocol for connector '"+name+"'",e); 251 } 252 break; 253 } 254 } 255 return (JettyWebConnector[]) results.toArray(new JettyWebConnector[results.size()]); 256 } catch (Exception e) { 257 throw (IllegalArgumentException )new IllegalArgumentException ("Unable to look up connectors for Jetty container '"+containerName +"': ").initCause(e); 258 } 259 } 260 261 public NetworkConnector[] getConnectorsForContainer(Object container) { 262 AbstractName containerName = kernel.getAbstractNameFor(container); 263 ProxyManager mgr = kernel.getProxyManager(); 264 try { 265 List results = new ArrayList (); 266 AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName()); 267 Set set = kernel.listGBeans(query); for (Iterator it = set.iterator(); it.hasNext();) { 269 AbstractName name = (AbstractName) it.next(); GBeanData data = kernel.getGBeanData(name); 271 ReferencePatterns refs = data.getReferencePatterns(JettyConnector.CONNECTOR_CONTAINER_REFERENCE); 272 if (containerName.equals(refs.getAbstractName())) { 273 results.add(mgr.createProxy(name, JettyWebConnector.class.getClassLoader())); 274 } 275 } 276 return (JettyWebConnector[]) results.toArray(new JettyWebConnector[results.size()]); 277 } catch (Exception e) { 278 throw (IllegalArgumentException ) new IllegalArgumentException ("Unable to look up connectors for Jetty container '"+containerName+"'").initCause(e); 279 } 280 } 281 282 public static final GBeanInfo GBEAN_INFO; 283 284 static { 285 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Jetty Web Manager", JettyManagerImpl.class); 286 infoFactory.addAttribute("kernel", Kernel.class, false); 287 infoFactory.addInterface(WebManager.class); 288 infoFactory.setConstructor(new String [] {"kernel"}); 289 GBEAN_INFO = infoFactory.getBeanInfo(); 290 } 291 292 public static GBeanInfo getGBeanInfo() { 293 return GBEAN_INFO; 294 } 295 } 296 | Popular Tags |