KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarActionProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbjarproject;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import org.apache.tools.ant.module.api.support.ActionUtils;
28 import org.netbeans.api.fileinfo.NonRecursiveFolder;
29 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
30 import org.netbeans.spi.project.ActionProvider;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Lookup;
37 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
38 import org.netbeans.api.debugger.*;
39 import org.netbeans.api.debugger.jpda.*;
40 import org.netbeans.api.java.project.JavaProjectConstants;
41 import org.netbeans.api.java.source.SourceUtils;
42 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
43 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
44 import org.netbeans.spi.project.support.ant.ReferenceHelper;
45 import org.openide.*;
46
47 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
48
49 /** Action provider of the Web project. This is the place where to do
50  * strange things to Web actions. E.g. compile-single.
51  */

52 class EjbJarActionProvider implements ActionProvider {
53     
54     // Definition of commands
55

56     private static final String JavaDoc COMMAND_COMPILE = "compile"; //NOI18N
57
private static final String JavaDoc COMMAND_VERIFY = "verify"; //NOI18N
58

59     // Commands available from Web project
60
private static final String JavaDoc[] supportedActions = {
61         COMMAND_BUILD,
62         COMMAND_CLEAN,
63         COMMAND_REBUILD,
64         COMMAND_COMPILE_SINGLE,
65         COMMAND_RUN,
66         COMMAND_RUN_SINGLE,
67         COMMAND_DEBUG,
68         COMMAND_DEBUG_SINGLE,
69         EjbProjectConstants.COMMAND_REDEPLOY,
70         JavaProjectConstants.COMMAND_JAVADOC,
71         COMMAND_TEST,
72         COMMAND_TEST_SINGLE,
73         COMMAND_DEBUG_TEST_SINGLE,
74         JavaProjectConstants.COMMAND_DEBUG_FIX,
75         COMMAND_COMPILE,
76         COMMAND_VERIFY,
77         COMMAND_DELETE,
78         COMMAND_COPY,
79         COMMAND_MOVE,
80         COMMAND_RENAME
81     };
82     
83     // Project
84
EjbJarProject project;
85     
86     // Ant project helper of the project
87
private final AntProjectHelper antProjectHelper;
88     private ReferenceHelper refHelper;
89         
90     /** Map from commands to ant targets */
91     Map JavaDoc/*<String,String[]>*/ commands;
92     
93     public EjbJarActionProvider(EjbJarProject project, AntProjectHelper antProjectHelper, ReferenceHelper refHelper) {
94         commands = new HashMap JavaDoc();
95         commands.put(COMMAND_BUILD, new String JavaDoc[] {"dist"}); // NOI18N
96
commands.put(COMMAND_CLEAN, new String JavaDoc[] {"clean"}); // NOI18N
97
commands.put(COMMAND_REBUILD, new String JavaDoc[] {"clean", "dist"}); // NOI18N
98
commands.put(COMMAND_COMPILE_SINGLE, new String JavaDoc[] {"compile-single"}); // NOI18N
99
commands.put(COMMAND_RUN, new String JavaDoc[] {"run"}); // NOI18N
100
commands.put(COMMAND_RUN_SINGLE, new String JavaDoc[] {"run-main"}); // NOI18N
101
commands.put(EjbProjectConstants.COMMAND_REDEPLOY, new String JavaDoc[] {"run"}); // NOI18N
102
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
103
commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String JavaDoc[] {"javadoc"}); // NOI18N
104
commands.put(COMMAND_TEST, new String JavaDoc[] {"test"}); // NOI18N
105
commands.put(COMMAND_TEST_SINGLE, new String JavaDoc[] {"test-single"}); // NOI18N
106
commands.put(COMMAND_DEBUG_TEST_SINGLE, new String JavaDoc[] {"debug-test"}); // NOI18N
107
commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String JavaDoc[] {"debug-fix"}); // NOI18N
108
commands.put(COMMAND_COMPILE, new String JavaDoc[] {"compile"}); // NOI18N
109
commands.put(COMMAND_VERIFY, new String JavaDoc[] {"verify"}); // NOI18N
110

111         this.antProjectHelper = antProjectHelper;
112         this.project = project;
113         this.refHelper = refHelper;
114     }
115     
116     private FileObject findBuildXml() {
117         return project.getProjectDirectory().getFileObject(project.getBuildXmlName ());
118     }
119     
120     public String JavaDoc[] getSupportedActions() {
121         return supportedActions;
122     }
123     
124     public void invokeAction(final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
125         if (COMMAND_DELETE.equals(command)) {
126             DefaultProjectOperations.performDefaultDeleteOperation(project);
127             return ;
128         }
129         
130         if (COMMAND_COPY.equals(command)) {
131             DefaultProjectOperations.performDefaultCopyOperation(project);
132             return ;
133         }
134         
135         if (COMMAND_MOVE.equals(command)) {
136             DefaultProjectOperations.performDefaultMoveOperation(project);
137             return ;
138         }
139         
140         if (COMMAND_RENAME.equals(command)) {
141             DefaultProjectOperations.performDefaultRenameOperation(project, null);
142             return ;
143         }
144
145         Runnable JavaDoc action = new Runnable JavaDoc () {
146             public void run () {
147                 Properties JavaDoc p = new Properties JavaDoc();
148                 String JavaDoc[] targetNames;
149         
150                 targetNames = getTargetNames(command, context, p);
151                 if (targetNames == null) {
152                     return;
153                 }
154                 if (targetNames.length == 0) {
155                     targetNames = null;
156                 }
157                 if (p.keySet().size() == 0) {
158                     p = null;
159                 }
160                 try {
161                     FileObject buildFo = findBuildXml();
162                     if (buildFo == null || !buildFo.isValid()) {
163                         //The build.xml was deleted after the isActionEnabled was called
164
NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(EjbJarActionProvider.class,
165                                 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE);
166                     DialogDisplayer.getDefault().notify(nd);
167                     }
168                     else {
169                         ActionUtils.runTarget(buildFo, targetNames, p);
170                 }
171                 }
172                 catch (IOException JavaDoc e) {
173                     ErrorManager.getDefault().notify(e);
174                 }
175             }
176         };
177         
178         action.run();
179     }
180     
181     /**
182      * @return array of targets or null to stop execution; can return empty array
183      */

184     /*private*/ String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context, Properties JavaDoc p) throws IllegalArgumentException JavaDoc {
185         String JavaDoc[] targetNames = (String JavaDoc[])commands.get(command);
186         
187         //EXECUTION PART
188
if (command.equals(COMMAND_RUN_SINGLE)) {
189             FileObject[] files = findTestSources(context, false);
190             if (files != null) {
191                 targetNames = setupTestSingle(p, files);
192             } else {
193                 // run Java
194
FileObject[] javaFiles = findJavaSources(context);
195                 if ((javaFiles != null) && (javaFiles.length>0)) {
196                     FileObject file = javaFiles[0];
197                     String JavaDoc clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),file), file);
198                     p.setProperty("javac.includes", clazz); // NOI18N
199
// Convert foo/FooTest.java -> foo.FooTest
200
if (clazz.endsWith(".java")) { // NOI18N
201
clazz = clazz.substring(0, clazz.length() - 5);
202                     }
203                     clazz = clazz.replace('/','.');
204
205                     if (!SourceUtils.getMainClasses(file).isEmpty()) {
206                         p.setProperty("run.class", clazz); // NOI18N
207
targetNames = (String JavaDoc[]) commands.get(COMMAND_RUN_SINGLE);
208                     } else {
209                         NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(EjbJarActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE);
210                         DialogDisplayer.getDefault().notify(nd);
211                         return null;
212                     }
213                 } else {
214                     return null;
215                 }
216             }
217         } else if (command.equals (COMMAND_RUN) || command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) {
218             if (!isSelectedServer ()) {
219                 return null;
220             }
221             if (isDebugged()) {
222                 p.setProperty("is.debugged", "true");
223             }
224             if (command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) {
225                 p.setProperty("forceRedeploy", "true"); //NOI18N
226
} else {
227                 p.setProperty("forceRedeploy", "false"); //NOI18N
228
}
229         //DEBUGGING PART
230
} else if (command.equals (COMMAND_DEBUG) || command.equals(COMMAND_DEBUG_SINGLE)) {
231             FileObject[] javaFiles = findJavaSources(context);
232             if (javaFiles != null && javaFiles.length == 1 && !SourceUtils.getMainClasses(javaFiles[0]).isEmpty()) {
233                 FileObject javaFile = javaFiles[0];
234                 // debug Java with Main method
235
String JavaDoc clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(), javaFile), javaFile);
236                 p.setProperty("javac.includes", clazz); // NOI18N
237
// Convert foo/FooTest.java -> foo.FooTest
238
if (clazz.endsWith(".java")) { // NOI18N
239
clazz = clazz.substring(0, clazz.length() - 5);
240                 }
241                 clazz = clazz.replace('/','.');
242                 p.setProperty("main.class", clazz); // NOI18N
243
targetNames = new String JavaDoc [] {"debug-single-main"};
244                 return targetNames;
245             }
246             
247             FileObject[] testFiles = findTestSources(context, false);
248             if (testFiles != null) {
249                 targetNames = setupDebugTestSingle(p, testFiles);
250             } else {
251                 if (!isSelectedServer()) {
252                     return null;
253                 }
254                 if (isDebugged()) {
255                     p.setProperty("is.debugged", "true");
256                 }
257             }
258         // APPLY CODE CHANGES DEBUGGING
259
} else if (command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) {
260             FileObject[] files = findJavaSources(context);
261             String JavaDoc path = null;
262             if (files != null) {
263                 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]);
264                 targetNames = new String JavaDoc[] {"debug-fix"}; // NOI18N
265
} else {
266                 files = findTestSources(context, false);
267                 path = FileUtil.getRelativePath(getRoot(project.getTestSourceRoots().getRoots(),files[0]), files[0]);
268                 targetNames = new String JavaDoc[] {"debug-fix-test"}; // NOI18N
269
}
270             // Convert foo/FooTest.java -> foo/FooTest
271
if (path.endsWith(".java")) { // NOI18N
272
path = path.substring(0, path.length() - 5);
273             }
274             p.setProperty("fix.includes", path); // NOI18N
275
//COMPILATION PART
276
} else if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
277             FileObject[] sourceRoots = project.getSourceRoots().getRoots();
278             FileObject[] files = findSourcesAndPackages( context, sourceRoots);
279             boolean recursive = (context.lookup(NonRecursiveFolder.class) == null);
280             if (files != null) {
281                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); // NOI18N
282
} else {
283                 FileObject[] testRoots = project.getTestSourceRoots().getRoots();
284                 files = findSourcesAndPackages(context, testRoots);
285                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); // NOI18N
286
targetNames = new String JavaDoc[] {"compile-test-single"}; // NOI18N
287
}
288         // TEST PART
289
} else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
290             FileObject[] files = findTestSourcesForSources(context);
291             targetNames = setupTestSingle(p, files);
292         }
293         else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
294             FileObject[] files = findTestSourcesForSources(context);
295             targetNames = setupDebugTestSingle(p, files);
296         } else {
297             if (targetNames == null) {
298                 throw new IllegalArgumentException JavaDoc(command);
299             }
300         }
301         
302         return targetNames;
303     }
304     
305     private String JavaDoc[] setupTestSingle(Properties JavaDoc p, FileObject[] files) {
306         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
307         FileObject root = getRoot(testSrcPath, files[0]);
308         p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
309
p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
310
return new String JavaDoc[] {"test-single"}; // NOI18N
311
}
312     
313     private String JavaDoc[] setupDebugTestSingle(Properties JavaDoc p, FileObject[] files) {
314         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
315         FileObject root = getRoot(testSrcPath, files[0]);
316         String JavaDoc path = FileUtil.getRelativePath(root, files[0]);
317         // Convert foo/FooTest.java -> foo.FooTest
318
p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); // NOI18N
319
return new String JavaDoc[] {"debug-test"}; // NOI18N
320
}
321     
322     public boolean isActionEnabled( String JavaDoc command, Lookup context ) {
323         FileObject buildXml = findBuildXml();
324         if (buildXml == null || !buildXml.isValid()) {
325             return false;
326         }
327         if ( command.equals( COMMAND_VERIFY ) ) {
328             return project.getEjbModule().hasVerifierSupport();
329         }
330         if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
331             return findSourcesAndPackages( context, project.getSourceRoots().getRoots()) != null
332                     || findSourcesAndPackages( context, project.getTestSourceRoots().getRoots()) != null;
333         }
334         if ( command.equals( COMMAND_RUN_SINGLE ) ) {
335             FileObject[] javaFiles = findTestSources(context,false);
336             if ((javaFiles != null) && (javaFiles.length > 0)) {
337                 return true;
338             }
339             FileObject files[] = findJavaSources(context);
340             return files != null && files.length == 1;
341         }
342         else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
343             return findTestSourcesForSources(context) != null;
344         }
345         else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
346             FileObject[] files = findTestSourcesForSources(context);
347             return files != null && files.length == 1;
348         } else if (command.equals(COMMAND_DEBUG_SINGLE)) {
349             FileObject[] testFiles = findTestSources(context, false);
350             FileObject[] javaFiles = findJavaSources(context);
351             return ((testFiles != null && testFiles.length == 1) ||
352                     (javaFiles != null && javaFiles.length == 1));
353         } else {
354             // other actions are global
355
return true;
356         }
357
358         
359     }
360     
361     // Private methods -----------------------------------------------------
362

363     private static final String JavaDoc SUBST = "Test.java"; // NOI18N
364
private static final Pattern JavaDoc SRCDIRJAVA = Pattern.compile("\\.java$"); // NOI18N
365

366     /** Find selected java sources
367      */

368     private FileObject[] findJavaSources(Lookup context) {
369         FileObject[] srcPath = project.getSourceRoots().getRoots();
370         for (int i=0; i< srcPath.length; i++) {
371             FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); // NOI18N
372
if (files != null) {
373                 return files;
374             }
375         }
376         return null;
377     }
378     
379     private FileObject[] findSourcesAndPackages (Lookup context, FileObject srcDir) {
380         if (srcDir != null) {
381             FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); // NOI18N
382
//Check if files are either packages of java files
383
if (files != null) {
384                 for (int i = 0; i < files.length; i++) {
385                     if (!files[i].isFolder() && !"java".equals(files[i].getExt())) {
386                         return null;
387                     }
388                 }
389             }
390             return files;
391         } else {
392             return null;
393         }
394     }
395     
396     private FileObject[] findSourcesAndPackages (Lookup context, FileObject[] srcRoots) {
397         for (int i=0; i<srcRoots.length; i++) {
398             FileObject[] result = findSourcesAndPackages(context, srcRoots[i]);
399             if (result != null) {
400                 return result;
401             }
402         }
403         return null;
404     }
405     
406     /** Find either selected tests or tests which belong to selected source files
407      */

408     private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) {
409         //XXX: Ugly, should be rewritten
410
FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
411         for (int i=0; i< testSrcPath.length; i++) {
412             FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); // NOI18N
413
if (files != null) {
414                 return files;
415             }
416         }
417         if (checkInSrcDir && testSrcPath.length>0) {
418             FileObject[] files = findJavaSources (context);
419             if (files != null) {
420                 //Try to find the test under the test roots
421
FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]);
422                 for (int i=0; i<testSrcPath.length; i++) {
423                     FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true);
424                     if (files2 != null) {
425                         return files2;
426                     }
427                 }
428             }
429         }
430         return null;
431     }
432     
433     /** Find tests corresponding to selected sources.
434      */

435     private FileObject[] findTestSourcesForSources(Lookup context) {
436         FileObject[] sourceFiles = findJavaSources(context);
437         if (sourceFiles == null) {
438             return null;
439         }
440         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
441         if (testSrcPath.length == 0) {
442             return null;
443         }
444         FileObject[] srcPath = project.getSourceRoots().getRoots();
445         FileObject srcDir = getRoot(srcPath, sourceFiles[0]);
446         for (int i=0; i<testSrcPath.length; i++) {
447             FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true);
448             if (files2 != null) {
449                 return files2;
450             }
451         }
452         return null;
453     }
454     
455     private FileObject getRoot (FileObject[] roots, FileObject file) {
456         FileObject srcDir = null;
457         for (int i=0; i< roots.length; i++) {
458             if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) {
459                 srcDir = roots[i];
460                 break;
461             }
462         }
463         return srcDir;
464     }
465
466     private boolean isDebugged() {
467         
468         J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
469         ServerDebugInfo sdi = jmp.getServerDebugInfo ();
470         if (sdi == null) {
471             return false;
472         }
473 // server.getServerInstance().getStartServer().getDebugInfo(null);
474
Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
475         
476         for (int i=0; i < sessions.length; i++) {
477             Session s = sessions[i];
478             if (s != null) {
479                 Object JavaDoc o = s.lookupFirst(null, AttachingDICookie.class);
480                 if (o != null) {
481                     AttachingDICookie attCookie = (AttachingDICookie)o;
482                     if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) {
483                         if (attCookie.getSharedMemoryName().equalsIgnoreCase(sdi.getShmemName())) {
484                             return true;
485                         }
486                     } else {
487                         if (attCookie.getHostName().equalsIgnoreCase(sdi.getHost())) {
488                             if (attCookie.getPortNumber() == sdi.getPort()) {
489                                 return true;
490                             }
491                         }
492                     }
493                 }
494             }
495         }
496         return false;
497     }
498     
499     private boolean isSelectedServer () {
500         String JavaDoc instance = antProjectHelper.getStandardPropertyEvaluator ().getProperty (EjbJarProjectProperties.J2EE_SERVER_INSTANCE);
501         if (instance != null) {
502             String JavaDoc id = Deployment.getDefault().getServerID(instance);
503             if (id != null) {
504                 return true;
505             }
506         }
507         
508         // if there is some server instance of the type which was used
509
// previously do not ask and use it
510
String JavaDoc serverType = antProjectHelper.getStandardPropertyEvaluator ().getProperty (EjbJarProjectProperties.J2EE_SERVER_TYPE);
511         if (serverType != null) {
512             String JavaDoc[] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType);
513             if (servInstIDs.length > 0) {
514                 setServerInstance(servInstIDs[0]);
515                 return true;
516             }
517         }
518
519         // no selected server => warning
520
String JavaDoc msg = NbBundle.getMessage(EjbJarActionProvider.class, "MSG_No_Server_Selected"); // NOI18N
521
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
522         return false;
523     }
524     
525     private void setServerInstance(final String JavaDoc serverInstanceId) {
526         EjbJarProjectProperties.setServerInstance(project, antProjectHelper, serverInstanceId);
527     }
528     
529 }
530
Popular Tags