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: 54d960356533be05e2ae7d24155ee49231a1215b (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/***************************************************************************************************
 * 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 Mar 25, 2004
 * 
 * To change the template for this generated file go to Window - Preferences - Java - Code
 * Generation - Code and Comments
 */
package org.eclipse.jst.j2ee.internal.web.operations;


import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
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.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jem.workbench.utility.JemProjectUtilities;

public class LibDirBuilder extends IncrementalProjectBuilder implements IResourceDeltaVisitor {

	//$NON-NLS-1$
	public static boolean TRACING = false;
	protected IProgressMonitor pMonitor = null;

	/**
	 * LibDirChangeListener constructor comment.
	 */
	public LibDirBuilder() {
		//Default constructor
	}

	/**
	 * Implemements a method in <code>IncrementalProjectBuilder</code>.
	 * 
	 * @see IncrementalProjectBuilder
	 */
	protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
		IResourceDelta delta = getDelta(getProject());
		boolean isFullBuild = (kind == IncrementalProjectBuilder.FULL_BUILD) || (delta == null);
		try {
			if (isFullBuild) {
				synch(getProject(), monitor);
			} else {
				pMonitor = monitor;
				delta.accept(this);
			}
		} catch (CoreException ex) {
			Logger.getLogger().log(ex);
		}
		return null;
	}

	/**
	 * Adds a new entry to the java runtime class path Creation date: (4/12/2001 1:22:03 PM)
	 * 
	 * @return boolean
	 * @param libentry_path
	 *            java.lang.String
	 */
	protected static boolean createLibEntry(IJavaProject javaProject, IPath library_path) {
		boolean added = false;

		try {
			WebProjectInfo projectInfo = new WebProjectInfo();
			projectInfo.setProjectName(javaProject.getProject().getName());

			IClasspathEntry[] cp = javaProject.getRawClasspath();

			//Try to make an entry for the java class path
			IClasspathEntry entry = JavaCore.newLibraryEntry(library_path, null, null);

			boolean addEntry = true;

			// Before the jar is added, search the existing classpath to make sure
			// it dose not already exist.
			for (int i = 0; i < cp.length; i++) {
				if (cp[i].equals(entry)) {
					addEntry = false;

				}

			}

			// Finally, add the jar if necessary

			if (addEntry) {

				IClasspathEntry[] newPath = new IClasspathEntry[cp.length + 1];
				int i = 0;
				for (i = 0; i < cp.length; i++) {
					if (i < cp.length) {
						newPath[i] = cp[i];
					}

				}
				newPath[i] = entry;

				javaProject.setRawClasspath(newPath, null);

			}

			added = true;

		} catch (JavaModelException ex) {
			Logger.getLogger().log(ex);
		}
		return added;
	}

	/**
	 * Removes entry to the java runtime class path Creation date: (4/12/2001 1:22:03 PM)
	 * 
	 * @return boolean
	 * @param libentry_path
	 *            java.lang.String
	 */
	protected static boolean removeLibEntry(IJavaProject javaProject, IPath library_path) {
		boolean added = false;
		try {
			WebProjectInfo projectInfo = new WebProjectInfo();
			projectInfo.setProjectName(javaProject.getProject().getName());

			IClasspathEntry[] cp = javaProject.getRawClasspath();

			//Make a class path entry to match with one you will remove
			IClasspathEntry entry = JavaCore.newLibraryEntry(library_path, null, null);

			int found = -1;
			// Before the jar is added, search the existing classpath to make sure
			// it dose not already exist.
			for (int i = 0; i < cp.length; i++) {
				if (cp[i].getPath().equals(entry.getPath())) {
					found = i;
				}

			}

			// Finally, remove the jar if necessary

			if (found != -1) {
				IClasspathEntry[] newPath = new IClasspathEntry[cp.length - 1];
				int i = 0;
				int pos = 0;
				for (i = 0; i <= newPath.length; i++) {
					if (i != found) {
						newPath[pos++] = cp[i];
					}
				}

				javaProject.setRawClasspath(newPath, null);
			}

			added = true;

		} catch (JavaModelException ex) {
			Logger.getLogger().log(ex);
		}
		return added;
	}

	/**
	 * Informs this builder that it is being started by the build management infrastructure. By the
	 * time this method is run, the builder's project is available and
	 * <code>setInitializationData</code> has been called.
	 * 
	 * @see BaseBuilder#startupOnInitialize()
	 */
	protected void startupOnInitialize() {
		super.startupOnInitialize();

		if (TRACING)
			Logger.getLogger().log(getClass().getName() + ProjectSupportResourceHandler.getString("24concat_INFO_", //$NON-NLS-1$
						(new Object[]{getProject()})));
		//$NON-NLS-1$ = ".startupOnInitialize() for "

	}

	/**
	 * Synchonizies the class path and the lib directories to catch any changes from the last use
	 * Creation date: (4/17/01 11:48:12 AM)
	 */
	protected static void synch(IProject project, IProgressMonitor monitor) {

		try {
			if (monitor == null) {
				monitor = new NullProgressMonitor();
			}
			monitor.beginTask(ProjectSupportResourceHandler.getString("Sychronize_Class_Path_UI_"), 4); //$NON-NLS-1$
			//$NON-NLS-1$ = "Sychronize Class Path"
            
			IContainer lib_folder = WebPropertiesUtil.getWebLibFolder(project);
			//Nothing to do if the lib folder does not exist.
			if (lib_folder == null || !lib_folder.isAccessible())
				return;
			IJavaProject javaProject = JemProjectUtilities.getJavaProject(project);
			IPath lib_path = lib_folder.getProjectRelativePath();
			IPath lib_full_path = lib_folder.getFullPath();

			IClasspathEntry[] cp = javaProject.getRawClasspath();

			boolean needsToBeModified = false;
			//Create a map of the lib projects in the current project
			Hashtable lib_jars = new Hashtable();
			IResource[] children = lib_folder.members();
			monitor.subTask(ProjectSupportResourceHandler.getString("Catalog_Lib_Directory__UI_")); //$NON-NLS-1$
			//$NON-NLS-1$ = "Catalog Lib Directory:"
			for (int j = 0; j < children.length; j++) {
				IResource child = children[j];
				//monitor.setTaskName(ResourceHandler.getString("Catalog_Lib_Directory__UI_") +
				// child); //$NON-NLS-1$ = "Catalog Lib Directory:"
				//Make sure it is a zip or a jar file
				if (child.getType() == IResource.FILE && (child.getFullPath().toString().toLowerCase().endsWith(".jar") //$NON-NLS-1$
							|| child.getFullPath().toString().toLowerCase().endsWith(".zip"))) { //$NON-NLS-1$
					lib_jars.put(child.getFullPath(), child);
				}

			}

			monitor.worked(1);
			monitor.subTask(ProjectSupportResourceHandler.getString("Update_ClassPath__UI_")); //$NON-NLS-1$
			//$NON-NLS-1$ = "Update ClassPath:"
			//Loop through all the classpath dirs looking for ones that may have
			//been deleted
			Vector newClassPathVector = new Vector();
			for (int j = 0; j < cp.length; j++) {

				//If it is a lib_path
				if (cp[j].getPath().toString().startsWith(lib_path.toString()) || cp[j].getPath().toString().startsWith(lib_full_path.toString())) {
					//It was already in the class path
					if (lib_jars.get(cp[j].getPath()) != null) {
						newClassPathVector.add(cp[j]);
						//Remove it from the hash table of paths to add back
						//monitor.setTaskName(ResourceHandler.getString("Catalog_Lib_Directory__UI_")
						// + cp[j].getPath()); //$NON-NLS-1$ = "Catalog Lib Directory:"
						lib_jars.remove(cp[j].getPath());

					} else {
						//You have removed something form the class path you
						//will need to re-build
						//monitor.setTaskName(ResourceHandler.getString("Catalog_Lib_Directory_Remo_UI_")
						// + cp[j].getPath()); //$NON-NLS-1$ = "Catalog Lib Directory:Remove "
						needsToBeModified = true;
					}
				} else {
					monitor.subTask(ProjectSupportResourceHandler.getString("Catalog_Lib_Directory__UI_") + cp[j].getPath()); //$NON-NLS-1$
					//$NON-NLS-1$ = "Catalog Lib Directory:"
					newClassPathVector.add(cp[j]);
				}
			}
			monitor.worked(1);
			monitor.subTask(ProjectSupportResourceHandler.getString("Update_ClassPath__UI_")); //$NON-NLS-1$
			//$NON-NLS-1$ = "Update ClassPath:"

			//Add any entries not already found
			Enumeration aenum = lib_jars.keys();
			while (aenum.hasMoreElements()) {
				IPath path = (IPath) aenum.nextElement();
				newClassPathVector.add(JavaCore.newLibraryEntry(path, null, null));
				//You have added something form the class path you
				//will need to re-build
				//monitor.setTaskName(ResourceHandler.getString("23concat_UI_", (new Object[] {
				// path }))); //$NON-NLS-1$ = "Catalog Lib Directory:Add {0}"
				needsToBeModified = true;
			}

			monitor.worked(1);
			monitor.subTask(ProjectSupportResourceHandler.getString("Set_ClassPath__UI_")); //$NON-NLS-1$
			//$NON-NLS-1$ = "Set ClassPath:"

			//Tansfer the vector to an array
			IClasspathEntry[] newClassPathArray = new IClasspathEntry[newClassPathVector.size()];

			for (int j = 0; j < newClassPathArray.length; j++) {
				newClassPathArray[j] = (IClasspathEntry) newClassPathVector.get(j);
			}

			//Only change the class path if there has been a modification
			if (needsToBeModified) {

				try {
					javaProject.setRawClasspath(newClassPathArray, monitor);
				} catch (Exception e) {
					Logger.getLogger().log(e);
				}
			}

		} catch (ClassCastException ex) {
			Logger.getLogger().log(ex);
		} catch (CoreException ex) {
			Logger.getLogger().log(ex);
		} finally {
			monitor.done();
		}

	}

	public boolean visit(IResourceDelta subdelta) throws CoreException {
		//Pull out resource
		try {
			IResource resource = subdelta.getResource();

			if (resource.getType() == IResource.FILE) {
				String filePath = subdelta.getFullPath().toString();
				//only allow .jar or .zip
				if (filePath.toLowerCase().endsWith(".jar") //$NON-NLS-1$
							|| filePath.toLowerCase().endsWith(".zip")) { //$NON-NLS-1$
					IProject project = resource.getProject();
					IJavaProject javaProject = JemProjectUtilities.getJavaProject(project);
					IPath lib_path = lib_path = project.getFullPath().append(WebPropertiesUtil.getWebLibFolder(project).getProjectRelativePath());
					int file_seg_count = subdelta.getFullPath().segmentCount();
					int lib_path_seg_count = lib_path.segmentCount();

					//File must be in the lib path and not a subdir
					if (filePath.startsWith(lib_path.toString()) && file_seg_count == lib_path_seg_count + 1) { //
						// Find out what happened
						//
						int kind = subdelta.getKind();
						switch (kind) {

							case IResourceDelta.ADDED :
								createLibEntry(javaProject, new Path(filePath));
								break;
							case IResourceDelta.REMOVED :
								removeLibEntry(javaProject, new Path(filePath));
								break;
							case IResourceDelta.ADDED_PHANTOM :
								break;
							case IResourceDelta.REMOVED_PHANTOM :
								break;
							case IResourceDelta.CHANGED :
								break;
						}

					}
				}
			} else if (resource.getType() == IResource.PROJECT) {
				synch(((IProject) resource), pMonitor);
			}
		} catch (ClassCastException ex) {
			//ignore it just means this is not a web project
		}
		return true;
	}
}

Back to the top