source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/ide/VAJLocalUtil.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 17.4 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.ide;
19
20import com.ibm.ivj.util.base.ExportCodeSpec;
21import com.ibm.ivj.util.base.ImportCodeSpec;
22import com.ibm.ivj.util.base.IvjException;
23import com.ibm.ivj.util.base.Package;
24import com.ibm.ivj.util.base.Project;
25import com.ibm.ivj.util.base.ProjectEdition;
26import com.ibm.ivj.util.base.ToolEnv;
27import com.ibm.ivj.util.base.Type;
28import com.ibm.ivj.util.base.Workspace;
29import java.io.File;
30import java.util.Date;
31import java.util.Enumeration;
32import java.util.Vector;
33import org.apache.tools.ant.BuildException;
34import org.apache.tools.ant.DirectoryScanner;
35
36
37/**
38 * Helper class for VAJ tasks. Holds Workspace singleton and
39 * wraps IvjExceptions into BuildExceptions
40 *
41 */
42abstract class VAJLocalUtil implements VAJUtil {
43 // singleton containing the VAJ workspace
44 private static Workspace workspace;
45
46 /**
47 * Wraps IvjException into a BuildException
48 *
49 * @return org.apache.tools.ant.BuildException
50 * @param errMsg Additional error message
51 * @param e IvjException which is wrapped
52 */
53 static BuildException createBuildException(
54 String errMsg, IvjException e) {
55 errMsg = errMsg + "\n" + e.getMessage();
56 String[] errors = e.getErrors();
57 if (errors != null) {
58 for (int i = 0; i < errors.length; i++) {
59 errMsg = errMsg + "\n" + errors[i];
60 }
61 }
62 return new BuildException(errMsg, e);
63 }
64
65 /**
66 * returns the current VAJ workspace.
67 * @return com.ibm.ivj.util.base.Workspace
68 */
69 static Workspace getWorkspace() {
70 if (workspace == null) {
71 workspace = ToolEnv.connectToWorkspace();
72 if (workspace == null) {
73 throw new BuildException(
74 "Unable to connect to Workspace! "
75 + "Make sure you are running in VisualAge for Java.");
76 }
77 }
78
79 return workspace;
80 }
81
82
83 //-----------------------------------------------------------
84 // export
85 //-----------------------------------------------------------
86
87 /**
88 * export packages
89 */
90 public void exportPackages(File dest,
91 String[] includePatterns, String[] excludePatterns,
92 boolean exportClasses, boolean exportDebugInfo,
93 boolean exportResources, boolean exportSources,
94 boolean useDefaultExcludes, boolean overwrite) {
95 if (includePatterns == null || includePatterns.length == 0) {
96 log("You must specify at least one include attribute. "
97 + "Not exporting", MSG_ERR);
98 } else {
99 try {
100 VAJWorkspaceScanner scanner = new VAJWorkspaceScanner();
101 scanner.setIncludes(includePatterns);
102 scanner.setExcludes(excludePatterns);
103 if (useDefaultExcludes) {
104 scanner.addDefaultExcludes();
105 }
106 scanner.scan();
107
108 Package[] packages = scanner.getIncludedPackages();
109
110 log("Exporting " + packages.length + " package(s) to "
111 + dest, MSG_INFO);
112 for (int i = 0; i < packages.length; i++) {
113 log(" " + packages[i].getName(), MSG_VERBOSE);
114 }
115
116 ExportCodeSpec exportSpec = new ExportCodeSpec();
117 exportSpec.setPackages(packages);
118 exportSpec.includeJava(exportSources);
119 exportSpec.includeClass(exportClasses);
120 exportSpec.includeResources(exportResources);
121 exportSpec.includeClassDebugInfo(exportDebugInfo);
122 exportSpec.useSubdirectories(true);
123 exportSpec.overwriteFiles(overwrite);
124 exportSpec.setExportDirectory(dest.getAbsolutePath());
125
126 getWorkspace().exportData(exportSpec);
127 } catch (IvjException ex) {
128 throw createBuildException("Exporting failed!", ex);
129 }
130 }
131 }
132
133
134 //-----------------------------------------------------------
135 // load
136 //-----------------------------------------------------------
137
138 /**
139 * Load specified projects.
140 */
141 public void loadProjects(Vector projectDescriptions) {
142 Vector expandedDescs = getExpandedDescriptions(projectDescriptions);
143
144 // output warnings for projects not found
145 for (Enumeration e = projectDescriptions.elements(); e.hasMoreElements();) {
146 VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
147 if (!d.projectFound()) {
148 log("No Projects match the name " + d.getName(), MSG_WARN);
149 }
150 }
151
152 log("Loading " + expandedDescs.size()
153 + " project(s) into workspace", MSG_INFO);
154
155 for (Enumeration e = expandedDescs.elements();
156 e.hasMoreElements();) {
157 VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
158
159 ProjectEdition pe;
160 if (d.getVersion().equals("*")) {
161 pe = findLatestProjectEdition(d.getName(), false);
162 } else if (d.getVersion().equals("**")) {
163 pe = findLatestProjectEdition(d.getName(), true);
164 } else {
165 pe = findProjectEdition(d.getName(), d.getVersion());
166 }
167 try {
168 log("Loading '" + pe.getName() + "', Version '"
169 + ((pe.getVersionName() != null) ? pe.getVersionName()
170 : "(" + pe.getVersionStamp() + ")")
171 + "' into Workspace", MSG_VERBOSE);
172 pe.loadIntoWorkspace();
173 } catch (IvjException ex) {
174 throw createBuildException("Project '" + d.getName()
175 + "' could not be loaded.", ex);
176 }
177 }
178 }
179
180 /**
181 * return project descriptions containing full project names instead
182 * of patterns with wildcards.
183 */
184 private Vector getExpandedDescriptions(Vector projectDescs) {
185 Vector expandedDescs = new Vector(projectDescs.size());
186 try {
187 String[] projectNames =
188 getWorkspace().getRepository().getProjectNames();
189 for (int i = 0; i < projectNames.length; i++) {
190 for (Enumeration e = projectDescs.elements();
191 e.hasMoreElements();) {
192 VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
193 String pattern = d.getName();
194 if (VAJWorkspaceScanner.match(pattern, projectNames[i])) {
195 d.setProjectFound();
196 expandedDescs.addElement(new VAJProjectDescription(projectNames[i],
197 d.getVersion()));
198 break;
199 }
200 }
201 }
202 } catch (IvjException e) {
203 throw createBuildException("VA Exception occurred: ", e);
204 }
205
206 return expandedDescs;
207 }
208
209 /**
210 * Finds a specific project edition in the repository.
211 *
212 * @param name project name
213 * @param versionName project version name
214 * @return com.ibm.ivj.util.base.ProjectEdition the specified edition
215 */
216 private ProjectEdition findProjectEdition(
217 String name, String versionName) {
218 try {
219 ProjectEdition[] editions = null;
220 editions = getWorkspace().getRepository().getProjectEditions(name);
221
222 if (editions == null) {
223 throw new BuildException("Project " + name + " doesn't exist");
224 }
225
226 ProjectEdition pe = null;
227 for (int i = 0; i < editions.length && pe == null; i++) {
228 if (versionName.equals(editions[i].getVersionName())) {
229 pe = editions[i];
230 }
231 }
232 if (pe == null) {
233 throw new BuildException("Version " + versionName
234 + " of Project " + name + " doesn't exist");
235 }
236 return pe;
237
238 } catch (IvjException e) {
239 throw createBuildException("VA Exception occurred: ", e);
240 }
241
242 }
243
244 /**
245 * Finds the latest project edition in the repository.
246 *
247 * @param name project name
248 * @param includeOpenEditions include open/scratch editions in the search?
249 * @return com.ibm.ivj.util.base.ProjectEdition the specified edition
250 */
251 private ProjectEdition findLatestProjectEdition(
252 String name,
253 boolean includeOpenEditions) {
254 try {
255 ProjectEdition[] editions = null;
256 editions = getWorkspace().getRepository().getProjectEditions(name);
257 if (editions == null) {
258 throw new BuildException("Project " + name + " doesn't exist");
259 }
260
261 // find latest (versioned) project edition by date
262 ProjectEdition pe = null;
263 // Let's hope there are no projects older than the epoch ;-)
264 Date latestStamp = new Date(0);
265 for (int i = 0; i < editions.length; i++) {
266 if (!includeOpenEditions && !editions[i].isVersion()) {
267 continue;
268 }
269 if (latestStamp.before(editions[i].getVersionStamp())) {
270 latestStamp = editions[i].getVersionStamp();
271 pe = editions[i];
272 }
273 }
274
275 if (pe == null) {
276 throw new BuildException("Can't determine latest edition for project " + name);
277 }
278 log("Using version " + ((pe.getVersionName() != null) ? pe.getVersionName()
279 : "(" + pe.getVersionStamp() + ")")
280 + " of " + pe.getName(), MSG_INFO);
281 return pe;
282 } catch (IvjException e) {
283 throw createBuildException("VA Exception occurred: ", e);
284 }
285
286 }
287
288
289
290 //-----------------------------------------------------------
291 // import
292 //-----------------------------------------------------------
293
294
295 /**
296 * Do the import.
297 */
298 public void importFiles(
299 String importProject, File srcDir,
300 String[] includePatterns, String[] excludePatterns,
301 boolean importClasses, boolean importResources,
302 boolean importSources, boolean useDefaultExcludes)
303 throws BuildException {
304
305 if (importProject == null || "".equals(importProject)) {
306 throw new BuildException("The VisualAge for Java project "
307 + "name is required!");
308 }
309
310 ImportCodeSpec importSpec = new ImportCodeSpec();
311 importSpec.setDefaultProject(getVAJProject(importProject));
312
313 DirectoryScanner ds = new DirectoryScanner();
314 ds.setBasedir(srcDir);
315 ds.setIncludes(includePatterns);
316 ds.setExcludes(excludePatterns);
317 if (useDefaultExcludes) {
318 ds.addDefaultExcludes();
319 }
320 ds.scan();
321
322 Vector classes = new Vector();
323 Vector sources = new Vector();
324 Vector resources = new Vector();
325
326 scanForImport(srcDir, ds.getIncludedFiles(), classes, sources, resources);
327
328 StringBuffer summaryLog = new StringBuffer("Importing ");
329 addFilesToImport(importSpec, importClasses, classes, "Class", summaryLog);
330 addFilesToImport(importSpec, importSources, sources, "Java", summaryLog);
331 addFilesToImport(importSpec, importResources, resources, "Resource", summaryLog);
332 importSpec.setResourcePath(srcDir.getAbsolutePath());
333
334 summaryLog.append(" into the project '");
335 summaryLog.append(importProject);
336 summaryLog.append("'.");
337
338 log(summaryLog.toString(), MSG_INFO);
339
340 try {
341 Type[] importedTypes = getWorkspace().importData(importSpec);
342 if (importedTypes == null) {
343 throw new BuildException("Unable to import into Workspace!");
344 } else {
345 log(importedTypes.length + " types imported", MSG_DEBUG);
346 for (int i = 0; i < importedTypes.length; i++) {
347 log(importedTypes[i].getPackage().getName()
348 + "." + importedTypes[i].getName()
349 + " into " + importedTypes[i].getProject().getName(),
350 MSG_DEBUG);
351 }
352 }
353 } catch (IvjException ivje) {
354 throw createBuildException("Error while importing into workspace: ",
355 ivje);
356 }
357 }
358
359 /**
360 * get a project from the Workspace.
361 */
362 static Project getVAJProject(String importProject) {
363 Project found = null;
364 Project[] currentProjects = getWorkspace().getProjects();
365
366 for (int i = 0; i < currentProjects.length; i++) {
367 Project p = currentProjects[i];
368 if (p.getName().equals(importProject)) {
369 found = p;
370 break;
371 }
372 }
373
374
375 if (found == null) {
376 try {
377 found = getWorkspace().createProject(importProject, true);
378 } catch (IvjException e) {
379 throw createBuildException("Error while creating Project "
380 + importProject + ": ", e);
381 }
382 }
383
384 return found;
385 }
386
387
388 /**
389 * Sort the files into classes, sources, and resources.
390 */
391 private void scanForImport(
392 File dir,
393 String[] files,
394 Vector classes,
395 Vector sources,
396 Vector resources) {
397 for (int i = 0; i < files.length; i++) {
398 String file = (new File(dir, files[i])).getAbsolutePath();
399 if (file.endsWith(".java") || file.endsWith(".JAVA")) {
400 sources.addElement(file);
401 } else
402 if (file.endsWith(".class") || file.endsWith(".CLASS")) {
403 classes.addElement(file);
404 } else {
405 // for resources VA expects the path relative to the resource path
406 resources.addElement(files[i]);
407 }
408 }
409 }
410
411 /**
412 * Adds files to an import specification. Helper method
413 * for importFiles()
414 *
415 * @param spec import specification
416 * @param doImport only add files if doImport is true
417 * @param files the files to add
418 * @param fileType type of files (Source/Class/Resource)
419 * @param summaryLog buffer for logging
420 */
421 private void addFilesToImport(ImportCodeSpec spec, boolean doImport,
422 Vector files, String fileType,
423 StringBuffer summaryLog) {
424
425 if (doImport) {
426 String[] fileArr = new String[files.size()];
427 files.copyInto(fileArr);
428 try {
429 // here it is assumed that fileType is one of the
430 // following strings: // "Java", "Class", "Resource"
431 String methodName = "set" + fileType + "Files";
432 Class[] methodParams = new Class[]{fileArr.getClass()};
433 java.lang.reflect.Method method =
434 spec.getClass().getDeclaredMethod(methodName, methodParams);
435 method.invoke(spec, new Object[]{fileArr});
436 } catch (Exception e) {
437 throw new BuildException(e);
438 }
439 if (files.size() > 0) {
440 logFiles(files, fileType);
441 summaryLog.append(files.size());
442 summaryLog.append(" " + fileType.toLowerCase() + " file");
443 summaryLog.append(files.size() > 1 ? "s, " : ", ");
444 }
445 }
446 }
447
448 /**
449 * Logs a list of file names to the message log
450 * @param fileNames java.util.Vector file names to be logged
451 * @param type java.lang.String file type
452 */
453 private void logFiles(Vector fileNames, String fileType) {
454 log(fileType + " files found for import:", MSG_VERBOSE);
455 for (Enumeration e = fileNames.elements(); e.hasMoreElements();) {
456 log(" " + e.nextElement(), MSG_VERBOSE);
457 }
458 }
459}
Note: See TracBrowser for help on using the repository browser.