Skip to main content
summaryrefslogtreecommitdiffstats
blob: cf2b266f26a862c86375e9e2d706a583c95bfa62 (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
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006 Springsite BV (The Netherlands) 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:
 *   Martin Taal
 * </copyright>
 *
 * $Id: RunGenerateJob.java,v 1.3 2006/09/08 22:57:34 mtaal Exp $
 */

package org.eclipse.emf.teneo.eclipse.genxml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.emf.teneo.eclipse.StoreEclipseException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;

/**
 * Generate a mapping file in a separate java program to use the classpath of the selected projects.
 */
public class RunGenerateJob extends WorkspaceJob implements IJavaLaunchConfigurationConstants {

	/** Standard name of the generation action */
	private static final String CONFIGURATION_NAME = "Elver JDO Generation";

	/** The list of java projects */
	private ArrayList jProjects; // IJavaProject

	/** Array of absolute paths to ecore files which are to be parsed */
	private String[] ecores;

	/** The location to put the result */
	private String targetFileName;

	/** The name of the main class */
	private String mainClass;

	/** The options */
	private HashMap options;

	/**
	 * The constructor.
	 */
	public RunGenerateJob(ArrayList jProjects, String[] ecores, String targetFileName, String mainClass, HashMap options) {
		super("Generate Mapping File");
		this.jProjects = jProjects;
		this.ecores = ecores;
		this.targetFileName = targetFileName;
		this.mainClass = mainClass;
		this.options = options;
	}

	/** Launches a separate java process using the classpaths of the passed projects */

	public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
		// first delete the old generation of the same name
		ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
		ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
		ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
		for (int i = 0; i < configurations.length; i++) {
			if (configurations[i].getName().compareTo(CONFIGURATION_NAME) == 0) {
				configurations[i].delete();
			}
		}
		
		// create a new launch configuration
		ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, CONFIGURATION_NAME);

		workingCopy.setAttribute(ATTR_DEFAULT_CLASSPATH, false);
		workingCopy.setAttribute(ATTR_CLASSPATH, determineClassPath());
		workingCopy.setAttribute(ATTR_MAIN_TYPE_NAME, mainClass);
		workingCopy.setAttribute(ATTR_PROJECT_NAME, ((IJavaProject) jProjects.get(0)).getProject().getName());
		//workingCopy.setAttribute(ATTR_WORKING_DIRECTORY, workingDir);
		workingCopy.setAttribute(ATTR_PROGRAM_ARGUMENTS, getProgramArguments());
		workingCopy.doSave();
		workingCopy.launch(ILaunchManager.RUN_MODE, null);
		return Status.OK_STATUS;
	}
	
	/** Collect the classpath as a list of strings */
	private ArrayList determineClassPath() throws CoreException {
		ArrayList result = new ArrayList();

		for (int i = 0; i < jProjects.size(); i++) {
			IJavaProject jProject = (IJavaProject) jProjects.get(i);
			IRuntimeClasspathEntry outputEntry = JavaRuntime.newDefaultProjectClasspathEntry(jProject);
			result.add(outputEntry.getMemento());
		}

		IPath systemLibsPath = new Path(JavaRuntime.JRE_CONTAINER);
		IRuntimeClasspathEntry systemLibsEntry = JavaRuntime.newRuntimeContainerClasspathEntry(systemLibsPath,
				IRuntimeClasspathEntry.STANDARD_CLASSES);
		result.add(systemLibsEntry.getMemento());

		return result;
	}
	
	/** Determine the classnames of the interfaces */
	private String getEPackageNames() {
		try {
			final ArrayList result = new ArrayList();
			for (int i = 0; i < jProjects.size(); i++) {
				final IJavaProject ijp = (IJavaProject)jProjects.get(i);
				for (int j = 0; j < ijp.getPackageFragmentRoots().length; j++) {
					IPackageFragmentRoot ipf = ijp.getPackageFragmentRoots()[j];
					if (ipf instanceof JarPackageFragmentRoot) continue;
					for (int c = 0; c < ipf.getChildren().length; c++) {
						walk(ipf.getChildren()[c], "", result);
					}
				}
			}
			
			StringBuffer resultStr = new StringBuffer();
			for (int i = 0; i < result.size(); i++) {
				final String epackage = (String)result.get(i);
				if (i > 0) resultStr.append(",");
				resultStr.append(epackage);
			}
			return resultStr.toString();
		} catch (Exception e) {
			throw new StoreEclipseException("Exception while looking for epackages in projects " + e.getMessage(), e);
		}
		
	}

	/** Walk the java element structure looking for EPackage sources */
	private void walk(IJavaElement ije, String pName, ArrayList result) throws Exception {
		if (ije instanceof JarPackageFragmentRoot || ije instanceof JarPackageFragmentRoot) return;
		if (!(ije instanceof PackageFragment)) return;

		final PackageFragment pf = (PackageFragment)ije;
		final String packageName = pName + (pName.length() > 0 ? "."  : "") + pf.getElementName();
		for (int c = 0; c < pf.getChildren().length; c++) {
			final IJavaElement child = pf.getChildren()[c];
			if (child instanceof PackageFragment) {
				walk(child, packageName, result);
			} else if (child instanceof ICompilationUnit){
				// a source, check if it implements an EPackage
				// TODO make this more robust for other packages which are also EPackages
				final ICompilationUnit icu = (ICompilationUnit)child;
				final String[] types = 	icu.getTypes()[0].getSuperInterfaceNames();
				for (int i = 0; i < types.length; i++) {
					if (types[i].compareTo("EPackage") == 0) {
						result.add(packageName + "." + icu.getTypes()[0].getElementName());
						return;
					}
				}
			}
		}
	}

	/** Returns the program arguments as a string */
	private String getProgramArguments() {
		StringBuffer args = new StringBuffer();
		append(args, targetFileName);
		Iterator keys = options.keySet().iterator();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			String value = (String) options.get(key);
			append(args, "+" + key + "," + value);
		}

		for (int i = 0; i < ecores.length; i++) {
			append(args, ecores[i]);
		}
		
		append(args, "-" + getEPackageNames());
		
		return args.toString();
	}

	/** Appends a program argument taking into account " and space */
	private void append(StringBuffer buffer, String value) {
		buffer.append("\"" + value + "\" ");
	}
}

Back to the top