1 19 20 package org.netbeans.modules.j2ee.earproject; 21 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 import org.apache.tools.ant.module.api.support.ActionUtils; 29 import org.netbeans.api.debugger.DebuggerManager; 30 import org.netbeans.api.debugger.Session; 31 import org.netbeans.api.debugger.jpda.AttachingDICookie; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants; 34 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 35 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 36 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo; 37 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties; 38 import org.netbeans.modules.web.api.webmodule.WebModule; 39 import org.netbeans.modules.web.spi.webmodule.WebModuleProvider; 40 import org.netbeans.spi.project.ActionProvider; 41 import org.netbeans.spi.project.SubprojectProvider; 42 import org.netbeans.spi.project.ui.support.DefaultProjectOperations; 43 import org.openide.DialogDisplayer; 44 import org.openide.ErrorManager; 45 import org.openide.NotifyDescriptor; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.util.Lookup; 49 import org.openide.util.NbBundle; 50 51 54 public class EarActionProvider implements ActionProvider { 55 56 private static final String COMMAND_COMPILE = "compile"; private static final String COMMAND_VERIFY = "verify"; 60 private static final String [] supportedActions = { 62 COMMAND_BUILD, 63 COMMAND_CLEAN, 64 COMMAND_REBUILD, 65 COMMAND_RUN, 66 COMMAND_DEBUG, 67 EjbProjectConstants.COMMAND_REDEPLOY, 68 COMMAND_VERIFY, 69 COMMAND_DELETE, 70 COMMAND_COPY, 71 COMMAND_MOVE, 72 COMMAND_RENAME 73 }; 74 75 EarProject project; 76 77 private final UpdateHelper updateHelper; 79 80 81 Map <String ,String []> commands; 82 83 public EarActionProvider(EarProject project, UpdateHelper updateHelper) { 84 commands = new HashMap <String , String []>(); 85 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_RUN, new String [] {"run"}); commands.put(COMMAND_DEBUG, new String [] {"debug"}); commands.put(EjbProjectConstants.COMMAND_REDEPLOY, new String [] {"run-deploy"}); commands.put(COMMAND_DEBUG, new String [] {"debug"}); commands.put(COMMAND_COMPILE, new String [] {"compile"}); commands.put(COMMAND_VERIFY, new String [] {"verify"}); 95 this.updateHelper = updateHelper; 96 this.project = project; 97 } 98 99 private FileObject findBuildXml() { 100 return project.getProjectDirectory().getFileObject(project.getBuildXmlName ()); 101 } 102 103 public String [] getSupportedActions() { 104 return supportedActions; 105 } 106 107 public void invokeAction( final String command, final Lookup context ) throws IllegalArgumentException { 108 if (COMMAND_DELETE.equals(command)) { 109 DefaultProjectOperations.performDefaultDeleteOperation(project); 110 return ; 111 } 112 113 if (COMMAND_COPY.equals(command)) { 114 DefaultProjectOperations.performDefaultCopyOperation(project); 115 return ; 116 } 117 118 if (COMMAND_MOVE.equals(command)) { 119 DefaultProjectOperations.performDefaultMoveOperation(project); 120 return ; 121 } 122 123 if (COMMAND_RENAME.equals(command)) { 124 DefaultProjectOperations.performDefaultRenameOperation(project, null); 125 return ; 126 } 127 128 Runnable action = new Runnable () { 129 public void run () { 130 Properties p = new Properties (); 131 String [] targetNames; 132 133 targetNames = getTargetNames(command, context, p); 134 if (targetNames == null) { 135 return; 136 } 137 if (targetNames.length == 0) { 138 targetNames = null; 139 } 140 if (p.keySet().size() == 0) { 141 p = null; 142 } 143 try { 144 ActionUtils.runTarget(findBuildXml(), targetNames, p); 145 } 146 catch (IOException e) { 147 ErrorManager.getDefault().notify(e); 148 } 149 } 150 }; 151 152 action.run(); 153 } 154 155 158 String [] getTargetNames(String command, Lookup context, Properties p) throws IllegalArgumentException { 159 String [] targetNames = commands.get(command); 160 161 if (command.equals (COMMAND_RUN) || command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) { if (!isSelectedServer ()) { 164 return null; 165 } 166 if (isDebugged()) { 167 p.setProperty("is.debugged", "true"); } 169 if (command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) { 170 p.setProperty("forceRedeploy", "true"); } else { 172 p.setProperty("forceRedeploy", "false"); } 174 } else if (command.equals (COMMAND_DEBUG)) { 176 if (!isSelectedServer ()) { 177 return null; 178 } 179 180 if (project.evaluator().getProperty("app.client") != null) { NotifyDescriptor nd; 183 nd = new NotifyDescriptor.Message(NbBundle.getMessage(EarActionProvider.class, "MSG_Server_State_Question"), NotifyDescriptor.QUESTION_MESSAGE); 184 nd.setOptionType(NotifyDescriptor.YES_NO_OPTION); 185 nd.setOptions(new Object [] {NotifyDescriptor.YES_OPTION, NotifyDescriptor.NO_OPTION}); 186 if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) { 187 nd = new NotifyDescriptor.Message(NbBundle.getMessage(EarActionProvider.class, "MSG_Server_State"), NotifyDescriptor.INFORMATION_MESSAGE); 188 Object o = DialogDisplayer.getDefault().notify(nd); 189 return null; 190 } 191 } 192 193 if (isDebugged()) { 194 p.setProperty("is.debugged", "true"); } 196 197 SubprojectProvider spp = (SubprojectProvider) project.getLookup().lookup(SubprojectProvider.class); 198 if (null != spp) { 199 StringBuilder edbd = new StringBuilder (); 200 final Set s = spp.getSubprojects(); 201 Iterator iter = s.iterator(); 202 while (iter.hasNext()) { 203 Project proj = (Project) iter.next(); 204 WebModuleProvider wmp = (WebModuleProvider) proj.getLookup().lookup(WebModuleProvider.class); 205 if (null != wmp) { 206 WebModule wm = wmp.findWebModule(proj.getProjectDirectory()); 207 if (null != wm) { 208 FileObject fo = wm.getDocumentBase(); 209 if (null != fo) { 210 edbd.append(FileUtil.toFile(fo).getAbsolutePath()+":"); } 212 } 213 } 214 } 215 p.setProperty("ear.docbase.dirs", edbd.toString()); } 217 } else { 219 if (targetNames == null) { 220 throw new IllegalArgumentException (command); 221 } 222 } 223 224 return targetNames; 225 } 226 227 public boolean isActionEnabled( String command, Lookup context ) { 228 if ( findBuildXml() == null ) { 229 return false; 230 } 231 if ( command.equals( COMMAND_VERIFY ) ) { 232 J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 233 return provider != null && provider.hasVerifierSupport(); 234 } 235 return true; 237 } 238 239 private boolean isDebugged() { 240 241 J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 242 if (null == jmp) { 243 return false; 245 } 246 ServerDebugInfo sdi = jmp.getServerDebugInfo (); 247 if (null == sdi) { 248 return false; 249 } 250 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 251 252 for (int i=0; i < sessions.length; i++) { 253 Session s = sessions[i]; 254 if (s != null) { 255 Object o = s.lookupFirst(null, AttachingDICookie.class); 256 if (o != null) { 257 AttachingDICookie attCookie = (AttachingDICookie)o; 258 if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) { 259 if (attCookie.getSharedMemoryName().equalsIgnoreCase(sdi.getShmemName())) { 260 return true; 261 } 262 } else { 263 if (attCookie.getHostName() != null 264 && attCookie.getHostName().equalsIgnoreCase(sdi.getHost()) 265 && attCookie.getPortNumber() == sdi.getPort()) { 266 return true; 267 } 268 } 269 } 270 } 271 } 272 return false; 273 } 274 275 private boolean isSelectedServer () { 276 String instance = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_INSTANCE); 278 if (instance != null) { 279 String id = Deployment.getDefault().getServerID(instance); 280 if (id != null) { 281 return true; 282 } 283 } 284 285 String serverType = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_TYPE); 288 if (serverType != null) { 289 String [] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType); 290 if (servInstIDs.length > 0) { 291 EarProjectProperties.setServerInstance(project, updateHelper, servInstIDs[0]); 292 return true; 293 } 294 } 295 296 String msg = NbBundle.getMessage(EarActionProvider.class, "MSG_No_Server_Selected"); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE)); 299 return false; 300 } 301 } 302 | Popular Tags |