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.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      // Part 1 of the transaction is the creation of a Module instance.
70      //
71  
72      // todo this bit sucks. Since renamed to ModuleDigester, it doesn't make sense anymore.
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       // Ensure that only this message is passed back to the client
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 }