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;
20  
21  import nl.toolforge.karma.core.boot.LocationStore;
22  import nl.toolforge.karma.core.boot.ManifestStore;
23  import nl.toolforge.karma.core.boot.WorkingContext;
24  import nl.toolforge.karma.core.bundle.BundleCache;
25  import nl.toolforge.karma.core.cmd.event.CommandResponseListener;
26  import nl.toolforge.karma.core.cmd.event.ErrorEvent;
27  import nl.toolforge.karma.core.cmd.event.MessageEvent;
28  import nl.toolforge.karma.core.cmd.event.SimpleMessage;
29  import nl.toolforge.karma.core.location.LocationException;
30  import nl.toolforge.karma.core.manifest.Manifest;
31  import nl.toolforge.karma.core.manifest.ManifestException;
32  import nl.toolforge.karma.core.vc.AuthenticationException;
33  import nl.toolforge.karma.core.vc.VersionControlException;
34  import org.apache.commons.cli.CommandLine;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import java.util.ResourceBundle;
39  import java.util.prefs.BackingStoreException;
40  import java.util.prefs.Preferences;
41  
42  /***
43   * <p>Command implementation to initialize a command context. This is implemented as a command to allow for better and
44   * more consistent event handling and messaging.
45   *
46   * <p>Although not impossible, this command should not be used in a user interface layer.
47   *
48   * @author D.A. Smedes
49   * @version $Id: KarmaInitializationCommand.java,v 1.7 2004/11/03 20:54:15 asmedes Exp $
50   */
51  //public final class KarmaInitializationCommand implements Command {
52  public final class KarmaInitializationCommand implements Command {
53  
54    private static final Log logger = LogFactory.getLog(KarmaInitializationCommand.class);
55  
56    private CommandResponse commandResponse = new CommandResponse();
57  
58    private CommandContext commandContext = null;
59    private boolean updateStores = false;
60  
61    /***
62     * Constructs a <code>KarmaInitializationCommand</code>.
63     */
64    KarmaInitializationCommand(boolean updateStores) {
65      this.updateStores = updateStores;
66    }
67  
68    /***
69     * Initializes a command context.
70     *
71     * @throws CommandException
72     */
73    public void execute() throws CommandException {
74  
75      try {
76  
77        if (updateStores) {
78  
79          try {
80            
81            ManifestStore mStore = commandContext.getWorkingContext().getConfiguration().getManifestStore();
82  
83            if (!mStore.getLocation().isAvailable()) {
84              commandResponse.addEvent(new MessageEvent(this, new SimpleMessage("Manifest store location unreachable!")));
85            } else {
86              commandResponse.addEvent(new MessageEvent(this, new SimpleMessage(("Updating manifests ..."))));
87              mStore.update();
88            }
89  
90            LocationStore lStore = commandContext.getWorkingContext().getConfiguration().getLocationStore();
91  
92            if (!lStore.getLocation().isAvailable()) {
93              commandResponse.addEvent(new MessageEvent(this, new SimpleMessage("Location store location unreachable!")));
94            } else {
95              commandResponse.addEvent(new MessageEvent(this, new SimpleMessage(("Updating locations ..."))));
96              lStore.update();
97            }
98  
99          } catch (VersionControlException e) {
100           logger.warn(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
101           commandResponse.addEvent(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
102         } catch (AuthenticationException e) {
103           logger.warn(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
104           commandResponse.addEvent(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
105         }
106       }
107 
108       commandResponse.addEvent(new MessageEvent(this, new SimpleMessage(getFrontendMessages().getString("message.LOADING_MANIFEST_FROM_HISTORY"))));
109 
110       // Try reloading the last manifest that was used.
111       //
112       Manifest currentManifest = commandContext.getWorkingContext().getManifestCollector().loadManifestFromHistory();
113       if (currentManifest != null) {
114         SimpleMessage message =
115             new SimpleMessage(getFrontendMessages().getString("message.MANIFEST_ACTIVATED"), new Object[]{currentManifest});
116         commandResponse.addEvent(new MessageEvent(this, message));
117 
118         // Register the command context with the listener to allow automaic updates of the manifest.
119         //
120         commandContext.changeCurrentManifest(currentManifest);
121         commandContext.register();
122       } else {
123         commandResponse.addEvent(new MessageEvent(this, new SimpleMessage(getFrontendMessages().getString("message.NO_MANIFEST_IN_HISTORY"))));
124       }
125 
126       try {
127         Preferences.userRoot().put(WorkingContext.WORKING_CONTEXT_PREFERENCE, commandContext.getWorkingContext().getName());
128         Preferences.userRoot().flush();
129       } catch (BackingStoreException e) {
130         // Too bad ...
131       }
132     } catch (ManifestException e) {
133       throw new CommandException(e, e.getErrorCode(), e.getMessageArguments());
134     } catch (LocationException e) {
135       throw new CommandException(e, e.getErrorCode(), e.getMessageArguments());
136     }
137   }
138 
139   /***
140    * Returns 'init' as the name of this command.
141    *
142    * @return 'init'
143    */
144   public String getName() {
145     return "init";
146   }
147 
148   /***
149    * Returns 'init' as the alias of this command.
150    *
151    * @return 'init'
152    */
153   public String getAlias() {
154     return "init";
155   }
156 
157   /***
158    * Returns the description for this command.
159    *
160    * @return The description for this command.
161    */
162   public String getDescription() {
163     return "This command initializes a command context.";
164   }
165 
166   /***
167    * Returns the help text for this command.
168    *
169    * @return Same as {@link #getDescription}.
170    * @see    {@link #getDescription}
171    */
172   public String getHelp() {
173     return getDescription();
174   }
175 
176   /***
177    * Empty implementation.
178    */
179   public void cleanUp() { }
180 
181   public void setContext(CommandContext context) {
182     this.commandContext = context;
183   }
184 
185   /***
186    * Empty implementation.
187    *
188    * @param commandLine Unused.
189    */
190   public void setCommandLine(CommandLine commandLine) {}
191 
192   /***
193    * Returns <code>null</code>
194    *
195    * @return <code>null</code>
196    */
197   public CommandLine getCommandLine() {
198     return null;
199   }
200 
201   public final void registerCommandResponseListener(CommandResponseListener responseListener) {
202     getCommandResponse().addCommandResponseListener(responseListener);
203   }
204 
205   public final void deregisterCommandResponseListener(CommandResponseListener responseListener) {
206     getCommandResponse().removeCommandReponseListener(responseListener);
207   }
208 
209   public CommandResponse getCommandResponse() {
210     return commandResponse;
211   }
212 
213   private ResourceBundle getFrontendMessages() {
214     return BundleCache.getInstance().getBundle(BundleCache.FRONTEND_MESSAGES_KEY);
215   }
216 }