Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 445   Methods: 11
NCLOC: 267   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VAJLocalUtil.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.ide;
 56   
 
 57   
 import com.ibm.ivj.util.base.ExportCodeSpec;
 58   
 import com.ibm.ivj.util.base.ImportCodeSpec;
 59   
 import com.ibm.ivj.util.base.IvjException;
 60   
 import com.ibm.ivj.util.base.Package;
 61   
 import com.ibm.ivj.util.base.Project;
 62   
 import com.ibm.ivj.util.base.ProjectEdition;
 63   
 import com.ibm.ivj.util.base.ToolEnv;
 64   
 import com.ibm.ivj.util.base.Type;
 65   
 import com.ibm.ivj.util.base.Workspace;
 66   
 import java.io.File;
 67   
 import java.util.Enumeration;
 68   
 import java.util.Vector;
 69   
 import org.apache.tools.ant.BuildException;
 70   
 import org.apache.tools.ant.DirectoryScanner;
 71   
 
 72   
 
 73   
 /**
 74   
  * Helper class for VAJ tasks. Holds Workspace singleton and
 75   
  * wraps IvjExceptions into BuildExceptions
 76   
  *
 77   
  * @author Wolf Siberski, TUI Infotec GmbH
 78   
  */
 79   
 abstract class VAJLocalUtil implements VAJUtil{
 80   
     // singleton containing the VAJ workspace
 81   
     private static Workspace workspace;
 82   
 
 83   
     /**
 84   
      * Wraps IvjException into a BuildException
 85   
      *
 86   
      * @return org.apache.tools.ant.BuildException
 87   
      * @param errMsg Additional error message
 88   
      * @param e IvjException which is wrapped
 89   
      */
 90  0
     static BuildException createBuildException(
 91   
                                                String errMsg, IvjException e) {
 92  0
         errMsg = errMsg + "\n" + e.getMessage();
 93  0
         String[] errors = e.getErrors();
 94  0
         if (errors != null) {
 95  0
             for (int i = 0; i < errors.length; i++) {
 96  0
                 errMsg = errMsg + "\n" + errors[i];
 97   
             }
 98   
         }
 99  0
         return new BuildException(errMsg, e);
 100   
     }
 101   
 
 102   
     /**
 103   
      * returns the current VAJ workspace.
 104   
      * @return com.ibm.ivj.util.base.Workspace
 105   
      */
 106  0
     static Workspace getWorkspace() {
 107  0
         if (workspace == null) {
 108  0
             workspace = ToolEnv.connectToWorkspace();
 109  0
             if (workspace == null) {
 110  0
                 throw new BuildException(
 111   
                                          "Unable to connect to Workspace! "
 112   
                                          + "Make sure you are running in VisualAge for Java.");
 113   
             }
 114   
         }
 115   
 
 116  0
         return workspace;
 117   
     }
 118   
 
 119   
 
 120   
     //-----------------------------------------------------------
 121   
     // export
 122   
     //-----------------------------------------------------------
 123   
 
 124   
     /**
 125   
      * export packages
 126   
      */
 127  0
     public void exportPackages(
 128   
                                File dest,
 129   
                                String[] includePatterns, String[] excludePatterns,
 130   
                                boolean exportClasses, boolean exportDebugInfo,
 131   
                                boolean exportResources, boolean exportSources,
 132   
                                boolean useDefaultExcludes,    boolean overwrite) {
 133  0
         if (includePatterns == null || includePatterns.length == 0) {
 134  0
             log("You must specify at least one include attribute. "
 135   
                 + "Not exporting", MSG_ERR);
 136   
         } else {
 137  0
             try {
 138  0
                 VAJWorkspaceScanner scanner = new VAJWorkspaceScanner();
 139  0
                 scanner.setIncludes(includePatterns);
 140  0
                 scanner.setExcludes(excludePatterns);
 141  0
                 if (useDefaultExcludes) {
 142  0
                     scanner.addDefaultExcludes();
 143   
                 }
 144  0
                 scanner.scan();
 145   
 
 146  0
                 Package[] packages = scanner.getIncludedPackages();
 147   
 
 148  0
                 log("Exporting " + packages.length + " package(s) to "
 149   
                     + dest, MSG_INFO);
 150  0
                 for (int i = 0; i < packages.length; i++) {
 151  0
                     log("    " + packages[i].getName(), MSG_VERBOSE);
 152   
                 }
 153   
 
 154  0
                 ExportCodeSpec exportSpec = new ExportCodeSpec();
 155  0
                 exportSpec.setPackages(packages);
 156  0
                 exportSpec.includeJava(exportSources);
 157  0
                 exportSpec.includeClass(exportClasses);
 158  0
                 exportSpec.includeResources(exportResources);
 159  0
                 exportSpec.includeClassDebugInfo(exportDebugInfo);
 160  0
                 exportSpec.useSubdirectories(true);
 161  0
                 exportSpec.overwriteFiles(overwrite);
 162  0
                 exportSpec.setExportDirectory(dest.getAbsolutePath());
 163   
 
 164  0
                 getWorkspace().exportData(exportSpec);
 165   
             } catch (IvjException ex) {
 166  0
                 throw createBuildException("Exporting failed!", ex);
 167   
             }
 168   
         }
 169   
     }
 170   
 
 171   
 
 172   
     //-----------------------------------------------------------
 173   
     // load
 174   
     //-----------------------------------------------------------
 175   
 
 176   
     /**
 177   
      * Load specified projects.
 178   
      */
 179  0
     public void loadProjects(Vector projectDescriptions) {
 180  0
         Vector expandedDescs = getExpandedDescriptions(projectDescriptions);
 181   
 
 182   
         // output warnings for projects not found
 183  0
         for (Enumeration e = projectDescriptions.elements(); e.hasMoreElements();) {
 184  0
             VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
 185  0
             if (!d.projectFound()) {
 186  0
                 log("No Projects match the name " + d.getName(), MSG_WARN);
 187   
             }
 188   
         }
 189   
 
 190  0
         log("Loading " + expandedDescs.size()
 191   
             + " project(s) into workspace", MSG_INFO);
 192   
 
 193  0
         for (Enumeration e = expandedDescs.elements();
 194  0
              e.hasMoreElements();) {
 195  0
             VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
 196   
 
 197  0
             ProjectEdition pe = findProjectEdition(d.getName(), d.getVersion());
 198  0
             try {
 199  0
                 log("Loading '" + d.getName() + "', Version '" + d.getVersion()
 200   
                     + "', into Workspace", MSG_VERBOSE);
 201  0
                 pe.loadIntoWorkspace();
 202   
             } catch (IvjException ex) {
 203  0
                 throw createBuildException("Project '" + d.getName()
 204   
                                             + "' could not be loaded.", ex);
 205   
             }
 206   
         }
 207   
     }
 208   
 
 209   
     /**
 210   
      * return project descriptions containing full project names instead
 211   
      * of patterns with wildcards.
 212   
      */
 213  0
     private Vector getExpandedDescriptions(Vector projectDescs) {
 214  0
         Vector expandedDescs = new Vector(projectDescs.size());
 215  0
         try {
 216  0
             String[] projectNames =
 217   
                 getWorkspace().getRepository().getProjectNames();
 218  0
             for (int i = 0; i < projectNames.length; i++) {
 219  0
                 for (Enumeration e = projectDescs.elements();
 220  0
                      e.hasMoreElements();) {
 221  0
                     VAJProjectDescription d = (VAJProjectDescription) e.nextElement();
 222  0
                     String pattern = d.getName();
 223  0
                     if (VAJWorkspaceScanner.match(pattern, projectNames[i])) {
 224  0
                         d.setProjectFound();
 225  0
                         expandedDescs.addElement(new VAJProjectDescription(
 226   
                                                                            projectNames[i], d.getVersion()));
 227  0
                         break;
 228   
                     }
 229   
                 }
 230   
             }
 231   
         } catch (IvjException e) {
 232  0
             throw createBuildException("VA Exception occured: ", e);
 233   
         }
 234   
 
 235  0
         return expandedDescs;
 236   
     }
 237   
 
 238   
     /**
 239   
      * Finds a specific project edition in the repository.
 240   
      *
 241   
      * @param name project name
 242   
      * @param versionName project version name
 243   
      * @return com.ibm.ivj.util.base.ProjectEdition the specified edition
 244   
      */
 245  0
     private ProjectEdition findProjectEdition(
 246   
                                               String name, String versionName) {
 247  0
         try {
 248  0
             ProjectEdition[] editions = null;
 249  0
             editions = getWorkspace().getRepository().getProjectEditions(name);
 250   
 
 251  0
             if (editions == null) {
 252  0
                 throw new BuildException("Project " + name + " doesn't exist");
 253   
             }
 254   
 
 255  0
             ProjectEdition pe = null;
 256  0
             for (int i = 0; i < editions.length && pe == null; i++) {
 257  0
                 if (versionName.equals(editions[i].getVersionName())) {
 258  0
                     pe = editions[i];
 259   
                 }
 260   
             }
 261  0
             if (pe == null) {
 262  0
                 throw new BuildException("Version " + versionName
 263   
                     + " of Project " + name + " doesn't exist");
 264   
             }
 265  0
             return pe;
 266   
 
 267   
         } catch (IvjException e) {
 268  0
             throw createBuildException("VA Exception occured: ", e);
 269   
         }
 270   
 
 271   
     }
 272   
 
 273   
 
 274   
 
 275   
     //-----------------------------------------------------------
 276   
     // import
 277   
     //-----------------------------------------------------------
 278   
 
 279   
 
 280   
     /**
 281   
      * Do the import.
 282   
      */
 283  0
     public void importFiles(
 284   
                             String importProject, File srcDir,
 285   
                             String[] includePatterns, String[] excludePatterns,
 286   
                             boolean importClasses, boolean importResources,
 287   
                             boolean importSources, boolean useDefaultExcludes)
 288   
         throws BuildException {
 289   
 
 290  0
         if (importProject == null || "".equals(importProject)) {
 291  0
             throw new BuildException("The VisualAge for Java project "
 292   
                                      + "name is required!");
 293   
         }
 294   
 
 295  0
         ImportCodeSpec importSpec = new ImportCodeSpec();
 296  0
         importSpec.setDefaultProject(getVAJProject(importProject));
 297   
 
 298  0
         DirectoryScanner ds = new DirectoryScanner();
 299  0
         ds.setBasedir(srcDir);
 300  0
         ds.setIncludes(includePatterns);
 301  0
         ds.setExcludes(excludePatterns);
 302  0
         if (useDefaultExcludes) {
 303  0
             ds.addDefaultExcludes();
 304   
         }
 305  0
         ds.scan();
 306   
 
 307  0
         Vector classes = new Vector();
 308  0
         Vector sources = new Vector();
 309  0
         Vector resources = new Vector();
 310   
 
 311  0
         scanForImport(srcDir, ds.getIncludedFiles(), classes, sources, resources);
 312   
 
 313  0
         StringBuffer summaryLog = new StringBuffer("Importing ");
 314  0
         addFilesToImport(importSpec, importClasses, classes, "Class", summaryLog);
 315  0
         addFilesToImport(importSpec, importSources, sources, "Java", summaryLog);
 316  0
         addFilesToImport(importSpec, importResources, resources, "Resource", summaryLog);
 317  0
         importSpec.setResourcePath(srcDir.getAbsolutePath());
 318   
 
 319  0
         summaryLog.append(" into the project '");
 320  0
         summaryLog.append(importProject);
 321  0
         summaryLog.append("'.");
 322   
 
 323  0
         log(summaryLog.toString(), MSG_INFO);
 324   
 
 325  0
         try {
 326  0
             Type[] importedTypes = getWorkspace().importData(importSpec);
 327  0
             if (importedTypes == null) {
 328  0
                 throw new BuildException("Unable to import into Workspace!");
 329   
             } else {
 330  0
                 log(importedTypes.length + " types imported", MSG_DEBUG);
 331  0
                 for (int i = 0; i < importedTypes.length; i++) {
 332  0
                     log(importedTypes[i].getPackage().getName()
 333   
                         + "." + importedTypes[i].getName()
 334   
                         + " into " + importedTypes[i].getProject().getName(),
 335   
                         MSG_DEBUG);
 336   
                 }
 337   
             }
 338   
         } catch (IvjException ivje) {
 339  0
             throw createBuildException("Error while importing into workspace: ",
 340   
                                        ivje);
 341   
         }
 342   
     }
 343   
 
 344   
     /**
 345   
      * get a project from the Workspace.
 346   
      */
 347  0
     static Project getVAJProject(String importProject) {
 348  0
         Project found = null;
 349  0
         Project[] currentProjects = getWorkspace().getProjects();
 350   
 
 351  0
         for (int i = 0; i < currentProjects.length; i++) {
 352  0
             Project p = currentProjects[i];
 353  0
             if (p.getName().equals(importProject)) {
 354  0
                 found = p;
 355  0
                 break;
 356   
             }
 357   
         }
 358   
 
 359   
 
 360  0
         if (found == null) {
 361  0
             try {
 362  0
                 found = getWorkspace().createProject(importProject, true);
 363   
             } catch (IvjException e) {
 364  0
                 throw createBuildException("Error while creating Project "
 365   
                                            + importProject + ": ", e);
 366   
             }
 367   
         }
 368   
 
 369  0
         return found;
 370   
     }
 371   
 
 372   
 
 373   
     /**
 374   
      * Sort the files into classes, sources, and resources.
 375   
      */
 376  0
     private void scanForImport(
 377   
                                File dir,
 378   
                                String[] files,
 379   
                                Vector classes,
 380   
                                Vector sources,
 381   
                                Vector resources) {
 382  0
         for (int i = 0; i < files.length; i++) {
 383  0
             String file = (new File(dir, files[i])).getAbsolutePath();
 384  0
             if (file.endsWith(".java") || file.endsWith(".JAVA")) {
 385  0
                 sources.addElement(file);
 386   
             } else
 387  0
                 if (file.endsWith(".class") || file.endsWith(".CLASS")) {
 388  0
                     classes.addElement(file);
 389   
                 } else {
 390   
                     // for resources VA expects the path relative to the resource path
 391  0
                     resources.addElement(files[i]);
 392   
                 }
 393   
         }
 394   
     }
 395   
 
 396   
     /**
 397   
      * Adds files to an import specification. Helper method
 398   
      * for importFiles()
 399   
      *
 400   
      * @param spec       import specification
 401   
      * @param doImport   only add files if doImport is true
 402   
      * @param files      the files to add
 403   
      * @param fileType   type of files (Source/Class/Resource)
 404   
      * @param summaryLog buffer for logging
 405   
      */
 406  0
     private void addFilesToImport(ImportCodeSpec spec, boolean doImport,
 407   
                                   Vector files, String fileType,
 408   
                                   StringBuffer summaryLog) {
 409   
 
 410  0
         if (doImport) {
 411  0
             String[] fileArr = new String[files.size()];
 412  0
             files.copyInto(fileArr);
 413  0
             try {
 414   
                 // here it is assumed that fileType is one of the
 415   
                 // following strings: // "Java", "Class", "Resource"
 416  0
                 String methodName = "set" + fileType + "Files";
 417  0
                 Class[] methodParams = new Class[]{fileArr.getClass()};
 418  0
                 java.lang.reflect.Method method =
 419   
                     spec.getClass().getDeclaredMethod(methodName, methodParams);
 420  0
                 method.invoke(spec, new Object[]{fileArr});
 421   
             } catch (Exception e) {
 422  0
                 throw new BuildException(e);
 423   
             }
 424  0
             if (files.size() > 0) {
 425  0
                 logFiles(files, fileType);
 426  0
                 summaryLog.append(files.size());
 427  0
                 summaryLog.append(" " + fileType.toLowerCase() + " file");
 428  0
                 summaryLog.append(files.size() > 1 ? "s, " : ", ");
 429   
             }
 430   
         }
 431   
     }
 432   
 
 433   
     /**
 434   
      * Logs a list of file names to the message log
 435   
      * @param fileNames java.util.Vector file names to be logged
 436   
      * @param type java.lang.String file type
 437   
      */
 438  0
     private void logFiles(Vector fileNames, String fileType) {
 439  0
         log(fileType + " files found for import:", MSG_VERBOSE);
 440  0
         for (Enumeration e = fileNames.elements(); e.hasMoreElements();) {
 441  0
             log("    " + e.nextElement(), MSG_VERBOSE);
 442   
         }
 443   
     }
 444   
 }
 445