KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > registry > internal > ServletManager


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12
13 package org.eclipse.equinox.http.registry.internal;
14
15 import java.io.IOException JavaDoc;
16 import java.util.*;
17 import javax.servlet.*;
18 import org.eclipse.core.runtime.*;
19 import org.osgi.framework.*;
20
21 public class ServletManager implements ExtensionPointTracker.Listener {
22
23     private static final String JavaDoc SERVLETS_EXTENSION_POINT = "org.eclipse.equinox.http.registry.servlets"; //$NON-NLS-1$
24

25     private static final String JavaDoc HTTPCONTEXT_NAME = "httpcontext-name"; //$NON-NLS-1$
26

27     private static final String JavaDoc PARAM_VALUE = "value"; //$NON-NLS-1$
28

29     private static final String JavaDoc PARAM_NAME = "name"; //$NON-NLS-1$
30

31     private static final String JavaDoc INIT_PARAM = "init-param"; //$NON-NLS-1$
32

33     private static final String JavaDoc SERVLET = "servlet"; //$NON-NLS-1$
34

35     private static final String JavaDoc ALIAS = "alias"; //$NON-NLS-1$
36

37     private static final String JavaDoc LOAD_ON_STARTUP = "load-on-startup"; //$NON-NLS-1$
38

39     private static final String JavaDoc HTTPCONTEXT_ID = "httpcontextId"; //$NON-NLS-1$
40

41     private static final String JavaDoc SERVICESELECTOR = "serviceSelector"; //$NON-NLS-1$
42

43     private static final String JavaDoc CLASS = "class"; //$NON-NLS-1$
44

45     private static final String JavaDoc FILTER = "filter"; //$NON-NLS-1$
46

47     private ExtensionPointTracker tracker;
48
49     private HttpRegistryManager httpRegistryManager;
50
51     private List registered = new ArrayList();
52
53     private ServiceReference reference;
54
55     public ServletManager(HttpRegistryManager httpRegistryManager, ServiceReference reference, IExtensionRegistry registry) {
56         this.httpRegistryManager = httpRegistryManager;
57         this.reference = reference;
58         tracker = new ExtensionPointTracker(registry, SERVLETS_EXTENSION_POINT, this);
59     }
60
61     public void start() {
62         tracker.open();
63     }
64
65     public void stop() {
66         tracker.close();
67     }
68
69     public void added(IExtension extension) {
70         IConfigurationElement[] elements = extension.getConfigurationElements();
71         for (int i = 0; i < elements.length; i++) {
72             IConfigurationElement serviceSelectorElement = elements[i];
73             if (!SERVICESELECTOR.equals(serviceSelectorElement.getName()))
74                 continue;
75             
76             org.osgi.framework.Filter serviceSelector = null;
77             String JavaDoc clazz = serviceSelectorElement.getAttribute(CLASS);
78             if (clazz != null) {
79                 try {
80                     serviceSelector = (org.osgi.framework.Filter) serviceSelectorElement.createExecutableExtension(CLASS);
81                 } catch (CoreException e) {
82                     // log it.
83
e.printStackTrace();
84                     return;
85                 }
86             } else {
87                 String JavaDoc filter = serviceSelectorElement.getAttribute(FILTER);
88                 if (filter == null)
89                     return;
90                 
91                 try {
92                     serviceSelector = FrameworkUtil.createFilter(filter);
93                 } catch (InvalidSyntaxException e) {
94                     // log it.
95
e.printStackTrace();
96                     return;
97                 }
98             }
99             
100             if (! serviceSelector.match(reference))
101                 return;
102             
103             break;
104         }
105         
106         for (int i = 0; i < elements.length; i++) {
107             IConfigurationElement servletElement = elements[i];
108             if (!SERVLET.equals(servletElement.getName()))
109                 continue;
110
111             ServletWrapper wrapper = new ServletWrapper(servletElement);
112             String JavaDoc alias = servletElement.getAttribute(ALIAS);
113             if (alias == null)
114                 continue; // alias is mandatory - ignore this.
115

116             Dictionary initparams = new Hashtable();
117             IConfigurationElement[] initParams = servletElement.getChildren(INIT_PARAM);
118             for (int j = 0; j < initParams.length; ++j) {
119                 String JavaDoc paramName = initParams[j].getAttribute(PARAM_NAME);
120                 String JavaDoc paramValue = initParams[j].getAttribute(PARAM_VALUE);
121                 initparams.put(paramName, paramValue);
122             }
123
124             boolean loadOnStartup = new Boolean JavaDoc(servletElement.getAttribute(LOAD_ON_STARTUP)).booleanValue();
125             if (loadOnStartup)
126                 wrapper.setLoadOnStartup();
127
128             String JavaDoc httpContextId = servletElement.getAttribute(HTTPCONTEXT_ID);
129             if (httpContextId == null) {
130                 httpContextId = servletElement.getAttribute(HTTPCONTEXT_NAME);
131             }
132
133             if (httpContextId != null && httpContextId.indexOf('.') == -1)
134                 httpContextId = servletElement.getNamespaceIdentifier() + "." + httpContextId; //$NON-NLS-1$
135

136             if (httpRegistryManager.addServletContribution(alias, wrapper, initparams, httpContextId, extension.getContributor()))
137                 registered.add(servletElement);
138         }
139     }
140
141     public void removed(IExtension extension) {
142         IConfigurationElement[] elements = extension.getConfigurationElements();
143         for (int i = 0; i < elements.length; i++) {
144             IConfigurationElement servletElement = elements[i];
145             if (registered.remove(servletElement)) {
146                 String JavaDoc alias = servletElement.getAttribute(ALIAS);
147                 httpRegistryManager.removeContribution(alias);
148             }
149         }
150     }
151
152     private static class ServletWrapper implements Servlet {
153
154         private static final String JavaDoc CLASS = "class"; //$NON-NLS-1$
155
private IConfigurationElement element;
156         private Servlet delegate;
157         private ServletConfig config;
158         private boolean loadOnStartup = false;
159
160         public ServletWrapper(IConfigurationElement element) {
161             this.element = element;
162         }
163
164         public void setLoadOnStartup() {
165             this.loadOnStartup = true;
166         }
167
168         public void init(ServletConfig config) throws ServletException {
169             this.config = config;
170             if (loadOnStartup)
171                 initializeDelegate();
172         }
173
174         public ServletConfig getServletConfig() {
175             return config;
176         }
177
178         public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException JavaDoc {
179             initializeDelegate();
180             delegate.service(arg0, arg1);
181         }
182
183         public String JavaDoc getServletInfo() {
184             return ""; //$NON-NLS-1$
185
}
186
187         public void destroy() {
188             destroyDelegate();
189         }
190
191         private void initializeDelegate() throws ServletException {
192             if (delegate == null) {
193                 try {
194                     Servlet newDelegate = (Servlet) element.createExecutableExtension(CLASS);
195                     newDelegate.init(config);
196                     delegate = newDelegate;
197                 } catch (CoreException e) {
198                     throw new ServletException(e);
199                 }
200             }
201         }
202
203         private void destroyDelegate() {
204             if (delegate != null) {
205                 Servlet doomedDelegate = delegate;
206                 delegate = null;
207                 doomedDelegate.destroy();
208             }
209         }
210     }
211 }
212
Popular Tags