KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > TargetDefinitionManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.pde.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.FileLocator;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtension;
23 import org.eclipse.core.runtime.IExtensionDelta;
24 import org.eclipse.core.runtime.IExtensionRegistry;
25 import org.eclipse.core.runtime.IRegistryChangeEvent;
26 import org.eclipse.core.runtime.IRegistryChangeListener;
27 import org.eclipse.core.runtime.Platform;
28 import org.osgi.framework.Bundle;
29
30 public class TargetDefinitionManager implements IRegistryChangeListener{
31     
32     Map JavaDoc fTargets;
33     private static String JavaDoc[] attributes;
34     {
35         attributes = new String JavaDoc[] {"id", "name" }; //$NON-NLS-1$ //$NON-NLS-2$
36
}
37
38     public void registryChanged(IRegistryChangeEvent event) {
39         IExtensionDelta[] deltas = event.getExtensionDeltas();
40         for (int i = 0; i < deltas.length; i++) {
41             IExtension extension = deltas[i].getExtension();
42             String JavaDoc extensionId = extension.getExtensionPointUniqueIdentifier();
43             if (extensionId.equals("org.eclipse.pde.core.targets")) { //$NON-NLS-1$
44
IConfigurationElement[] elems = extension.getConfigurationElements();
45                 if (deltas[i].getKind() == IExtensionDelta.ADDED)
46                     add(elems);
47                 else
48                     remove(elems);
49             }
50         }
51     }
52     
53     public IConfigurationElement[] getTargets() {
54         if (fTargets == null)
55             loadElements();
56         return (IConfigurationElement[])fTargets.values().toArray(new IConfigurationElement[fTargets.size()]);
57     }
58     
59     public IConfigurationElement[] getSortedTargets() {
60         if (fTargets == null)
61             loadElements();
62         IConfigurationElement[] result = (IConfigurationElement[])fTargets.values().toArray(new IConfigurationElement[fTargets.size()]);
63         Arrays.sort(result, new Comparator JavaDoc() {
64
65             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
66                 String JavaDoc value1 = getString((IConfigurationElement)o1);
67                 String JavaDoc value2 = getString((IConfigurationElement)o2);
68                 return value1.compareTo(value2);
69             }
70             
71             private String JavaDoc getString(IConfigurationElement elem){
72                 String JavaDoc name = elem.getAttribute("name"); //$NON-NLS-1$
73
String JavaDoc id = elem.getAttribute("id"); //$NON-NLS-1$
74
name = name + " [" + id + "]"; //$NON-NLS-1$ //$NON-NLS-2$
75
return name;
76             }
77             
78         });
79         return result;
80     }
81     
82     public IConfigurationElement getTarget(String JavaDoc id) {
83         if (fTargets == null)
84             loadElements();
85         return (IConfigurationElement)fTargets.get(id);
86     }
87     
88     private void loadElements() {
89         fTargets = new HashMap JavaDoc();
90         IExtensionRegistry registry = Platform.getExtensionRegistry();
91         registry.addRegistryChangeListener(this);
92         IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.pde.core.targets"); //$NON-NLS-1$
93
add(elements);
94     }
95     
96     private boolean isValid (IConfigurationElement elem) {
97         String JavaDoc value;
98         for (int i = 0; i < attributes.length; i++) {
99             value = elem.getAttribute(attributes[i]);
100             if (value == null || value.equals("")) //$NON-NLS-1$
101
return false;
102         }
103         value = elem.getAttribute("definition"); //$NON-NLS-1$
104
String JavaDoc symbolicName = elem.getDeclaringExtension().getNamespaceIdentifier();
105         URL JavaDoc url = getResourceURL(symbolicName, value);
106         try {
107             if (url != null && url.openStream().available() > 0)
108                 return true;
109         } catch (IOException JavaDoc e) {
110             // file does not exist
111
}
112         return false;
113     }
114     
115     public static URL JavaDoc getResourceURL(String JavaDoc bundleID, String JavaDoc resourcePath) {
116         try {
117             Bundle bundle = Platform.getBundle(bundleID);
118             if (bundle != null && resourcePath != null) {
119                 URL JavaDoc entry = bundle.getEntry(resourcePath);
120                 if (entry != null)
121                     return FileLocator.toFileURL(entry);
122             }
123         } catch (IOException JavaDoc e) {
124         }
125         return null;
126     }
127     
128     private void add(IConfigurationElement[] elems) {
129         for (int i = 0; i < elems.length; i++) {
130             IConfigurationElement elem = elems[i];
131             if (isValid(elem)) {
132                 String JavaDoc id = elem.getAttribute("id"); //$NON-NLS-1$
133
fTargets.put(id, elem);
134             }
135         }
136     }
137     
138     private void remove(IConfigurationElement[] elems) {
139         for (int i = 0 ; i < elems.length; i++) {
140             fTargets.remove(elems[i].getAttribute("id")); //$NON-NLS-1$
141
}
142     }
143     
144     public void shutdown() {
145         IExtensionRegistry registry = Platform.getExtensionRegistry();
146         registry.removeRegistryChangeListener(this);
147     }
148
149 }
150
Popular Tags