| 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.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.CompositeCommand; |
| 28 |
|
import nl.toolforge.karma.core.cmd.event.ErrorEvent; |
| 29 |
|
import nl.toolforge.karma.core.cmd.event.MessageEvent; |
| 30 |
|
import nl.toolforge.karma.core.cmd.event.SimpleMessage; |
| 31 |
|
import nl.toolforge.karma.core.location.LocationException; |
| 32 |
|
import nl.toolforge.karma.core.manifest.Manifest; |
| 33 |
|
import nl.toolforge.karma.core.manifest.ManifestException; |
| 34 |
|
import nl.toolforge.karma.core.manifest.ManifestFactory; |
| 35 |
|
import nl.toolforge.karma.core.manifest.ManifestLoader; |
| 36 |
|
import nl.toolforge.karma.core.module.Module; |
| 37 |
|
import nl.toolforge.karma.core.vc.AuthenticationException; |
| 38 |
|
import nl.toolforge.karma.core.vc.ModuleStatus; |
| 39 |
|
import nl.toolforge.karma.core.vc.VersionControlException; |
| 40 |
|
import nl.toolforge.karma.core.vc.cvsimpl.Utils; |
| 41 |
|
import nl.toolforge.karma.core.vc.cvsimpl.threads.CVSLogThread; |
| 42 |
|
import nl.toolforge.karma.core.vc.threads.ParallelRunner; |
| 43 |
|
import org.apache.commons.logging.Log; |
| 44 |
|
import org.apache.commons.logging.LogFactory; |
| 45 |
|
|
| 46 |
|
import java.io.File; |
| 47 |
|
import java.io.FileWriter; |
| 48 |
|
import java.io.IOException; |
| 49 |
|
import java.util.Iterator; |
| 50 |
|
import java.util.Map; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
public class CreateRelease extends CompositeCommand { |
| 60 |
|
|
| 61 |
0 |
private static final Log logger = LogFactory.getLog(CreateRelease.class); |
| 62 |
|
|
| 63 |
0 |
private CommandResponse commandResponse = new CommandResponse(); |
| 64 |
|
|
| 65 |
|
public CreateRelease(CommandDescriptor descriptor) { |
| 66 |
0 |
super(descriptor); |
| 67 |
0 |
} |
| 68 |
|
|
| 69 |
|
public void execute() throws CommandException { |
| 70 |
|
|
| 71 |
0 |
String manifestName = getCommandLine().getOptionValue("d"); |
| 72 |
|
|
| 73 |
0 |
Manifest releaseManifest = null; |
| 74 |
|
|
| 75 |
0 |
if (manclass="keyword">ifestName == null) { |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
0 |
if (!getContext().isManclass="keyword">ifestLoaded()) { |
| 80 |
0 |
throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
0 |
releaseManifest = getContext().getCurrentManifest(); |
| 84 |
|
|
| 85 |
|
} else { |
| 86 |
|
|
| 87 |
|
try { |
| 88 |
0 |
ManifestFactory factory = new ManifestFactory(); |
| 89 |
0 |
ManifestLoader loader = new ManifestLoader(getWorkingContext()); |
| 90 |
0 |
releaseManifest = factory.create(getWorkingContext(), loader.load(manifestName)); |
| 91 |
0 |
} catch (ManifestException e) { |
| 92 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
| 93 |
0 |
} catch (LocationException e) { |
| 94 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
| 95 |
0 |
} |
| 96 |
|
} |
| 97 |
|
|
| 98 |
0 |
String releaseName = getCommandLine().getOptionValue("r"); |
| 99 |
|
|
| 100 |
0 |
File releaseManifestFile = new File(getWorkingContext().getConfiguration().getManifestStore().getModule().getBaseDir(), releaseName + ".xml"); |
| 101 |
|
|
| 102 |
0 |
if (releaseManclass="keyword">ifestFile.exists()) { |
| 103 |
0 |
if (!getCommandLine().hasOption("o")) { |
| 104 |
0 |
throw new CommandException( |
| 105 |
|
ManifestException.DUPLICATE_MANIFEST_FILE, |
| 106 |
|
new Object[] {releaseName, getWorkingContext().getConfiguration().getManifestStore().getModule().getBaseDir().getPath()} |
| 107 |
|
); |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
0 |
SimpleMessage message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_RELEASE_STARTED"), new Object[]{releaseName}); |
| 115 |
0 |
commandResponse.addEvent(new MessageEvent(this, message)); |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
0 |
ParallelRunner runner = new ParallelRunner(releaseManifest, CVSLogThread.class); |
| 121 |
0 |
runner.execute(100); |
| 122 |
|
|
| 123 |
0 |
Map statusOverview = runner.retrieveResults(); |
| 124 |
|
|
| 125 |
0 |
Map modules = releaseManifest.getAllModules(); |
| 126 |
|
|
| 127 |
0 |
for (Iterator i = modules.values().iterator(); i.hasNext();) { |
| 128 |
|
|
| 129 |
0 |
Module module = (Module) i.next(); |
| 130 |
0 |
ModuleStatus moduleStatus = (ModuleStatus) statusOverview.get(module); |
| 131 |
|
|
| 132 |
0 |
if (moduleStatus.connectionFailure()) { |
| 133 |
|
|
| 134 |
0 |
getCommandResponse().addEvent(new ErrorEvent(this, LocationException.CONNECTION_EXCEPTION)); |
| 135 |
0 |
message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_RELEASE_FAILED")); |
| 136 |
0 |
getCommandResponse().addEvent(new MessageEvent(this, message)); |
| 137 |
|
|
| 138 |
0 |
return; |
| 139 |
|
} |
| 140 |
0 |
if (!moduleStatus.existsInRepository()) { |
| 141 |
|
|
| 142 |
0 |
getCommandResponse().addEvent(new ErrorEvent(this, VersionControlException.MODULE_NOT_IN_REPOSITORY, class="keyword">new Object[]{module.getName()})); |
| 143 |
|
|
| 144 |
|
|
| 145 |
0 |
message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_RELEASE_FAILED")); |
| 146 |
0 |
getCommandResponse().addEvent(new MessageEvent(this, message)); |
| 147 |
0 |
return; |
| 148 |
|
} |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
0 |
StringBuffer buffer = new StringBuffer(); |
| 156 |
0 |
buffer.append("<?xml version=\ŕ.0\"?>\n"); |
| 157 |
|
|
| 158 |
0 |
buffer.append("<manifest type=\"release\" version=\ŕ-0\">\n"); |
| 159 |
|
|
| 160 |
0 |
buffer.append(" <modules>\n"); |
| 161 |
|
|
| 162 |
0 |
for (Iterator i = modules.values().iterator(); i.hasNext();) { |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
0 |
Module module = (Module) i.next(); |
| 167 |
|
|
| 168 |
0 |
if (getContext().getCurrentManclass="keyword">ifest().getState(module).equals(Module.WORKING)) { |
| 169 |
0 |
throw new CommandException(CommandException.MODULE_CANNOT_BE_WORKING_FOR_RELEASE_MANIFEST, class="keyword">new Object[]{module.getName()}); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
0 |
buffer.append(getModule(module, getCommandLine().hasOption("u"))); |
| 173 |
|
} |
| 174 |
|
|
| 175 |
0 |
buffer.append(" </modules>\n"); |
| 176 |
|
|
| 177 |
0 |
buffer.append("</manifest>\n"); |
| 178 |
|
|
| 179 |
0 |
FileWriter writer = null; |
| 180 |
|
try { |
| 181 |
|
|
| 182 |
|
|
| 183 |
0 |
writer = new FileWriter(releaseManifestFile); |
| 184 |
|
|
| 185 |
0 |
writer.write(buffer.toString()); |
| 186 |
0 |
writer.flush(); |
| 187 |
0 |
} catch (IOException e) { |
| 188 |
0 |
e.printStackTrace(); |
| 189 |
|
} finally { |
| 190 |
0 |
try { |
| 191 |
0 |
writer.close(); |
| 192 |
0 |
} catch (IOException e) { |
| 193 |
0 |
logger.error(e); |
| 194 |
0 |
} |
| 195 |
0 |
} |
| 196 |
|
|
| 197 |
0 |
boolean store = getCommandLine().hasOption("s"); |
| 198 |
|
|
| 199 |
0 |
if (store) { |
| 200 |
|
try { |
| 201 |
0 |
getWorkingContext().getConfiguration().getManifestStore().commit(releaseName); |
| 202 |
|
|
| 203 |
0 |
message = new SimpleMessage(getFrontendMessages().getString("message.RELEASE_COMMITTED"), new Object[]{releaseName}); |
| 204 |
0 |
commandResponse.addEvent(new MessageEvent(this, message)); |
| 205 |
|
|
| 206 |
0 |
} catch (AuthenticationException e) { |
| 207 |
0 |
logger.error(e); |
| 208 |
0 |
throw new CommandException(e, e.getErrorCode(), e.getMessageArguments()); |
| 209 |
0 |
} catch (VersionControlException e) { |
| 210 |
0 |
logger.error(e); |
| 211 |
0 |
throw new CommandException(e, e.getErrorCode(), e.getMessageArguments()); |
| 212 |
0 |
} |
| 213 |
|
} else { |
| 214 |
0 |
message = new SimpleMessage(getFrontendMessages().getString("message.RELEASE_NOT_COMMITTED"), new Object[]{releaseName}); |
| 215 |
0 |
commandResponse.addEvent(new MessageEvent(this, message)); |
| 216 |
|
} |
| 217 |
|
|
| 218 |
0 |
message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_RELEASE_FINISHED"), new Object[]{releaseName}); |
| 219 |
0 |
commandResponse.addEvent(new MessageEvent(this, message)); |
| 220 |
|
|
| 221 |
0 |
boolean loadManifest = getCommandLine().hasOption("l"); |
| 222 |
|
|
| 223 |
0 |
if (loadManclass="keyword">ifest) { |
| 224 |
0 |
|
| 225 |
0 |
Command command = null; |
| 226 |
0 |
try { |
| 227 |
0 |
command = CommandFactory.getInstance().getCommand("select-manifest -m ".concat(releaseName)); |
| 228 |
0 |
} catch (CommandLoadException e) { |
| 229 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
| 230 |
0 |
} |
| 231 |
0 |
command.registerCommandResponseListener(this); |
| 232 |
0 |
getContext().execute(command); |
| 233 |
0 |
} |
| 234 |
0 |
|
| 235 |
|
|
| 236 |
0 |
} |
| 237 |
|
|
| 238 |
0 |
public CommandResponse getCommandResponse() { |
| 239 |
0 |
return commandResponse; |
| 240 |
0 |
} |
| 241 |
|
|
| 242 |
|
private String getModule(Module module, boolean useRemote) throws CommandException { |
| 243 |
0 |
|
| 244 |
|
String n; |
| 245 |
|
String v; |
| 246 |
|
String l; |
| 247 |
|
|
| 248 |
0 |
n = "\"" + module.getName() + "\""; |
| 249 |
0 |
l = module.getLocation().getId(); |
| 250 |
|
|
| 251 |
0 |
if (module.hasVersion()) { |
| 252 |
|
|
| 253 |
0 |
v = module.getVersion().getVersionNumber(); |
| 254 |
|
|
| 255 |
|
} else { |
| 256 |
|
|
| 257 |
|
try { |
| 258 |
0 |
if (useRemote) { |
| 259 |
0 |
v = Utils.getLastVersion(module).getVersionNumber(); |
| 260 |
|
} else { |
| 261 |
0 |
v = Utils.getLocalVersion(module).getVersionNumber(); |
| 262 |
|
} |
| 263 |
0 |
} catch (VersionControlException e) { |
| 264 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
| 265 |
0 |
} |
| 266 |
|
} |
| 267 |
|
|
| 268 |
0 |
return " <module name=" + n + " version=\"" + v + "\" location=\"" + l + "\"/>\n"; |
| 269 |
|
|
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
} |