Coverage report

  %line %branch
nl.toolforge.karma.core.cmd.impl.BuildModule
0% 
0% 

 1  
 /*
 2  
 Karma core - Core of the Karma application
 3  
 Copyright (C) 2004  Toolforge <www.toolforge.nl>
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, write to the Free Software
 17  
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 */
 19  
 package nl.toolforge.karma.core.cmd.impl;
 20  
 
 21  
 import nl.toolforge.karma.core.cmd.Command;
 22  
 import nl.toolforge.karma.core.cmd.CommandDescriptor;
 23  
 import nl.toolforge.karma.core.cmd.CommandException;
 24  
 import nl.toolforge.karma.core.cmd.CommandFactory;
 25  
 import nl.toolforge.karma.core.cmd.CommandLoadException;
 26  
 import nl.toolforge.karma.core.cmd.CommandResponse;
 27  
 import nl.toolforge.karma.core.cmd.event.ExceptionEvent;
 28  
 import nl.toolforge.karma.core.cmd.event.MessageEvent;
 29  
 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
 30  
 import nl.toolforge.karma.core.cmd.util.DependencyException;
 31  
 import nl.toolforge.karma.core.cmd.util.DependencyHelper;
 32  
 import nl.toolforge.karma.core.manifest.ManifestException;
 33  
 import nl.toolforge.karma.core.module.Module;
 34  
 import nl.toolforge.karma.core.module.ModuleTypeException;
 35  
 import org.apache.commons.logging.Log;
 36  
 import org.apache.commons.logging.LogFactory;
 37  
 import org.apache.tools.ant.BuildException;
 38  
 import org.apache.tools.ant.DirectoryScanner;
 39  
 import org.apache.tools.ant.Project;
 40  
 
 41  
 /**
 42  
  * Builds a module in a manifest. Building a module means that all java sources will be compiled into the
 43  
  * modules' build directory on disk.
 44  
  * <p>
 45  
  * BuildModule supports the following variables in the karma.properties:
 46  
  * <ul>
 47  
  *   <li>java.compiler</li>
 48  
  *   <li>java.source</li>
 49  
  *   <li>java.target</li>
 50  
  *   <li>java.debug</li>
 51  
  *   <li>java.debuglevel</li>
 52  
  *   <li>javac.nowarn</li>
 53  
  *   <li>javac.optimize</li>
 54  
  *   <li>javac.deprecation</li>
 55  
  *   <li>javac.verbose</li>
 56  
  *   <li>javac.depend</li>
 57  
  * </ul>
 58  
  *
 59  
  * @author D.A. Smedes
 60  
  * @author W.H. Schraal
 61  
  * @version $Id: BuildModule.java,v 1.61 2004/11/10 23:53:08 asmedes Exp $
 62  
  */
 63  
 public class BuildModule extends AbstractBuildCommand {
 64  
 
 65  0
   private static final Log logger = LogFactory.getLog(BuildModule.class);
 66  
 
 67  0
   private CommandResponse commandResponse = new CommandResponse();
 68  
 
 69  
   public BuildModule(CommandDescriptor descriptor) {
 70  0
     super(descriptor);
 71  0
   }
 72  
 
 73  
   public void execute() throws CommandException {
 74  
 
 75  0
     super.execute();
 76  
 
 77  
 
 78  0
     if (!getBuildEnvironment().getModuleSourceDirectory().exists()) {
 79  
       // No point in building a module, if no src/java is available or no .java files are present in that dir.
 80  
       //
 81  0
       throw new CommandException(CommandException.NO_SRC_DIR, class="keyword">new Object[] {getCurrentModule().getName()});
 82  
     }
 83  0
     DirectoryScanner scanner = new DirectoryScanner();
 84  0
     scanner.setBasedir(getBuildEnvironment().getModuleSourceDirectory());
 85  0
     scanner.setIncludes(new String[]{"**/*.java"});
 86  0
     scanner.scan();
 87  0
     if (scanner.getIncludedFiles().length == 0) {
 88  
       // No point in building a module, if no src/java is available or no .java files are present in that dir.
 89  
       //
 90  0
       throw new CommandException(CommandException.NO_SRC_DIR, class="keyword">new Object[] {getCurrentModule().getName()});
 91  
     }
 92  
 
 93  0
     DependencyHelper helper = new DependencyHelper(getCurrentManifest());
 94  
 
 95  0
     Project project = getAntProject("build-module.xml");
 96  
 
 97  
     try {
 98  0
       boolean dependenciesChecked = false;
 99  0
       while (!dependenciesChecked) {
 100  
         try {
 101  0
           project.setProperty("classpath", helper.getClassPath(getCurrentModule()));
 102  0
           dependenciesChecked = true;
 103  0
         } catch (DependencyException de) {
 104  0
           if ( !getCommandLine().hasOption("n") ||
 105  
                de.getErrorCode().equals(DependencyException.DEPENDENCY_NOT_FOUND)) {
 106  
             //a dependency was not found. Let's build it.
 107  
             //if it's a module, build it.
 108  
             //else, rethrow the exception, since we can do nothing about it.
 109  0
             String dep = (String) de.getMessageArguments()[0];
 110  
             try {
 111  0
               Module module = getCurrentManifest().getModule(dep);
 112  
 
 113  0
               Command command = null;
 114  
               try {
 115  0
                 getCommandResponse().addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Module `{0}` is needed, but is not built yet. Doing that now.", new Object[]{module.getName()})));
 116  0
                 String commandLineString = "bm -m " + module.getName();
 117  0
                 logger.info("Going to: "+commandLineString);
 118  0
                 command = CommandFactory.getInstance().getCommand(commandLineString);
 119  0
                 command.setContext(getContext());
 120  0
                 command.registerCommandResponseListener(getResponseListener());
 121  0
                 command.execute();
 122  
 
 123  
                 //the dependency built successfully.
 124  0
               } catch (CommandLoadException e) {
 125  0
                 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 126  
               } finally {
 127  0
                 if ( command != null ) {
 128  0
                   command.deregisterCommandResponseListener(getResponseListener());
 129  
                 }
 130  0
               }
 131  0
             } catch (ManifestException me) {
 132  
               //obviously it was not a module...
 133  0
               throw de;
 134  0
             }
 135  
           } else {
 136  
             //rethrow the exception. Don't know what to do with it here.
 137  0
             throw de;
 138  
           }
 139  0
         }
 140  
       }
 141  0
       project.setProperty("module-build-dir", getBuildEnvironment().getModuleBuildDirectory().getPath());
 142  0
       project.setProperty("module-source-dir", getBuildEnvironment().getModuleSourceDirectory().getPath());
 143  0
       project.setProperty("java.compiler", getWorkingContext().getProperties().getProperty("java.compiler", "modern"));
 144  0
       project.setProperty("java.source", getWorkingContext().getProperties().getProperty("java.source", "1.4"));
 145  0
       project.setProperty("java.target", getWorkingContext().getProperties().getProperty("java.target", "1.4"));
 146  0
       project.setProperty("java.debug", getWorkingContext().getProperties().getProperty("java.debug", "off"));
 147  0
       project.setProperty("java.debuglevel", getWorkingContext().getProperties().getProperty("java.debuglevel", "none"));
 148  0
       project.setProperty("javac.nowarn", getWorkingContext().getProperties().getProperty("javac.nowarn", "off"));
 149  0
       project.setProperty("javac.optimize", getWorkingContext().getProperties().getProperty("javac.optimize", "off"));
 150  0
       project.setProperty("javac.deprecation", getWorkingContext().getProperties().getProperty("javac.deprecation", "off"));
 151  0
       project.setProperty("javac.verbose", getWorkingContext().getProperties().getProperty("javac.verbose", "no"));
 152  0
       project.setProperty("javac.depend", getWorkingContext().getProperties().getProperty("javac.depend", "off"));
 153  
 
 154  0
       project.executeTarget("run");
 155  
 
 156  0
     } catch (DependencyException e) {
 157  0
       logger.error(e);
 158  0
       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 159  0
     } catch (BuildException e) {
 160  0
       logger.error(e);
 161  0
       commandResponse.addEvent(new ExceptionEvent(this, e));
 162  0
       throw new CommandException(e, CommandException.BUILD_FAILED, class="keyword">new Object[] {getCurrentModule().getName()});
 163  0
     } catch (ModuleTypeException e) {
 164  0
       logger.error(e);
 165  0
       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
 166  0
     }
 167  
 
 168  0
     SimpleMessage message = new SimpleMessage(getFrontendMessages().getString("message.MODULE_BUILT"), new Object[] {getCurrentModule().getName()});
 169  0
     commandResponse.addEvent(new MessageEvent(this, message));
 170  0
   }
 171  
 
 172  
   public CommandResponse getCommandResponse() {
 173  0
     return this.commandResponse;
 174  
   }
 175  
 
 176  
 
 177  
 
 178  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.