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.KarmaRuntimeException;
22  import nl.toolforge.karma.core.cmd.Command;
23  import nl.toolforge.karma.core.cmd.CommandDescriptor;
24  import nl.toolforge.karma.core.cmd.CommandException;
25  import nl.toolforge.karma.core.cmd.CommandFactory;
26  import nl.toolforge.karma.core.cmd.CommandLoadException;
27  import nl.toolforge.karma.core.cmd.CommandResponse;
28  import nl.toolforge.karma.core.cmd.CompositeCommand;
29  import nl.toolforge.karma.core.cmd.event.ErrorEvent;
30  import nl.toolforge.karma.core.cmd.threads.ParallelCommandWrapper;
31  import nl.toolforge.karma.core.manifest.ManifestException;
32  import nl.toolforge.karma.core.module.Module;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import java.util.Collection;
37  import java.util.Iterator;
38  
39  /***
40   * This command updates all modules in the active manifest on a developers' local system.
41   *
42   * @author D.A. Smedes
43   * @version $Id: UpdateAllModulesCommand.java,v 1.38 2004/11/16 22:31:57 asmedes Exp $
44   */
45  public class UpdateAllModulesCommand extends CompositeCommand {
46  
47    private static final Log logger = LogFactory.getLog(UpdateAllModulesCommand.class);
48  
49    /***
50     * Gets the commands' response object.
51     *
52     * @return The commands' response object.
53     */
54    public CommandResponse getCommandResponse() {
55      return commandResponse;
56    }
57  
58    private CommandResponse commandResponse = new CommandResponse();
59  
60    /***
61     * Creates a <code>UpdateAllModulesCommand</code> for module <code>module</code> that should be updated.
62     *
63     * @param descriptor The command descriptor for this command.
64     */
65    public UpdateAllModulesCommand(CommandDescriptor descriptor) {
66      super(descriptor);
67    }
68  
69    /***
70     * This command will update all modules in the active manifest from the version control system. An update is done when
71     * the module is already present, otherwise a checkout will be performed. The checkout directory for the module
72     * is relative to the root directory of the <code>active</code> manifest.
73     */
74    public void execute() throws CommandException {
75  
76      // A manifest must be present for this command
77      //
78      if (!getContext().isManifestLoaded()) {
79        throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST);
80      }
81  
82      Collection modules = getContext().getCurrentManifest().getAllModules().values();
83  
84  //    try {
85  //      for (Iterator i = modules.iterator(); i.hasNext();) {
86  //        Module module = (Module) i.next();
87  //
88  //        String commandLineString = "um -m " + module.getName();
89  //        Command clone = null;
90  //        try {
91  //          clone = CommandFactory.getInstance().getCommand(commandLineString);
92  //        } catch (CommandLoadException e) {
93  //          throw new CommandException(e.getErrorCode(), e.getMessageArguments());
94  //        }
95  //        clone.setContext(getContext());
96  //
97  //        clone.registerCommandResponseListener(getResponseListener());
98  //        clone.execute();
99  //        clone.deregisterCommandResponseListener(getResponseListener());
100 //
101 //      }
102 //    } catch (CommandException c) {
103 //      commandResponse.addEvent(new MessageEvent(this, new SimpleMessage(c.getMessage())));
104 //    }
105 
106     // Initialize an array of threads.
107     //
108     ParallelCommandWrapper[] threads = new ParallelCommandWrapper[modules.size()];
109 
110     int j = 0;
111     for (Iterator i = modules.iterator(); i.hasNext();) {
112       Module module = (Module) i.next();
113 
114       String commandLineString = "um -m " + module.getName();
115       Command clone = null;
116       try {
117         clone = CommandFactory.getInstance().getCommand(commandLineString);
118       } catch (CommandLoadException e) {
119         throw new CommandException(e.getErrorCode(), e.getMessageArguments());
120       }
121       clone.setContext(getContext());
122 
123       threads[j] = new ParallelCommandWrapper(clone, getResponseListener());
124       try {
125         Thread.currentThread().sleep(0);
126       } catch (InterruptedException iex) {
127         logger.error(iex);
128       }
129       threads[j].start();
130       j++;
131     }
132 
133     int totalThreads = threads.length;
134 
135     for (int i = 0; i < totalThreads; i++) {
136 
137       try {
138         threads[i].join();
139 
140         if (threads[i].getException() != null) {
141           getCommandResponse().addEvent(
142               new ErrorEvent(
143                   threads[i].getException().getErrorCode(),
144                   threads[i].getException().getMessageArguments()
145               )
146           );
147         }
148       } catch (InterruptedException e) {
149         logger.error(e);
150         throw new KarmaRuntimeException(e.getMessage());
151       }
152     }
153   }
154 }