001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.project;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.List;
048: import java.util.Properties;
049: import org.apache.tools.ant.module.api.support.ActionUtils;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.api.project.ProjectManager;
052: import org.netbeans.modules.java.api.common.SourceRoots;
053: import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
054: import org.netbeans.spi.project.ActionProvider;
055: import org.netbeans.spi.project.CopyOperationImplementation;
056: import org.netbeans.spi.project.DeleteOperationImplementation;
057: import org.netbeans.spi.project.MoveOperationImplementation;
058: import org.netbeans.spi.project.support.ant.AntProjectHelper;
059: import org.netbeans.spi.project.support.ant.EditableProperties;
060: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
061: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
062: import org.netbeans.spi.project.support.ant.PropertyUtils;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.util.Lookup;
066: import org.openide.util.lookup.Lookups;
067:
068: /**
069: *
070: * @author Jan Lahoda
071: */
072: public class WebProjectOperations implements
073: DeleteOperationImplementation, CopyOperationImplementation,
074: MoveOperationImplementation {
075:
076: private WebProject project;
077:
078: public WebProjectOperations(WebProject project) {
079: this .project = project;
080: }
081:
082: private static void addFile(FileObject projectDirectory,
083: String fileName, List/*<FileObject>*/result) {
084: FileObject file = projectDirectory.getFileObject(fileName);
085:
086: if (file != null) {
087: result.add(file);
088: }
089: }
090:
091: public List/*<FileObject>*/getMetadataFiles() {
092: FileObject projectDirectory = project.getProjectDirectory();
093: List/*<FileObject>*/files = new ArrayList();
094:
095: addFile(projectDirectory, "nbproject", files); // NOI18N
096: addFile(projectDirectory, project.getBuildXmlName(), files);
097: addFile(projectDirectory, "catalog.xml", files); //NOI18N
098:
099: return files;
100: }
101:
102: public List/*<FileObject>*/getDataFiles() {
103: List/*<FileObject>*/files = new ArrayList();
104:
105: FileObject docRoot = project.getAPIWebModule()
106: .getDocumentBase();
107: if (docRoot != null)
108: files.add(docRoot);
109:
110: FileObject confDir = project.getWebModule().getConfDir();
111: if (confDir != null)
112: files.add(confDir);
113:
114: File resourceDir = project.getWebModule()
115: .getResourceDirectory();
116: if (resourceDir != null) {
117: FileObject resourceFO = FileUtil.toFileObject(resourceDir);
118: if (resourceFO != null)
119: files.add(resourceFO);
120: }
121:
122: SourceRoots src = project.getSourceRoots();
123: FileObject[] srcRoots = src.getRoots();
124:
125: for (int cntr = 0; cntr < srcRoots.length; cntr++) {
126: files.add(srcRoots[cntr]);
127: }
128:
129: PropertyEvaluator evaluator = project.evaluator();
130: String prop = evaluator
131: .getProperty(WebProjectProperties.SOURCE_ROOT);
132: if (prop != null) {
133: FileObject projectDirectory = project.getProjectDirectory();
134: FileObject srcDir = project.getAntProjectHelper()
135: .resolveFileObject(prop);
136: if (projectDirectory != srcDir && !files.contains(srcDir))
137: files.add(srcDir);
138: }
139:
140: SourceRoots test = project.getTestSourceRoots();
141: FileObject[] testRoots = test.getRoots();
142:
143: for (int cntr = 0; cntr < testRoots.length; cntr++) {
144: files.add(testRoots[cntr]);
145: }
146:
147: return files;
148: }
149:
150: public void notifyDeleting() throws IOException {
151: WebActionProvider ap = (WebActionProvider) project.getLookup()
152: .lookup(WebActionProvider.class);
153:
154: assert ap != null;
155:
156: Lookup context = Lookups.fixed(new Object[0]);
157: Properties p = new Properties();
158: String[] targetNames = ap.getTargetNames(
159: ActionProvider.COMMAND_CLEAN, context, p);
160: FileObject buildXML = project.getProjectDirectory()
161: .getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
162:
163: assert targetNames != null;
164: assert targetNames.length > 0;
165:
166: ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
167: }
168:
169: public void notifyDeleted() throws IOException {
170: project.getAntProjectHelper().notifyDeleted();
171: }
172:
173: public void notifyCopying() {
174: //nothing.
175: }
176:
177: public void notifyCopied(Project original, File originalPath,
178: final String newName) {
179: if (original == null) {
180: //nothing for the original project
181: return;
182: }
183:
184: final String oldProjectName = project.getName();
185:
186: project.getReferenceHelper().fixReferences(originalPath);
187:
188: project.setName(newName);
189:
190: ProjectManager.mutex().writeAccess(new Runnable() {
191: public void run() {
192: AntProjectHelper helper = project.getAntProjectHelper();
193: EditableProperties projectProps = helper
194: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
195:
196: String warName = (String) projectProps
197: .get(WebProjectProperties.WAR_NAME);
198: String warEarName = (String) projectProps
199: .get(WebProjectProperties.WAR_EAR_NAME);
200: String oldName = warName.substring(0,
201: warName.length() - 4);
202: if (warName.endsWith(".war")
203: && oldName.equals(oldProjectName)) //NOI18N
204: projectProps.put(WebProjectProperties.WAR_NAME,
205: PropertyUtils
206: .getUsablePropertyName(newName)
207: + ".war"); //NOI18N
208: if (warEarName.endsWith(".war")
209: && oldName.equals(oldProjectName)) //NOI18N
210: projectProps.put(WebProjectProperties.WAR_EAR_NAME,
211: PropertyUtils
212: .getUsablePropertyName(newName)
213: + ".war"); //NOI18N
214:
215: ProjectWebModule wm = (ProjectWebModule) project
216: .getLookup().lookup(ProjectWebModule.class);
217: if (wm != null) //should not be null
218: wm.setContextPath("/" + newName);
219:
220: helper.putProperties(
221: AntProjectHelper.PROJECT_PROPERTIES_PATH,
222: projectProps);
223: }
224: });
225: }
226:
227: public void notifyMoving() throws IOException {
228: notifyDeleting();
229: }
230:
231: public void notifyMoved(Project original, File originalPath,
232: final String newName) {
233: if (original == null) {
234: project.getAntProjectHelper().notifyDeleted();
235: return;
236: }
237:
238: final String oldProjectName = project.getName();
239:
240: project.setName(newName);
241: project.getReferenceHelper().fixReferences(originalPath);
242:
243: ProjectManager.mutex().writeAccess(new Runnable() {
244: public void run() {
245: AntProjectHelper helper = project.getAntProjectHelper();
246: EditableProperties projectProps = helper
247: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
248: EditableProperties privateProps = helper
249: .getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
250:
251: String warName = (String) projectProps
252: .get(WebProjectProperties.WAR_NAME);
253: String warEarName = (String) projectProps
254: .get(WebProjectProperties.WAR_EAR_NAME);
255: String oldName = warName.substring(0,
256: warName.length() - 4);
257: if (warName.endsWith(".war")
258: && oldName.equals(oldProjectName)) //NOI18N
259: projectProps.put(WebProjectProperties.WAR_NAME,
260: newName + ".war"); //NOI18N
261: if (warEarName.endsWith(".war")
262: && oldName.equals(oldProjectName)) //NOI18N
263: projectProps.put(WebProjectProperties.WAR_EAR_NAME,
264: newName + ".war"); //NOI18N
265:
266: ProjectWebModule wm = (ProjectWebModule) project
267: .getLookup().lookup(ProjectWebModule.class);
268: String serverId = privateProps
269: .getProperty(WebProjectProperties.J2EE_SERVER_INSTANCE);
270: String oldCP = wm.getContextPath(serverId);
271: if (oldCP != null && oldName.equals(oldCP.substring(1)))
272: wm.setContextPath(serverId, "/" + newName); //NOI18N
273:
274: helper.putProperties(
275: AntProjectHelper.PROJECT_PROPERTIES_PATH,
276: projectProps);
277: }
278: });
279: }
280:
281: }
|