1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 21 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.regex.Pattern ; 27 import org.apache.tools.ant.module.api.support.ActionUtils; 28 import org.netbeans.api.fileinfo.NonRecursiveFolder; 29 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 30 import org.netbeans.spi.project.ActionProvider; 31 import org.netbeans.spi.project.support.ant.AntProjectHelper; 32 import org.netbeans.spi.project.ui.support.DefaultProjectOperations; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.NbBundle; 36 import org.openide.util.Lookup; 37 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 38 import org.netbeans.api.debugger.*; 39 import org.netbeans.api.debugger.jpda.*; 40 import org.netbeans.api.java.project.JavaProjectConstants; 41 import org.netbeans.api.java.source.SourceUtils; 42 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 43 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 44 import org.netbeans.spi.project.support.ant.ReferenceHelper; 45 import org.openide.*; 46 47 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants; 48 49 52 class EjbJarActionProvider implements ActionProvider { 53 54 56 private static final String COMMAND_COMPILE = "compile"; private static final String COMMAND_VERIFY = "verify"; 59 private static final String [] supportedActions = { 61 COMMAND_BUILD, 62 COMMAND_CLEAN, 63 COMMAND_REBUILD, 64 COMMAND_COMPILE_SINGLE, 65 COMMAND_RUN, 66 COMMAND_RUN_SINGLE, 67 COMMAND_DEBUG, 68 COMMAND_DEBUG_SINGLE, 69 EjbProjectConstants.COMMAND_REDEPLOY, 70 JavaProjectConstants.COMMAND_JAVADOC, 71 COMMAND_TEST, 72 COMMAND_TEST_SINGLE, 73 COMMAND_DEBUG_TEST_SINGLE, 74 JavaProjectConstants.COMMAND_DEBUG_FIX, 75 COMMAND_COMPILE, 76 COMMAND_VERIFY, 77 COMMAND_DELETE, 78 COMMAND_COPY, 79 COMMAND_MOVE, 80 COMMAND_RENAME 81 }; 82 83 EjbJarProject project; 85 86 private final AntProjectHelper antProjectHelper; 88 private ReferenceHelper refHelper; 89 90 91 Map commands; 92 93 public EjbJarActionProvider(EjbJarProject project, AntProjectHelper antProjectHelper, ReferenceHelper refHelper) { 94 commands = new HashMap (); 95 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-main"}); commands.put(EjbProjectConstants.COMMAND_REDEPLOY, new String [] {"run"}); commands.put(COMMAND_DEBUG, 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"}); 111 this.antProjectHelper = antProjectHelper; 112 this.project = project; 113 this.refHelper = refHelper; 114 } 115 116 private FileObject findBuildXml() { 117 return project.getProjectDirectory().getFileObject(project.getBuildXmlName ()); 118 } 119 120 public String [] getSupportedActions() { 121 return supportedActions; 122 } 123 124 public void invokeAction(final String command, final Lookup context ) throws IllegalArgumentException { 125 if (COMMAND_DELETE.equals(command)) { 126 DefaultProjectOperations.performDefaultDeleteOperation(project); 127 return ; 128 } 129 130 if (COMMAND_COPY.equals(command)) { 131 DefaultProjectOperations.performDefaultCopyOperation(project); 132 return ; 133 } 134 135 if (COMMAND_MOVE.equals(command)) { 136 DefaultProjectOperations.performDefaultMoveOperation(project); 137 return ; 138 } 139 140 if (COMMAND_RENAME.equals(command)) { 141 DefaultProjectOperations.performDefaultRenameOperation(project, null); 142 return ; 143 } 144 145 Runnable action = new Runnable () { 146 public void run () { 147 Properties p = new Properties (); 148 String [] targetNames; 149 150 targetNames = getTargetNames(command, context, p); 151 if (targetNames == null) { 152 return; 153 } 154 if (targetNames.length == 0) { 155 targetNames = null; 156 } 157 if (p.keySet().size() == 0) { 158 p = null; 159 } 160 try { 161 FileObject buildFo = findBuildXml(); 162 if (buildFo == null || !buildFo.isValid()) { 163 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(EjbJarActionProvider.class, 165 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE); 166 DialogDisplayer.getDefault().notify(nd); 167 } 168 else { 169 ActionUtils.runTarget(buildFo, targetNames, p); 170 } 171 } 172 catch (IOException e) { 173 ErrorManager.getDefault().notify(e); 174 } 175 } 176 }; 177 178 action.run(); 179 } 180 181 184 String [] getTargetNames(String command, Lookup context, Properties p) throws IllegalArgumentException { 185 String [] targetNames = (String [])commands.get(command); 186 187 if (command.equals(COMMAND_RUN_SINGLE)) { 189 FileObject[] files = findTestSources(context, false); 190 if (files != null) { 191 targetNames = setupTestSingle(p, files); 192 } else { 193 FileObject[] javaFiles = findJavaSources(context); 195 if ((javaFiles != null) && (javaFiles.length>0)) { 196 FileObject file = javaFiles[0]; 197 String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),file), file); 198 p.setProperty("javac.includes", clazz); if (clazz.endsWith(".java")) { clazz = clazz.substring(0, clazz.length() - 5); 202 } 203 clazz = clazz.replace('/','.'); 204 205 if (!SourceUtils.getMainClasses(file).isEmpty()) { 206 p.setProperty("run.class", clazz); targetNames = (String []) commands.get(COMMAND_RUN_SINGLE); 208 } else { 209 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(EjbJarActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE); 210 DialogDisplayer.getDefault().notify(nd); 211 return null; 212 } 213 } else { 214 return null; 215 } 216 } 217 } else if (command.equals (COMMAND_RUN) || command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) { 218 if (!isSelectedServer ()) { 219 return null; 220 } 221 if (isDebugged()) { 222 p.setProperty("is.debugged", "true"); 223 } 224 if (command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) { 225 p.setProperty("forceRedeploy", "true"); } else { 227 p.setProperty("forceRedeploy", "false"); } 229 } else if (command.equals (COMMAND_DEBUG) || command.equals(COMMAND_DEBUG_SINGLE)) { 231 FileObject[] javaFiles = findJavaSources(context); 232 if (javaFiles != null && javaFiles.length == 1 && !SourceUtils.getMainClasses(javaFiles[0]).isEmpty()) { 233 FileObject javaFile = javaFiles[0]; 234 String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(), javaFile), javaFile); 236 p.setProperty("javac.includes", clazz); if (clazz.endsWith(".java")) { clazz = clazz.substring(0, clazz.length() - 5); 240 } 241 clazz = clazz.replace('/','.'); 242 p.setProperty("main.class", clazz); targetNames = new String [] {"debug-single-main"}; 244 return targetNames; 245 } 246 247 FileObject[] testFiles = findTestSources(context, false); 248 if (testFiles != null) { 249 targetNames = setupDebugTestSingle(p, testFiles); 250 } else { 251 if (!isSelectedServer()) { 252 return null; 253 } 254 if (isDebugged()) { 255 p.setProperty("is.debugged", "true"); 256 } 257 } 258 } else if (command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) { 260 FileObject[] files = findJavaSources(context); 261 String path = null; 262 if (files != null) { 263 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]); 264 targetNames = new String [] {"debug-fix"}; } else { 266 files = findTestSources(context, false); 267 path = FileUtil.getRelativePath(getRoot(project.getTestSourceRoots().getRoots(),files[0]), files[0]); 268 targetNames = new String [] {"debug-fix-test"}; } 270 if (path.endsWith(".java")) { path = path.substring(0, path.length() - 5); 273 } 274 p.setProperty("fix.includes", path); } else if ( command.equals( COMMAND_COMPILE_SINGLE ) ) { 277 FileObject[] sourceRoots = project.getSourceRoots().getRoots(); 278 FileObject[] files = findSourcesAndPackages( context, sourceRoots); 279 boolean recursive = (context.lookup(NonRecursiveFolder.class) == null); 280 if (files != null) { 281 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); } else { 283 FileObject[] testRoots = project.getTestSourceRoots().getRoots(); 284 files = findSourcesAndPackages(context, testRoots); 285 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); targetNames = new String [] {"compile-test-single"}; } 288 } else if ( command.equals( COMMAND_TEST_SINGLE ) ) { 290 FileObject[] files = findTestSourcesForSources(context); 291 targetNames = setupTestSingle(p, files); 292 } 293 else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) { 294 FileObject[] files = findTestSourcesForSources(context); 295 targetNames = setupDebugTestSingle(p, files); 296 } else { 297 if (targetNames == null) { 298 throw new IllegalArgumentException (command); 299 } 300 } 301 302 return targetNames; 303 } 304 305 private String [] setupTestSingle(Properties p, FileObject[] files) { 306 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 307 FileObject root = getRoot(testSrcPath, files[0]); 308 p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); return new String [] {"test-single"}; } 312 313 private String [] setupDebugTestSingle(Properties p, FileObject[] files) { 314 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 315 FileObject root = getRoot(testSrcPath, files[0]); 316 String path = FileUtil.getRelativePath(root, files[0]); 317 p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); return new String [] {"debug-test"}; } 321 322 public boolean isActionEnabled( String command, Lookup context ) { 323 FileObject buildXml = findBuildXml(); 324 if (buildXml == null || !buildXml.isValid()) { 325 return false; 326 } 327 if ( command.equals( COMMAND_VERIFY ) ) { 328 return project.getEjbModule().hasVerifierSupport(); 329 } 330 if ( command.equals( COMMAND_COMPILE_SINGLE ) ) { 331 return findSourcesAndPackages( context, project.getSourceRoots().getRoots()) != null 332 || findSourcesAndPackages( context, project.getTestSourceRoots().getRoots()) != null; 333 } 334 if ( command.equals( COMMAND_RUN_SINGLE ) ) { 335 FileObject[] javaFiles = findTestSources(context,false); 336 if ((javaFiles != null) && (javaFiles.length > 0)) { 337 return true; 338 } 339 FileObject files[] = findJavaSources(context); 340 return files != null && files.length == 1; 341 } 342 else if ( command.equals( COMMAND_TEST_SINGLE ) ) { 343 return findTestSourcesForSources(context) != null; 344 } 345 else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) { 346 FileObject[] files = findTestSourcesForSources(context); 347 return files != null && files.length == 1; 348 } else if (command.equals(COMMAND_DEBUG_SINGLE)) { 349 FileObject[] testFiles = findTestSources(context, false); 350 FileObject[] javaFiles = findJavaSources(context); 351 return ((testFiles != null && testFiles.length == 1) || 352 (javaFiles != null && javaFiles.length == 1)); 353 } else { 354 return true; 356 } 357 358 359 } 360 361 363 private static final String SUBST = "Test.java"; private static final Pattern SRCDIRJAVA = Pattern.compile("\\.java$"); 366 368 private FileObject[] findJavaSources(Lookup context) { 369 FileObject[] srcPath = project.getSourceRoots().getRoots(); 370 for (int i=0; i< srcPath.length; i++) { 371 FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); if (files != null) { 373 return files; 374 } 375 } 376 return null; 377 } 378 379 private FileObject[] findSourcesAndPackages (Lookup context, FileObject srcDir) { 380 if (srcDir != null) { 381 FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); if (files != null) { 384 for (int i = 0; i < files.length; i++) { 385 if (!files[i].isFolder() && !"java".equals(files[i].getExt())) { 386 return null; 387 } 388 } 389 } 390 return files; 391 } else { 392 return null; 393 } 394 } 395 396 private FileObject[] findSourcesAndPackages (Lookup context, FileObject[] srcRoots) { 397 for (int i=0; i<srcRoots.length; i++) { 398 FileObject[] result = findSourcesAndPackages(context, srcRoots[i]); 399 if (result != null) { 400 return result; 401 } 402 } 403 return null; 404 } 405 406 408 private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) { 409 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 411 for (int i=0; i< testSrcPath.length; i++) { 412 FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); if (files != null) { 414 return files; 415 } 416 } 417 if (checkInSrcDir && testSrcPath.length>0) { 418 FileObject[] files = findJavaSources (context); 419 if (files != null) { 420 FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]); 422 for (int i=0; i<testSrcPath.length; i++) { 423 FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true); 424 if (files2 != null) { 425 return files2; 426 } 427 } 428 } 429 } 430 return null; 431 } 432 433 435 private FileObject[] findTestSourcesForSources(Lookup context) { 436 FileObject[] sourceFiles = findJavaSources(context); 437 if (sourceFiles == null) { 438 return null; 439 } 440 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 441 if (testSrcPath.length == 0) { 442 return null; 443 } 444 FileObject[] srcPath = project.getSourceRoots().getRoots(); 445 FileObject srcDir = getRoot(srcPath, sourceFiles[0]); 446 for (int i=0; i<testSrcPath.length; i++) { 447 FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true); 448 if (files2 != null) { 449 return files2; 450 } 451 } 452 return null; 453 } 454 455 private FileObject getRoot (FileObject[] roots, FileObject file) { 456 FileObject srcDir = null; 457 for (int i=0; i< roots.length; i++) { 458 if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) { 459 srcDir = roots[i]; 460 break; 461 } 462 } 463 return srcDir; 464 } 465 466 private boolean isDebugged() { 467 468 J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 469 ServerDebugInfo sdi = jmp.getServerDebugInfo (); 470 if (sdi == null) { 471 return false; 472 } 473 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 475 476 for (int i=0; i < sessions.length; i++) { 477 Session s = sessions[i]; 478 if (s != null) { 479 Object o = s.lookupFirst(null, AttachingDICookie.class); 480 if (o != null) { 481 AttachingDICookie attCookie = (AttachingDICookie)o; 482 if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) { 483 if (attCookie.getSharedMemoryName().equalsIgnoreCase(sdi.getShmemName())) { 484 return true; 485 } 486 } else { 487 if (attCookie.getHostName().equalsIgnoreCase(sdi.getHost())) { 488 if (attCookie.getPortNumber() == sdi.getPort()) { 489 return true; 490 } 491 } 492 } 493 } 494 } 495 } 496 return false; 497 } 498 499 private boolean isSelectedServer () { 500 String instance = antProjectHelper.getStandardPropertyEvaluator ().getProperty (EjbJarProjectProperties.J2EE_SERVER_INSTANCE); 501 if (instance != null) { 502 String id = Deployment.getDefault().getServerID(instance); 503 if (id != null) { 504 return true; 505 } 506 } 507 508 String serverType = antProjectHelper.getStandardPropertyEvaluator ().getProperty (EjbJarProjectProperties.J2EE_SERVER_TYPE); 511 if (serverType != null) { 512 String [] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType); 513 if (servInstIDs.length > 0) { 514 setServerInstance(servInstIDs[0]); 515 return true; 516 } 517 } 518 519 String msg = NbBundle.getMessage(EjbJarActionProvider.class, "MSG_No_Server_Selected"); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE)); 522 return false; 523 } 524 525 private void setServerInstance(final String serverInstanceId) { 526 EjbJarProjectProperties.setServerInstance(project, antProjectHelper, serverInstanceId); 527 } 528 529 } 530 | Popular Tags |