View Javadoc

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    private static final Log logger = LogFactory.getLog(BuildModule.class);
66  
67    private CommandResponse commandResponse = new CommandResponse();
68  
69    public BuildModule(CommandDescriptor descriptor) {
70      super(descriptor);
71    }
72  
73    public void execute() throws CommandException {
74  
75      super.execute();
76  
77  
78      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        throw new CommandException(CommandException.NO_SRC_DIR, new Object[] {getCurrentModule().getName()});
82      }
83      DirectoryScanner scanner = new DirectoryScanner();
84      scanner.setBasedir(getBuildEnvironment().getModuleSourceDirectory());
85      scanner.setIncludes(new String[]{"**/*.java"});
86      scanner.scan();
87      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        throw new CommandException(CommandException.NO_SRC_DIR, new Object[] {getCurrentModule().getName()});
91      }
92  
93      DependencyHelper helper = new DependencyHelper(getCurrentManifest());
94  
95      Project project = getAntProject("build-module.xml");
96  
97      try {
98        boolean dependenciesChecked = false;
99        while (!dependenciesChecked) {
100         try {
101           project.setProperty("classpath", helper.getClassPath(getCurrentModule()));
102           dependenciesChecked = true;
103         } catch (DependencyException de) {
104           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             String dep = (String) de.getMessageArguments()[0];
110             try {
111               Module module = getCurrentManifest().getModule(dep);
112 
113               Command command = null;
114               try {
115                 getCommandResponse().addEvent(new MessageEvent(this, new SimpleMessage("Module `{0}` is needed, but is not built yet. Doing that now.", new Object[]{module.getName()})));
116                 String commandLineString = "bm -m " + module.getName();
117                 logger.info("Going to: "+commandLineString);
118                 command = CommandFactory.getInstance().getCommand(commandLineString);
119                 command.setContext(getContext());
120                 command.registerCommandResponseListener(getResponseListener());
121                 command.execute();
122 
123                 //the dependency built successfully.
124               } catch (CommandLoadException e) {
125                 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
126               } finally {
127                 if ( command != null ) {
128                   command.deregisterCommandResponseListener(getResponseListener());
129                 }
130               }
131             } catch (ManifestException me) {
132               //obviously it was not a module...
133               throw de;
134             }
135           } else {
136             //rethrow the exception. Don't know what to do with it here.
137             throw de;
138           }
139         }
140       }
141       project.setProperty("module-build-dir", getBuildEnvironment().getModuleBuildDirectory().getPath());
142       project.setProperty("module-source-dir", getBuildEnvironment().getModuleSourceDirectory().getPath());
143       project.setProperty("java.compiler", getWorkingContext().getProperties().getProperty("java.compiler", "modern"));
144       project.setProperty("java.source", getWorkingContext().getProperties().getProperty("java.source", "1.4"));
145       project.setProperty("java.target", getWorkingContext().getProperties().getProperty("java.target", "1.4"));
146       project.setProperty("java.debug", getWorkingContext().getProperties().getProperty("java.debug", "off"));
147       project.setProperty("java.debuglevel", getWorkingContext().getProperties().getProperty("java.debuglevel", "none"));
148       project.setProperty("javac.nowarn", getWorkingContext().getProperties().getProperty("javac.nowarn", "off"));
149       project.setProperty("javac.optimize", getWorkingContext().getProperties().getProperty("javac.optimize", "off"));
150       project.setProperty("javac.deprecation", getWorkingContext().getProperties().getProperty("javac.deprecation", "off"));
151       project.setProperty("javac.verbose", getWorkingContext().getProperties().getProperty("javac.verbose", "no"));
152       project.setProperty("javac.depend", getWorkingContext().getProperties().getProperty("javac.depend", "off"));
153 
154       project.executeTarget("run");
155 
156     } catch (DependencyException e) {
157       logger.error(e);
158       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
159     } catch (BuildException e) {
160       logger.error(e);
161       commandResponse.addEvent(new ExceptionEvent(this, e));
162       throw new CommandException(e, CommandException.BUILD_FAILED, new Object[] {getCurrentModule().getName()});
163     } catch (ModuleTypeException e) {
164       logger.error(e);
165       throw new CommandException(e.getErrorCode(), e.getMessageArguments());
166     }
167 
168     SimpleMessage message = new SimpleMessage(getFrontendMessages().getString("message.MODULE_BUILT"), new Object[] {getCurrentModule().getName()});
169     commandResponse.addEvent(new MessageEvent(this, message));
170   }
171 
172   public CommandResponse getCommandResponse() {
173     return this.commandResponse;
174   }
175 
176 
177 
178 }