blob: f9e5502b03efa045bcbaff6f5cae34891552d2ad [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.j2ee.clientproject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tools.ant.module.api.support.ActionUtils;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
import org.netbeans.modules.j2ee.dd.api.client.AppClient;
import org.netbeans.modules.j2ee.dd.api.client.DDProvider;
import org.netbeans.modules.java.api.common.SourceRoots;
import org.netbeans.spi.project.ActionProvider;
import org.netbeans.spi.project.CopyOperationImplementation;
import org.netbeans.spi.project.DeleteOperationImplementation;
import org.netbeans.spi.project.MoveOperationImplementation;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.netbeans.spi.project.support.ant.EditableProperties;
import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.netbeans.spi.project.support.ant.PropertyUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;
/**
*
* @author Jan Lahoda
*/
public class AppClientProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
private final AppClientProject project;
//RELY: Valid only on original project after the notifyMoving or notifyCopying was called
private String libraryPath;
//RELY: Valid only on original project after the notifyMoving or notifyCopying was called
private File libraryFile;
//RELY: Valid only on original project after the notifyMoving or notifyCopying was called
private boolean libraryWithinProject;
//RELY: Valid only on original project after the notifyMoving or notifyCopying was called
private String absolutesRelPath;
public AppClientProjectOperations(AppClientProject project) {
this.project = project;
}
private static void addFile(FileObject projectDirectory, String fileName, List<FileObject> result) {
FileObject file = projectDirectory.getFileObject(fileName);
if (file != null) {
result.add(file);
}
}
public List<FileObject> getMetadataFiles() {
FileObject projectDirectory = project.getProjectDirectory();
List<FileObject> files = new ArrayList<FileObject>();
addFile(projectDirectory, "nbproject", files); // NOI18N
addFile(projectDirectory, "build.xml", files); // NOI18N
addFile(projectDirectory, "catalog.xml", files); //NOI18N
return files;
}
public List<FileObject> getDataFiles() {
List<FileObject> files = new ArrayList<FileObject>();
FileObject metaInf = project.getCarModule().getMetaInf();
if (metaInf != null) {
files.add(metaInf);
}
SourceRoots src = project.getSourceRoots();
FileObject[] srcRoots = src.getRoots();
for (int cntr = 0; cntr < srcRoots.length; cntr++) {
files.add(srcRoots[cntr]);
}
PropertyEvaluator evaluator = project.evaluator();
String prop = evaluator.getProperty(AppClientProjectProperties.SOURCE_ROOT);
if (prop != null) {
FileObject projectDirectory = project.getProjectDirectory();
FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop);
if (projectDirectory != srcDir && !files.contains(srcDir)) {
files.add(srcDir);
}
}
SourceRoots test = project.getTestSourceRoots();
FileObject[] testRoots = test.getRoots();
for (int cntr = 0; cntr < testRoots.length; cntr++) {
files.add(testRoots[cntr]);
}
File resourceDir = project.getCarModule().getResourceDirectory();
if (resourceDir != null) {
FileObject resourceFO = FileUtil.toFileObject(resourceDir);
if (resourceFO != null) {
files.add(resourceFO);
}
}
// add libraries folder if it is within project:
AntProjectHelper helper = project.getAntProjectHelper();
if (helper.getLibrariesLocation() != null) {
File f = helper.resolveFile(helper.getLibrariesLocation());
if (f != null && f.exists()) {
FileObject libFolder = FileUtil.toFileObject(f).getParent();
if (FileUtil.isParentOf(project.getProjectDirectory(), libFolder)) {
files.add(libFolder);
}
}
}
return files;
}
public void notifyDeleting() throws IOException {
AppClientActionProvider ap = project.getLookup().lookup(AppClientActionProvider.class);
assert ap != null;
Lookup context = Lookups.fixed(new Object[0]);
Properties p = new Properties();
String[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p);
FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
assert targetNames != null;
assert targetNames.length > 0;
ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
}
public void notifyDeleted() throws IOException {
project.getAntProjectHelper().notifyDeleted();
}
public void notifyCopying() {
rememberLibraryLocation();
}
public void notifyCopied(Project original, File originalPath, String nueName) {
if (original == null) {
//do nothing for the original project.
return ;
}
AppClientProjectOperations origOperations = original.getLookup().lookup(AppClientProjectOperations.class);
fixLibraryLocation(origOperations);
project.getReferenceHelper().fixReferences(originalPath);
fixOtherReferences(originalPath);
project.setName(nueName);
}
public void notifyMoving() throws IOException {
if (!this.project.getUpdateHelper().requestUpdate()) {
throw new IOException(NbBundle.getMessage(AppClientProjectOperations.class,
"MSG_OldProjectMetadata")); // NOI18N
}
rememberLibraryLocation();
notifyDeleting();
}
public void notifyMoved(Project original, File originalPath, final String newName) {
if (original == null) {
project.getAntProjectHelper().notifyDeleted();
return ;
}
AppClientProjectOperations origOperations = original.getLookup().lookup(AppClientProjectOperations.class);
fixLibraryLocation(origOperations);
final String oldProjectName = project.getName();
project.setName(newName);
project.getReferenceHelper().fixReferences(originalPath);
fixOtherReferences(originalPath);
ProjectManager.mutex().writeAccess(new Runnable() {
public void run() {
AntProjectHelper helper = project.getAntProjectHelper();
EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
String jarName = projectProps.get(AppClientProjectProperties.JAR_NAME);
String oldName = jarName.substring(0, jarName.length() - 4);
if (jarName.endsWith(".jar") && oldName.equals(oldProjectName)) { //NOI18N
projectProps.put(AppClientProjectProperties.JAR_NAME, newName + ".jar"); //NOI18N
}
helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
FileObject ddFO = project.getCarModule().getDeploymentDescriptor();
if (ddFO != null) {
try {
AppClient appClient = DDProvider.getDefault().getDDRoot(ddFO);
appClient.setDisplayName(newName);
appClient.write(ddFO);
} catch (IOException ioe) {
Logger.getLogger("global").log(Level.WARNING, null, ioe);
}
}
}
});
}
private void fixOtherReferences(final File originalPath) {
final String property = AppClientProjectProperties.META_INF;
final File projectDir = FileUtil.toFile(project.getProjectDirectory());
ProjectManager.mutex().writeAccess(new Runnable() {
public void run() {
EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
String path = props.getProperty(property);
if (path == null) {
return;
}
if (path.startsWith(originalPath.getAbsolutePath())) {
String relative = PropertyUtils.relativizeFile(originalPath, new File(path));
String fixedPath = new File(projectDir, relative).getAbsolutePath();
props.setProperty(property, fixedPath);
project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
}
}
});
}
private void fixLibraryLocation(AppClientProjectOperations original) throws IllegalArgumentException {
String libPath = original.libraryPath;
if (libPath != null) {
if (!new File(libPath).isAbsolute()) {
//relative path to libraries
if (!original.libraryWithinProject) {
File file = original.libraryFile;
if (file == null) {
// could happen in some rare cases, but in that case the original project was already broken, don't fix.
return;
}
String relativized = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
if (relativized != null) {
project.getAntProjectHelper().setLibrariesLocation(relativized);
} else {
//cannot relativize, use absolute path
project.getAntProjectHelper().setLibrariesLocation(file.getAbsolutePath());
}
} else {
//got copied over to new location.. the relative path is the same..
}
} else {
//absolute path to libraries..
if (original.libraryWithinProject) {
if (original.absolutesRelPath != null) {
project.getAntProjectHelper().setLibrariesLocation(PropertyUtils.resolveFile(FileUtil.toFile(project.getProjectDirectory()), original.absolutesRelPath).getAbsolutePath());
}
} else {
// absolute path to an external folder stays the same.
}
}
}
}
private void rememberLibraryLocation() {
libraryWithinProject = false;
absolutesRelPath = null;
libraryPath = project.getAntProjectHelper().getLibrariesLocation();
if (libraryPath != null) {
File prjRoot = FileUtil.toFile(project.getProjectDirectory());
libraryFile = PropertyUtils.resolveFile(prjRoot, libraryPath);
if (FileOwnerQuery.getOwner(libraryFile.toURI()) == project &&
libraryFile.getAbsolutePath().startsWith(prjRoot.getAbsolutePath())) {
//do not update the relative path if within the project..
libraryWithinProject = true;
FileObject fo = FileUtil.toFileObject(libraryFile);
if (new File(libraryPath).isAbsolute() && fo != null) {
// if absolte path within project, it will get moved/copied..
absolutesRelPath = FileUtil.getRelativePath(project.getProjectDirectory(), fo);
}
}
}
}
}