001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Properties;
029: import org.apache.tools.ant.module.api.support.ActionUtils;
030: import org.netbeans.spi.project.ActionProvider;
031: import org.netbeans.spi.project.support.ant.AntProjectHelper;
032: import org.openide.execution.ExecutorTask;
033: import org.openide.filesystems.FileObject;
034: import org.openide.util.Lookup;
035: import org.openide.util.Task;
036: import org.openide.util.TaskListener;
037: import org.openide.windows.IOProvider;
038: import org.openide.windows.OutputWriter;
039: import org.netbeans.modules.j2ee.deployment.plugins.api.*;
040: import org.netbeans.api.debugger.*;
041: import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
042: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
043: import org.netbeans.modules.compapp.projects.base.ui.customizer.VisualClassPathItem;
044:
045: import org.netbeans.spi.project.support.ant.ReferenceHelper;
046: import org.openide.*;
047: import org.netbeans.api.project.ant.AntArtifact;
048: import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
049:
050: import org.netbeans.modules.bpel.project.IcanproConstants;
051:
052: class BpelproActionProvider implements ActionProvider {
053:
054: // Commands available from Web project
055: private static final String[] supportedActions = { COMMAND_BUILD,
056: COMMAND_CLEAN, COMMAND_REBUILD, COMMAND_DELETE,
057: // IcanproConstants.POPULATE_CATALOG,
058: COMMAND_DELETE, COMMAND_COPY, COMMAND_MOVE, COMMAND_RENAME };
059:
060: // Project
061: BpelproProject project;
062:
063: // Ant project helper of the project
064: private AntProjectHelper antProjectHelper;
065: private ReferenceHelper refHelper;
066:
067: /** Map from commands to ant targets */
068: Map/*<String,String[]>*/commands;
069:
070: public BpelproActionProvider(BpelproProject project,
071: AntProjectHelper antProjectHelper, ReferenceHelper refHelper) {
072: commands = new HashMap();
073: commands.put(COMMAND_BUILD, new String[] { "dist_se" }); // NOI18N
074: commands.put(COMMAND_CLEAN, new String[] { "clean" }); // NOI18N
075: commands.put(COMMAND_REBUILD,
076: new String[] { "clean", "dist_se" }); // NOI18N
077: //commands.put(IcanproConstants.POPULATE_CATALOG, new String[] {"populate"});
078: //commands.put(IcanproConstants.COMMAND_REDEPLOY, new String[] {"run"}); // NOI18N
079: //commands.put(IcanproConstants.COMMAND_DEPLOY, new String[] {"run"}); // NOI18N
080:
081: this .antProjectHelper = antProjectHelper;
082: this .project = project;
083: this .refHelper = refHelper;
084: }
085:
086: /**
087: * @return array of targets or null to stop execution; can return empty array
088: */
089: /*private*/String[] getTargetNames(String command, Lookup context,
090: Properties p) throws IllegalArgumentException {
091: String[] targetNames = (String[]) commands.get(command);
092: return targetNames;
093: }
094:
095: private FileObject findBuildXml() {
096: return project.getProjectDirectory().getFileObject(
097: project.getBuildXmlName());
098: }
099:
100: public String[] getSupportedActions() {
101: return supportedActions;
102: }
103:
104: public void invokeAction(String command, Lookup context)
105: throws IllegalArgumentException {
106: if (COMMAND_COPY.equals(command)) {
107: DefaultProjectOperations
108: .performDefaultCopyOperation(project);
109: return;
110: }
111:
112: if (COMMAND_MOVE.equals(command)) {
113: DefaultProjectOperations
114: .performDefaultMoveOperation(project);
115: return;
116: }
117:
118: if (COMMAND_RENAME.equals(command)) {
119: DefaultProjectOperations.performDefaultRenameOperation(
120: project, null);
121: return;
122: }
123: if (COMMAND_DELETE.equals(command)) {
124: DefaultProjectOperations
125: .performDefaultDeleteOperation(project);
126: return;
127: }
128: if (command.equals(IcanproConstants.POPULATE_CATALOG)) {
129: BpelProjectRetriever bpRetriever = new BpelProjectRetriever(
130: project.getProjectDirectory());
131: bpRetriever.execute();
132: return;
133: }
134: Properties p = null;
135: String[] targetNames = (String[]) commands.get(command);
136: //EXECUTION PART
137: //// if (command.equals (IcanproConstants.COMMAND_DEPLOY) || command.equals (IcanproConstants.COMMAND_REDEPLOY)) {
138: //// if (!isSelectedServer ()) {
139: //// return;
140: //// }
141: //// if (isDebugged()) {
142: //// NotifyDescriptor nd;
143: //// ProjectInformation pi = (ProjectInformation)project.getLookup().lookup(ProjectInformation.class);
144: //// String text = pi.getDisplayName();
145: //// nd = new NotifyDescriptor.Confirmation(
146: //// NbBundle.getMessage(BpelproActionProvider.class, "MSG_SessionRunning", text),
147: //// NotifyDescriptor.OK_CANCEL_OPTION);
148: //// Object o = DialogDisplayer.getDefault().notify(nd);
149: //// if (o.equals(NotifyDescriptor.OK_OPTION)) {
150: //// DebuggerManager.getDebuggerManager().getCurrentSession().kill();
151: //// } else {
152: //// return;
153: //// }
154: //// }
155: //// } else {
156: //// p = null;
157: //// if (targetNames == null) {
158: //// throw new IllegalArgumentException(command);
159: //// }
160: //// }
161:
162: // if build command then build any depedent project
163: if (command.equals(COMMAND_BUILD)) {
164: try {
165: buildDependentProjectsAndRunTask(targetNames, p);
166: } catch (IOException e) {
167: ErrorManager.getDefault().notify(e);
168: }
169: } else {
170: runTask(targetNames, p);
171: }
172: }
173:
174: private void runTask(String[] targetNames, Properties p) {
175: try {
176: ActionUtils.runTarget(findBuildXml(), targetNames, p);
177: } catch (IOException e) {
178: ErrorManager.getDefault().notify(e);
179: }
180: }
181:
182: private void buildDependentProjectsAndRunTask(String[] targetNames,
183: Properties p) throws IOException {
184: IcanproProjectProperties app = this .project
185: .getProjectProperties();
186: List items = (List) app
187: .get(IcanproProjectProperties.JAVAC_CLASSPATH);
188: ArrayList artifacts = new ArrayList();
189:
190: if (items != null) {
191: for (int i = 0, size = items.size(); i < size; i++) {
192: VisualClassPathItem vi = (VisualClassPathItem) items
193: .get(i);
194: AntArtifact aa = (AntArtifact) vi.getObject();
195: String loc = aa.getProject().getProjectDirectory()
196: .getPath()
197: + "/" + aa.getArtifactLocations()[0].getPath();
198: File asa = new File(loc);
199: log("Dependent Project artifact jar: " + loc + ", ["
200: + (asa.exists() ? "exist" : "missing") + "]");
201: if (!asa.exists()) {
202: artifacts.add(aa);
203: }
204: }
205: }
206: if (artifacts.size() != 0) {
207: //use AntTaskListener which invokes the target on
208: //current project build script after all the depedent projects
209: //are build
210: AntTaskListener antTaskListener = new AntTaskListener(
211: targetNames, p);
212: antTaskListener.setTotalTasks(artifacts.size());
213: Iterator it = artifacts.iterator();
214: while (it.hasNext()) {
215: AntArtifact aa = (AntArtifact) it.next();
216: String loc = aa.getProject().getProjectDirectory()
217: .getPath()
218: + "/" + aa.getArtifactLocations()[0].getPath();
219: log("Building dependent project " + loc + "...");
220: ExecutorTask task = ActionUtils.runTarget(aa
221: .getScriptFile(), new String[] { aa
222: .getTargetName() }, null);
223: task.addTaskListener(antTaskListener);
224: }
225: } else {
226: //no need to build depedent projects
227: //directly invoke target on current project build script;
228: runTask(targetNames, p);
229: }
230: }
231:
232: public boolean isActionEnabled(String command, Lookup context) {
233: return findBuildXml() != null;
234: }
235:
236: private boolean isDebugged() {
237: return false;
238: }
239:
240: private void log(String str) {
241: OutputWriter out = IOProvider.getDefault().getStdOut();
242: out.println(str);
243: out.flush();
244: }
245:
246: class AntTaskListener implements TaskListener {
247: int totalTaskCount;
248: int finishedTaskCount = 0;
249: private String[] mTargetNames;
250: private Properties mProperties;
251:
252: public AntTaskListener(String[] targetNames, Properties p) {
253: this .mTargetNames = targetNames;
254: this .mProperties = p;
255: }
256:
257: public void setTotalTasks(int total) {
258: this .totalTaskCount = total;
259: }
260:
261: public void taskFinished(Task task) {
262: finishedTaskCount++;
263: if (finishedTaskCount == totalTaskCount) {
264: runTask(this.mTargetNames, this.mProperties);
265: }
266: }
267: }
268: }
|