Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: bf3d67905880a03743545f55b7c4602653a72690 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2003, 2007 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
 *******************************************************************************/
package org.eclipse.jst.j2ee.navigator.internal.dnd;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jst.j2ee.application.internal.operations.EARComponentImportDataModelProvider;
import org.eclipse.jst.j2ee.applicationclient.internal.creation.AppClientComponentImportDataModelProvider;
import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.CommonarchiveFactory;
import org.eclipse.jst.j2ee.datamodel.properties.IJ2EEComponentImportDataModelProperties;
import org.eclipse.jst.j2ee.internal.ejb.project.operations.EJBComponentImportDataModelProvider;
import org.eclipse.jst.j2ee.internal.jca.operations.ConnectorComponentImportDataModelProvider;
import org.eclipse.jst.j2ee.internal.navigator.ui.Messages;
import org.eclipse.jst.j2ee.internal.plugin.IJ2EEModuleConstants;
import org.eclipse.jst.j2ee.internal.web.archive.operations.WebComponentImportDataModelProvider;
import org.eclipse.jst.j2ee.navigator.internal.plugin.J2EENavigatorPlugin;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
import org.eclipse.ui.navigator.CommonDropAdapter;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

public class ImportJ2EEModuleDropAssistant extends AddProjectToEARDropAssistant {
	
	@Override
	public boolean isSupportedType(TransferData aTransferType) { 
		return FileTransfer.getInstance().isSupportedType(aTransferType);
	}
	
	@Override
	public IStatus handleDrop(CommonDropAdapter aDropAdapter, final DropTargetEvent aDropTargetEvent, final Object aTarget) {		
		
		if(FileTransfer.getInstance().isSupportedType(aDropAdapter.getCurrentTransfer())) {
			
			final Shell shell = getShell();
		
			IProgressService service = PlatformUI.getWorkbench().getProgressService();	 
			Job importArtifactsJob = new Job(Messages.ImportJ2EEModuleDropAssistant_Importing_Java_Enterprise_Edition_artifacts) {
				@Override
				protected IStatus run(IProgressMonitor monitor) {					
					
					IProject targetEARProject = getProject(aTarget);
					if(targetEARProject != null) { 
						/* If this isn't an ear project, we can't add the newly created modules to it. */
						if(!hasEarFacet(targetEARProject))
							targetEARProject = null;
					} /* otherwise we assume the IWorkspaceRoot and just don't add the new modules an an existing ear */

					String[] names = (String[]) aDropTargetEvent.data;
					
					monitor.beginTask(Messages.ImportJ2EEModuleDropAssistant_Importing_Java_Enterprise_Edition_artifacts, names.length);
					final MultiStatus status = new MultiStatus(J2EENavigatorPlugin.PLUGIN_ID, 0, Messages.ImportJ2EEModuleDropAssistant_Importing_Java_Enterprise_Edition_artifacts, null);
					
					SubProgressMonitor submonitor = new SubProgressMonitor(monitor, 10);
					IDataModel importDataModel = null;
					boolean performSimpleJarCopy = false;
					List simpleJarsToCopyList = null;
					IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
					List createdComponents = new ArrayList();
					for(int i=0; i<names.length && !monitor.isCanceled(); i++) {
						try {
							importDataModel = null;
							performSimpleJarCopy = false;
							int separatorIndex = names[i].lastIndexOf(File.separatorChar);
							int dotIndex = names[i].lastIndexOf('.');
							if(separatorIndex > 0 && separatorIndex+1 < dotIndex) {
								String filename = names[i].substring(separatorIndex+1, dotIndex);
								if(root.getProject(filename).exists()) {
									filename = calculateValidProjectName(filename);
								}
								String extension = names[i].substring(dotIndex);
								if(IJ2EEModuleConstants.WAR_EXT.equals(extension)) {
									importDataModel = DataModelFactory.createDataModel(new WebComponentImportDataModelProvider());
								} else if(IJ2EEModuleConstants.RAR_EXT.equals(extension)) {
									importDataModel = DataModelFactory.createDataModel(new ConnectorComponentImportDataModelProvider());
								} else if(IJ2EEModuleConstants.EAR_EXT.equals(extension)) {
									importDataModel = DataModelFactory.createDataModel(new EARComponentImportDataModelProvider());
								} else if(IJ2EEModuleConstants.JAR_EXT.equals(extension)) {  									
									Archive archive = null;
									try {
										archive = CommonarchiveFactory.eINSTANCE.openArchive(names[i]);
										if(archive.isApplicationClientFile())
											importDataModel = DataModelFactory.createDataModel(new AppClientComponentImportDataModelProvider());
										else if(archive.isEJBJarFile())
											importDataModel = DataModelFactory.createDataModel(new EJBComponentImportDataModelProvider());
										else {
											performSimpleJarCopy = true; //handle Utility jars as regular jars.
											if(simpleJarsToCopyList == null){
												simpleJarsToCopyList = new ArrayList();
												simpleJarsToCopyList.add(names[i]);
											}
										}
										
									} finally {
										if(archive != null)
											archive.close();
									}
								} 
								
								if(importDataModel != null) {
									importDataModel.setStringProperty(IJ2EEComponentImportDataModelProperties.FILE_NAME, names[i]);
									importDataModel.setStringProperty(IJ2EEComponentImportDataModelProperties.PROJECT_NAME, filename);
									importDataModel.getDefaultOperation().execute(submonitor, null);
									
									createdComponents.add(importDataModel.getProperty(IJ2EEComponentImportDataModelProperties.COMPONENT));
									
								} else if(!performSimpleJarCopy){
									status.add(J2EENavigatorPlugin.createErrorStatus(0, NLS.bind(Messages.ImportJ2EEModuleDropAssistant_Could_not_recognize_extension_0_, extension), null));
								}
								
							} 
						} catch (Throwable e) {
							String msg = e.getMessage() != null ? e.getMessage() : e.toString();
							status.add(J2EENavigatorPlugin.createErrorStatus(0, msg, e));
						} 						
					}
					
					if(targetEARProject != null) {
						List createdModuleProjects = new ArrayList();
						for(int i=0; i<createdComponents.size(); i++) {
							IVirtualComponent component = (IVirtualComponent) createdComponents.get(i);							
							/* If this isn't an ear project, we can't add the newly created modules to it. */
							if(!hasEarFacet(component.getProject())) {
								createdModuleProjects.add(component.getProject());
							}
						}
						IDataModel dataModel = getAddModuleDataModel(targetEARProject, createdModuleProjects);
						IUndoableOperation dropOperation = dataModel.getDefaultOperation();
						IStatus addProjectsStatus = null;
						try {
							addProjectsStatus = dropOperation.execute(monitor, null);
							if(addProjectsStatus != null)
								status.add(addProjectsStatus);
						} catch (ExecutionException e) { 
							String msg = e.getMessage() != null ? e.getMessage() : e.toString();
							status.add(J2EENavigatorPlugin.createErrorStatus(0, msg, null));
						}
						//copy the simpleJarsOver
						if(simpleJarsToCopyList != null ){
							try{
								final String [] jarsToCopyArray = new String[simpleJarsToCopyList.size()];
								simpleJarsToCopyList.toArray(jarsToCopyArray);
								
								CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(shell);
								operation.copyFilesInCurrentThread(jarsToCopyArray, targetEARProject, monitor);
							}catch (Throwable e) {
								String msg = e.getMessage() != null ? e.getMessage() : e.toString();
								status.add(J2EENavigatorPlugin.createErrorStatus(0, msg, e));
							} 	
						}	
					}
					
					return status;
				}

			};
			
			service.showInDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), importArtifactsJob);
			importArtifactsJob.setRule(ResourcesPlugin.getWorkspace().getRoot());
			importArtifactsJob.schedule();
			return Status.OK_STATUS;
		}
		return Status.CANCEL_STATUS;
	}
	

	@Override
	public IStatus validateDrop(Object target, int operation, TransferData transferType) {
		IStatus status = Status.CANCEL_STATUS;
		if(FileTransfer.getInstance().isSupportedType(transferType)) {
			IProject project = null;
			if(target instanceof IWorkspaceRoot)		
				status = Status.OK_STATUS;
			else if( (project = getProject(target)) != null && hasEarFacet(project))
				status = Status.OK_STATUS;
		}
		return status;
	}

}

Back to the top