Skip to main content
summaryrefslogtreecommitdiffstats
blob: 523261126d5427241bfc5f5b2776d0da37bc9531 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 * Copyright (c) 2004, 2005 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
 *******************************************************************************/
/*
 *  $RCSfile: LocalFileConfigurationContributorController.java,v $
 *  $Revision: 1.13 $  $Date: 2005/10/26 18:48:19 $ 
 */
package org.eclipse.jem.internal.proxy.remote;

import java.net.URL;
import java.util.*;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.osgi.framework.Bundle;

import org.eclipse.jem.internal.proxy.core.*;
import org.eclipse.jem.util.plugin.JEMUtilPlugin;
 
/**
 * This version works with local files being added to the classpath, and the classpath is strings.
 * 
 * @since 1.0.0
 */
public class LocalFileConfigurationContributorController implements IConfigurationContributionController {
	
	private URL[] classpathInfo;
	private URL[][] bootpathInfo;
	private List classpath;
	private List prependBootpath;
	private List appendBootpath;
	private List javaLibraryPath;
	private ProxyLaunchSupport.LaunchInfo launchInfo;
	
	public LocalFileConfigurationContributorController(URL[] classpathInfo, URL[][] bootpathInfo, ProxyLaunchSupport.LaunchInfo launchInfo) {
		this.classpathInfo = classpathInfo;
		this.bootpathInfo = bootpathInfo;
		this.launchInfo = launchInfo;
	}
	
	public URL[] getFinalClasspath() {
		if (classpath == null)
			return classpathInfo;
		else
			return (URL[]) classpath.toArray(new URL[classpath.size()]);
	}

	public URL[] getFinalPrependBootpath() {
		if (prependBootpath == null)
			return bootpathInfo[0];
		else
			return (URL[]) prependBootpath.toArray(new URL[prependBootpath.size()]);
	}
	
	public URL[] getFinalAppendBootpath() {
		if (appendBootpath == null)
			return bootpathInfo[2];
		else
			return (URL[]) prependBootpath.toArray(new URL[appendBootpath.size()]);
	}	

	public List getFinalJavaLibraryPath() {
		if (javaLibraryPath == null)
			return Collections.EMPTY_LIST;
		else
			return javaLibraryPath;
	}
	
	protected List getClasspath() {
		if (classpath == null) {
			classpath = new ArrayList(classpathInfo.length);
			addAll(classpath, classpathInfo, -1);
		}
		return classpath;
	}
	
	protected List getPrependBootpath() {
		if (prependBootpath == null) {
			if (bootpathInfo[0] != null) {
				prependBootpath = new ArrayList(bootpathInfo[0].length);
				addAll(prependBootpath, bootpathInfo[0], -1);
			} else
				prependBootpath = new ArrayList(1);
		}
		return prependBootpath;
	}
	
	protected List getAppendBootpath() {
		if (appendBootpath == null) {
			if (bootpathInfo[2] != null) {
				appendBootpath = new ArrayList(bootpathInfo[2].length);
				addAll(appendBootpath, bootpathInfo[2], -1);
			} else
				appendBootpath = new ArrayList(1);
		}
		return appendBootpath;
	}	
	
	protected List getJavaLibraryPath() {
		if (javaLibraryPath == null) {
			javaLibraryPath = new ArrayList(3);
		}
		return javaLibraryPath;
	}
	
	protected void addAll(List toList, Object[] array, int loc) {
		for (int i = 0; i < array.length; i++) {
			// Need a dup check, sometimes dups come in especially during development
			if (toList.contains(array[i]))
				continue;
			if (loc == -1)
				toList.add(array[i]);
			else
				toList.add(loc++, array[i]);
		}
	}
	
	protected void addLocations(List toList, IRuntimeClasspathEntry[] entries) {
		for (int i = 0; i < entries.length; i++) {
			IRuntimeClasspathEntry entry = entries[i];
			URL location = ProxyLaunchSupport.convertStringPathToURL(entry.getLocation());
			if (location != null && !toList.contains(location))
				toList.add(location);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#getJavaProject()
	 */
	public IJavaProject getJavaProject() {
		return launchInfo.getJavaProject();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeProject(org.eclipse.core.resources.IProject)
	 */
	public void contributeProject(IProject project) throws CoreException {
		IJavaProject jproject = JavaCore.create(project);
		IRuntimeClasspathEntry[] projPath = JavaRuntime.computeUnresolvedRuntimeClasspath(jproject);
		boolean jreContainerFound= false;
		for (int i = 0; i < projPath.length; i++) {
			IRuntimeClasspathEntry entry= projPath[i];
			if (entry.getClasspathProperty() == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES || entry.getClasspathProperty() == IRuntimeClasspathEntry.STANDARD_CLASSES) {
				int entryKind= entry.getClasspathEntry().getEntryKind();
				String segment0= entry.getPath().segment(0);
				if (entryKind == IClasspathEntry.CPE_CONTAINER && JavaRuntime.JRE_CONTAINER.equals(segment0)
						|| entryKind == IClasspathEntry.CPE_VARIABLE && JavaRuntime.JRELIB_VARIABLE.equals(segment0)) {
					jreContainerFound= true;
				} else {
					if (jreContainerFound)
						addLocations(getAppendBootpath(), JavaRuntime.resolveRuntimeClasspathEntry(entry, jproject));
					else
						addLocations(getPrependBootpath(), JavaRuntime.resolveRuntimeClasspathEntry(entry, jproject));
				}
			} else {
				addLocations(getClasspath(), JavaRuntime.resolveRuntimeClasspathEntry(entry, jproject));
			}
		}			
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(java.lang.String, int)
	 */
	public void contributeClasspath(String classpath, int typeFlag) {
		contributeClasspath(ProxyLaunchSupport.convertStringPathToURL(classpath), typeFlag);
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(java.net.URL, int)
	 */
	public void contributeClasspath(URL classpathURL, int typeFlag) {
		if (classpathURL == null)
			return;
		switch (typeFlag) {
			case PREPEND_BOOT_CLASSPATH:
				if (!getPrependBootpath().contains(classpathURL))
					getPrependBootpath().add(classpathURL);
				break;
			case PREPEND_USER_CLASSPATH:
				if (!getClasspath().contains(classpathURL))
					getClasspath().add(0, classpathURL);
				break;
			case APPEND_USER_CLASSPATH:
				if (!getClasspath().contains(classpathURL))					
					getClasspath().add(classpathURL);
				break;				
			case APPEND_BOOT_CLASSPATH:
				if (!getAppendBootpath().contains(classpathURL))					
					getAppendBootpath().add(classpathURL);
				break;
			case APPEND_JAVA_LIBRARY_PATH:
				if (!getJavaLibraryPath().contains(classpathURL))					
					getJavaLibraryPath().add(classpathURL);
				break;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(org.osgi.framework.Bundle, java.lang.String, int, boolean)
	 */
	public void contributeClasspath(Bundle bundle, String relativePath, int typeFlag, boolean nlsLocalize) {
		// If not nls localize, or if it is java library path, then just find the one in the plugin/fragment and add it.
		if (nlsLocalize)
			contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeAllFromBundleAndFragments(bundle, relativePath), typeFlag);
		else if (typeFlag == IConfigurationContributionController.APPEND_JAVA_LIBRARY_PATH) {
			if (relativePath == null || relativePath.length() == 0) {
				// PDE is not here to help us extract that @#$ dll
			    JEMUtilPlugin.getLogger().log("Can't extract a directory from the root of a plugin."); //$NON-NLS-1$
			    return;
			}
			URL contribution = ProxyPlugin.getPlugin().urlLocalizeFromBundleAndFragments(bundle, relativePath);
			contributeClasspath(contribution, typeFlag);			
		} else {
			if (relativePath != null)
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeFromBundleOnly(bundle, relativePath), typeFlag);
			else
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeBundle(bundle), typeFlag);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(org.osgi.framework.Bundle, org.eclipse.core.runtime.IPath, int, boolean)
	 */
	public void contributeClasspath(Bundle bundle, IPath relativePath, int typeFlag, boolean nlsLocalize) {
		if (nlsLocalize)
			if (relativePath != null)
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeAllFromBundleAndFragments(bundle, relativePath), typeFlag);
			else
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeBundleAndFragments(bundle), typeFlag);
		else if (typeFlag == IConfigurationContributionController.APPEND_JAVA_LIBRARY_PATH) {
			if (relativePath == null || relativePath.segmentCount() == 0) {
				// PDE is not here to help us extract that @#$ dll
			    JEMUtilPlugin.getLogger().log("Can't extract a directory from the root of a plugin."); //$NON-NLS-1$
			    return;
			}
			URL contribution = ProxyPlugin.getPlugin().urlLocalizeFromBundleAndFragments(bundle, relativePath);
			contributeClasspath(contribution, typeFlag);			
		} else {
			if (relativePath != null)
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeFromBundleOnly(bundle, relativePath), typeFlag);
			else
				contributeClasspath(ProxyPlugin.getPlugin().urlLocalizeBundle(bundle), typeFlag);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(java.lang.String[], int)
	 */
	public void contributeClasspath(String[] classpaths, int typeFlag) {
		contributeClasspath(ProxyLaunchSupport.convertStringPathsToURL(classpaths), typeFlag);
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IConfigurationContributionController#contributeClasspath(java.net.URL[], int)
	 */
	public void contributeClasspath(URL[] classpathURLs, int typeFlag) {
		if (classpathURLs == null)
			return;
		switch (typeFlag) {
			case PREPEND_BOOT_CLASSPATH:
				addAll(getPrependBootpath(), classpathURLs, -1);
				break;
			case PREPEND_USER_CLASSPATH:
				addAll(getClasspath(), classpathURLs, 0);
				break;
			case APPEND_USER_CLASSPATH:
				addAll(getClasspath(), classpathURLs, -1);
				break;				
			case APPEND_BOOT_CLASSPATH:
				addAll(getAppendBootpath(), classpathURLs, -1);
				break;
			case APPEND_JAVA_LIBRARY_PATH:
				addAll(getJavaLibraryPath(), classpathURLs, -1);
				break;
		}
	}

}

Back to the top