KickJava   Java API By Example, From Geeks To Geeks.

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


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.osgi.framework.*;
19
20 public class ResourceManager implements ExtensionPointTracker.Listener {
21
22     private static final String JavaDoc RESOURCES_EXTENSION_POINT = "org.eclipse.equinox.http.registry.resources"; //$NON-NLS-1$
23

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

26     private static final String JavaDoc BASE_NAME = "base-name"; //$NON-NLS-1$
27

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

30     private static final String JavaDoc RESOURCE = "resource"; //$NON-NLS-1$
31

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

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

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

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

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

109             String JavaDoc baseName = resourceElement.getAttribute(BASE_NAME);
110             if (baseName == null)
111                 baseName = ""; //$NON-NLS-1$
112

113             String JavaDoc httpContextId = resourceElement.getAttribute(HTTPCONTEXT_ID);
114             if (httpContextId == null) {
115                 httpContextId = resourceElement.getAttribute(HTTPCONTEXT_NAME);
116             }
117
118             if (httpContextId != null && httpContextId.indexOf('.') == -1)
119                 httpContextId = resourceElement.getNamespaceIdentifier() + "." + httpContextId; //$NON-NLS-1$
120

121             if (httpRegistryManager.addResourcesContribution(alias, baseName, httpContextId, extension.getContributor()))
122                 registered.add(resourceElement);
123         }
124     }
125
126     public void removed(IExtension extension) {
127         IConfigurationElement[] elements = extension.getConfigurationElements();
128         for (int i = 0; i < elements.length; i++) {
129             IConfigurationElement resourceElement = elements[i];
130             if (registered.remove(resourceElement)) {
131                 String JavaDoc alias = resourceElement.getAttribute(ALIAS);
132                 httpRegistryManager.removeContribution(alias);
133             }
134         }
135     }
136 }
137
Popular Tags