001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.IOException;
013: import java.net.URL;
014: import java.util.Arrays;
015: import java.util.Comparator;
016: import java.util.HashMap;
017: import java.util.Map;
018:
019: import org.eclipse.core.runtime.FileLocator;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtension;
022: import org.eclipse.core.runtime.IExtensionDelta;
023: import org.eclipse.core.runtime.IExtensionRegistry;
024: import org.eclipse.core.runtime.IRegistryChangeEvent;
025: import org.eclipse.core.runtime.IRegistryChangeListener;
026: import org.eclipse.core.runtime.Platform;
027: import org.osgi.framework.Bundle;
028:
029: public class TargetDefinitionManager implements IRegistryChangeListener {
030:
031: Map fTargets;
032: private static String[] attributes;
033: {
034: attributes = new String[] { "id", "name" }; //$NON-NLS-1$ //$NON-NLS-2$
035: }
036:
037: public void registryChanged(IRegistryChangeEvent event) {
038: IExtensionDelta[] deltas = event.getExtensionDeltas();
039: for (int i = 0; i < deltas.length; i++) {
040: IExtension extension = deltas[i].getExtension();
041: String extensionId = extension
042: .getExtensionPointUniqueIdentifier();
043: if (extensionId.equals("org.eclipse.pde.core.targets")) { //$NON-NLS-1$
044: IConfigurationElement[] elems = extension
045: .getConfigurationElements();
046: if (deltas[i].getKind() == IExtensionDelta.ADDED)
047: add(elems);
048: else
049: remove(elems);
050: }
051: }
052: }
053:
054: public IConfigurationElement[] getTargets() {
055: if (fTargets == null)
056: loadElements();
057: return (IConfigurationElement[]) fTargets.values().toArray(
058: new IConfigurationElement[fTargets.size()]);
059: }
060:
061: public IConfigurationElement[] getSortedTargets() {
062: if (fTargets == null)
063: loadElements();
064: IConfigurationElement[] result = (IConfigurationElement[]) fTargets
065: .values().toArray(
066: new IConfigurationElement[fTargets.size()]);
067: Arrays.sort(result, new Comparator() {
068:
069: public int compare(Object o1, Object o2) {
070: String value1 = getString((IConfigurationElement) o1);
071: String value2 = getString((IConfigurationElement) o2);
072: return value1.compareTo(value2);
073: }
074:
075: private String getString(IConfigurationElement elem) {
076: String name = elem.getAttribute("name"); //$NON-NLS-1$
077: String id = elem.getAttribute("id"); //$NON-NLS-1$
078: name = name + " [" + id + "]"; //$NON-NLS-1$ //$NON-NLS-2$
079: return name;
080: }
081:
082: });
083: return result;
084: }
085:
086: public IConfigurationElement getTarget(String id) {
087: if (fTargets == null)
088: loadElements();
089: return (IConfigurationElement) fTargets.get(id);
090: }
091:
092: private void loadElements() {
093: fTargets = new HashMap();
094: IExtensionRegistry registry = Platform.getExtensionRegistry();
095: registry.addRegistryChangeListener(this );
096: IConfigurationElement[] elements = registry
097: .getConfigurationElementsFor("org.eclipse.pde.core.targets"); //$NON-NLS-1$
098: add(elements);
099: }
100:
101: private boolean isValid(IConfigurationElement elem) {
102: String value;
103: for (int i = 0; i < attributes.length; i++) {
104: value = elem.getAttribute(attributes[i]);
105: if (value == null || value.equals("")) //$NON-NLS-1$
106: return false;
107: }
108: value = elem.getAttribute("definition"); //$NON-NLS-1$
109: String symbolicName = elem.getDeclaringExtension()
110: .getNamespaceIdentifier();
111: URL url = getResourceURL(symbolicName, value);
112: try {
113: if (url != null && url.openStream().available() > 0)
114: return true;
115: } catch (IOException e) {
116: // file does not exist
117: }
118: return false;
119: }
120:
121: public static URL getResourceURL(String bundleID,
122: String resourcePath) {
123: try {
124: Bundle bundle = Platform.getBundle(bundleID);
125: if (bundle != null && resourcePath != null) {
126: URL entry = bundle.getEntry(resourcePath);
127: if (entry != null)
128: return FileLocator.toFileURL(entry);
129: }
130: } catch (IOException e) {
131: }
132: return null;
133: }
134:
135: private void add(IConfigurationElement[] elems) {
136: for (int i = 0; i < elems.length; i++) {
137: IConfigurationElement elem = elems[i];
138: if (isValid(elem)) {
139: String id = elem.getAttribute("id"); //$NON-NLS-1$
140: fTargets.put(id, elem);
141: }
142: }
143: }
144:
145: private void remove(IConfigurationElement[] elems) {
146: for (int i = 0; i < elems.length; i++) {
147: fTargets.remove(elems[i].getAttribute("id")); //$NON-NLS-1$
148: }
149: }
150:
151: public void shutdown() {
152: IExtensionRegistry registry = Platform.getExtensionRegistry();
153: registry.removeRegistryChangeListener(this);
154: }
155:
156: }
|