1 19 20 package org.netbeans.modules.web.project; 21 22 import java.io.IOException ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.Set ; 32 import java.util.regex.Pattern ; 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 ; 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 83 class WebActionProvider implements ActionProvider { 84 85 87 private static final String COMMAND_COMPILE = "compile"; private static final String COMMAND_VERIFY = "verify"; 90 private static final String [] 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 WebProject project; 116 117 private UpdateHelper updateHelper; 119 120 121 Map commands; 122 123 public WebActionProvider(WebProject project, UpdateHelper updateHelper) { 124 125 commands = new HashMap (); 126 commands.put(COMMAND_BUILD, new String [] {"dist"}); commands.put(COMMAND_CLEAN, new String [] {"clean"}); commands.put(COMMAND_REBUILD, new String [] {"clean", "dist"}); commands.put(COMMAND_COMPILE_SINGLE, new String [] {"compile-single"}); commands.put(COMMAND_RUN, new String [] {"run"}); commands.put(COMMAND_RUN_SINGLE, new String [] {"run"}); commands.put(WebProjectConstants.COMMAND_REDEPLOY, new String [] {"run-deploy"}); commands.put(COMMAND_DEBUG, new String [] {"debug"}); commands.put(COMMAND_DEBUG_SINGLE, new String [] {"debug"}); commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String [] {"javadoc"}); commands.put(COMMAND_TEST, new String [] {"test"}); commands.put(COMMAND_TEST_SINGLE, new String [] {"test-single"}); commands.put(COMMAND_DEBUG_TEST_SINGLE, new String [] {"debug-test"}); commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String [] {"debug-fix"}); commands.put(COMMAND_COMPILE, new String [] {"compile"}); commands.put(COMMAND_VERIFY, new String [] {"verify"}); 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 [] getSupportedActions() { 155 return supportedActions; 156 } 157 158 public void invokeAction( final String command, final Lookup context ) throws IllegalArgumentException { 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 action = new Runnable () { 180 public void run () { 181 Properties p = new Properties (); 182 String [] 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 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 e) { 207 ErrorManager.getDefault().notify(e); 208 } 209 } 210 }; 211 212 action.run(); 213 } 214 215 218 String [] getTargetNames(String command, Lookup context, Properties p) throws IllegalArgumentException { 219 String [] targetNames = (String [])commands.get(command); 220 221 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 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"); } else { 243 p.setProperty("forceRedeploy", "false"); } 245 files = findJsps( context ); 247 if (files!=null && files.length>0) { 248 String raw = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.COMPILE_JSPS); 250 boolean compile = decodeBoolean(raw); 251 if (!compile) { 252 setAllPropertiesForSingleJSPCompilation(p, files); 253 } 254 255 String requestParams = RequestParametersQuery.getFileAndParameters(files [0]); 256 if (requestParams != null) { 257 p.setProperty("client.urlPart", requestParams); } else { 259 return null; 260 } 261 } else { 262 FileObject[] htmlFiles = findHtml(context); 264 if ((htmlFiles != null) && (htmlFiles.length>0)) { 265 String url = "/" + FileUtil.getRelativePath(WebModule.getWebModule (htmlFiles[0]).getDocumentBase (), htmlFiles[0]); if (url != null) { 267 url = org.openide.util.Utilities.replaceString(url, " ", "%20"); 268 p.setProperty("client.urlPart", url); } else { 270 return null; 271 } 272 } else { 273 FileObject[] javaFiles = findJavaSources(context); 275 if ((javaFiles != null) && (javaFiles.length>0)) { 276 FileObject javaFile = javaFiles[0]; 277 if (!SourceUtils.getMainClasses(javaFile).isEmpty()) { 278 String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),javaFile), javaFile); 280 p.setProperty("javac.includes", clazz); if (clazz.endsWith(".java")) { clazz = clazz.substring(0, clazz.length() - 5); 284 } 285 clazz = clazz.replace('/','.'); 286 287 p.setProperty("run.class", clazz); targetNames = new String [] {"run-main"}; 289 } 290 else { 291 String executionUri = (String )javaFile.getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI); 294 if (executionUri!=null) { 295 p.setProperty("client.urlPart", executionUri); } else { 297 WebModule webModule = WebModule.getWebModule(javaFile); 298 String [] 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 res = DialogDisplayer.getDefault().notify(desc); 304 if (res.equals(NotifyDescriptor.YES_OPTION)) { 305 p.setProperty("client.urlPart", uriPanel.getServletUri()); try { 307 javaFile.setAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI,uriPanel.getServletUri()); 308 } catch (IOException ex){} 309 } else return null; 310 } else { 311 String mes = java.text.MessageFormat.format ( 312 NbBundle.getMessage (WebActionProvider.class, "TXT_noExecutableClass"), 313 new Object [] {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 } 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 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"); } else { 347 p.setProperty("forceRedeploy", "false"); } 349 } 350 351 } 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 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 String raw = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.COMPILE_JSPS); 376 boolean compile = decodeBoolean(raw); 377 if (!compile) { 378 setAllPropertiesForSingleJSPCompilation(p, files); 379 } 380 381 String requestParams = RequestParametersQuery.getFileAndParameters(files [0]); 382 if (requestParams != null) { 383 p.setProperty("client.urlPart", requestParams); } else { 385 return null; 386 } 387 } else { 388 FileObject[] htmlFiles = findHtml(context); 390 if ((htmlFiles != null) && (htmlFiles.length>0)) { 391 String url = "/" + FileUtil.getRelativePath(WebModule.getWebModule (htmlFiles[0]).getDocumentBase (), htmlFiles[0]); if (url != null) { 393 url = org.openide.util.Utilities.replaceString(url, " ", "%20"); 394 p.setProperty("client.urlPart", url); } else { 396 return null; 397 } 398 } else { 399 FileObject[] javaFiles = findJavaSources(context); 402 if ((javaFiles != null) && (javaFiles.length>0)) { 403 FileObject javaFile = javaFiles[0]; 404 if (!SourceUtils.getMainClasses(javaFile).isEmpty()) { 405 String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),javaFile), javaFile); 407 p.setProperty("javac.includes", clazz); if (clazz.endsWith(".java")) { clazz = clazz.substring(0, clazz.length() - 5); 411 } 412 clazz = clazz.replace('/','.'); 413 414 p.setProperty("debug.class", clazz); targetNames = new String [] {"debug-single-main"}; 416 } 417 else { 418 String executionUri = (String )javaFile.getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI); 421 if (executionUri!=null) { 422 p.setProperty("client.urlPart", executionUri); } else { 424 WebModule webModule = WebModule.getWebModule(javaFile); 425 String [] 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 res = DialogDisplayer.getDefault().notify(desc); 431 if (res.equals(NotifyDescriptor.YES_OPTION)) { 432 p.setProperty("client.urlPart", uriPanel.getServletUri()); try { 434 javaFile.setAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI,uriPanel.getServletUri()); 435 } catch (IOException ex){} 436 } else return null; 437 } else { 438 String mes = java.text.MessageFormat.format ( 439 NbBundle.getMessage (WebActionProvider.class, "TXT_missingServletMappings"), 440 new Object [] {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 } else if (command.equals (COMMAND_DEBUG)) { 454 if (!isSelectedServer ()) { 455 return null; 456 } 457 if (isDebugged()) { 458 p.setProperty("is.debugged", "true"); 459 } 460 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) { List serviceClients = wscs.getServiceClients(); 471 HashSet scNames = new HashSet (); 473 for (Iterator scIt = serviceClients.iterator(); scIt.hasNext(); ) { 474 WsCompileClientEditorSupport.ServiceSettings serviceClientSettings = 475 (WsCompileClientEditorSupport.ServiceSettings)scIt.next(); 476 scNames.add(serviceClientSettings.getServiceName()); 477 } 478 479 StringBuffer clientDCP = new StringBuffer (); StringBuffer clientWDD = new StringBuffer (); 482 Set globalPath = GlobalPathRegistry.getDefault().getSourceRoots(); 484 HashSet serverNames = new HashSet (); 485 for (Iterator 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())) continue; 492 WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(serverProject.getProjectDirectory()); 493 if (wss != null) { List services = wss.getServices(); 495 boolean match = false; 496 for (Iterator sIt = services.iterator(); sIt.hasNext(); ) { 497 WsCompileEditorSupport.ServiceSettings serviceSettings = 498 (WsCompileEditorSupport.ServiceSettings)sIt.next(); 499 String serviceName = serviceSettings.getServiceName(); 500 if (scNames.contains(serviceName)) { match = true; 502 break; } 504 } 505 if (match) { AntProjectHelper serverHelper = wss.getAntProjectHelper(); 508 String dcp = serverHelper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.DEBUG_CLASSPATH); 509 if (dcp != null) { 510 String [] pathTokens = PropertyUtils.tokenizePath(dcp); 511 for (int i = 0; i < pathTokens.length; i++) { 512 File f = new File (pathTokens[i]); 513 if (!f.isAbsolute()) 514 pathTokens[i] = serverProject.getProjectDirectory().getPath() + "/" + pathTokens[i]; 515 clientDCP.append(pathTokens[i] + ":"); 516 } 517 } 518 519 String wdd = serverHelper.getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 520 if (wdd != null) { 521 String [] pathTokens = PropertyUtils.tokenizePath(wdd); 522 for (int i = 0; i < pathTokens.length; i++) { 523 File f = new File (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 path = null; 540 if (files != null) { 541 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]); 542 targetNames = new String [] {"debug-fix"}; } else { 544 return null; 545 } 546 if (path.endsWith(".java")) { path = path.substring(0, path.length() - 5); 549 } 550 p.setProperty("fix.includes", path); 552 } 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)); } 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)); targetNames = new String [] {"compile-test-single"}; } 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 [] {"compile-single-jsp"}; 576 } else { 577 return null; 578 } 579 } 580 } 581 582 } 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 (command); 592 } 593 } 594 return targetNames; 595 } 596 597 private String [] setupTestSingle(Properties 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)); p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); return new String [] {"test-single"}; } 604 605 private String [] setupDebugTestSingle(Properties p, FileObject[] files) { 606 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 607 FileObject root = getRoot(testSrcPath, files[0]); 608 String path = FileUtil.getRelativePath(root, files[0]); 609 p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); return new String [] {"debug-test"}; } 613 614 616 public void invalidateClassFile(WebProject wp, FileObject jsp) { 617 String dir = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.BUILD_GENERATED_DIR); 618 if (dir == null) { 619 return; 620 } 621 dir = dir + "/src"; WebModule wm = WebModule.getWebModule(jsp); 623 if (wm == null) { 624 return; 625 } 626 String name = JspNameUtil.getServletName(wm.getDocumentBase(), jsp); 627 if (name == null) { 628 return; 629 } 630 String filePath = name.substring(0, name.lastIndexOf('.')).replace('.', '/'); 631 632 String fileClass = dir + '/' + filePath + ".class"; String fileJava = dir + '/' + filePath + ".java"; 635 File fC = updateHelper.getAntProjectHelper().resolveFile(fileClass); 636 File 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 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 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 filename = (String )includes.get(i); 661 filename = FileUtil.toFile(wm.getDocumentBase()).getPath() + filename; 662 File f = new File (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 private boolean decodeBoolean(String raw) { 676 if ( raw != null ) { 677 String lowecaseRaw = raw.toLowerCase(); 678 679 if ( lowecaseRaw.equals( "true") || lowecaseRaw.equals( "yes") || lowecaseRaw.equals( "enabled") ) return true; 683 } 684 685 return false; 686 } 687 688 private void setAllPropertiesForSingleJSPCompilation(Properties p, FileObject[] files) { 689 p.setProperty("jsp.includes", getBuiltJspFileNamesAsPath(files)); 691 692 p.setProperty("javac.jsp.includes", getCommaSeparatedGeneratedJavaFiles(files)); 694 } 695 696 public String getCommaSeparatedGeneratedJavaFiles(FileObject[] jspFiles) { 697 StringBuffer b = new StringBuffer (); 698 for (int i = 0; i < jspFiles.length; i++) { 699 String 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 710 private String getJspResource(FileObject jsp) { 711 ProjectWebModule pwm = project.getWebModule (); 712 FileObject webDir = pwm.getDocumentBase (); 713 return FileUtil.getRelativePath(webDir, jsp); 714 } 715 716 public File getBuiltJsp(FileObject jsp) { 717 ProjectWebModule pwm = project.getWebModule (); 718 FileObject webDir = pwm.getDocumentBase (); 719 String relFile = FileUtil.getRelativePath(webDir, jsp).replace('/', File.separatorChar); 720 File webBuildDir = pwm.getContentDirectoryAsFile(); 721 return new File (webBuildDir, relFile); 722 } 723 724 public String getBuiltJspFileNamesAsPath(FileObject[] files) { 725 StringBuffer b = new StringBuffer (); 726 for (int i = 0; i < files.length; i++) { 727 String 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 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 FileObject files [] = findJsps (context); 756 if (files != null && files.length >0) return true; 757 files = findHtml(context); 759 if (files != null && files.length >0) return true; 760 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"))) return true; 767 else if (isDDServlet(context, javaFiles[0])) { 768 try { 769 javaFiles[0].setAttribute("org.netbeans.modules.web.IsServletFile",Boolean.TRUE); } catch (IOException ex){} 771 return true; 772 } else return true; 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 return true; 790 } 791 792 793 } 794 795 797 private static final String SUBST = "Test.java"; 799 802 private static FileObject[] findSelectedFilesByMimeType(Lookup context, FileObject dir, String mimeType, String suffix, boolean strict) { 803 if (dir != null && !dir.isFolder()) { 804 throw new IllegalArgumentException ("Not a folder: " + dir); } 806 List files = new ArrayList (); 807 Iterator 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 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 SRCDIRJAVA = Pattern.compile("\\.java$"); 838 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); 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); 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 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 900 private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) { 901 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 903 for (int i=0; i< testSrcPath.length; i++) { 904 FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); if (files != null) { 906 return files; 907 } 908 } 909 if (checkInSrcDir && testSrcPath.length>0) { 910 FileObject[] files = findSources (context); 911 if (files != null) { 912 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 ioe) { 935 } 937 if (webXml != null) { 938 EjbLocalRef[] ejbLocalRefs = webXml.getEjbLocalRef(); 939 if ((ejbLocalRefs != null) && (ejbLocalRefs.length > 0)) { if (!isInJ2eeApp(p)) { 941 return true; 942 } 943 } 944 } 945 } 946 return false; 947 } 948 949 private boolean isInJ2eeApp(Project p) { 950 Set globalPath = GlobalPathRegistry.getDefault().getSourceRoots(); 951 Iterator 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 j2eeAppProvider = project.getLookup().lookup(J2eeAppProvider.class); 957 if (j2eeAppProvider != null) { J2eeAppProvider j2eeApp = (J2eeAppProvider)j2eeAppProvider; 959 J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders(); 960 if ((j2eeModules != null) && (j2eeModules.length > 0)) { 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 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 shmem = attCookie.getSharedMemoryName(); 996 if (shmem == null) continue; 997 if (shmem.equalsIgnoreCase(sdi.getShmemName())) 998 return true; 999 } else { 1000 String 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 instance = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_INSTANCE); 1015 if (instance != null) { 1016 J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 1017 String sdi = jmp.getServerInstanceID(); 1018 if (sdi != null) { 1019 String id = Deployment.getDefault().getServerID(sdi); 1020 if (id != null) { 1021 return true; 1022 } 1023 } 1024 } 1025 1026 String serverType = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_TYPE); 1029 if (serverType != null) { 1030 String [] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType); 1031 if (servInstIDs.length > 0) { 1032 setServerInstance(servInstIDs[0]); 1033 return true; 1034 } 1035 } 1036 1037 String msg = NbBundle.getMessage(WebActionProvider.class, "MSG_No_Server_Selected"); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE)); 1040 return false; 1041 } 1042 1043 private void setServerInstance(String 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"); String 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); if (servlet!=null) return true; 1057 else return false; 1058 } catch (IOException ex) {return false;} 1059 } 1060 1061 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 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); if (files != null) { 1102 return files; 1103 } 1104 } 1105 return null; 1106 } 1107 1108 1109 1110 1111} 1112 | Popular Tags |