KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > ui > util > DerbyUtils


1 /*
2
3     Derby - Class org.apache.derby.ui.util.DerbyUtils
4
5     Licensed to the Apache Software Foundation (ASF) under one or more
6     contributor license agreements. See the NOTICE file distributed with
7     this work for additional information regarding copyright ownership.
8     The ASF licenses this file to you under the Apache License, Version 2.0
9     (the "License"); you may not use this file except in compliance with
10     the License. You may obtain a copy of the License at
11     
12        http://www.apache.org/licenses/LICENSE-2.0
13     
14     Unless required by applicable law or agreed to in writing, software
15     distributed under the License is distributed on an "AS IS" BASIS,
16     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17     See the License for the specific language governing permissions and
18     limitations under the License.
19
20 */

21
22 package org.apache.derby.ui.util;
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.derby.ui.DerbyPlugin;
29 import org.apache.derby.ui.common.CommonNames;
30 import org.apache.derby.ui.properties.DerbyProperties;
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IProject;
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.core.runtime.IPath;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.Path;
37 import org.eclipse.core.runtime.Platform;
38 import org.eclipse.debug.core.DebugPlugin;
39 import org.eclipse.debug.core.ILaunch;
40 import org.eclipse.debug.core.ILaunchConfiguration;
41 import org.eclipse.debug.core.ILaunchConfigurationType;
42 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
43 import org.eclipse.debug.core.ILaunchManager;
44 import org.eclipse.debug.core.model.IProcess;
45 import org.eclipse.jdt.core.IClasspathEntry;
46 import org.eclipse.jdt.core.JavaCore;
47 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
48 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
49 import org.eclipse.jdt.launching.JavaRuntime;
50 import org.eclipse.osgi.util.ManifestElement;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleException;
53 import org.osgi.framework.Constants;
54
55
56
57 public class DerbyUtils {
58     
59     private static ManifestElement[] getElements(String JavaDoc bundleName) throws BundleException {
60         String JavaDoc requires = (String JavaDoc)Platform.getBundle(bundleName).getHeaders().get(Constants.BUNDLE_CLASSPATH);
61         return ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, requires);
62     }
63     public static IClasspathEntry[] addDerbyJars(IClasspathEntry[] rawCP) throws Exception JavaDoc{
64         
65         IClasspathEntry[] newRawCP= null;
66         try{
67             //New OSGI way
68
ManifestElement[] elements_core, elements_ui;
69             elements_core = getElements(CommonNames.CORE_PATH);
70             elements_ui=getElements(CommonNames.UI_PATH);
71             
72             Bundle bundle=Platform.getBundle(CommonNames.CORE_PATH);
73             URL JavaDoc pluginURL = bundle.getEntry("/");
74             URL JavaDoc jarURL=null;
75             URL JavaDoc localURL=null;
76
77             newRawCP=new IClasspathEntry[rawCP.length + (elements_core.length) + (elements_ui.length-1)];
78             System.arraycopy(rawCP, 0, newRawCP, 0, rawCP.length);
79             
80             //Add the CORE jars
81
int oldLength=rawCP.length;
82              for(int i=0;i<elements_core.length;i++){
83                 jarURL= new URL JavaDoc(pluginURL,elements_core[i].getValue());
84                 localURL=Platform.asLocalURL(jarURL);
85                 newRawCP[oldLength+i]=JavaCore.newLibraryEntry(new Path(localURL.getPath()), null, null);
86                 
87             }
88              // Add the UI jars
89
bundle=Platform.getBundle(CommonNames.UI_PATH);
90             pluginURL = bundle.getEntry("/");
91             oldLength=oldLength+elements_core.length -1;
92             for(int i=0;i<elements_ui.length;i++){
93                 if(!(elements_ui[i].getValue().toLowerCase().equals("ui.jar"))){
94                     jarURL= new URL JavaDoc(pluginURL,elements_ui[i].getValue());
95                     localURL=Platform.asLocalURL(jarURL);
96                     newRawCP[oldLength+i]=JavaCore.newLibraryEntry(new Path(localURL.getPath()), null, null);
97                 }
98             }
99             return newRawCP;
100         }catch(Exception JavaDoc e){
101             throw e;
102         }
103         
104     }
105     public static IClasspathEntry[] removeDerbyJars(IClasspathEntry[] rawCP) throws Exception JavaDoc{
106         ArrayList JavaDoc arrL=new ArrayList JavaDoc();
107         for (int i=0;i<rawCP.length;i++){
108             arrL.add(rawCP[i]);
109         }
110         IClasspathEntry[] newRawCP= null;
111         try{
112             ManifestElement[] elements_core, elements_ui;
113             elements_core = getElements(CommonNames.CORE_PATH);
114             elements_ui=getElements(CommonNames.UI_PATH);
115             
116             Bundle bundle;
117             URL JavaDoc pluginURL,jarURL,localURL;
118
119             boolean add;
120             IClasspathEntry icp=null;
121             for (int j=0;j<arrL.size();j++){
122                 bundle=Platform.getBundle(CommonNames.CORE_PATH);
123                 pluginURL = bundle.getEntry("/");
124                 add=true;
125                 icp=(IClasspathEntry)arrL.get(j);
126                 //remove 'core' jars
127
for (int i=0;i<elements_core.length;i++){
128                     jarURL= new URL JavaDoc(pluginURL,elements_core[i].getValue());
129                     localURL=Platform.asLocalURL(jarURL);
130                     if(((icp).equals(JavaCore.newLibraryEntry(new Path(localURL.getPath()), null, null)))||
131                             icp.getPath().toString().toLowerCase().endsWith("derby.jar")||
132                             icp.getPath().toString().toLowerCase().endsWith("derbynet.jar")||
133                             icp.getPath().toString().toLowerCase().endsWith("derbyclient.jar")||
134                             icp.getPath().toString().toLowerCase().endsWith("derbytools.jar")){
135                         add=false;
136                     }
137                 }
138                 if(!add){
139                     arrL.remove(j);
140                     j=j-1;
141                 }
142                 //REMOVE 'ui' jars
143
bundle=Platform.getBundle(CommonNames.UI_PATH);
144                 pluginURL = bundle.getEntry("/");
145                 add=true;
146                 
147                 for (int i=0;i<elements_ui.length;i++){
148                     if(!(elements_ui[i].getValue().toLowerCase().equals("ui.jar"))){
149                         jarURL= new URL JavaDoc(pluginURL,elements_ui[i].getValue());
150                         localURL=Platform.asLocalURL(jarURL);
151                         if((icp).equals(JavaCore.newLibraryEntry(new Path(localURL.getPath()), null, null))){
152                             add=false;
153                         }
154                     }
155                 }
156                 if(!add){
157                     arrL.remove(j);
158                     j=j-1;
159                 }
160             }
161             newRawCP=new IClasspathEntry[arrL.size()];
162             for (int i=0;i<arrL.size();i++){
163                 newRawCP[i]=(IClasspathEntry)arrL.get(i);
164             }
165             return newRawCP;
166         }catch(Exception JavaDoc e){
167             e.printStackTrace();
168             //return rawCP;
169
throw e;
170         }
171         
172     }
173     protected static ILaunch launch(IProject proj, String JavaDoc name, String JavaDoc mainClass, String JavaDoc args, String JavaDoc vmargs, String JavaDoc app) throws CoreException {
174         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
175     
176         ILaunchConfigurationType type=null;
177         if(app.equalsIgnoreCase(CommonNames.START_DERBY_SERVER)){
178             //type= manager.getLaunchConfigurationType("org.apache.derby.ui.startDerbyServerLaunchConfigurationType");
179
type= manager.getLaunchConfigurationType(CommonNames.START_SERVER_LAUNCH_CONFIG_TYPE);
180         }else if(app.equalsIgnoreCase(CommonNames.SHUTDOWN_DERBY_SERVER)){
181             //type= manager.getLaunchConfigurationType("org.apache.derby.ui.stopDerbyServerLaunchConfigurationType");
182
type= manager.getLaunchConfigurationType(CommonNames.STOP_SERVER_LAUNCH_CONFIG_TYPE);
183         }else if(app.equalsIgnoreCase(CommonNames.IJ)){
184             //type= manager.getLaunchConfigurationType("org.apache.derby.ui.ijDerbyLaunchConfigurationType");
185
type= manager.getLaunchConfigurationType(CommonNames.IJ_LAUNCH_CONFIG_TYPE);
186         }else if(app.equalsIgnoreCase(CommonNames.SYSINFO)){
187             //type= manager.getLaunchConfigurationType("org.apache.derby.ui.sysinfoDerbyLaunchConfigurationType");
188
type= manager.getLaunchConfigurationType(CommonNames.SYSINFO_LAUNCH_CONFIG_TYPE);
189         }else{
190             type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
191         }
192         ILaunchConfiguration config = null;
193         // if the configuration already exists, delete it
194
ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
195         for (int i = 0; i < configurations.length; i++) {
196             if (configurations[i].getName().equals(name))
197                 configurations[i].delete();
198         }
199         // else create a new one
200
if (config == null) {
201             ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name);
202             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
203                 proj.getProject().getName());
204             // current directory should be the project root
205
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
206                 proj.getProject().getLocation().toString());
207             // use the suplied args
208
if((vmargs!=null)&&!(vmargs.equals(""))){
209                 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmargs);
210             }
211             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
212                 mainClass);
213             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
214                 args);
215             // saves the new config
216
config = wc.doSave();
217         }
218         ILaunch launch=config.launch(ILaunchManager.RUN_MODE, null);
219         config.delete();
220         return launch;
221     }
222     public static void runIJ(IFile currentScript, IProject currentProject) throws CoreException {
223
224         String JavaDoc launchType="";
225         String JavaDoc args="";
226         
227         //the above some times throws wrong 'create=true|false' errors
228
String JavaDoc vmargs="";
229         DerbyProperties dprop=new DerbyProperties(currentProject);
230         if((dprop.getSystemHome()!=null)&& !(dprop.getSystemHome().equals(""))){
231             vmargs+=CommonNames.D_SYSTEM_HOME+dprop.getSystemHome();
232         }
233         
234         if(currentScript!=null){
235             launchType=CommonNames.SQL_SCRIPT;
236             
237             //Preferable to use the full String with quotes to take care of spaces
238
//in file names
239
args="\""+currentScript.getLocation().toOSString()+"\"";
240         }else{
241             launchType=CommonNames.IJ;
242             args="";
243         }
244         
245         ILaunch launch=launch(currentProject,launchType,CommonNames.IJ_CLASS,args, vmargs, CommonNames.IJ);
246         IProcess ip=launch.getProcesses()[0];
247         String JavaDoc procName="["+currentProject.getName()+"] - "+CommonNames.IJ+" "+args;
248         ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName);
249     }
250     public static void runSysInfo(IProject currentProject) throws CoreException {
251         String JavaDoc args="";
252         ILaunch launch=launch(currentProject,CommonNames.SYSINFO,CommonNames.SYSINFO_CLASS,args, null, CommonNames.SYSINFO);
253         IProcess ip=launch.getProcesses()[0];
254         String JavaDoc procName="["+currentProject.getName()+"] - "+CommonNames.SYSINFO;
255         ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName);
256     }
257     //another launch mechanism
258
public void launch() throws CoreException{
259         DerbyPlugin plugin = DerbyPlugin.getDefault();
260
261         // constructs a classpath from the default JRE...
262
IPath systemLibs = new Path(JavaRuntime.JRE_CONTAINER);
263         IRuntimeClasspathEntry systemLibsEntry = JavaRuntime.newRuntimeContainerClasspathEntry(
264             systemLibs, IRuntimeClasspathEntry.STANDARD_CLASSES);
265         systemLibsEntry.setClasspathProperty(IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
266         //include org.apache.derby.core plugin
267
IRuntimeClasspathEntry derbyCPEntry = null;
268         List JavaDoc classpath = new ArrayList JavaDoc();
269         classpath.add(systemLibsEntry.getMemento());
270         
271         try {
272             ManifestElement[] elements_core, elements_ui;
273             elements_core = getElements(CommonNames.CORE_PATH);
274             elements_ui=getElements(CommonNames.UI_PATH);
275             
276             Bundle bundle;
277             URL JavaDoc pluginURL,jarURL,localURL;
278             bundle=Platform.getBundle(CommonNames.CORE_PATH);
279             pluginURL = bundle.getEntry("/");
280             for(int i=0;i<elements_core.length;i++){
281                 if(!elements_core[i].getValue().toLowerCase().endsWith("derbynet.jar")){
282                     jarURL= new URL JavaDoc(pluginURL,elements_core[i].getValue());
283                     localURL=Platform.asLocalURL(jarURL);
284                     derbyCPEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(localURL.getPath()));
285                     derbyCPEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
286                     classpath.add(derbyCPEntry.getMemento());
287                 }
288             }
289             bundle=Platform.getBundle(CommonNames.CORE_PATH);
290             pluginURL = bundle.getEntry("/");
291             for(int i=0;i<elements_ui.length;i++){
292                 if(!elements_ui[i].getValue().toLowerCase().equals("ui.jar")){
293                     jarURL= new URL JavaDoc(pluginURL,elements_ui[i].getValue());
294                     localURL=Platform.asLocalURL(jarURL);
295                     derbyCPEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(localURL.getPath()));
296                     derbyCPEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
297                     classpath.add(derbyCPEntry.getMemento());
298                 }
299             }
300         }
301         catch(Exception JavaDoc e) {
302             e.printStackTrace();
303             Logger.log("Error in launch() "+e,IStatus.ERROR);
304         }
305     
306     }
307 }
308
Popular Tags