]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/osgi/SlcLaunchShortcut.java
Remove warnings
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / osgi / SlcLaunchShortcut.java
1 package org.argeo.slc.ui.launch.osgi;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.StringTokenizer;
9
10 import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.core.runtime.Assert;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.debug.core.ILaunchConfigurationType;
15 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.osgi.service.resolver.BundleDescription;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.core.plugin.PluginRegistry;
22 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
23 import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
24 import org.eclipse.swt.widgets.Shell;
25
26 public class SlcLaunchShortcut extends OSGiLaunchShortcut {
27 private Boolean debug = false;
28
29 private String springOsgiExtenderId = "org.springframework.osgi.extender";
30 private String slcSupportEquinoxId = "org.argeo.slc.support.equinox";
31 // private String slcAgentId = "org.argeo.slc.agent";
32 // private String osgiBootId = "org.argeo.slc.osgiboot";
33
34 private ISelection selection = null;
35 private StringBuffer name = null;
36
37 private final List<String> defaultBundlesToStart = new ArrayList<String>();
38 private List<String> bundlesToStart = new ArrayList<String>();
39
40 public SlcLaunchShortcut() {
41 super();
42 defaultBundlesToStart.add(springOsgiExtenderId);
43 defaultBundlesToStart.add(slcSupportEquinoxId);
44 // defaultBundlesToStart.add(slcAgentId);
45 }
46
47 public void launch(ISelection selection, String mode) {
48 this.selection = selection;
49 this.name = new StringBuffer();
50
51 bundlesToStart = new ArrayList<String>();
52 bundlesToStart.addAll(defaultBundlesToStart);
53 // Evaluate selection
54 if (selection != null) {
55 addSelectedProjects(bundlesToStart);
56 }
57
58 super.launch(selection, mode);
59
60 // Reset
61 this.selection = null;
62 this.name = null;
63 bundlesToStart = null;
64 }
65
66 protected void initializeConfiguration(
67 ILaunchConfigurationWorkingCopy configuration) {
68 try {
69 super.initializeConfiguration(configuration);
70
71 // Convert bundle lists
72 String targetBundles = configuration.getAttribute(
73 IPDELauncherConstants.TARGET_BUNDLES, "");
74 configuration.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
75 convertBundleList(bundlesToStart, targetBundles));
76
77 String wkSpaceBundles = configuration.getAttribute(
78 IPDELauncherConstants.WORKSPACE_BUNDLES, "");
79 configuration.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
80 convertBundleList(bundlesToStart, wkSpaceBundles));
81
82 // Update other default information
83 configuration.setAttribute(
84 IPDELauncherConstants.DEFAULT_AUTO_START, false);
85
86 } catch (CoreException e) {
87 Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()
88 .getActiveWorkbenchWindow().getShell();
89 ErrorDialog.openError(shell, "Error",
90 "Cannot execute SLC launch shortcut", e.getStatus());
91 }
92
93 }
94
95 protected String convertBundleList(List<String> bundlesToStart,
96 String original) {
97 StringBuffer bufBundles = new StringBuffer(1024);
98 StringTokenizer stComa = new StringTokenizer(original, ",");
99 boolean first = true;
100 while (stComa.hasMoreTokens()) {
101 if (first)
102 first = false;
103 else
104 bufBundles.append(',');
105
106 String tkComa = stComa.nextToken();
107 int indexAt = tkComa.indexOf('@');
108 boolean modified = false;
109 if (indexAt >= 0) {
110 String bundelId = tkComa.substring(0, indexAt);
111
112 if (bundlesToStart.contains(bundelId)) {
113 bufBundles.append(bundelId).append('@').append(
114 "default:true");
115 modified = true;
116 if (debug)
117 System.out.println("Will start " + bundelId);
118 }
119 }
120
121 if (!modified)
122 bufBundles.append(tkComa);
123 }
124 String output = bufBundles.toString();
125 return output;
126 }
127
128 protected void addSelectedProjects(List<String> bundlesToStart) {
129 Assert.isNotNull(selection);
130
131 Map<String, IPluginModelBase> bundleProjects = new HashMap<String, IPluginModelBase>();
132 for (IPluginModelBase modelBase : PluginRegistry.getWorkspaceModels()) {
133 IProject bundleProject = modelBase.getUnderlyingResource()
134 .getProject();
135 bundleProjects.put(bundleProject.getName(), modelBase);
136 }
137
138 IStructuredSelection sSelection = (IStructuredSelection) selection;
139 for (Iterator<?> it = sSelection.iterator(); it.hasNext();) {
140 Object obj = it.next();
141 if (obj instanceof IProject) {
142 IProject project = (IProject) obj;
143 if (bundleProjects.containsKey(project.getName())) {
144 IPluginModelBase modelBase = bundleProjects.get(project
145 .getName());
146
147 BundleDescription bundleDescription = null;
148 if (modelBase.isFragmentModel()) {
149 BundleDescription[] hosts = modelBase
150 .getBundleDescription().getHost().getHosts();
151 for (BundleDescription bd : hosts) {
152 if (debug)
153 System.out.println("Host for "
154 + modelBase.getBundleDescription()
155 .getSymbolicName() + ": "
156 + bd.getSymbolicName());
157 bundleDescription = bd;
158 }
159 } else {
160 bundleDescription = modelBase.getBundleDescription();
161 }
162
163 if (bundleDescription != null) {
164 String symbolicName = bundleDescription
165 .getSymbolicName();
166 String bundleName = bundleDescription.getName();
167
168 bundlesToStart.add(symbolicName);
169
170 if (name.length() > 0)
171 name.append(" ");
172 if (bundleName != null)
173 name.append(bundleName);
174 else
175 name.append(symbolicName);
176 }
177 }
178 }
179 }
180 }
181
182 protected String getName(ILaunchConfigurationType type) {
183 if (name != null && !name.toString().trim().equals(""))
184 return name.toString();
185 else
186 return "SLC";
187 }
188
189 }