KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.equinox.http.registry.internal;
12
13 import java.util.*;
14 import javax.servlet.Servlet JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import org.eclipse.core.runtime.IContributor;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.osgi.framework.*;
19 import org.osgi.service.http.*;
20 import org.osgi.service.packageadmin.PackageAdmin;
21
22 public class HttpRegistryManager {
23
24     class ResourcesContribution {
25         String JavaDoc alias;
26         String JavaDoc baseName;
27         String JavaDoc httpContextId;
28         IContributor contributor;
29
30         public ResourcesContribution(String JavaDoc alias, String JavaDoc baseName, String JavaDoc httpContextId, IContributor contributor) {
31             this.alias = alias;
32             this.baseName = baseName;
33             this.httpContextId = httpContextId;
34             this.contributor = contributor;
35         }
36     }
37
38     class ServletContribution {
39         String JavaDoc alias;
40         Servlet JavaDoc servlet;
41         Dictionary initparams;
42         String JavaDoc httpContextId;
43         IContributor contributor;
44
45         public ServletContribution(String JavaDoc alias, Servlet JavaDoc servlet, Dictionary initparams, String JavaDoc httpContextId, IContributor contributor) {
46             this.alias = alias;
47             this.servlet = servlet;
48             this.initparams = initparams;
49             this.httpContextId = httpContextId;
50             this.contributor = contributor;
51         }
52     }
53
54     class HttpContextContribution {
55         HttpContext context;
56         IContributor contributor;
57
58         public HttpContextContribution(HttpContext context, IContributor contributor) {
59             this.context = context;
60             this.contributor = contributor;
61         }
62     }
63
64     private HttpContextManager httpContextManager;
65     private ServletManager servletManager;
66     private ResourceManager resourceManager;
67     private HttpService httpService;
68     private PackageAdmin packageAdmin;
69     private Map contexts = new HashMap();
70     private Map servlets = new HashMap();
71     private Map resources = new HashMap();
72     private Set registered = new HashSet();
73
74     public HttpRegistryManager(ServiceReference reference, HttpService httpService, PackageAdmin packageAdmin, IExtensionRegistry registry) {
75         this.httpService = httpService;
76         this.packageAdmin = packageAdmin;
77
78         httpContextManager = new HttpContextManager(this, registry);
79         servletManager = new ServletManager(this, reference, registry);
80         resourceManager = new ResourceManager(this, reference, registry);
81     }
82
83     public void start() {
84         httpContextManager.start();
85         servletManager.start();
86         resourceManager.start();
87     }
88
89     public void stop() {
90         resourceManager.stop();
91         servletManager.stop();
92         httpContextManager.stop();
93     }
94
95     public synchronized boolean addResourcesContribution(String JavaDoc alias, String JavaDoc baseName, String JavaDoc httpContextId, IContributor contributor) {
96         if (resources.containsKey(alias) || servlets.containsKey(alias))
97             return false; // TODO: should log this
98

99         ResourcesContribution contribution = new ResourcesContribution(alias, baseName, httpContextId, contributor);
100         resources.put(alias, contribution);
101         if (httpContextId == null || contexts.containsKey(httpContextId))
102             registerResources(contribution);
103
104         return true;
105     }
106
107     public synchronized boolean addServletContribution(String JavaDoc alias, Servlet JavaDoc servlet, Dictionary initparams, String JavaDoc httpContextId, IContributor contributor) {
108         if (resources.containsKey(alias) || servlets.containsKey(alias))
109             return false; // TODO: should log this
110

111         ServletContribution contribution = new ServletContribution(alias, servlet, initparams, httpContextId, contributor);
112         servlets.put(alias, contribution);
113         if (httpContextId == null || contexts.containsKey(httpContextId))
114             registerServlet(contribution);
115
116         return true;
117     }
118
119     public synchronized void removeContribution(String JavaDoc alias) {
120         resources.remove(alias);
121         servlets.remove(alias);
122         unregister(alias);
123     }
124
125     public synchronized HttpContext getHttpContext(String JavaDoc httpContextId, Bundle bundle) {
126         HttpContextContribution contribution = (HttpContextContribution) contexts.get(httpContextId);
127         if (contribution == null)
128             return null;
129
130         if (System.getSecurityManager() != null) {
131             Bundle httpContextBundle = getBundle(contribution.contributor);
132             AdminPermission resourcePermission = new AdminPermission(httpContextBundle, "resource"); //$NON-NLS-1$
133
if (!bundle.hasPermission(resourcePermission))
134                 return null;
135         }
136         return contribution.context;
137     }
138
139     public synchronized boolean addHttpContextContribution(String JavaDoc httpContextId, HttpContext context, IContributor contributor) {
140         if (contexts.containsKey(httpContextId))
141             return false; // TODO: should log this
142

143         contexts.put(httpContextId, new HttpContextContribution(context, contributor));
144         for (Iterator it = resources.values().iterator(); it.hasNext();) {
145             ResourcesContribution contribution = (ResourcesContribution) it.next();
146             if (httpContextId.equals(contribution.httpContextId))
147                 registerResources(contribution);
148         }
149
150         for (Iterator it = servlets.values().iterator(); it.hasNext();) {
151             ServletContribution contribution = (ServletContribution) it.next();
152             if (httpContextId.equals(contribution.httpContextId))
153                 registerServlet(contribution);
154         }
155         return true;
156     }
157
158     public synchronized void removeHttpContextContribution(String JavaDoc httpContextId) {
159         if (contexts.remove(httpContextId) != null) {
160             for (Iterator it = resources.values().iterator(); it.hasNext();) {
161                 ResourcesContribution contribution = (ResourcesContribution) it.next();
162                 if (httpContextId.equals(contribution.httpContextId))
163                     unregister(contribution.alias);
164             }
165
166             for (Iterator it = servlets.values().iterator(); it.hasNext();) {
167                 ServletContribution contribution = (ServletContribution) it.next();
168                 if (httpContextId.equals(contribution.httpContextId))
169                     unregister(contribution.alias);
170             }
171         }
172     }
173
174     public DefaultRegistryHttpContext createDefaultRegistryHttpContext() {
175         HttpContext defaultContext = httpService.createDefaultHttpContext();
176         return new DefaultRegistryHttpContext(defaultContext);
177     }
178
179     public Bundle getBundle(IContributor contributor) {
180         return getBundle(contributor.getName());
181     }
182
183     public Bundle getBundle(String JavaDoc symbolicName) {
184         Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
185         if (bundles == null)
186             return null;
187         //Return the first bundle that is not installed or uninstalled
188
for (int i = 0; i < bundles.length; i++) {
189             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
190                 return bundles[i];
191             }
192         }
193         return null;
194     }
195
196     private void registerResources(ResourcesContribution contribution) {
197         HttpContext context = getHttpContext(contribution.httpContextId, contribution.contributor);
198         if (context == null)
199             return;
200         try {
201             httpService.registerResources(contribution.alias, contribution.baseName, context);
202             registered.add(contribution.alias);
203         } catch (NamespaceException e) {
204             // TODO: should log this
205
e.printStackTrace();
206         }
207     }
208
209     private void registerServlet(ServletContribution contribution) {
210         HttpContext context = getHttpContext(contribution.httpContextId, contribution.contributor);
211         if (context == null)
212             return;
213         try {
214             httpService.registerServlet(contribution.alias, contribution.servlet, contribution.initparams, context);
215             registered.add(contribution.alias);
216         } catch (NamespaceException e) {
217             // TODO: should log this
218
e.printStackTrace();
219         } catch (ServletException JavaDoc e) {
220             // TODO: should log this
221
e.printStackTrace();
222         }
223     }
224
225     private void unregister(String JavaDoc alias) {
226         if (registered.remove(alias))
227             httpService.unregister(alias);
228     }
229
230     private HttpContext getHttpContext(String JavaDoc httpContextId, IContributor contributor) {
231         if (httpContextId == null) {
232             DefaultRegistryHttpContext defaultContext = createDefaultRegistryHttpContext();
233             defaultContext.addResourceMapping(getBundle(contributor), null);
234             return defaultContext;
235         }
236
237         HttpContextContribution contribution = (HttpContextContribution) contexts.get(httpContextId);
238         if (System.getSecurityManager() != null) {
239             Bundle contributorBundle = getBundle(contributor);
240             Bundle httpContextBundle = getBundle(contribution.contributor);
241             AdminPermission resourcePermission = new AdminPermission(httpContextBundle, "resource"); //$NON-NLS-1$
242
if (!contributorBundle.hasPermission(resourcePermission))
243                 return null;
244         }
245         return contribution.context;
246     }
247 }
248
Popular Tags