1 12 13 package org.eclipse.equinox.http.registry.internal; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 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 HTTPCONTEXTS_EXTENSION_POINT = "org.eclipse.equinox.http.registry.httpcontexts"; private static final String HTTPCONTEXT = "httpcontext"; private static final String NAME = "name"; private static final String ID = "id"; private static final String CLASS = "class"; private static final String PATH = "path"; private static final String MIMEMAPPING = "mime-mapping"; private static final String MIMEEXTENSION = "extension"; private static final String MIMETYPE = "mime-type"; private static final String RESOURCEMAPPING = "resource-mapping"; private static final String BUNDLE = "bundle"; 37 private List registered = new ArrayList (); 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 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; 71 HttpContext context = null; 72 String clazz = httpContextElement.getAttribute(CLASS); 73 if (clazz != null) { 74 try { 75 context = (HttpContext) httpContextElement.createExecutableExtension(CLASS); 76 } catch (CoreException e) { 77 e.printStackTrace(); 79 continue; 80 } 81 } else { 82 Bundle contributingBundle = httpRegistryManager.getBundle(extension.getContributor()); 83 DefaultRegistryHttpContext defaultContext = httpRegistryManager.createDefaultRegistryHttpContext(); 84 85 String 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 path = resourceMappingElement.getAttribute(PATH); 93 Bundle resourceBundle = contributingBundle; 94 String 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"); 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 mimeExtension = mimeMappingElement.getAttribute(MIMEEXTENSION); 112 String 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 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; 139 if (registered.remove(httpContextElement)) 140 httpRegistryManager.removeHttpContextContribution(httpContextId); 141 } 142 } 143 } 144 | Popular Tags |