KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > Application


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import java.io.FilenameFilter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Vector JavaDoc;
33 import org.apache.log4j.Level;
34 import org.objectweb.jac.core.Jac;
35 import org.objectweb.jac.util.File;
36 import org.objectweb.jac.util.Pipe;
37 import org.objectweb.jac.util.Strings;
38
39 public class Application extends ModelElement {
40
41     public Application() {
42     }
43
44     Vector JavaDoc hosts = new Vector JavaDoc();
45    
46     /**
47      * Get the value of hosts.
48      * @return value of hosts.
49      */

50     public List JavaDoc getHosts() {
51         return hosts;
52     }
53    
54     public void addHost(String JavaDoc host) {
55         hosts.add(host);
56     }
57
58     public void removeHost(String JavaDoc host) {
59         hosts.remove(host);
60     }
61
62     String JavaDoc name;
63    
64     /**
65      * Get the value of name.
66      * @return value of name.
67      */

68     public String JavaDoc getName() {
69         return name;
70     }
71    
72     /**
73      * Set the value of name.
74      * @param v Value to assign to name.
75      */

76     public void setName(String JavaDoc v) {
77         this.name = v;
78     }
79
80     Class JavaDoc mainClass;
81    
82     /**
83      * Get the value of mainClass.
84      * @return value of mainClass.
85      */

86     public Class JavaDoc getMainClass() {
87         return mainClass;
88     }
89    
90     /**
91      * Set the value of mainClass.
92      * @param v Value to assign to mainClass.
93      */

94     public void setMainClass(Class JavaDoc v) {
95         this.mainClass = v;
96     }
97    
98     Vector JavaDoc aspectConfigurations = new Vector JavaDoc();
99    
100     /**
101      * Get the value of aspectConfigurations.
102      * @return value of aspectConfigurations.
103      */

104     public List JavaDoc getAspectConfigurations() {
105         return aspectConfigurations;
106     }
107    
108     public void addAspectConfiguration(AspectConfiguration a) {
109         aspectConfigurations.add(a);
110     }
111
112     public void removeAspectConfiguration(AspectConfiguration a) {
113         aspectConfigurations.remove(a);
114     }
115
116     Vector JavaDoc externalPaths = new Vector JavaDoc();
117     /**
118      * Get the value of externalPaths.
119      * @return value of externalPaths.
120      */

121     public List JavaDoc getExternalPaths() {
122         return externalPaths;
123     }
124     public void addExternalPath(String JavaDoc path) {
125         externalPaths.add(path);
126     }
127     public void removeExternalPath(String JavaDoc path) {
128         externalPaths.remove(path);
129     }
130
131     /**
132      * Classes that JAC must not translate
133      */

134     Set JavaDoc ignoredClasses = new HashSet JavaDoc();
135     public Set JavaDoc getIgnoredClasses() {
136         return ignoredClasses;
137     }
138     public void addIgnoredClass(String JavaDoc expr) {
139         ignoredClasses.add(expr);
140     }
141     public void removeIgnoredClass(String JavaDoc expr) {
142         ignoredClasses.remove(expr);
143     }
144
145     Project project;
146     /**
147      * Get the value of project.
148      * @return value of project.
149      */

150     public Project getProject() {
151         return project;
152     }
153     /**
154      * Set the value of project.
155      * @param v Value to assign to project.
156      */

157     public void setProject(Project v) {
158         this.project = v;
159     }
160
161     boolean startSwingGUI = false;
162     public void setStartSwingGUI(boolean value) {
163         this.startSwingGUI = value;
164     }
165     public boolean getStartSwingGUI() {
166         return startSwingGUI;
167     }
168
169     String JavaDoc swingGUIs;
170     public String JavaDoc getSwingGUIs() {
171         return swingGUIs;
172     }
173     public void setSwingGUIs(String JavaDoc v) {
174         this.swingGUIs = v;
175     }
176    
177     String JavaDoc webGUIs;
178     public String JavaDoc getWebGUIs() {
179         return webGUIs;
180     }
181     public void setWebGUIs(String JavaDoc v) {
182         this.webGUIs = v;
183     }
184    
185     boolean startWebGUI = false;
186     public void setStartWebGUI(boolean value) {
187         this.startWebGUI = value;
188     }
189     public boolean getStartWebGUI() {
190         return startWebGUI;
191     }
192
193     boolean enableDistribution = true;
194     public void setEnableDistribution(boolean value) {
195         this.enableDistribution = value;
196     }
197     public boolean isDistributionEnabled() {
198         return enableDistribution;
199     }
200
201     String JavaDoc serverName = "s1";
202     public void setServerName(String JavaDoc name) {
203         this.serverName = name;
204     }
205     public String JavaDoc getServerName() {
206         return serverName;
207     }
208    
209     String JavaDoc otherOptions = "";
210     public String JavaDoc getOtherOption() {
211         return otherOptions;
212     }
213     public void setOtherOptions(String JavaDoc options) {
214         this.otherOptions = options;
215     }
216
217     String JavaDoc jvmOptions = "";
218     public String JavaDoc getJvmOption() {
219         return jvmOptions;
220     }
221     public void setJvmOptions(String JavaDoc options) {
222         this.jvmOptions = options;
223     }
224
225     Map JavaDoc properties = new Hashtable JavaDoc();
226     public Map JavaDoc getProperties() {
227         return properties;
228     }
229     public void addProperty(String JavaDoc name, String JavaDoc value) {
230         properties.put(name,value);
231     }
232     public void removeProperty(String JavaDoc name) {
233         properties.remove(name);
234     }
235
236     boolean enableDebugging;
237     public boolean isEnableDebugging() {
238         return enableDebugging;
239     }
240     public void setEnableDebugging(boolean enableDebugging) {
241         this.enableDebugging = enableDebugging;
242     }
243
244     int debugPort = 4444;
245     public int getDebugPort() {
246         return debugPort;
247     }
248     public void setDebugPort(int debugPort) {
249         this.debugPort = debugPort;
250     }
251
252     transient Process JavaDoc currentProcess = null;
253     public void resetCurrentProcess() {
254         currentProcess = null;
255     }
256
257     public boolean isStarted() {
258         return currentProcess!=null;
259     }
260     public boolean isNotStarted() {
261         return currentProcess==null;
262     }
263
264     public File getJacFile() {
265         return new File(
266             new File(project.getGenerationPath(),getGenerationName()),
267             getGenerationName()+".jac");
268     }
269
270     /**
271      * Returns the command and arguments to run the application
272      */

273     public String JavaDoc[] getRunCommandAndArgs() {
274         File dir = new File(project.getGenerationPath(),name);
275         Vector JavaDoc runCmd = new Vector JavaDoc();
276         runCmd.add("java");
277
278         runCmd.addAll(Strings.splitToList(jvmOptions," "));
279
280         if (enableDebugging) {
281             runCmd.add("-Xdebug");
282             runCmd.add("-Xrunjdwp:transport=dt_socket,address="+debugPort+",server=y,suspend=n");
283         }
284
285         runCmd.add("-Djava.security.policy="+
286                    org.objectweb.jac.core.Jac.getJacRoot()+"jac.policy");
287
288         // So that is works on MacOS X
289
runCmd.add("-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel");
290
291         File jar = new File(org.objectweb.jac.core.Jac.getJacRoot()+"jac.jar");
292         String JavaDoc jacroot = org.objectweb.jac.core.Jac.getJacRoot();
293         String JavaDoc sep = System.getProperty("path.separator");
294
295         // add the classpath defined in the project and all jars in $JAC_ROOT/lib
296
List JavaDoc classpath = new LinkedList JavaDoc(getProject().getClasspath());
297         File libDir = new File(jacroot,"lib");
298         classpath.add(new File(jacroot,"classes"));
299         classpath.add(new File(jacroot,"src"));
300         if (jar.exists())
301             classpath.add(jar);
302         classpath.addAll(
303             libDir.listFilesRecursively(
304                 new FilenameFilter JavaDoc() {
305                         public boolean accept(java.io.File JavaDoc file, String JavaDoc name) {
306                             return name.endsWith(".jar");
307                         }
308                     }
309             ));
310         if (classpath.size()>0) {
311             runCmd.add("-classpath");
312             runCmd.add(Strings.join(classpath,sep));
313         }
314
315         Iterator JavaDoc it = properties.entrySet().iterator();
316         while (it.hasNext()) {
317             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
318             runCmd.add("-D"+entry.getKey()+"="+entry.getValue());
319         }
320
321         runCmd.add("org.objectweb.jac.core.Jac");
322          
323         runCmd.add("-w");
324         if (enableDistribution) {
325             runCmd.add("-D");
326             if (serverName!=null && !serverName.equals(""))
327                 runCmd.add(serverName);
328         }
329
330         if (!Strings.isEmpty(jacroot)) {
331             runCmd.add("-R");
332             runCmd.add(jacroot);
333         }
334         runCmd.add("-C");
335         runCmd.add(project.getGenerationPath().getPath()+
336                    sep+new File(project.getGenerationPath(),"classes").getPath()+
337                    sep+project.getClasspathString());
338
339         String JavaDoc msg="Starting application '"+name+"'";
340         if (startSwingGUI) {
341             runCmd.add("-G");
342             runCmd.add(swingGUIs);
343             msg = msg+" (Swing GUIs: "+swingGUIs+")";
344         }
345
346         if (startWebGUI) {
347             runCmd.add("-W");
348             runCmd.add(webGUIs);
349             msg = msg+" (Web GUIs: "+webGUIs+")";
350         }
351
352         runCmd.addAll(Strings.splitToList(otherOptions," "));
353
354         runCmd.add(new File(dir,name+".jac").getPath());
355
356         return (String JavaDoc[])runCmd.toArray(new String JavaDoc[] {});
357     }
358
359     public void start() throws IOException JavaDoc {
360         if (!startSwingGUI && !startWebGUI) {
361             throw new RuntimeException JavaDoc(
362                 "You should probably start a GUI (see \"Run options\""+
363                 " of the application)");
364         }
365
366         if (isStarted()) {
367             stop();
368         }
369
370         String JavaDoc msg="Starting application '"+name+"'";
371         if (startSwingGUI) {
372             msg = msg+" (Swing GUIs: "+swingGUIs+")";
373         }
374         if (startWebGUI) {
375             msg = msg+" (Web GUIs: "+webGUIs+")";
376         }
377         org.objectweb.jac.aspects.gui.Actions.showStatus(msg);
378
379         String JavaDoc[] runCmd = getRunCommandAndArgs();
380         //Log.trace("ide.run",0,"Run command = "+Arrays.asList(runCmd));
381
currentProcess = Runtime.getRuntime().exec(runCmd);
382
383         Pipe outputPipe = new Pipe(currentProcess.getInputStream(),System.out);
384         outputPipe.start();
385         Pipe errorPipe = new Pipe(currentProcess.getErrorStream(),System.out);
386         errorPipe.start();
387         new Thread JavaDoc() {
388                 public void run() {
389                     while (currentProcess!=null) {
390                         try {
391                             currentProcess.waitFor();
392                             //Log.trace("ide","Current process completed");
393
resetCurrentProcess();
394                         } catch (InterruptedException JavaDoc e) {
395                             //Log.trace("ide","Exception while waiting for "+currentProcess);
396
}
397                     }
398                 }
399             }.start();
400     }
401
402     public void stop() {
403         if (currentProcess!=null) {
404             currentProcess.destroy();
405             currentProcess = null;
406         }
407         org.objectweb.jac.aspects.gui.Actions.showStatus("Application stopped.");
408     }
409
410     public void startSlaves() throws IOException JavaDoc {
411         File dir = new File(project.getGenerationPath(),name);
412         Vector JavaDoc runCmd = new Vector JavaDoc();
413         runCmd.add("go");
414         runCmd.add(""+getHosts().size());
415         File jar = new File(org.objectweb.jac.core.Jac.getJacRoot()+"jac.jar");
416         runCmd.add("-C");
417         runCmd.add(project.getGenerationPath().getPath()+
418                    System.getProperty("path.separator")+
419                    new File(project.getGenerationPath(),"classes").getPath());
420       
421         String JavaDoc msg="Starting slaves...";
422
423         org.objectweb.jac.aspects.gui.Actions.showStatus(msg);
424
425         //Log.trace("ide.run",0,"Run command = "+runCmd);
426
Runtime.getRuntime().exec(
427             (String JavaDoc[])runCmd.toArray(new String JavaDoc[] {}));
428       
429     }
430
431     /**
432      * Changes a trace for the running process
433      * @param category category of the trace
434      * @param level level of the trace (0=quiet,1=verbose,2=more verbose...)
435      */

436     public void setTrace(String JavaDoc category, Level level) throws Exception JavaDoc {
437         if (currentProcess!=null) {
438             Jac.remoteSetTrace(name,serverName,category,level.toInt());
439         }
440     }
441
442     public boolean canStartSlaves() {
443         return getHosts().size()>0;
444     }
445
446     public void generateCode() throws CannotGenerateException, IOException JavaDoc
447     {
448         project.checkGenerationPath();
449         CodeGeneration.createApplicationCode(
450             project,this,new File(project.getGenerationPath().getPath(),name));
451     }
452
453     /**
454      * Returns available main classes (Classes wich have a static void
455      * main(String[]) method)
456      * @param application the application to return main classes for
457      */

458     public static Collection JavaDoc getMainClasses(Application application) {
459         Vector JavaDoc mainClasses = new Vector JavaDoc();
460         if (application.getProject()!=null) {
461             Iterator JavaDoc it = application.getProject().getPackages().iterator();
462             while (it.hasNext()) {
463                 Package JavaDoc pkg = (Package JavaDoc)it.next();
464                 mainClasses.addAll(pkg.getMainClasses());
465             }
466         }
467         return mainClasses;
468     }
469 }
470
Popular Tags