1 17 18 package org.apache.tools.ant.taskdefs.optional.ide; 19 20 import com.ibm.ivj.util.base.ExportCodeSpec; 21 import com.ibm.ivj.util.base.ImportCodeSpec; 22 import com.ibm.ivj.util.base.IvjException; 23 import com.ibm.ivj.util.base.Package; 24 import com.ibm.ivj.util.base.Project; 25 import com.ibm.ivj.util.base.ProjectEdition; 26 import com.ibm.ivj.util.base.ToolEnv; 27 import com.ibm.ivj.util.base.Type; 28 import com.ibm.ivj.util.base.Workspace; 29 import java.io.File ; 30 import java.util.Date ; 31 import java.util.Enumeration ; 32 import java.util.Vector ; 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.DirectoryScanner; 35 36 37 42 abstract class VAJLocalUtil implements VAJUtil { 43 private static Workspace workspace; 45 46 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 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 87 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 138 141 public void loadProjects(Vector projectDescriptions) { 142 Vector expandedDescs = getExpandedDescriptions(projectDescriptions); 143 144 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 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 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 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 ProjectEdition pe = null; 263 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 294 295 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 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 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 resources.addElement(files[i]); 407 } 408 } 409 } 410 411 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 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 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 } 460 | Popular Tags |