KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebActionProvider


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.web.project;
21
22 import java.io.IOException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import org.apache.tools.ant.module.api.support.ActionUtils;
34 import org.netbeans.api.java.classpath.GlobalPathRegistry;
35 import org.netbeans.api.java.source.SourceUtils;
36 import org.netbeans.api.project.FileOwnerQuery;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
39 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
40 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
41 import org.netbeans.spi.project.ActionProvider;
42 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.loaders.DataObject;
46 import org.openide.util.Lookup;
47 import org.openide.util.NbBundle;
48 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
49 import org.netbeans.api.debugger.*;
50 import org.netbeans.api.debugger.jpda.*;
51 import org.netbeans.api.java.project.JavaProjectConstants;
52 import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
53 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
54 import org.netbeans.modules.web.project.ui.SetExecutionUriAction;
55 import org.netbeans.modules.web.project.parser.ParserWebModule;
56 import org.netbeans.modules.web.project.parser.JspNameUtil;
57 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
58 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
59 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
60 import org.openide.DialogDisplayer;
61 import org.openide.NotifyDescriptor;
62 import org.netbeans.modules.web.api.webmodule.WebModule;
63 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
64 import java.util.HashSet JavaDoc;
65 import org.netbeans.api.fileinfo.NonRecursiveFolder;
66 import org.netbeans.modules.web.api.webmodule.RequestParametersQuery;
67 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
68 import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
69 import org.netbeans.modules.web.project.ui.ServletUriPanel;
70 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
71 import org.netbeans.modules.websvc.api.client.WsCompileClientEditorSupport;
72 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
73 import org.netbeans.modules.websvc.api.webservices.WsCompileEditorSupport;
74 import org.netbeans.spi.project.support.ant.AntProjectHelper;
75 import org.netbeans.spi.project.support.ant.PropertyUtils;
76 import org.openide.DialogDescriptor;
77 import org.openide.ErrorManager;
78
79
80 /** Action provider of the Web project. This is the place where to do
81  * strange things to Web actions. E.g. compile-single.
82  */

83 class WebActionProvider implements ActionProvider {
84     
85     // Definition of commands
86

87     private static final String JavaDoc COMMAND_COMPILE = "compile"; //NOI18N
88
private static final String JavaDoc COMMAND_VERIFY = "verify"; //NOI18N
89

90     // Commands available from Web project
91
private static final String JavaDoc[] supportedActions = {
92         COMMAND_BUILD,
93         COMMAND_CLEAN,
94         COMMAND_REBUILD,
95         COMMAND_COMPILE_SINGLE,
96         COMMAND_RUN,
97         COMMAND_RUN_SINGLE,
98         COMMAND_DEBUG,
99         WebProjectConstants.COMMAND_REDEPLOY,
100         COMMAND_DEBUG_SINGLE,
101         JavaProjectConstants.COMMAND_JAVADOC,
102         COMMAND_TEST,
103         COMMAND_TEST_SINGLE,
104         COMMAND_DEBUG_TEST_SINGLE,
105         JavaProjectConstants.COMMAND_DEBUG_FIX,
106         COMMAND_COMPILE,
107         COMMAND_VERIFY,
108         COMMAND_DELETE,
109         COMMAND_COPY,
110         COMMAND_MOVE,
111         COMMAND_RENAME
112     };
113     
114     // Project
115
WebProject project;
116     
117     // Ant project helper of the project
118
private UpdateHelper updateHelper;
119
120     /** Map from commands to ant targets */
121     Map JavaDoc/*<String,String[]>*/ commands;
122     
123     public WebActionProvider(WebProject project, UpdateHelper updateHelper) {
124         
125         commands = new HashMap JavaDoc();
126             commands.put(COMMAND_BUILD, new String JavaDoc[] {"dist"}); // NOI18N
127
commands.put(COMMAND_CLEAN, new String JavaDoc[] {"clean"}); // NOI18N
128
commands.put(COMMAND_REBUILD, new String JavaDoc[] {"clean", "dist"}); // NOI18N
129
// the target name is compile-single, except for JSPs, where it is compile-single-jsp
130
commands.put(COMMAND_COMPILE_SINGLE, new String JavaDoc[] {"compile-single"}); // NOI18N
131
commands.put(COMMAND_RUN, new String JavaDoc[] {"run"}); // NOI18N
132
// the target name is run, except for Java files with main method, where it is run-main
133
commands.put(COMMAND_RUN_SINGLE, new String JavaDoc[] {"run"}); // NOI18N
134
commands.put(WebProjectConstants.COMMAND_REDEPLOY, new String JavaDoc[] {"run-deploy"}); // NOI18N
135
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
136
// the target name is debug, except for Java files with main method, where it is debug-single-main
137
commands.put(COMMAND_DEBUG_SINGLE, new String JavaDoc[] {"debug"}); // NOI18N
138
commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String JavaDoc[] {"javadoc"}); // NOI18N
139
commands.put(COMMAND_TEST, new String JavaDoc[] {"test"}); // NOI18N
140
commands.put(COMMAND_TEST_SINGLE, new String JavaDoc[] {"test-single"}); // NOI18N
141
commands.put(COMMAND_DEBUG_TEST_SINGLE, new String JavaDoc[] {"debug-test"}); // NOI18N
142
commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String JavaDoc[] {"debug-fix"}); // NOI18N
143
commands.put(COMMAND_COMPILE, new String JavaDoc[] {"compile"}); // NOI18N
144
commands.put(COMMAND_VERIFY, new String JavaDoc[] {"verify"}); // NOI18N
145

146         this.updateHelper = updateHelper;
147         this.project = project;
148     }
149     
150     private FileObject findBuildXml() {
151         return project.getProjectDirectory().getFileObject(project.getBuildXmlName());
152     }
153     
154     public String JavaDoc[] getSupportedActions() {
155         return supportedActions;
156     }
157     
158     public void invokeAction( final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
159         if (COMMAND_DELETE.equals(command)) {
160             DefaultProjectOperations.performDefaultDeleteOperation(project);
161             return ;
162         }
163                 
164         if (COMMAND_COPY.equals(command)) {
165             DefaultProjectOperations.performDefaultCopyOperation(project);
166             return ;
167         }
168         
169         if (COMMAND_MOVE.equals(command)) {
170             DefaultProjectOperations.performDefaultMoveOperation(project);
171             return ;
172         }
173         
174         if (COMMAND_RENAME.equals(command)) {
175             DefaultProjectOperations.performDefaultRenameOperation(project, null);
176             return ;
177         }
178
179         Runnable JavaDoc action = new Runnable JavaDoc () {
180             public void run () {
181                 Properties JavaDoc p = new Properties JavaDoc();
182                 String JavaDoc[] targetNames;
183         
184                 targetNames = getTargetNames(command, context, p);
185                 if (targetNames == null) {
186                     return;
187                 }
188                 if (targetNames.length == 0) {
189                     targetNames = null;
190                 }
191                 if (p.keySet().size() == 0) {
192                     p = null;
193                 }
194                 try {
195                     FileObject buildFo = findBuildXml();
196                     if (buildFo == null || !buildFo.isValid()) {
197                         //The build.xml was deleted after the isActionEnabled was called
198
NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(WebActionProvider.class,
199                                 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE);
200                     DialogDisplayer.getDefault().notify(nd);
201                     }
202                     else {
203                         ActionUtils.runTarget(buildFo, targetNames, p);
204                 }
205                 }
206                 catch (IOException JavaDoc e) {
207                     ErrorManager.getDefault().notify(e);
208                 }
209             }
210         };
211         
212         action.run();
213     }
214
215     /**
216      * @return array of targets or null to stop execution; can return empty array
217      */

218     String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context, Properties JavaDoc p) throws IllegalArgumentException JavaDoc {
219         String JavaDoc[] targetNames = (String JavaDoc[])commands.get(command);
220         
221         // RUN-SINGLE
222
if (command.equals(COMMAND_RUN_SINGLE)) {
223             FileObject[] files = findTestSources(context, false);
224             if (files != null) {
225                 targetNames = setupTestSingle(p, files);
226             } else {
227                 if (!isSelectedServer ()) {
228                     return null;
229                 }
230                 if (isDebugged()) {
231                     p.setProperty("is.debugged", "true");
232                 }
233                 // 51462 - if there's an ejb reference, but no j2ee app, run/deploy will not work
234
if (isEjbRefAndNoJ2eeApp(project)) {
235                     NotifyDescriptor nd;
236                     nd = new NotifyDescriptor.Message(NbBundle.getMessage(WebActionProvider.class, "MSG_EjbRef"), NotifyDescriptor.INFORMATION_MESSAGE);
237                     DialogDisplayer.getDefault().notify(nd);
238                     return null;
239                 }
240                 if (command.equals (WebProjectConstants.COMMAND_REDEPLOY)) {
241                     p.setProperty("forceRedeploy", "true"); //NOI18N
242
} else {
243                     p.setProperty("forceRedeploy", "false"); //NOI18N
244
}
245                 // run a JSP
246
files = findJsps( context );
247                 if (files!=null && files.length>0) {
248                     // possibly compile the JSP, if we are not compiling all of them
249
String JavaDoc raw = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.COMPILE_JSPS);
250                     boolean compile = decodeBoolean(raw);
251                     if (!compile) {
252                         setAllPropertiesForSingleJSPCompilation(p, files);
253                     }
254
255                     String JavaDoc requestParams = RequestParametersQuery.getFileAndParameters(files [0]);
256                     if (requestParams != null) {
257                         p.setProperty("client.urlPart", requestParams); //NOI18N
258
} else {
259                         return null;
260                     }
261                 } else {
262                     // run HTML file
263
FileObject[] htmlFiles = findHtml(context);
264                     if ((htmlFiles != null) && (htmlFiles.length>0)) {
265                         String JavaDoc url = "/" + FileUtil.getRelativePath(WebModule.getWebModule (htmlFiles[0]).getDocumentBase (), htmlFiles[0]); // NOI18N
266
if (url != null) {
267                             url = org.openide.util.Utilities.replaceString(url, " ", "%20");
268                             p.setProperty("client.urlPart", url); //NOI18N
269
} else {
270                             return null;
271                         }
272                     } else {
273                         // run Java
274
FileObject[] javaFiles = findJavaSources(context);
275                         if ((javaFiles != null) && (javaFiles.length>0)) {
276                             FileObject javaFile = javaFiles[0];
277                             if (!SourceUtils.getMainClasses(javaFile).isEmpty()) {
278                                 // run Java with Main method
279
String JavaDoc clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),javaFile), javaFile);
280                                 p.setProperty("javac.includes", clazz); // NOI18N
281
// Convert foo/FooTest.java -> foo.FooTest
282
if (clazz.endsWith(".java")) { // NOI18N
283
clazz = clazz.substring(0, clazz.length() - 5);
284                                 }
285                                 clazz = clazz.replace('/','.');
286
287                                 p.setProperty("run.class", clazz); // NOI18N
288
targetNames = new String JavaDoc [] {"run-main"};
289                             }
290                             else {
291                                 // run servlet
292
// PENDING - what about servlets with main method? servlet should take precedence
293
String JavaDoc executionUri = (String JavaDoc)javaFile.getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI);
294                                 if (executionUri!=null) {
295                                     p.setProperty("client.urlPart", executionUri); //NOI18N
296
} else {
297                                     WebModule webModule = WebModule.getWebModule(javaFile);
298                                     String JavaDoc[] urlPatterns = SetExecutionUriAction.getServletMappings(webModule,javaFile);
299                                     if (urlPatterns!=null && urlPatterns.length>0) {
300                                         ServletUriPanel uriPanel = new ServletUriPanel(urlPatterns,null,true);
301                                         DialogDescriptor desc = new DialogDescriptor(uriPanel,
302                                             NbBundle.getMessage (WebActionProvider.class, "TTL_setServletExecutionUri"));
303                                         Object JavaDoc res = DialogDisplayer.getDefault().notify(desc);
304                                         if (res.equals(NotifyDescriptor.YES_OPTION)) {
305                                             p.setProperty("client.urlPart", uriPanel.getServletUri()); //NOI18N
306
try {
307                                                 javaFile.setAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI,uriPanel.getServletUri());
308                                             } catch (IOException JavaDoc ex){}
309                                         } else return null;
310                                     } else {
311                                         String JavaDoc mes = java.text.MessageFormat.format (
312                                                 NbBundle.getMessage (WebActionProvider.class, "TXT_noExecutableClass"),
313                                                 new Object JavaDoc [] {javaFile.getName()});
314                                         NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
315                                         DialogDisplayer.getDefault().notify(desc);
316                                         return null;
317                                     }
318                                 }
319                             }
320                         }
321                     }
322                 }
323             }
324
325         // RUN, REDEPLOY
326
} else if (command.equals(COMMAND_RUN) || command.equals (WebProjectConstants.COMMAND_REDEPLOY)) {
327             FileObject[] files = findTestSources(context, false);
328             if (files != null) {
329                 targetNames = setupTestSingle(p, files);
330             } else {
331                 if (!isSelectedServer ()) {
332                     return null;
333                 }
334                 if (isDebugged()) {
335                     p.setProperty("is.debugged", "true");
336                 }
337                 // 51462 - if there's an ejb reference, but no j2ee app, run/deploy will not work
338
if (isEjbRefAndNoJ2eeApp(project)) {
339                     NotifyDescriptor nd;
340                     nd = new NotifyDescriptor.Message(NbBundle.getMessage(WebActionProvider.class, "MSG_EjbRef"), NotifyDescriptor.INFORMATION_MESSAGE);
341                     DialogDisplayer.getDefault().notify(nd);
342                     return null;
343                 }
344                 if (command.equals (WebProjectConstants.COMMAND_REDEPLOY)) {
345                     p.setProperty("forceRedeploy", "true"); //NOI18N
346
} else {
347                     p.setProperty("forceRedeploy", "false"); //NOI18N
348
}
349             }
350
351         // DEBUG-SINGLE
352
} else if (command.equals(COMMAND_DEBUG_SINGLE)) {
353             FileObject[] files = findTestSources(context, false);
354             if (files != null) {
355                 targetNames = setupDebugTestSingle(p, files);
356             } else {
357                 if (!isSelectedServer ()) {
358                     return null;
359                 }
360                 if (isDebugged()) {
361                     p.setProperty("is.debugged", "true");
362                 }
363                 // 51462 - if there's an ejb reference, but no j2ee app, debug will not work
364
if (isEjbRefAndNoJ2eeApp(project)) {
365                     NotifyDescriptor nd;
366                     nd = new NotifyDescriptor.Message(NbBundle.getMessage(WebActionProvider.class, "MSG_EjbRef"), NotifyDescriptor.INFORMATION_MESSAGE);
367                     DialogDisplayer.getDefault().notify(nd);
368                     return null;
369                 }
370
371                 files = findJsps( context );
372                 if ((files != null) && (files.length>0)) {
373                     // debug jsp
374
// possibly compile the JSP, if we are not compiling all of them
375
String JavaDoc raw = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.COMPILE_JSPS);
376                     boolean compile = decodeBoolean(raw);
377                     if (!compile) {
378                         setAllPropertiesForSingleJSPCompilation(p, files);
379                     }
380
381                     String JavaDoc requestParams = RequestParametersQuery.getFileAndParameters(files [0]);
382                     if (requestParams != null) {
383                         p.setProperty("client.urlPart", requestParams); //NOI18N
384
} else {
385                         return null;
386                     }
387                 } else {
388                     // debug HTML file
389
FileObject[] htmlFiles = findHtml(context);
390                     if ((htmlFiles != null) && (htmlFiles.length>0)) {
391                         String JavaDoc url = "/" + FileUtil.getRelativePath(WebModule.getWebModule (htmlFiles[0]).getDocumentBase (), htmlFiles[0]); // NOI18N
392
if (url != null) {
393                             url = org.openide.util.Utilities.replaceString(url, " ", "%20");
394                             p.setProperty("client.urlPart", url); //NOI18N
395
} else {
396                             return null;
397                         }
398                     } else {
399                         // debug Java
400
// debug servlet
401
FileObject[] javaFiles = findJavaSources(context);
402                         if ((javaFiles != null) && (javaFiles.length>0)) {
403                             FileObject javaFile = javaFiles[0];
404                             if (!SourceUtils.getMainClasses(javaFile).isEmpty()) {
405                                 // debug Java with Main method
406
String JavaDoc clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),javaFile), javaFile);
407                                 p.setProperty("javac.includes", clazz); // NOI18N
408
// Convert foo/FooTest.java -> foo.FooTest
409
if (clazz.endsWith(".java")) { // NOI18N
410
clazz = clazz.substring(0, clazz.length() - 5);
411                                 }
412                                 clazz = clazz.replace('/','.');
413
414                                 p.setProperty("debug.class", clazz); // NOI18N
415
targetNames = new String JavaDoc [] {"debug-single-main"};
416                             }
417                             else {
418                                 // run servlet
419
// PENDING - what about servlets with main method? servlet should take precedence
420
String JavaDoc executionUri = (String JavaDoc)javaFile.getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI);
421                                 if (executionUri!=null) {
422                                     p.setProperty("client.urlPart", executionUri); //NOI18N
423
} else {
424                                     WebModule webModule = WebModule.getWebModule(javaFile);
425                                     String JavaDoc[] urlPatterns = SetExecutionUriAction.getServletMappings(webModule,javaFile);
426                                     if (urlPatterns!=null && urlPatterns.length>0) {
427                                         ServletUriPanel uriPanel = new ServletUriPanel(urlPatterns,null,true);
428                                         DialogDescriptor desc = new DialogDescriptor(uriPanel,
429                                             NbBundle.getMessage (WebActionProvider.class, "TTL_setServletExecutionUri"));
430                                         Object JavaDoc res = DialogDisplayer.getDefault().notify(desc);
431                                         if (res.equals(NotifyDescriptor.YES_OPTION)) {
432                                             p.setProperty("client.urlPart", uriPanel.getServletUri()); //NOI18N
433
try {
434                                                 javaFile.setAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI,uriPanel.getServletUri());
435                                             } catch (IOException JavaDoc ex){}
436                                         } else return null;
437                                     } else {
438                                         String JavaDoc mes = java.text.MessageFormat.format (
439                                                 NbBundle.getMessage (WebActionProvider.class, "TXT_missingServletMappings"),
440                                                 new Object JavaDoc [] {javaFile.getName()});
441                                         NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
442                                         DialogDisplayer.getDefault().notify(desc);
443                                         return null;
444                                     }
445                                 }
446                             }
447                         }
448                     }
449                 }
450             }
451
452         //DEBUG
453
} else if (command.equals (COMMAND_DEBUG)) {
454             if (!isSelectedServer ()) {
455                 return null;
456             }
457             if (isDebugged()) {
458                 p.setProperty("is.debugged", "true");
459             }
460             // 51462 - if there's an ejb reference, but no j2ee app, debug will not work
461
if (isEjbRefAndNoJ2eeApp(project)) {
462                 NotifyDescriptor nd;
463                 nd = new NotifyDescriptor.Message(NbBundle.getMessage(WebActionProvider.class, "MSG_EjbRef"), NotifyDescriptor.INFORMATION_MESSAGE);
464                 DialogDisplayer.getDefault().notify(nd);
465                 return null;
466             }
467
468             WebServicesClientSupport wscs = WebServicesClientSupport.getWebServicesClientSupport(project.getProjectDirectory());
469             if (wscs != null) { //project contains ws reference
470
List JavaDoc serviceClients = wscs.getServiceClients();
471                 //we store all ws client names into hash set for later fast searching
472
HashSet JavaDoc scNames = new HashSet JavaDoc();
473                 for (Iterator JavaDoc scIt = serviceClients.iterator(); scIt.hasNext(); ) {
474                     WsCompileClientEditorSupport.ServiceSettings serviceClientSettings =
475                             (WsCompileClientEditorSupport.ServiceSettings)scIt.next();
476                     scNames.add(serviceClientSettings.getServiceName());
477                 }
478
479                 StringBuffer JavaDoc clientDCP = new StringBuffer JavaDoc();//additional debug.classpath
480
StringBuffer JavaDoc clientWDD = new StringBuffer JavaDoc();//additional web.docbase.dir
481

482                 //we find all projects containg a web service
483
Set JavaDoc globalPath = GlobalPathRegistry.getDefault().getSourceRoots();
484                 HashSet JavaDoc serverNames = new HashSet JavaDoc();
485                 //iteration through all source roots
486
for (Iterator JavaDoc iter = globalPath.iterator(); iter.hasNext(); ) {
487                     FileObject sourceRoot = (FileObject)iter.next();
488                     Project serverProject = FileOwnerQuery.getOwner(sourceRoot);
489                     if (serverProject != null) {
490                         if (!serverNames.add(serverProject.getProjectDirectory().getName())) //project was already visited
491
continue;
492                         WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(serverProject.getProjectDirectory());
493                         if (wss != null) { //project contains ws
494
List JavaDoc services = wss.getServices();
495                             boolean match = false;
496                             for (Iterator JavaDoc sIt = services.iterator(); sIt.hasNext(); ) {
497                                 WsCompileEditorSupport.ServiceSettings serviceSettings =
498                                         (WsCompileEditorSupport.ServiceSettings)sIt.next();
499                                 String JavaDoc serviceName = serviceSettings.getServiceName();
500                                 if (scNames.contains(serviceName)) { //matching ws name found
501
match = true;
502                                     break; //no need to continue
503
}
504                             }
505                             if (match) { //matching ws name found in project
506
//we need to add project's source folders onto a debugger's search path
507
AntProjectHelper serverHelper = wss.getAntProjectHelper();
508                                 String JavaDoc dcp = serverHelper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.DEBUG_CLASSPATH);
509                                 if (dcp != null) {
510                                     String JavaDoc[] pathTokens = PropertyUtils.tokenizePath(dcp);
511                                     for (int i = 0; i < pathTokens.length; i++) {
512                                         File JavaDoc f = new File JavaDoc(pathTokens[i]);
513                                         if (!f.isAbsolute())
514                                             pathTokens[i] = serverProject.getProjectDirectory().getPath() + "/" + pathTokens[i];
515                                         clientDCP.append(pathTokens[i] + ":");
516                                     }
517                                 }
518
519                                 String JavaDoc wdd = serverHelper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
520                                 if (wdd != null) {
521                                     String JavaDoc[] pathTokens = PropertyUtils.tokenizePath(wdd);
522                                     for (int i = 0; i < pathTokens.length; i++) {
523                                         File JavaDoc f = new File JavaDoc(pathTokens[i]);
524                                         if (!f.isAbsolute())
525                                             pathTokens[i] = serverProject.getProjectDirectory().getPath() + "/" + pathTokens[i];
526                                         clientWDD.append(pathTokens[i] + ":");
527                                     }
528                                 }
529                             }
530                         }
531                     }
532                 }
533                 p.setProperty(WebProjectProperties.WS_DEBUG_CLASSPATHS, clientDCP.toString());
534                 p.setProperty(WebProjectProperties.WS_WEB_DOCBASE_DIRS, clientWDD.toString());
535             }
536
537         } else if (command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) {
538             FileObject[] files = findJavaSources(context);
539             String JavaDoc path = null;
540             if (files != null) {
541                 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]);
542                 targetNames = new String JavaDoc[] {"debug-fix"}; // NOI18N
543
} else {
544                 return null;
545             }
546             // Convert foo/FooTest.java -> foo/FooTest
547
if (path.endsWith(".java")) { // NOI18N
548
path = path.substring(0, path.length() - 5);
549             }
550             p.setProperty("fix.includes", path); // NOI18N
551

552         //COMPILATION PART
553
} else if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
554             FileObject[] sourceRoots = project.getSourceRoots().getRoots();
555             FileObject[] files = findJavaSourcesAndPackages( context, sourceRoots);
556             boolean recursive = (context.lookup(NonRecursiveFolder.class) == null);
557             if (files != null) {
558                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots, files[0]), recursive)); // NOI18N
559
} else {
560                 FileObject[] testRoots = project.getTestSourceRoots().getRoots();
561                 files = findJavaSourcesAndPackages(context, testRoots);
562                 if (files != null) {
563                     p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); // NOI18N
564
targetNames = new String JavaDoc[] {"compile-test-single"}; // NOI18N
565
} else {
566                     files = findJsps (context);
567                     if (files != null) {
568                         for (int i=0; i < files.length; i++) {
569                             FileObject jsp = files[i];
570                             if (areIncludesModified(jsp)) {
571                                 invalidateClassFile(project, jsp);
572                             }
573                         }
574                         setAllPropertiesForSingleJSPCompilation(p, files);
575                         targetNames = new String JavaDoc [] {"compile-single-jsp"};
576                     } else {
577                         return null;
578                     }
579                 }
580             }
581
582         //TEST PART
583
} else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
584             FileObject[] files = findTestSourcesForSources(context);
585             targetNames = setupTestSingle(p, files);
586         } else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
587             FileObject[] files = findTestSourcesForSources(context);
588             targetNames = setupDebugTestSingle(p, files);
589         } else {
590             if (targetNames == null) {
591                 throw new IllegalArgumentException JavaDoc(command);
592             }
593         }
594         return targetNames;
595     }
596
597     private String JavaDoc[] setupTestSingle(Properties JavaDoc p, FileObject[] files) {
598         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
599         FileObject root = getRoot(testSrcPath, files[0]);
600         p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
601
p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
602
return new String JavaDoc[] {"test-single"}; // NOI18N
603
}
604
605     private String JavaDoc[] setupDebugTestSingle(Properties JavaDoc p, FileObject[] files) {
606         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
607         FileObject root = getRoot(testSrcPath, files[0]);
608         String JavaDoc path = FileUtil.getRelativePath(root, files[0]);
609         // Convert foo/FooTest.java -> foo.FooTest
610
p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); // NOI18N
611
return new String JavaDoc[] {"debug-test"}; // NOI18N
612
}
613
614     /* Deletes translated class/java file to force recompilation of the page with all includes
615      */

616     public void invalidateClassFile(WebProject wp, FileObject jsp) {
617         String JavaDoc dir = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.BUILD_GENERATED_DIR);
618         if (dir == null) {
619             return;
620         }
621         dir = dir + "/src"; //NOI18N
622
WebModule wm = WebModule.getWebModule(jsp);
623         if (wm == null) {
624             return;
625         }
626         String JavaDoc name = JspNameUtil.getServletName(wm.getDocumentBase(), jsp);
627         if (name == null) {
628             return;
629         }
630         String JavaDoc filePath = name.substring(0, name.lastIndexOf('.')).replace('.', '/');
631         
632         String JavaDoc fileClass = dir + '/' + filePath + ".class"; //NOI18N
633
String JavaDoc fileJava = dir + '/' + filePath + ".java"; //NOI18N
634

635         File JavaDoc fC = updateHelper.getAntProjectHelper().resolveFile(fileClass);
636         File JavaDoc fJ = updateHelper.getAntProjectHelper().resolveFile(fileJava);
637         if ((fJ != null) && (fJ.exists())) {
638             fJ.delete();
639         }
640         if ((fC != null) && (fC.exists())) {
641             fC.delete();
642         }
643     }
644     
645     /* checks if timestamp of any of the included pages is higher than the top page
646      */

647     public boolean areIncludesModified(FileObject jsp){
648         boolean modified = false;
649         WebModule wm = WebModule.getWebModule(jsp);
650         JspParserAPI jspParser = JspParserFactory.getJspParser();
651         JspParserAPI.ParseResult result = jspParser.analyzePage(jsp, new ParserWebModule(wm), JspParserAPI.ERROR_IGNORE);
652         if (!result.isParsingSuccess()) {
653             modified = true;
654         } else {
655             List JavaDoc includes = result.getPageInfo().getDependants();
656             if ((includes != null) && (includes.size() > 0)) {
657                 long jspTS = jsp.lastModified().getTime();
658                 int size = includes.size();
659                 for (int i=0; i<size; i++) {
660                     String JavaDoc filename = (String JavaDoc)includes.get(i);
661                     filename = FileUtil.toFile(wm.getDocumentBase()).getPath() + filename;
662                     File JavaDoc f = new File JavaDoc(filename);
663                     long incTS = f.lastModified();
664                     if (incTS > jspTS) {
665                         modified = true;
666                         break;
667                     }
668                 }
669             }
670         }
671         return modified;
672     }
673     
674     // PENDING - should not this be in some kind of an API?
675
private boolean decodeBoolean(String JavaDoc raw) {
676         if ( raw != null ) {
677            String JavaDoc lowecaseRaw = raw.toLowerCase();
678                
679            if ( lowecaseRaw.equals( "true") || // NOI18N
680
lowecaseRaw.equals( "yes") || // NOI18N
681
lowecaseRaw.equals( "enabled") ) // NOI18N
682
return true;
683         }
684             
685         return false;
686     }
687     
688     private void setAllPropertiesForSingleJSPCompilation(Properties JavaDoc p, FileObject[] files) {
689         p.setProperty("jsp.includes", getBuiltJspFileNamesAsPath(files)); // NOI18N
690
/*ActionUtils.antIncludesList(files, project.getWebModule ().getDocumentBase ())*/
691         
692         p.setProperty("javac.jsp.includes", getCommaSeparatedGeneratedJavaFiles(files)); // NOI18N
693

694     }
695     
696     public String JavaDoc getCommaSeparatedGeneratedJavaFiles(FileObject[] jspFiles) {
697         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
698         for (int i = 0; i < jspFiles.length; i++) {
699             String JavaDoc jspRes = getJspResource(jspFiles[i]);
700             if (i > 0) {
701                 b.append(',');
702             }
703             b.append(Utils.getGeneratedJavaResource(jspRes));
704         }
705         return b.toString();
706     }
707     
708     /** Returns a resource name for a given JSP separated by / (does not start with a /).
709      */

710     private String JavaDoc getJspResource(FileObject jsp) {
711         ProjectWebModule pwm = project.getWebModule ();
712         FileObject webDir = pwm.getDocumentBase ();
713         return FileUtil.getRelativePath(webDir, jsp);
714     }
715     
716     public File JavaDoc getBuiltJsp(FileObject jsp) {
717         ProjectWebModule pwm = project.getWebModule ();
718         FileObject webDir = pwm.getDocumentBase ();
719         String JavaDoc relFile = FileUtil.getRelativePath(webDir, jsp).replace('/', File.separatorChar);
720         File JavaDoc webBuildDir = pwm.getContentDirectoryAsFile();
721         return new File JavaDoc(webBuildDir, relFile);
722     }
723     
724     public String JavaDoc getBuiltJspFileNamesAsPath(FileObject[] files) {
725         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
726         for (int i = 0; i < files.length; i++) {
727             String JavaDoc path = getBuiltJsp(files[i]).getAbsolutePath();
728             if (i > 0) {
729                 b.append(File.pathSeparator);
730             }
731             b.append(path);
732         }
733         return b.toString();
734     }
735     
736     
737     public boolean isActionEnabled( String JavaDoc command, Lookup context ) {
738         FileObject buildXml = findBuildXml();
739         if (buildXml == null || !buildXml.isValid()) {
740             return false;
741         }
742         if ( command.equals( COMMAND_DEBUG_SINGLE ) ) {
743             return findJavaSources(context) != null || findJsps(context) != null || findHtml(context) != null || findTestSources(context, false) != null;
744         }
745         else if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
746             return findJavaSourcesAndPackages(context, project.getSourceRoots().getRoots()) != null
747                    || findJavaSourcesAndPackages(context, project.getTestSourceRoots().getRoots()) != null
748                    || findJsps (context) != null;
749         }
750         else if ( command.equals( COMMAND_VERIFY ) ) {
751             return project.getWebModule().hasVerifierSupport();
752         }
753         else if ( command.equals( COMMAND_RUN_SINGLE ) ) {
754             // test for jsps
755
FileObject files [] = findJsps (context);
756             if (files != null && files.length >0) return true;
757             // test for html pages
758
files = findHtml(context);
759             if (files != null && files.length >0) return true;
760             // test for servlets
761
FileObject[] javaFiles = findJavaSources(context);
762             if (javaFiles!=null && javaFiles.length > 0) {
763                 if (javaFiles[0].getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI)!=null)
764                     return true;
765                 else if (Boolean.TRUE.equals(javaFiles[0].getAttribute("org.netbeans.modules.web.IsServletFile"))) //NOI18N
766
return true;
767                 else if (isDDServlet(context, javaFiles[0])) {
768                     try {
769                         javaFiles[0].setAttribute("org.netbeans.modules.web.IsServletFile",Boolean.TRUE); //NOI18N
770
} catch (IOException JavaDoc ex){}
771                     return true;
772                 } else return true; /* because of java main classes, otherwise we would return false */
773             }
774             javaFiles = findTestSources(context,false);
775             if ((javaFiles != null) && (javaFiles.length > 0)) {
776                 return true;
777             }
778             return false;
779         }
780         else if ( command.equals( COMMAND_TEST_SINGLE )) {
781             return findTestSourcesForSources(context) != null;
782         }
783         else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
784             FileObject[] files = findTestSourcesForSources(context);
785             return files != null && files.length == 1;
786         }
787         else {
788             // other actions are global
789
return true;
790         }
791
792         
793     }
794     
795     // Private methods -----------------------------------------------------
796

797     private static final String JavaDoc SUBST = "Test.java"; // NOI18N
798

799     /*
800      * copied from ActionUtils and reworked so that it checks for mimeType of files, and DOES NOT include files with suffix 'suffix'
801      */

802     private static FileObject[] findSelectedFilesByMimeType(Lookup context, FileObject dir, String JavaDoc mimeType, String JavaDoc suffix, boolean strict) {
803         if (dir != null && !dir.isFolder()) {
804             throw new IllegalArgumentException JavaDoc("Not a folder: " + dir); // NOI18N
805
}
806         List JavaDoc/*<FileObject>*/ files = new ArrayList JavaDoc();
807         Iterator JavaDoc it = context.lookup(new Lookup.Template(DataObject.class)).allInstances().iterator();
808         while (it.hasNext()) {
809             DataObject d = (DataObject)it.next();
810             FileObject f = d.getPrimaryFile();
811             boolean matches = FileUtil.toFile(f) != null;
812             if (dir != null) {
813                 matches &= (FileUtil.isParentOf(dir, f) || dir == f);
814             }
815             if (mimeType != null) {
816                 matches &= f.getMIMEType().equals(mimeType);
817             }
818             if (suffix != null) {
819                 matches &= !f.getNameExt().endsWith(suffix);
820             }
821             // Generally only files from one project will make sense.
822
// Currently the action UI infrastructure (PlaceHolderAction)
823
// checks for that itself. Should there be another check here?
824
if (matches) {
825                 files.add(f);
826             } else if (strict) {
827                 return null;
828             }
829         }
830         if (files.isEmpty()) {
831             return null;
832         }
833         return (FileObject[])files.toArray(new FileObject[files.size()]);
834     }
835         
836     private static final Pattern JavaDoc SRCDIRJAVA = Pattern.compile("\\.java$"); // NOI18N
837

838     /** Find selected java sources
839      */

840     private FileObject[] findJavaSources(Lookup context) {
841         FileObject[] srcPath = project.getSourceRoots().getRoots();
842         for (int i=0; i< srcPath.length; i++) {
843             FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); // NOI18N
844
if (files != null) {
845                 return files;
846             }
847         }
848         return null;
849     }
850     
851     private FileObject[] findJavaSourcesAndPackages (Lookup context, FileObject srcDir) {
852         if (srcDir != null) {
853             FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); // NOI18N
854
//Check if files are either packages of java files
855
if (files != null) {
856                 for (int i = 0; i < files.length; i++) {
857                     if (!files[i].isFolder() && !"java".equals(files[i].getExt())) {
858                         return null;
859                     }
860                 }
861             }
862             return files;
863         } else {
864             return null;
865         }
866     }
867     
868     private FileObject[] findJavaSourcesAndPackages (Lookup context, FileObject[] srcRoots) {
869         for (int i=0; i<srcRoots.length; i++) {
870             FileObject[] result = findJavaSourcesAndPackages(context, srcRoots[i]);
871             if (result != null) {
872                 return result;
873             }
874         }
875         return null;
876     }
877     
878     private FileObject[] findHtml(Lookup context) {
879         FileObject webDir = project.getWebModule ().getDocumentBase ();
880         FileObject[] files = null;
881         if (webDir != null) {
882             files = findSelectedFilesByMimeType(context, webDir, "text/html", null, true);
883         }
884         return files;
885     }
886     
887     /** Find selected jsps
888      */

889     private FileObject[] findJsps(Lookup context) {
890         FileObject webDir = project.getWebModule ().getDocumentBase ();
891         FileObject[] files = null;
892         if (webDir != null) {
893             files = findSelectedFilesByMimeType(context, webDir, "text/x-jsp", ".jspf", true);
894         }
895         return files;
896     }
897  
898     /** Find either selected tests or tests which belong to selected source files
899      */

900     private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) {
901         //XXX: Ugly, should be rewritten
902
FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
903         for (int i=0; i< testSrcPath.length; i++) {
904             FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); // NOI18N
905
if (files != null) {
906                 return files;
907             }
908         }
909         if (checkInSrcDir && testSrcPath.length>0) {
910             FileObject[] files = findSources (context);
911             if (files != null) {
912                 //Try to find the test under the test roots
913
FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]);
914                 for (int i=0; i<testSrcPath.length; i++) {
915                     FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true);
916                     if (files2 != null) {
917                         return files2;
918                     }
919                 }
920             }
921         }
922         return null;
923     }
924     
925     private boolean isEjbRefAndNoJ2eeApp(Project p) {
926
927         WebModule wmod = WebModule.getWebModule(p.getProjectDirectory());
928         if (wmod != null) {
929             WebApp webXml = null;
930             try {
931                 FileObject webXmlFo = wmod.getDeploymentDescriptor();
932                 if (webXmlFo==null) return false;
933                 webXml = DDProvider.getDefault().getMergedDDRoot(webXmlFo);
934             } catch (IOException JavaDoc ioe) {
935                 // ignore
936
}
937             if (webXml != null) {
938                 EjbLocalRef[] ejbLocalRefs = webXml.getEjbLocalRef();
939                 if ((ejbLocalRefs != null) && (ejbLocalRefs.length > 0)) { // there's an ejb reference in this module
940
if (!isInJ2eeApp(p)) {
941                         return true;
942                     }
943                 }
944             }
945         }
946         return false;
947     }
948     
949     private boolean isInJ2eeApp(Project p) {
950         Set JavaDoc globalPath = GlobalPathRegistry.getDefault().getSourceRoots();
951         Iterator JavaDoc iter = globalPath.iterator();
952         while (iter.hasNext()) {
953             FileObject sourceRoot = (FileObject)iter.next();
954             Project project = FileOwnerQuery.getOwner(sourceRoot);
955             if (project != null) {
956                 Object JavaDoc j2eeAppProvider = project.getLookup().lookup(J2eeAppProvider.class);
957                 if (j2eeAppProvider != null) { // == it is j2ee app
958
J2eeAppProvider j2eeApp = (J2eeAppProvider)j2eeAppProvider;
959                     J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
960                     if ((j2eeModules != null) && (j2eeModules.length > 0)) { // == there are some modules in the j2ee app
961
J2eeModuleProvider affectedPrjProvider =
962                                 (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class);
963                         if (affectedPrjProvider != null) {
964                             if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) {
965                                 return true;
966                             }
967                         }
968                     }
969                 }
970             }
971         }
972         return false;
973     }
974     
975     
976     private boolean isDebugged() {
977         J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
978         Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
979         ServerDebugInfo sdi = null;
980         
981         if (sessions != null && sessions.length > 0) {
982             sdi = jmp.getServerDebugInfo ();
983             if (sdi == null)
984                 return false;
985         }
986         
987         if (sessions != null) {
988             for (int i=0; i < sessions.length; i++) {
989                 Session s = sessions[i];
990                 if (s != null) {
991                     Object JavaDoc o = s.lookupFirst(null, AttachingDICookie.class);
992                     if (o != null) {
993                         AttachingDICookie attCookie = (AttachingDICookie)o;
994                         if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) {
995                             String JavaDoc shmem = attCookie.getSharedMemoryName();
996                             if (shmem == null) continue;
997                             if (shmem.equalsIgnoreCase(sdi.getShmemName()))
998                                 return true;
999                         } else {
1000                            String JavaDoc hostname = attCookie.getHostName();
1001                            if (hostname == null) continue;
1002                            if (hostname.equalsIgnoreCase(sdi.getHost()))
1003                                if (attCookie.getPortNumber() == sdi.getPort())
1004                                    return true;
1005                        }
1006                    }
1007                }
1008            }
1009        }
1010        return false;
1011    }
1012    
1013    private boolean isSelectedServer () {
1014        String JavaDoc instance = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_INSTANCE);
1015        if (instance != null) {
1016            J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
1017            String JavaDoc sdi = jmp.getServerInstanceID();
1018            if (sdi != null) {
1019                String JavaDoc id = Deployment.getDefault().getServerID(sdi);
1020                if (id != null) {
1021                    return true;
1022                }
1023            }
1024        }
1025        
1026        // if there is some server instance of the type which was used
1027
// previously do not ask and use it
1028
String JavaDoc serverType = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_TYPE);
1029        if (serverType != null) {
1030            String JavaDoc[] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType);
1031            if (servInstIDs.length > 0) {
1032                setServerInstance(servInstIDs[0]);
1033                return true;
1034            }
1035        }
1036        
1037        // no selected server => warning
1038
String JavaDoc msg = NbBundle.getMessage(WebActionProvider.class, "MSG_No_Server_Selected"); // NOI18N
1039
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
1040        return false;
1041    }
1042    
1043    private void setServerInstance(String JavaDoc serverInstanceId) {
1044        WebProjectProperties.setServerInstance(project, updateHelper, serverInstanceId);
1045    }
1046    
1047    private boolean isDDServlet(Lookup context, FileObject javaClass) {
1048        FileObject webDir = project.getWebModule ().getDocumentBase ();
1049        if (webDir==null) return false;
1050        FileObject fo = webDir.getFileObject("WEB-INF/web.xml"); //NOI18N
1051
String JavaDoc className = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(), javaClass), javaClass);
1052        if (fo==null) return false;
1053        try {
1054            WebApp webApp = DDProvider.getDefault().getDDRoot(fo);
1055            Servlet servlet = (Servlet)webApp.findBeanByName("Servlet","ServletClass",className); //NOI18N
1056
if (servlet!=null) return true;
1057            else return false;
1058        } catch (IOException JavaDoc ex) {return false;}
1059    }
1060    
1061    /** Find tests corresponding to selected sources.
1062     */

1063    private FileObject[] findTestSourcesForSources(Lookup context) {
1064        FileObject[] sourceFiles = findSources(context);
1065        if (sourceFiles == null) {
1066            return null;
1067        }
1068        FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
1069        if (testSrcPath.length == 0) {
1070            return null;
1071        }
1072        FileObject[] srcPath = project.getSourceRoots().getRoots();
1073        FileObject srcDir = getRoot(srcPath, sourceFiles[0]);
1074        for (int i=0; i<testSrcPath.length; i++) {
1075            FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true);
1076            if (files2 != null) {
1077                return files2;
1078            }
1079        }
1080        return null;
1081    }
1082
1083    private FileObject getRoot (FileObject[] roots, FileObject file) {
1084        FileObject srcDir = null;
1085        for (int i=0; i< roots.length; i++) {
1086            if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) {
1087                srcDir = roots[i];
1088                break;
1089            }
1090        }
1091        return srcDir;
1092    }
1093
1094    /** Find selected sources, the sources has to be under single source root,
1095     * @param context the lookup in which files should be found
1096     */

1097    private FileObject[] findSources(Lookup context) {
1098        FileObject[] srcPath = project.getSourceRoots().getRoots();
1099        for (int i=0; i< srcPath.length; i++) {
1100            FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); // NOI18N
1101
if (files != null) {
1102                return files;
1103            }
1104        }
1105        return null;
1106    }
1107
1108    
1109    
1110    
1111}
1112
Popular Tags