KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarActionProvider


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

19
20 package org.netbeans.modules.j2ee.earproject;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
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 /**
52  * Action provider of the Enterprise Application project.
53  */

54 public class EarActionProvider implements ActionProvider {
55     
56     // Definition of commands
57
private static final String JavaDoc COMMAND_COMPILE = "compile"; //NOI18N
58
private static final String JavaDoc COMMAND_VERIFY = "verify"; //NOI18N
59

60     // Commands available from J2ee projects
61
private static final String JavaDoc[] 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     // Ant project helper of the project
78
private final UpdateHelper updateHelper;
79         
80     /** Map from commands to ant targets */
81     Map JavaDoc<String JavaDoc,String JavaDoc[]> commands;
82     
83     public EarActionProvider(EarProject project, UpdateHelper updateHelper) {
84         commands = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
85         commands.put(COMMAND_BUILD, new String JavaDoc[] {"dist"}); // NOI18N
86
commands.put(COMMAND_CLEAN, new String JavaDoc[] {"clean"}); // NOI18N
87
commands.put(COMMAND_REBUILD, new String JavaDoc[] {"clean", "dist"}); // NOI18N
88
commands.put(COMMAND_RUN, new String JavaDoc[] {"run"}); // NOI18N
89
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
90
commands.put(EjbProjectConstants.COMMAND_REDEPLOY, new String JavaDoc[] {"run-deploy"}); // NOI18N
91
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
92
commands.put(COMMAND_COMPILE, new String JavaDoc[] {"compile"}); // NOI18N
93
commands.put(COMMAND_VERIFY, new String JavaDoc[] {"verify"}); // NOI18N
94

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 JavaDoc[] getSupportedActions() {
104         return supportedActions;
105     }
106     
107     public void invokeAction( final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
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 JavaDoc action = new Runnable JavaDoc () {
129             public void run () {
130                 Properties JavaDoc p = new Properties JavaDoc();
131                 String JavaDoc[] 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 JavaDoc e) {
147                     ErrorManager.getDefault().notify(e);
148                 }
149             }
150         };
151         
152         action.run();
153     }
154
155     /**
156      * @return array of targets or null to stop execution; can return empty array
157      */

158     String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context, Properties JavaDoc p) throws IllegalArgumentException JavaDoc {
159         String JavaDoc[] targetNames = commands.get(command);
160         
161         //EXECUTION PART
162
if (command.equals (COMMAND_RUN) || command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) { // || command.equals (COMMAND_DEBUG)) {
163
if (!isSelectedServer ()) {
164                 return null;
165             }
166             if (isDebugged()) {
167                 p.setProperty("is.debugged", "true"); // NOI18N
168
}
169             if (command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) {
170                 p.setProperty("forceRedeploy", "true"); //NOI18N
171
} else {
172                 p.setProperty("forceRedeploy", "false"); //NOI18N
173
}
174         //DEBUGGING PART
175
} else if (command.equals (COMMAND_DEBUG)) {
176             if (!isSelectedServer ()) {
177                 return null;
178             }
179             
180             //see issue 83056
181
if (project.evaluator().getProperty("app.client") != null) { //MOI18N
182
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 JavaDoc[] {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 JavaDoc o = DialogDisplayer.getDefault().notify(nd);
189                     return null;
190                 }
191             }
192             
193             if (isDebugged()) {
194                 p.setProperty("is.debugged", "true"); // NOI18N
195
}
196
197             SubprojectProvider spp = (SubprojectProvider) project.getLookup().lookup(SubprojectProvider.class);
198             if (null != spp) {
199                 StringBuilder JavaDoc edbd = new StringBuilder JavaDoc();
200                 final Set JavaDoc s = spp.getSubprojects();
201                 Iterator JavaDoc 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()+":"); //NOI18N
211
}
212                         }
213                     }
214                 }
215                 p.setProperty("ear.docbase.dirs", edbd.toString()); // NOI18N
216
}
217         //COMPILATION PART
218
} else {
219             if (targetNames == null) {
220                 throw new IllegalArgumentException JavaDoc(command);
221             }
222         }
223
224         return targetNames;
225     }
226         
227     public boolean isActionEnabled( String JavaDoc 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         // other actions are global
236
return true;
237     }
238     
239     private boolean isDebugged() {
240         
241         J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
242         if (null == jmp) {
243             // XXX this is a bug that I don't know about fixing yet
244
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 JavaDoc 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         // XXX determine what to do with the ejb jar project properties
277
String JavaDoc instance = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_INSTANCE);
278         if (instance != null) {
279             String JavaDoc id = Deployment.getDefault().getServerID(instance);
280             if (id != null) {
281                 return true;
282             }
283         }
284         
285         // if there is some server instance of the type which was used
286
// previously do not ask and use it
287
String JavaDoc serverType = updateHelper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (EarProjectProperties.J2EE_SERVER_TYPE);
288         if (serverType != null) {
289             String JavaDoc[] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType);
290             if (servInstIDs.length > 0) {
291                 EarProjectProperties.setServerInstance(project, updateHelper, servInstIDs[0]);
292                 return true;
293             }
294         }
295         
296         // no selected server => warning
297
String JavaDoc msg = NbBundle.getMessage(EarActionProvider.class, "MSG_No_Server_Selected"); // NOI18N
298
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
299         return false;
300     }
301 }
302
Popular Tags