1 21 22 package org.apache.derby.ui.util; 23 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.List ; 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 bundleName) throws BundleException { 60 String requires = (String )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 { 64 65 IClasspathEntry[] newRawCP= null; 66 try{ 67 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 pluginURL = bundle.getEntry("/"); 74 URL jarURL=null; 75 URL 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 int oldLength=rawCP.length; 82 for(int i=0;i<elements_core.length;i++){ 83 jarURL= new URL (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 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 (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 e){ 101 throw e; 102 } 103 104 } 105 public static IClasspathEntry[] removeDerbyJars(IClasspathEntry[] rawCP) throws Exception { 106 ArrayList arrL=new ArrayList (); 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 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 for (int i=0;i<elements_core.length;i++){ 128 jarURL= new URL (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 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 (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 e){ 167 e.printStackTrace(); 168 throw e; 170 } 171 172 } 173 protected static ILaunch launch(IProject proj, String name, String mainClass, String args, String vmargs, String 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(CommonNames.START_SERVER_LAUNCH_CONFIG_TYPE); 180 }else if(app.equalsIgnoreCase(CommonNames.SHUTDOWN_DERBY_SERVER)){ 181 type= manager.getLaunchConfigurationType(CommonNames.STOP_SERVER_LAUNCH_CONFIG_TYPE); 183 }else if(app.equalsIgnoreCase(CommonNames.IJ)){ 184 type= manager.getLaunchConfigurationType(CommonNames.IJ_LAUNCH_CONFIG_TYPE); 186 }else if(app.equalsIgnoreCase(CommonNames.SYSINFO)){ 187 type= manager.getLaunchConfigurationType(CommonNames.SYSINFO_LAUNCH_CONFIG_TYPE); 189 }else{ 190 type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); 191 } 192 ILaunchConfiguration config = null; 193 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 if (config == null) { 201 ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name); 202 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, 203 proj.getProject().getName()); 204 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, 206 proj.getProject().getLocation().toString()); 207 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 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 launchType=""; 225 String args=""; 226 227 String 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 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 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 args=""; 252 ILaunch launch=launch(currentProject,CommonNames.SYSINFO,CommonNames.SYSINFO_CLASS,args, null, CommonNames.SYSINFO); 253 IProcess ip=launch.getProcesses()[0]; 254 String procName="["+currentProject.getName()+"] - "+CommonNames.SYSINFO; 255 ip.setAttribute(IProcess.ATTR_PROCESS_LABEL,procName); 256 } 257 public void launch() throws CoreException{ 259 DerbyPlugin plugin = DerbyPlugin.getDefault(); 260 261 IPath systemLibs = new Path(JavaRuntime.JRE_CONTAINER); 263 IRuntimeClasspathEntry systemLibsEntry = JavaRuntime.newRuntimeContainerClasspathEntry( 264 systemLibs, IRuntimeClasspathEntry.STANDARD_CLASSES); 265 systemLibsEntry.setClasspathProperty(IRuntimeClasspathEntry.BOOTSTRAP_CLASSES); 266 IRuntimeClasspathEntry derbyCPEntry = null; 268 List classpath = new ArrayList (); 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 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 (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 (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 e) { 302 e.printStackTrace(); 303 Logger.log("Error in launch() "+e,IStatus.ERROR); 304 } 305 306 } 307 } 308 | Popular Tags |