KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > JettyManagerImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty6;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
52  * Jetty implementation of WebManager. Knows how to manipulate
53  * other Jetty objects for management purposes.
54  *
55  * @version $Rev:386276 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
56  */

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 JavaDoc getProductName() {
66         return "Jetty";
67     }
68
69     /**
70      * Creates a new connector, and returns the ObjectName for it. Note that
71      * the connector may well require further customization before being fully
72      * functional (e.g. SSL settings for an HTTPS connector).
73      */

74     public WebConnector addConnector(WebContainer container, String JavaDoc uniqueName, String JavaDoc protocol, String JavaDoc 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             //todo: default HTTPS settings
85
} else if (protocol.equals(PROTOCOL_AJP)) {
86             connector = new GBeanData(name, AJP13Connector.GBEAN_INFO);
87         } else {
88             throw new IllegalArgumentException JavaDoc("Invalid protocol '" + protocol + "'");
89         }
90         connector.setAttribute("host", host);
91         connector.setAttribute("port", new Integer JavaDoc(port));
92         connector.setAttribute("minThreads", new Integer JavaDoc(10));
93         connector.setAttribute("maxThreads", new Integer JavaDoc(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     /**
113      * Get a list of containers for this web implementation.
114      */

115     public Object JavaDoc[] getContainers() {
116         ProxyManager proxyManager = kernel.getProxyManager();
117         AbstractNameQuery query = new AbstractNameQuery(JettyContainer.class.getName());
118         Set JavaDoc names = kernel.listGBeans(query);
119         JettyContainer[] results = new JettyContainer[names.size()];
120         int i=0;
121         for (Iterator JavaDoc 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     /**
129      * Gets the protocols that this web container supports (that you can create
130      * connectors for).
131      */

132     public String JavaDoc[] getSupportedProtocols() {
133         return new String JavaDoc[]{PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_AJP};
134     }
135
136     /**
137      * Removes a connector. This shuts it down if necessary, and removes it
138      * from the server environment. It must be a connector that this container
139      * is responsible for.
140      * @param connectorName
141      */

142     public void removeConnector(AbstractName connectorName) {
143         try {
144             GBeanInfo info = kernel.getGBeanInfo(connectorName);
145             boolean found = false;
146             Set JavaDoc intfs = info.getInterfaces();
147             for (Iterator JavaDoc it = intfs.iterator(); it.hasNext();) {
148                 String JavaDoc intf = (String JavaDoc) 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 + "'"); //todo: what if we want to remove a failed GBean?
170
} catch (Exception JavaDoc e) {
171             log.error(e);
172         }
173     }
174
175     /**
176      * Gets the ObjectNames of any existing connectors for the specified
177      * protocol.
178      *
179      * @param protocol A protocol as returned by getSupportedProtocols
180      */

181     public NetworkConnector[] getConnectors(String JavaDoc protocol) {
182         if(protocol == null) {
183             return getConnectors();
184         }
185         List JavaDoc result = new ArrayList JavaDoc();
186         ProxyManager proxyManager = kernel.getProxyManager();
187         AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName());
188         Set JavaDoc names = kernel.listGBeans(query);
189         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc names = kernel.listGBeans(query);
205         if(names.size() == 0) {
206             return null;
207         } else if(names.size() > 1) {
208             throw new IllegalStateException JavaDoc("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     /**
214      * Gets the ObjectNames of any existing connectors.
215      */

216     public NetworkConnector[] getConnectors() {
217         ProxyManager proxyManager = kernel.getProxyManager();
218         AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName());
219         Set JavaDoc names = kernel.listGBeans(query);
220         JettyWebConnector[] results = new JettyWebConnector[names.size()];
221         int i=0;
222         for (Iterator JavaDoc 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 JavaDoc container, String JavaDoc 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 JavaDoc results = new ArrayList JavaDoc();
237             AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName());
238             Set JavaDoc set = kernel.listGBeans(query); // all Jetty connectors
239
for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
240                 AbstractName name = (AbstractName) it.next(); // a single Jetty connector
241
GBeanData data = kernel.getGBeanData(name);
242                 ReferencePatterns refs = data.getReferencePatterns(JettyConnector.CONNECTOR_CONTAINER_REFERENCE);
243                 if(containerName.equals(refs.getAbstractName())) {
244                     try {
245                         String JavaDoc testProtocol = (String JavaDoc) kernel.getAttribute(name, "protocol");
246                         if(testProtocol != null && testProtocol.equals(protocol)) {
247                             results.add(mgr.createProxy(name, JettyWebConnector.class.getClassLoader()));
248                         }
249                     } catch (Exception JavaDoc 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 JavaDoc e) {
257             throw (IllegalArgumentException JavaDoc)new IllegalArgumentException JavaDoc("Unable to look up connectors for Jetty container '"+containerName +"': ").initCause(e);
258         }
259     }
260
261     public NetworkConnector[] getConnectorsForContainer(Object JavaDoc container) {
262         AbstractName containerName = kernel.getAbstractNameFor(container);
263         ProxyManager mgr = kernel.getProxyManager();
264         try {
265             List JavaDoc results = new ArrayList JavaDoc();
266             AbstractNameQuery query = new AbstractNameQuery(JettyWebConnector.class.getName());
267             Set JavaDoc set = kernel.listGBeans(query); // all Jetty connectors
268
for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
269                 AbstractName name = (AbstractName) it.next(); // a single Jetty connector
270
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 JavaDoc e) {
278             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc("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 JavaDoc[] {"kernel"});
289         GBEAN_INFO = infoFactory.getBeanInfo();
290     }
291
292     public static GBeanInfo getGBeanInfo() {
293         return GBEAN_INFO;
294     }
295 }
296
Popular Tags