001: /*
002:
003: Derby - Class org.apache.derby.ui.util.DerbyUtils
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.ui.util;
023:
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: import org.apache.derby.ui.DerbyPlugin;
029: import org.apache.derby.ui.common.CommonNames;
030: import org.apache.derby.ui.properties.DerbyProperties;
031: import org.eclipse.core.resources.IFile;
032: import org.eclipse.core.resources.IProject;
033: import org.eclipse.core.runtime.CoreException;
034: import org.eclipse.core.runtime.IPath;
035: import org.eclipse.core.runtime.IStatus;
036: import org.eclipse.core.runtime.Path;
037: import org.eclipse.core.runtime.Platform;
038: import org.eclipse.debug.core.DebugPlugin;
039: import org.eclipse.debug.core.ILaunch;
040: import org.eclipse.debug.core.ILaunchConfiguration;
041: import org.eclipse.debug.core.ILaunchConfigurationType;
042: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
043: import org.eclipse.debug.core.ILaunchManager;
044: import org.eclipse.debug.core.model.IProcess;
045: import org.eclipse.jdt.core.IClasspathEntry;
046: import org.eclipse.jdt.core.JavaCore;
047: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
048: import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
049: import org.eclipse.jdt.launching.JavaRuntime;
050: import org.eclipse.osgi.util.ManifestElement;
051: import org.osgi.framework.Bundle;
052: import org.osgi.framework.BundleException;
053: import org.osgi.framework.Constants;
054:
055: public class DerbyUtils {
056:
057: private static ManifestElement[] getElements(String bundleName)
058: throws BundleException {
059: String requires = (String) Platform.getBundle(bundleName)
060: .getHeaders().get(Constants.BUNDLE_CLASSPATH);
061: return ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH,
062: requires);
063: }
064:
065: public static IClasspathEntry[] addDerbyJars(IClasspathEntry[] rawCP)
066: throws Exception {
067:
068: IClasspathEntry[] newRawCP = null;
069: try {
070: //New OSGI way
071: ManifestElement[] elements_core, elements_ui;
072: elements_core = getElements(CommonNames.CORE_PATH);
073: elements_ui = getElements(CommonNames.UI_PATH);
074:
075: Bundle bundle = Platform.getBundle(CommonNames.CORE_PATH);
076: URL pluginURL = bundle.getEntry("/");
077: URL jarURL = null;
078: URL localURL = null;
079:
080: newRawCP = new IClasspathEntry[rawCP.length
081: + (elements_core.length) + (elements_ui.length - 1)];
082: System.arraycopy(rawCP, 0, newRawCP, 0, rawCP.length);
083:
084: //Add the CORE jars
085: int oldLength = rawCP.length;
086: for (int i = 0; i < elements_core.length; i++) {
087: jarURL = new URL(pluginURL, elements_core[i].getValue());
088: localURL = Platform.asLocalURL(jarURL);
089: newRawCP[oldLength + i] = JavaCore.newLibraryEntry(
090: new Path(localURL.getPath()), null, null);
091:
092: }
093: // Add the UI jars
094: bundle = Platform.getBundle(CommonNames.UI_PATH);
095: pluginURL = bundle.getEntry("/");
096: oldLength = oldLength + elements_core.length - 1;
097: for (int i = 0; i < elements_ui.length; i++) {
098: if (!(elements_ui[i].getValue().toLowerCase()
099: .equals("ui.jar"))) {
100: jarURL = new URL(pluginURL, elements_ui[i]
101: .getValue());
102: localURL = Platform.asLocalURL(jarURL);
103: newRawCP[oldLength + i] = JavaCore.newLibraryEntry(
104: new Path(localURL.getPath()), null, null);
105: }
106: }
107: return newRawCP;
108: } catch (Exception e) {
109: throw e;
110: }
111:
112: }
113:
114: public static IClasspathEntry[] removeDerbyJars(
115: IClasspathEntry[] rawCP) throws Exception {
116: ArrayList arrL = new ArrayList();
117: for (int i = 0; i < rawCP.length; i++) {
118: arrL.add(rawCP[i]);
119: }
120: IClasspathEntry[] newRawCP = null;
121: try {
122: ManifestElement[] elements_core, elements_ui;
123: elements_core = getElements(CommonNames.CORE_PATH);
124: elements_ui = getElements(CommonNames.UI_PATH);
125:
126: Bundle bundle;
127: URL pluginURL, jarURL, localURL;
128:
129: boolean add;
130: IClasspathEntry icp = null;
131: for (int j = 0; j < arrL.size(); j++) {
132: bundle = Platform.getBundle(CommonNames.CORE_PATH);
133: pluginURL = bundle.getEntry("/");
134: add = true;
135: icp = (IClasspathEntry) arrL.get(j);
136: //remove 'core' jars
137: for (int i = 0; i < elements_core.length; i++) {
138: jarURL = new URL(pluginURL, elements_core[i]
139: .getValue());
140: localURL = Platform.asLocalURL(jarURL);
141: if (((icp).equals(JavaCore.newLibraryEntry(
142: new Path(localURL.getPath()), null, null)))
143: || icp.getPath().toString().toLowerCase()
144: .endsWith("derby.jar")
145: || icp.getPath().toString().toLowerCase()
146: .endsWith("derbynet.jar")
147: || icp.getPath().toString().toLowerCase()
148: .endsWith("derbyclient.jar")
149: || icp.getPath().toString().toLowerCase()
150: .endsWith("derbytools.jar")) {
151: add = false;
152: }
153: }
154: if (!add) {
155: arrL.remove(j);
156: j = j - 1;
157: }
158: //REMOVE 'ui' jars
159: bundle = Platform.getBundle(CommonNames.UI_PATH);
160: pluginURL = bundle.getEntry("/");
161: add = true;
162:
163: for (int i = 0; i < elements_ui.length; i++) {
164: if (!(elements_ui[i].getValue().toLowerCase()
165: .equals("ui.jar"))) {
166: jarURL = new URL(pluginURL, elements_ui[i]
167: .getValue());
168: localURL = Platform.asLocalURL(jarURL);
169: if ((icp).equals(JavaCore.newLibraryEntry(
170: new Path(localURL.getPath()), null,
171: null))) {
172: add = false;
173: }
174: }
175: }
176: if (!add) {
177: arrL.remove(j);
178: j = j - 1;
179: }
180: }
181: newRawCP = new IClasspathEntry[arrL.size()];
182: for (int i = 0; i < arrL.size(); i++) {
183: newRawCP[i] = (IClasspathEntry) arrL.get(i);
184: }
185: return newRawCP;
186: } catch (Exception e) {
187: e.printStackTrace();
188: //return rawCP;
189: throw e;
190: }
191:
192: }
193:
194: protected static ILaunch launch(IProject proj, String name,
195: String mainClass, String args, String vmargs, String app)
196: throws CoreException {
197: ILaunchManager manager = DebugPlugin.getDefault()
198: .getLaunchManager();
199:
200: ILaunchConfigurationType type = null;
201: if (app.equalsIgnoreCase(CommonNames.START_DERBY_SERVER)) {
202: //type= manager.getLaunchConfigurationType("org.apache.derby.ui.startDerbyServerLaunchConfigurationType");
203: type = manager
204: .getLaunchConfigurationType(CommonNames.START_SERVER_LAUNCH_CONFIG_TYPE);
205: } else if (app
206: .equalsIgnoreCase(CommonNames.SHUTDOWN_DERBY_SERVER)) {
207: //type= manager.getLaunchConfigurationType("org.apache.derby.ui.stopDerbyServerLaunchConfigurationType");
208: type = manager
209: .getLaunchConfigurationType(CommonNames.STOP_SERVER_LAUNCH_CONFIG_TYPE);
210: } else if (app.equalsIgnoreCase(CommonNames.IJ)) {
211: //type= manager.getLaunchConfigurationType("org.apache.derby.ui.ijDerbyLaunchConfigurationType");
212: type = manager
213: .getLaunchConfigurationType(CommonNames.IJ_LAUNCH_CONFIG_TYPE);
214: } else if (app.equalsIgnoreCase(CommonNames.SYSINFO)) {
215: //type= manager.getLaunchConfigurationType("org.apache.derby.ui.sysinfoDerbyLaunchConfigurationType");
216: type = manager
217: .getLaunchConfigurationType(CommonNames.SYSINFO_LAUNCH_CONFIG_TYPE);
218: } else {
219: type = manager
220: .getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
221: }
222: ILaunchConfiguration config = null;
223: // if the configuration already exists, delete it
224: ILaunchConfiguration[] configurations = manager
225: .getLaunchConfigurations(type);
226: for (int i = 0; i < configurations.length; i++) {
227: if (configurations[i].getName().equals(name))
228: configurations[i].delete();
229: }
230: // else create a new one
231: if (config == null) {
232: ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
233: name);
234: wc
235: .setAttribute(
236: IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
237: proj.getProject().getName());
238: // current directory should be the project root
239: wc
240: .setAttribute(
241: IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
242: proj.getProject().getLocation().toString());
243: // use the suplied args
244: if ((vmargs != null) && !(vmargs.equals(""))) {
245: wc
246: .setAttribute(
247: IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
248: vmargs);
249: }
250: wc
251: .setAttribute(
252: IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
253: mainClass);
254: wc
255: .setAttribute(
256: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
257: args);
258: // saves the new config
259: config = wc.doSave();
260: }
261: ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null);
262: config.delete();
263: return launch;
264: }
265:
266: public static void runIJ(IFile currentScript,
267: IProject currentProject) throws CoreException {
268:
269: String launchType = "";
270: String args = "";
271:
272: //the above some times throws wrong 'create=true|false' errors
273: String vmargs = "";
274: DerbyProperties dprop = new DerbyProperties(currentProject);
275: if ((dprop.getSystemHome() != null)
276: && !(dprop.getSystemHome().equals(""))) {
277: vmargs += CommonNames.D_SYSTEM_HOME + dprop.getSystemHome();
278: }
279:
280: if (currentScript != null) {
281: launchType = CommonNames.SQL_SCRIPT;
282:
283: //Preferable to use the full String with quotes to take care of spaces
284: //in file names
285: args = "\"" + currentScript.getLocation().toOSString()
286: + "\"";
287: } else {
288: launchType = CommonNames.IJ;
289: args = "";
290: }
291:
292: ILaunch launch = launch(currentProject, launchType,
293: CommonNames.IJ_CLASS, args, vmargs, CommonNames.IJ);
294: IProcess ip = launch.getProcesses()[0];
295: String procName = "[" + currentProject.getName() + "] - "
296: + CommonNames.IJ + " " + args;
297: ip.setAttribute(IProcess.ATTR_PROCESS_LABEL, procName);
298: }
299:
300: public static void runSysInfo(IProject currentProject)
301: throws CoreException {
302: String args = "";
303: ILaunch launch = launch(currentProject, CommonNames.SYSINFO,
304: CommonNames.SYSINFO_CLASS, args, null,
305: CommonNames.SYSINFO);
306: IProcess ip = launch.getProcesses()[0];
307: String procName = "[" + currentProject.getName() + "] - "
308: + CommonNames.SYSINFO;
309: ip.setAttribute(IProcess.ATTR_PROCESS_LABEL, procName);
310: }
311:
312: //another launch mechanism
313: public void launch() throws CoreException {
314: DerbyPlugin plugin = DerbyPlugin.getDefault();
315:
316: // constructs a classpath from the default JRE...
317: IPath systemLibs = new Path(JavaRuntime.JRE_CONTAINER);
318: IRuntimeClasspathEntry systemLibsEntry = JavaRuntime
319: .newRuntimeContainerClasspathEntry(systemLibs,
320: IRuntimeClasspathEntry.STANDARD_CLASSES);
321: systemLibsEntry
322: .setClasspathProperty(IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
323: //include org.apache.derby.core plugin
324: IRuntimeClasspathEntry derbyCPEntry = null;
325: List classpath = new ArrayList();
326: classpath.add(systemLibsEntry.getMemento());
327:
328: try {
329: ManifestElement[] elements_core, elements_ui;
330: elements_core = getElements(CommonNames.CORE_PATH);
331: elements_ui = getElements(CommonNames.UI_PATH);
332:
333: Bundle bundle;
334: URL pluginURL, jarURL, localURL;
335: bundle = Platform.getBundle(CommonNames.CORE_PATH);
336: pluginURL = bundle.getEntry("/");
337: for (int i = 0; i < elements_core.length; i++) {
338: if (!elements_core[i].getValue().toLowerCase()
339: .endsWith("derbynet.jar")) {
340: jarURL = new URL(pluginURL, elements_core[i]
341: .getValue());
342: localURL = Platform.asLocalURL(jarURL);
343: derbyCPEntry = JavaRuntime
344: .newArchiveRuntimeClasspathEntry(new Path(
345: localURL.getPath()));
346: derbyCPEntry
347: .setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
348: classpath.add(derbyCPEntry.getMemento());
349: }
350: }
351: bundle = Platform.getBundle(CommonNames.CORE_PATH);
352: pluginURL = bundle.getEntry("/");
353: for (int i = 0; i < elements_ui.length; i++) {
354: if (!elements_ui[i].getValue().toLowerCase().equals(
355: "ui.jar")) {
356: jarURL = new URL(pluginURL, elements_ui[i]
357: .getValue());
358: localURL = Platform.asLocalURL(jarURL);
359: derbyCPEntry = JavaRuntime
360: .newArchiveRuntimeClasspathEntry(new Path(
361: localURL.getPath()));
362: derbyCPEntry
363: .setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
364: classpath.add(derbyCPEntry.getMemento());
365: }
366: }
367: } catch (Exception e) {
368: e.printStackTrace();
369: Logger.log("Error in launch() " + e, IStatus.ERROR);
370: }
371:
372: }
373: }
|