001: /*******************************************************************************
002: * Copyright (c) 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.ui.launcher;
011:
012: import java.util.Arrays;
013: import java.util.Comparator;
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IExtensionRegistry;
020: import org.eclipse.core.runtime.IRegistryChangeEvent;
021: import org.eclipse.core.runtime.IRegistryChangeListener;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
024: import org.eclipse.jface.preference.IPreferenceStore;
025: import org.eclipse.pde.internal.ui.IPreferenceConstants;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.ui.launcher.OSGiLaunchConfigurationInitializer;
028:
029: public class OSGiFrameworkManager implements IRegistryChangeListener {
030:
031: public static final String POINT_ID = "org.eclipse.pde.ui.osgiFrameworks"; //$NON-NLS-1$
032: public static final String DEFAULT_FRAMEWORK = "org.eclipse.pde.ui.EquinoxFramework"; //$NON-NLS-1$
033:
034: public static final String ATT_ID = "id"; //$NON-NLS-1$
035: public static final String ATT_NAME = "name"; //$NON-NLS-1$
036: public static final String ATT_DELEGATE = "launcherDelegate"; //$NON-NLS-1$
037: public static final String ATT_INITIALIZER = "initializer"; //$NON-NLS-1$
038:
039: public static final String ELEMENT_FRAMEWORK = "framework"; //$NON-NLS-1$
040:
041: private Map fFrameworks;
042:
043: public IConfigurationElement[] getFrameworks() {
044: if (fFrameworks == null)
045: loadElements();
046: return (IConfigurationElement[]) fFrameworks.values().toArray(
047: new IConfigurationElement[fFrameworks.size()]);
048: }
049:
050: public IConfigurationElement[] getSortedFrameworks() {
051: IConfigurationElement[] elements = getFrameworks();
052: return orderElements(elements);
053: }
054:
055: private void loadElements() {
056: fFrameworks = new HashMap();
057: IExtensionRegistry registry = Platform.getExtensionRegistry();
058: IConfigurationElement[] elements = registry
059: .getConfigurationElementsFor(POINT_ID);
060: for (int i = 0; i < elements.length; i++) {
061: String id = elements[i].getAttribute(ATT_ID);
062: if (id == null
063: || elements[i].getAttribute(ATT_NAME) == null
064: || elements[i].getAttribute(ATT_DELEGATE) == null)
065: continue;
066: fFrameworks.put(id, elements[i]);
067: }
068: }
069:
070: private IConfigurationElement[] orderElements(
071: IConfigurationElement[] elems) {
072: Arrays.sort(elems, new Comparator() {
073: public int compare(Object o1, Object o2) {
074: String name1 = ((IConfigurationElement) o1)
075: .getAttribute(ATT_NAME);
076: String name2 = ((IConfigurationElement) o2)
077: .getAttribute(ATT_NAME);
078: if (name1 != null)
079: return name1.compareToIgnoreCase(name2);
080: return 1;
081: }
082: });
083: return elems;
084: }
085:
086: public void registryChanged(IRegistryChangeEvent event) {
087: //TODO implement
088: }
089:
090: public String getDefaultFramework() {
091: IPreferenceStore store = PDEPlugin.getDefault()
092: .getPreferenceStore();
093: return store
094: .getString(IPreferenceConstants.DEFAULT_OSGI_FRAMEOWRK);
095: }
096:
097: public OSGiLaunchConfigurationInitializer getDefaultInitializer() {
098: return getInitializer(getDefaultFramework());
099: }
100:
101: public OSGiLaunchConfigurationInitializer getInitializer(
102: String frameworkID) {
103: if (fFrameworks == null)
104: loadElements();
105: if (fFrameworks.containsKey(frameworkID)) {
106: try {
107: IConfigurationElement element = (IConfigurationElement) fFrameworks
108: .get(frameworkID);
109: if (element.getAttribute(ATT_INITIALIZER) != null) {
110: Object result = element
111: .createExecutableExtension(ATT_INITIALIZER);
112: if (result instanceof OSGiLaunchConfigurationInitializer)
113: return (OSGiLaunchConfigurationInitializer) result;
114: }
115: } catch (CoreException e) {
116: }
117: }
118: return new OSGiLaunchConfigurationInitializer();
119: }
120:
121: public LaunchConfigurationDelegate getFrameworkLauncher(
122: String frameworkID) {
123: if (fFrameworks == null)
124: loadElements();
125: if (fFrameworks.containsKey(frameworkID)) {
126: try {
127: IConfigurationElement element = (IConfigurationElement) fFrameworks
128: .get(frameworkID);
129: Object result = element
130: .createExecutableExtension(ATT_DELEGATE);
131: if (result instanceof LaunchConfigurationDelegate)
132: return (LaunchConfigurationDelegate) result;
133: } catch (CoreException e) {
134: }
135: }
136: return null;
137: }
138:
139: public String getFrameworkName(String frameworkID) {
140: if (fFrameworks == null)
141: loadElements();
142: if (fFrameworks.containsKey(frameworkID)) {
143: IConfigurationElement element = (IConfigurationElement) fFrameworks
144: .get(frameworkID);
145: return element.getAttribute(ATT_NAME);
146: }
147: return null;
148: }
149:
150: }
|