KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.equinox.http.registry.internal.ExtensionPointTracker.Listener;
19 import org.osgi.framework.AdminPermission;
20 import org.osgi.framework.Bundle;
21 import org.osgi.service.http.HttpContext;
22
23 public class HttpContextManager implements Listener {
24
25     private static final String JavaDoc HTTPCONTEXTS_EXTENSION_POINT = "org.eclipse.equinox.http.registry.httpcontexts"; //$NON-NLS-1$
26
private static final String JavaDoc HTTPCONTEXT = "httpcontext"; //$NON-NLS-1$
27
private static final String JavaDoc NAME = "name"; //$NON-NLS-1$
28
private static final String JavaDoc ID = "id"; //$NON-NLS-1$
29
private static final String JavaDoc CLASS = "class"; //$NON-NLS-1$
30
private static final String JavaDoc PATH = "path"; //$NON-NLS-1$
31
private static final String JavaDoc MIMEMAPPING = "mime-mapping"; //$NON-NLS-1$
32
private static final String JavaDoc MIMEEXTENSION = "extension"; //$NON-NLS-1$
33
private static final String JavaDoc MIMETYPE = "mime-type"; //$NON-NLS-1$
34
private static final String JavaDoc RESOURCEMAPPING = "resource-mapping"; //$NON-NLS-1$
35
private static final String JavaDoc BUNDLE = "bundle"; //$NON-NLS-1$
36

37     private List JavaDoc registered = new ArrayList JavaDoc();
38     private HttpRegistryManager httpRegistryManager;
39     private ExtensionPointTracker tracker;
40
41     public HttpContextManager(HttpRegistryManager httpRegistryManager, IExtensionRegistry registry) {
42         this.httpRegistryManager = httpRegistryManager;
43         tracker = new ExtensionPointTracker(registry, HTTPCONTEXTS_EXTENSION_POINT, this);
44     }
45
46     public void start() {
47         tracker.open();
48     }
49
50     public void stop() {
51         tracker.close();
52     }
53
54     public void added(IExtension extension) {
55         IConfigurationElement[] elements = extension.getConfigurationElements();
56         for (int i = 0; i < elements.length; i++) {
57             IConfigurationElement httpContextElement = elements[i];
58             if (!HTTPCONTEXT.equals(httpContextElement.getName()))
59                 continue;
60
61             String JavaDoc httpContextId = httpContextElement.getAttribute(ID);
62             if (httpContextId == null) {
63                 httpContextId = httpContextElement.getAttribute(NAME);
64                 if (httpContextId == null)
65                     continue;
66             }
67
68             if (httpContextId.indexOf('.') == -1)
69                 httpContextId = httpContextElement.getNamespaceIdentifier() + "." + httpContextId; //$NON-NLS-1$
70

71             HttpContext context = null;
72             String JavaDoc clazz = httpContextElement.getAttribute(CLASS);
73             if (clazz != null) {
74                 try {
75                     context = (HttpContext) httpContextElement.createExecutableExtension(CLASS);
76                 } catch (CoreException e) {
77                     // log it.
78
e.printStackTrace();
79                     continue;
80                 }
81             } else {
82                 Bundle contributingBundle = httpRegistryManager.getBundle(extension.getContributor());
83                 DefaultRegistryHttpContext defaultContext = httpRegistryManager.createDefaultRegistryHttpContext();
84
85                 String JavaDoc oldPath = httpContextElement.getAttribute(PATH);
86                 if (oldPath != null)
87                     defaultContext.addResourceMapping(contributingBundle, oldPath);
88
89                 IConfigurationElement[] resourceMappingElements = httpContextElement.getChildren(RESOURCEMAPPING);
90                 for (int j = 0; j < resourceMappingElements.length; j++) {
91                     IConfigurationElement resourceMappingElement = resourceMappingElements[i];
92                     String JavaDoc path = resourceMappingElement.getAttribute(PATH);
93                     Bundle resourceBundle = contributingBundle;
94                     String JavaDoc bundleName = resourceMappingElement.getAttribute(BUNDLE);
95                     if (bundleName != null) {
96                         resourceBundle = httpRegistryManager.getBundle(bundleName);
97                         if (resourceBundle == null)
98                             continue;
99                         if (System.getSecurityManager() != null) {
100                             AdminPermission resourcePermission = new AdminPermission(resourceBundle, "resource"); //$NON-NLS-1$
101
if (!contributingBundle.hasPermission(resourcePermission))
102                                 continue;
103                         }
104                     }
105                     defaultContext.addResourceMapping(resourceBundle, path);
106                 }
107
108                 IConfigurationElement[] mimeMappingElements = httpContextElement.getChildren(MIMEMAPPING);
109                 for (int j = 0; j < mimeMappingElements.length; j++) {
110                     IConfigurationElement mimeMappingElement = mimeMappingElements[i];
111                     String JavaDoc mimeExtension = mimeMappingElement.getAttribute(MIMEEXTENSION);
112                     String JavaDoc mimeType = mimeMappingElement.getAttribute(MIMETYPE);
113                     defaultContext.addMimeMapping(mimeExtension, mimeType);
114                 }
115                 context = defaultContext;
116             }
117
118             if (httpRegistryManager.addHttpContextContribution(httpContextId, context, extension.getContributor()))
119                 registered.add(httpContextElement);
120         }
121     }
122
123     public void removed(IExtension extension) {
124         IConfigurationElement[] elements = extension.getConfigurationElements();
125         for (int i = 0; i < elements.length; i++) {
126             IConfigurationElement httpContextElement = elements[i];
127             if (!HTTPCONTEXT.equals(httpContextElement.getName()))
128                 continue;
129
130             String JavaDoc httpContextId = httpContextElement.getAttribute(ID);
131             if (httpContextId == null) {
132                 httpContextId = httpContextElement.getAttribute(NAME);
133                 if (httpContextId == null)
134                     continue;
135             }
136             if (httpContextId.indexOf('.') == -1)
137                 httpContextId = httpContextElement.getNamespaceIdentifier() + "." + httpContextId; //$NON-NLS-1$
138

139             if (registered.remove(httpContextElement))
140                 httpRegistryManager.removeHttpContextContribution(httpContextId);
141         }
142     }
143 }
144
Popular Tags