KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > OSGiFrameworkManager


1 /*******************************************************************************
2  * Copyright (c) 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.ui.launcher;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.IRegistryChangeEvent;
22 import org.eclipse.core.runtime.IRegistryChangeListener;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.pde.internal.ui.IPreferenceConstants;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.ui.launcher.OSGiLaunchConfigurationInitializer;
29
30 public class OSGiFrameworkManager implements IRegistryChangeListener {
31     
32     public static final String JavaDoc POINT_ID = "org.eclipse.pde.ui.osgiFrameworks"; //$NON-NLS-1$
33
public static final String JavaDoc DEFAULT_FRAMEWORK = "org.eclipse.pde.ui.EquinoxFramework"; //$NON-NLS-1$
34

35     public static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
36
public static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
37
public static final String JavaDoc ATT_DELEGATE = "launcherDelegate"; //$NON-NLS-1$
38
public static final String JavaDoc ATT_INITIALIZER = "initializer"; //$NON-NLS-1$
39

40     public static final String JavaDoc ELEMENT_FRAMEWORK = "framework"; //$NON-NLS-1$
41

42     private Map JavaDoc fFrameworks;
43     
44     public IConfigurationElement[] getFrameworks() {
45         if (fFrameworks == null)
46             loadElements();
47         return (IConfigurationElement[])fFrameworks.values().toArray(new IConfigurationElement[fFrameworks.size()]);
48     }
49     
50     public IConfigurationElement[] getSortedFrameworks() {
51         IConfigurationElement[] elements = getFrameworks();
52         return orderElements(elements);
53     }
54     
55     private void loadElements() {
56         fFrameworks = new HashMap JavaDoc();
57         IExtensionRegistry registry = Platform.getExtensionRegistry();
58         IConfigurationElement[] elements = registry.getConfigurationElementsFor(POINT_ID);
59         for (int i = 0; i < elements.length; i++) {
60             String JavaDoc id = elements[i].getAttribute(ATT_ID);
61             if (id == null
62                     || elements[i].getAttribute(ATT_NAME) == null
63                     || elements[i].getAttribute(ATT_DELEGATE) == null)
64                 continue;
65             fFrameworks.put(id, elements[i]);
66         }
67     }
68     
69     private IConfigurationElement[] orderElements(IConfigurationElement[] elems) {
70         Arrays.sort(elems, new Comparator JavaDoc() {
71             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
72                 String JavaDoc name1 = ((IConfigurationElement)o1).getAttribute(ATT_NAME);
73                 String JavaDoc name2 = ((IConfigurationElement)o2).getAttribute(ATT_NAME);
74                 if (name1 != null)
75                     return name1.compareToIgnoreCase(name2);
76                 return 1;
77             }
78         });
79         return elems;
80     }
81     
82     
83     public void registryChanged(IRegistryChangeEvent event) {
84         //TODO implement
85
}
86     
87     public String JavaDoc getDefaultFramework() {
88         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
89         return store.getString(IPreferenceConstants.DEFAULT_OSGI_FRAMEOWRK);
90     }
91     
92     public OSGiLaunchConfigurationInitializer getDefaultInitializer() {
93         return getInitializer(getDefaultFramework());
94     }
95     
96     public OSGiLaunchConfigurationInitializer getInitializer(String JavaDoc frameworkID) {
97         if (fFrameworks == null)
98             loadElements();
99         if (fFrameworks.containsKey(frameworkID)) {
100             try {
101                 IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID);
102                 if (element.getAttribute(ATT_INITIALIZER) != null) {
103                     Object JavaDoc result = element.createExecutableExtension(ATT_INITIALIZER);
104                     if (result instanceof OSGiLaunchConfigurationInitializer)
105                         return (OSGiLaunchConfigurationInitializer)result;
106                 }
107             } catch (CoreException e) {
108             }
109         }
110         return new OSGiLaunchConfigurationInitializer();
111     }
112     
113     public LaunchConfigurationDelegate getFrameworkLauncher(String JavaDoc frameworkID) {
114         if (fFrameworks == null)
115             loadElements();
116         if (fFrameworks.containsKey(frameworkID)) {
117             try {
118                 IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID);
119                 Object JavaDoc result = element.createExecutableExtension(ATT_DELEGATE);
120                 if (result instanceof LaunchConfigurationDelegate)
121                     return (LaunchConfigurationDelegate)result;
122             } catch (CoreException e) {
123             }
124         }
125         return null;
126     }
127     
128     public String JavaDoc getFrameworkName(String JavaDoc frameworkID) {
129         if (fFrameworks == null)
130             loadElements();
131         if (fFrameworks.containsKey(frameworkID)) {
132             IConfigurationElement element = (IConfigurationElement)fFrameworks.get(frameworkID);
133             return element.getAttribute(ATT_NAME);
134         }
135         return null;
136     }
137
138
139 }
140
Popular Tags