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.CommandDescriptor;
22 import nl.toolforge.karma.core.cmd.CommandException;
23 import nl.toolforge.karma.core.cmd.CommandResponse;
24 import nl.toolforge.karma.core.cmd.DefaultCommand;
25 import nl.toolforge.karma.core.cmd.event.MessageEvent;
26 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
27 import nl.toolforge.karma.core.location.LocationException;
28 import nl.toolforge.karma.core.module.Module;
29 import nl.toolforge.karma.core.module.ModuleDigester;
30 import nl.toolforge.karma.core.module.ModuleFactory;
31 import nl.toolforge.karma.core.vc.AuthenticationException;
32 import nl.toolforge.karma.core.vc.AuthenticatorKey;
33 import nl.toolforge.karma.core.vc.Authenticators;
34 import nl.toolforge.karma.core.vc.VersionControlException;
35 import org.apache.commons.cli.CommandLine;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.util.regex.PatternSyntaxException;
40
41 /***
42 * Creates a module in a repository. Modules are created using a layout template (instances of
43 * <code>ModuleLayoutTemplate</code>).
44 *
45 * @author D.A. Smedes
46 * @version $Id: CreateModuleCommand.java,v 1.49 2004/11/10 23:53:08 asmedes Exp $
47 */
48 public class CreateModuleCommand extends DefaultCommand {
49
50 private static Log logger = LogFactory.getLog(CreateModuleCommand.class);
51
52 private CommandResponse commandResponse = new CommandResponse();
53
54 public CreateModuleCommand(CommandDescriptor descriptor) {
55 super(descriptor);
56 }
57
58 /***
59 * Physical creation of a module in a version control system.
60 */
61 public void execute() throws CommandException {
62
63 CommandLine commandLine = getCommandLine();
64
65 String locationAlias = commandLine.getOptionValue("l");
66 String moduleName = commandLine.getOptionValue("m");
67 String comment = commandLine.getOptionValue("c");
68
69
70
71
72
73 ModuleDigester digester = null;
74 try {
75 digester = new ModuleDigester(moduleName, locationAlias);
76 } catch (PatternSyntaxException e) {
77 throw new CommandException(CommandException.INVALID_ARGUMENT, new Object[]{moduleName, e.getMessage()});
78 }
79
80 Module.Type moduleType = new Module.Type();
81 try {
82 moduleType.setType(commandLine.getOptionValue("t"));
83 } catch (IllegalArgumentException e1) {
84 throw new CommandException(CommandException.INVALID_ARGUMENT);
85 }
86
87 Module module = null;
88 try {
89 ModuleFactory factory = new ModuleFactory(getWorkingContext());
90 module = factory.create(digester, moduleType);
91 } catch (LocationException e) {
92 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
93 }
94
95 SimpleMessage message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_MODULE_STARTED"), new Object[]{moduleName, locationAlias});
96 commandResponse.addEvent(new MessageEvent(this, message));
97
98 try {
99
100 AuthenticatorKey key = new AuthenticatorKey(getWorkingContext().getName(), module.getLocation().getId());
101 module.createRemote(Authenticators.getAuthenticator(key), comment);
102
103
104
105 message = new SimpleMessage(getFrontendMessages().getString("message.CREATE_MODULE_SUCCESSFULL"), new Object[]{moduleName, locationAlias});
106 commandResponse.addEvent(new MessageEvent(this, message));
107
108 } catch (VersionControlException e) {
109 logger.error(e);
110 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
111 } catch (AuthenticationException e) {
112 logger.error(e);
113 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
114 }
115 }
116
117 public CommandResponse getCommandResponse() {
118 return this.commandResponse;
119 }
120 }