| /******************************************************************************* |
| * Copyright (c) 2003, 2004 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| /* |
| * Created on Nov 5, 2003 |
| * |
| * To change the template for this generated file go to |
| * Window>Preferences>Java>Code Generation>Code and Comments |
| */ |
| package org.eclipse.jst.j2ee.application.operations; |
| |
| import java.io.IOException; |
| import java.lang.reflect.InvocationTargetException; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.List; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.jdt.core.IClasspathEntry; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jdt.core.JavaCore; |
| import org.eclipse.jdt.core.JavaModelException; |
| import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveConstants; |
| import org.eclipse.jst.j2ee.internal.project.J2EENature; |
| import org.eclipse.jst.j2ee.internal.project.ManifestFileCreationAction; |
| import org.eclipse.wst.common.jdt.internal.integration.JavaProjectCreationDataModel; |
| import org.eclipse.wst.common.jdt.internal.integration.JavaProjectCreationOperation; |
| |
| |
| /** |
| * @author jsholl |
| * |
| * To change the template for this generated type comment go to |
| * Window>Preferences>Java>Code Generation>Code and Comments |
| */ |
| public abstract class J2EEModuleCreationOperation extends J2EEProjectCreationOperation { |
| |
| public J2EEModuleCreationOperation(J2EEModuleCreationDataModel dataModel) { |
| super(dataModel); |
| } |
| |
| public J2EEModuleCreationOperation() { |
| super(); |
| } |
| |
| protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| J2EEModuleCreationDataModel dataModel = (J2EEModuleCreationDataModel) operationDataModel; |
| createProject(monitor); |
| if (dataModel.getBooleanProperty(J2EEModuleCreationDataModel.CREATE_DEFAULT_FILES)) { |
| createDeploymentDescriptor(monitor); |
| J2EENature nature = (J2EENature) dataModel.getProjectDataModel().getProject().getNature(dataModel.getJ2EENatureID()); |
| createManifest(nature, monitor); |
| } |
| linkToEARIfNecessary(dataModel, monitor); |
| } |
| |
| protected abstract void createDeploymentDescriptor(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException; |
| |
| public static void linkToEARIfNecessary(J2EEModuleCreationDataModel moduleModel, IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| if (moduleModel.getBooleanProperty(J2EEModuleCreationDataModel.ADD_TO_EAR)) { |
| EARProjectCreationDataModel earModel = moduleModel.getEarProjectCreationDataModel(); |
| if (!earModel.getTargetProject().exists()) { |
| EARProjectCreationOperation earOp = new EARProjectCreationOperation(earModel); |
| earOp.doRun(monitor); |
| } |
| AddArchiveProjectToEARDataModel addModuleModel = moduleModel.getAddModuleToEARDataModel(); |
| AddArchiveProjectToEAROperation addModuleOp = new AddArchiveProjectToEAROperation(addModuleModel); |
| addModuleOp.doRun(monitor); |
| } |
| } |
| |
| protected void createProject(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| JavaProjectCreationDataModel projectModel = ((J2EEModuleCreationDataModel) operationDataModel).getJavaProjectCreationDataModel(); |
| JavaProjectCreationOperation javaProjectOperation = new JavaProjectCreationOperation(projectModel); |
| javaProjectOperation.doRun(monitor); |
| updateClasspath(projectModel); |
| J2EEModuleCreationDataModel dataModel = (J2EEModuleCreationDataModel) operationDataModel; |
| J2EENature nature = (J2EENature) dataModel.getProjectDataModel().getProject().getNature(dataModel.getJ2EENatureID()); |
| setVersion(nature, monitor); |
| addServerTarget(monitor); |
| } |
| |
| /** |
| * @param projectModel |
| * @throws JavaModelException |
| */ |
| private void updateClasspath(JavaProjectCreationDataModel projectModel) throws JavaModelException { |
| ClassPathSelection classpath = ((J2EEModuleCreationDataModel) getOperationDataModel()).getClassPathSelection(); |
| if (classpath == null || classpath.getClasspathElements().size() == 0) |
| return; |
| IClasspathEntry[] newEntries = classpath.getClasspathEntriesForSelected(); |
| IProject project = projectModel.getProject(); |
| if (project == null) |
| return; |
| IJavaProject javaProject = JavaCore.create(project); |
| IClasspathEntry[] existingEntries = javaProject.getRawClasspath(); |
| |
| List allEntries = new ArrayList(); |
| if (existingEntries != null && existingEntries.length > 0) |
| allEntries.addAll(Arrays.asList(existingEntries)); |
| if (newEntries != null && newEntries.length > 0) |
| allEntries.addAll(Arrays.asList(newEntries)); |
| IClasspathEntry[] finalEntriesArray = new IClasspathEntry[allEntries.size()]; |
| allEntries.toArray(finalEntriesArray); |
| javaProject.setRawClasspath(finalEntriesArray, new NullProgressMonitor()); |
| } |
| |
| /** |
| * @param monitor |
| */ |
| protected void createManifest(J2EENature nature, IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| if (nature.getEMFRoot() != null) { |
| try { |
| IFile file = nature.getEMFRoot().getFile(new Path(ArchiveConstants.MANIFEST_URI)); |
| ManifestFileCreationAction.createManifestFile(file, nature.getProject()); |
| } catch (IOException ioe) { |
| com.ibm.wtp.common.logger.proxy.Logger.getLogger().logError(ioe); |
| return; |
| } |
| UpdateManifestOperation op = new UpdateManifestOperation(((J2EEModuleCreationDataModel) operationDataModel).getUpdateManifestDataModel()); |
| op.doRun(monitor); |
| } |
| } |
| |
| protected void setVersion(J2EENature nature, IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| J2EEModuleCreationDataModel dataModel = (J2EEModuleCreationDataModel) operationDataModel; |
| nature.setModuleVersion(dataModel.getIntProperty(J2EEModuleCreationDataModel.J2EE_MODULE_VERSION)); |
| } |
| |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.wst.common.frameworks.internal.operation.WTPOperation#dispose(org.eclipse.core.runtime.IProgressMonitor) |
| */ |
| protected void dispose(IProgressMonitor pm) { |
| try { |
| getOperationDataModel().dispose(); |
| super.dispose(pm); |
| } catch (RuntimeException re) { |
| } |
| } |
| |
| } |