1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
77
78 if (!getContext().isManifestLoaded()) {
79 throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST);
80 }
81
82 Collection modules = getContext().getCurrentManifest().getAllModules().values();
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 }