Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e12610e8e97bba94ea67030213355dd5a622391 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Created on Feb 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.eclipse.jst.j2ee.internal.deployables;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.wst.common.frameworks.internal.operations.WTPOperation;
import org.eclipse.wst.common.modulecore.ModuleCore;
import org.eclipse.wst.common.modulecore.WorkbenchModule;
import org.eclipse.wst.common.modulecore.WorkbenchModuleResource;
import org.eclipse.wst.common.modulecore.internal.builder.DeployableModuleBuilder;
import org.eclipse.wst.common.modulecore.internal.builder.DeployableModuleBuilderDataModel;

import com.ibm.wtp.emf.workbench.ProjectUtilities;

/**
 * @author jialin
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JavaDeployableModuleBuilderOperation extends WTPOperation {
	
	public JavaDeployableModuleBuilderOperation(JavaDeployableModuleBuilderDataModel dataModel) {
		super(dataModel);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.frameworks.internal.operations.WTPOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {

		// preparation
		JavaDeployableModuleBuilderDataModel dataModel = (JavaDeployableModuleBuilderDataModel) operationDataModel;
		WorkbenchModule workbenchModule = (WorkbenchModule)dataModel.getProperty(DeployableModuleBuilderDataModel.WORKBENCH_MODULE);
		
		IProject project = (IProject)dataModel.getProperty(DeployableModuleBuilderDataModel.PROJECT);
		IPath projectPath = project.getFullPath();
		IJavaProject javaProj = ProjectUtilities.getJavaProject(project);
		List javaSourceFolderList = ProjectUtilities.getSourceContainers(project);
		
		// create output container folder if it does not exist
		IFolder outputContainer = (IFolder)dataModel.getProperty(DeployableModuleBuilderDataModel.OUTPUT_CONTAINER);
		if(!outputContainer.exists())
			createFolder(outputContainer);
		
		IPath outputContainerPath = outputContainer.getFullPath();

		// copy resources except the java source folder
		List resourceList = workbenchModule.getResources();
		for (int i = 0; i < resourceList.size(); i++) {
			WorkbenchModuleResource wmr = (WorkbenchModuleResource)resourceList.get(i);
			URI sourceURI = wmr.getSourcePath();
			IPath sourcePath = new Path(sourceURI.toString());
			IResource sourceResource =  ModuleCore.getEclipseResource(wmr);
			if (sourceResource == null)
				continue;
			// check if it is a java source folder
			if (javaSourceFolderList.contains(sourceResource)) 
				continue;
			// create parent folders for deploy folder if not exist
			URI deployURI = wmr.getDeployedPath();
			IPath deployPath = outputContainerPath.append(deployURI.toString());
			IPath parentPath = deployPath.removeLastSegments(1);
			createFolder(parentPath);
			DeployableModuleBuilder.smartCopy(sourceResource, deployPath, new NullProgressMonitor());
		}

		// set Java specific output path, do it after resource copy
		IClasspathEntry[] cpe = javaProj.getRawClasspath();
		boolean classpathModified = false;
		for (int i = 0; i < resourceList.size(); i++) {
			WorkbenchModuleResource wmr = (WorkbenchModuleResource)resourceList.get(i);
			URI sourceURI = wmr.getSourcePath();
			IPath sourcePath = new Path(sourceURI.toString());
			IResource sourceResource = ModuleCore.getEclipseResource(wmr);
			// check if it is a java source folder
			if (javaSourceFolderList.contains(sourceResource)) {
				// get the classpath entry
				int index = -1;
				for (int j = 0; j < cpe.length; j++) {
					if (cpe[j].getPath().equals(sourcePath)) {
						index = j;
						break;
					}
				}
				URI deployURI = wmr.getDeployedPath();
				IPath classFilesPath = outputContainerPath.append(deployURI.toString());
				// check if the classpath is modified. Use relative path to avoid 
				// the problem that drive letter could be upper or lower case
				IPath relativeClassFilesPath = classFilesPath.makeRelative();
				IPath oldClassFilesPath = ((ClasspathEntry)cpe[index]).specificOutputLocation;
				IPath oldRelativeClassFilesPath = null;
				if (oldClassFilesPath != null)
					oldRelativeClassFilesPath = oldClassFilesPath.makeRelative();
				if (!relativeClassFilesPath.equals(oldRelativeClassFilesPath)) {
					createFolder(classFilesPath);
					((ClasspathEntry)cpe[index]).specificOutputLocation = classFilesPath;
					classpathModified = true;
				}
			}
		}
		// update classpath only when it is modified
		if (classpathModified)
			javaProj.setRawClasspath(cpe, new NullProgressMonitor());
	}

	/**
	 * @param outputContainer
	 */
	private void createFolder(IFolder outputContainer) {
		IContainer parentContainer = outputContainer.getParent();
		if(parentContainer != null && !parentContainer.exists() && parentContainer.getType() == IResource.FOLDER) {			
			createFolder((IFolder)outputContainer.getParent());
		}
		try {
			if(!outputContainer.exists())
				outputContainer.create(true, true, null);
		} catch (CoreException e) { 
			e.printStackTrace();
		}
		
	}

	/**
	 * Get resource for given absolute path
	 * 
	 * @exception com.ibm.itp.core.api.resources.CoreException
	 */
	private IResource getResource(IPath absolutePath) throws CoreException {
		IResource resource = null;
		if (absolutePath != null && !absolutePath.isEmpty()) 
			resource = ResourcesPlugin.getWorkspace().getRoot().findMember(absolutePath);
		return resource;
	}

	 
	/**
	 * Create a folder for given absolute path
	 * 
	 * @exception com.ibm.itp.core.api.resources.CoreException
	 */
	private IFolder createFolder(IPath absolutePath) throws CoreException {
		if (absolutePath == null || absolutePath.isEmpty())
			return null;
		IFolder folder = getWorkspace().getRoot().getFolder(absolutePath);
		// check if the parent is there
		IContainer parent = folder.getParent();
		if (parent != null && !parent.exists() && (parent instanceof IFolder))
			createFolder(parent.getFullPath());
		if (!folder.exists())
			folder.create(true, true, new NullProgressMonitor());
		return folder;
	}
}

Back to the top