KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > JonasBootstrap


1 package com.bull.eclipse.jonas;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7  
8  
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.model.ISourceLocator;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.launching.JavaRuntime;
24 import org.eclipse.jdt.launching.sourcelookup.JavaSourceLocator;
25
26 import com.bull.eclipse.jonas.editors.ProjectListElement;
27 import com.bull.eclipse.jonas.utils.FileUtil;
28 import com.bull.eclipse.jonas.utils.StringUtil;
29 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
30 import com.bull.eclipse.jonas.utils.VMLauncherUtility;
31
32
33 /**
34  * Start and stop Jonas
35  * Subclasses contains all information specific to a Jonas Version
36  */

37     
38 public abstract class JonasBootstrap {
39
40     public abstract String JavaDoc[] getClasspath();
41     public abstract String JavaDoc[] getVmArgs();
42     public abstract String JavaDoc[] getPrgArgs(String JavaDoc command,String JavaDoc[] options);
43     public abstract String JavaDoc getStartCommand();
44     public abstract String JavaDoc getStopCommand();
45     public abstract String JavaDoc getAdminCommand();
46     public abstract String JavaDoc getMainClass();
47     public abstract Vector JavaDoc getJonasJarPath();
48     public abstract Vector JavaDoc getJonasWebJarPath();
49
50     abstract public String JavaDoc getLabel();
51
52                     
53     /**
54      * See %JONAS_ROOT%/bin/nt/jonas.bat
55      */

56     public void start() throws CoreException {
57         this.runJonasBootsrap(getStartCommand(), true, null);
58     }
59     /**
60      * See %JONAS_ROOT%/bin/nt/jonas.bat stop
61      */

62     public void stop() throws CoreException {
63         this.runJonasBootsrap(getStopCommand(), false, null);
64     }
65
66     /**
67      * See %JONAS_ROOT%/bin/nt/jonas.bat stop
68      */

69     public void admin(String JavaDoc[] options) throws CoreException {
70         this.runJonasBootsrap(getAdminCommand(), false, options);
71     }
72         
73     /**
74      * Simply stop and start
75      */

76     public void restart() throws CoreException {
77         JonasLauncherPlugin.getDefault().setEventRestart(true);
78         this.stop();
79         //The start is made in JonasPLuginClass.handleDebugEvents method
80
}
81     
82     /**
83      * Launch a new JVM running Jonas Main class
84      * Set classpath, bootclasspath and environment variable
85      */

86
87     private void runJonasBootsrap(String JavaDoc jonasBootOption, boolean showInDebugger, String JavaDoc[] options) throws CoreException{
88         String JavaDoc[] prgArgs = this.getPrgArgs(jonasBootOption,options);
89
90         String JavaDoc[] classpath = new String JavaDoc[0];
91         classpath = addPreferenceJvmToClasspath(classpath);
92         classpath = addPreferenceProjectListToClasspath(classpath);
93         classpath = StringUtil.concat(classpath, this.getClasspath() );
94                 
95         String JavaDoc[] vmArgs = this.getVmArgs();
96         vmArgs = addPreferenceParameters(vmArgs);
97     
98         String JavaDoc[] bootClasspath = addPreferenceJvmToBootClasspath(new String JavaDoc[0]);
99         
100         VMLauncherUtility.runVM(getLabel(), getMainClass(), classpath, bootClasspath, vmArgs, prgArgs, getSourceLocator(), isDebugMode(), showInDebugger);
101     }
102     
103     private boolean isDebugMode() {
104         return JonasLauncherPlugin.getDefault().isDebugMode();
105     }
106     
107     /**
108      * Set Jonas process source locator.
109      * Initial implementation is to return all Java Projects
110      * This could decrease debugger performance (I don't know, have to ask to eclipse team)
111      * Todo after 2.0 is released : add sourceLocator only for allJonasProjects
112      */

113     
114     private ISourceLocator getSourceLocator() throws CoreException {
115         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
116         IProject[] allProjects = root.getProjects();
117         
118         ArrayList JavaDoc tempList = new ArrayList JavaDoc(allProjects.length);
119         for (int i = 0; i < allProjects.length; i++) {
120             if((allProjects[i].isOpen()) && allProjects[i].hasNature(JavaCore.NATURE_ID)) {
121                 tempList.add(allProjects[i].getNature(JavaCore.NATURE_ID));
122             }
123         }
124
125         ISourceLocator sourceLocator = null;
126         if(!tempList.isEmpty()) {
127             IJavaProject[] javaProjects = (IJavaProject[])tempList.toArray(new IJavaProject[1]);
128             sourceLocator = new JavaSourceLocator(javaProjects, true);
129         }
130         
131         return sourceLocator;
132     }
133
134     
135     protected String JavaDoc getJonasDir() {
136         return JonasLauncherPlugin.getDefault().getJonasDir();
137     }
138
139
140     protected String JavaDoc getBaseDir() {
141         return JonasLauncherPlugin.getDefault().getBaseDir();
142     }
143     
144     protected String JavaDoc getXtraClasspath() {
145         return JonasLauncherPlugin.getDefault().getXtraClasspath();
146     }
147     
148     protected String JavaDoc getOrb() {
149         String JavaDoc orb = JonasLauncherPlugin.getDefault().getOrb();
150         File JavaDoc jndiTemplate = new File JavaDoc(TemplateDirUtil.getTemplateDir() + File.separator + "jndi.properties." + orb.toLowerCase());
151         File JavaDoc jndiFile = new File JavaDoc(TemplateDirUtil.getTemplateDir() + File.separator + "jndi.properties");
152         try {
153             FileUtil.copyFile(jndiTemplate,jndiFile);
154         } catch (IOException JavaDoc ie) {
155
156             //We will use the JONAS_BASE/conf/jndi.properties file
157
}
158         return orb;
159     }
160     
161     private String JavaDoc[] addPreferenceProjectListToClasspath(String JavaDoc[] previouscp) {
162         List JavaDoc projectsList = JonasLauncherPlugin.getDefault().getProjectsInCP();
163         String JavaDoc[] result = previouscp;
164         Iterator JavaDoc it = projectsList.iterator();
165         while(it.hasNext()) {
166             try {
167                 ProjectListElement ple = (ProjectListElement)it.next();
168                 IJavaProject jproject = JavaCore.create(ple.getProject());
169                 result = this.addProjectToClasspath(result, jproject);
170             } catch (Exception JavaDoc e) {
171                 // nothing will be added to classpath
172
}
173         }
174         
175         return result;
176         
177     }
178             
179     private String JavaDoc[] addProjectToClasspath(String JavaDoc[] previouscp, IJavaProject project) throws CoreException {
180 // IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
181
// IJavaProject project = JavaCore.getJavaCore().create(root.getProject(projectName));
182
if((project != null) && (project.exists() && project.isOpen())) {
183             String JavaDoc[] projectcp = JavaRuntime.computeDefaultRuntimeClassPath(project);
184             return StringUtil.concat(projectcp, previouscp);
185         } else {
186             return previouscp;
187         }
188     }
189
190     private String JavaDoc[] addPreferenceParameters(String JavaDoc[] previous) {
191         String JavaDoc[] prefParams = StringUtil.cutString(JonasLauncherPlugin.getDefault().getJvmParamaters(), JonasPluginResources.PREF_PAGE_LIST_SEPARATOR);
192         return StringUtil.concat(previous, prefParams);
193     }
194
195     private String JavaDoc[] addPreferenceJvmToClasspath(String JavaDoc[] previous) {
196         String JavaDoc[] prefClasspath = StringUtil.cutString(JonasLauncherPlugin.getDefault().getJvmClasspath(), JonasPluginResources.PREF_PAGE_LIST_SEPARATOR);
197         return StringUtil.concat(previous, prefClasspath);
198     }
199
200     private String JavaDoc[] addPreferenceJvmToBootClasspath(String JavaDoc[] previous) {
201         String JavaDoc[] prefBootClasspath = StringUtil.cutString(JonasLauncherPlugin.getDefault().getJvmBootClasspath(), JonasPluginResources.PREF_PAGE_LIST_SEPARATOR);
202         return StringUtil.concat(previous, prefBootClasspath);
203     }
204
205
206 }
207
208
Popular Tags