Coverage report

  %line %branch
nl.toolforge.karma.core.cmd.KarmaInitializationCommand
0% 
0% 

 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  0
   private static final Log logger = LogFactory.getLog(KarmaInitializationCommand.class);
 55  
 
 56  0
   private CommandResponse commandResponse = new CommandResponse();
 57  
 
 58  0
   private CommandContext commandContext = null;
 59  0
   private boolean updateStores = false;
 60  
 
 61  
   /**
 62  
    * Constructs a <code>KarmaInitializationCommand</code>.
 63  
    */
 64  0
   KarmaInitializationCommand(boolean updateStores) {
 65  0
     this.updateStores = updateStores;
 66  0
   }
 67  
 
 68  
   /**
 69  
    * Initializes a command context.
 70  
    *
 71  
    * @throws CommandException
 72  
    */
 73  
   public void execute() throws CommandException {
 74  
 
 75  
     try {
 76  
 
 77  0
       if (updateStores) {
 78  
 
 79  
         try {
 80  
           
 81  0
           ManifestStore mStore = commandContext.getWorkingContext().getConfiguration().getManifestStore();
 82  
 
 83  0
           if (!mStore.getLocation().isAvailable()) {
 84  0
             commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Manifest store location unreachable!")));
 85  
           } else {
 86  0
             commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage(("Updating manifests ..."))));
 87  0
             mStore.update();
 88  
           }
 89  
 
 90  0
           LocationStore lStore = commandContext.getWorkingContext().getConfiguration().getLocationStore();
 91  
 
 92  0
           if (!lStore.getLocation().isAvailable()) {
 93  0
             commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Location store location unreachable!")));
 94  
           } else {
 95  0
             commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage(("Updating locations ..."))));
 96  0
             lStore.update();
 97  
           }
 98  
 
 99  0
         } catch (VersionControlException e) {
 100  0
           logger.warn(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
 101  0
           commandResponse.addEvent(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
 102  0
         } catch (AuthenticationException e) {
 103  0
           logger.warn(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
 104  0
           commandResponse.addEvent(new ErrorEvent(this, e.getErrorCode(), e.getMessageArguments()));
 105  0
         }
 106  
       }
 107  
 
 108  0
       commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage(getFrontendMessages().getString("message.LOADING_MANIFEST_FROM_HISTORY"))));
 109  
 
 110  
       // Try reloading the last manifest that was used.
 111  
       //
 112  0
       Manifest currentManifest = commandContext.getWorkingContext().getManifestCollector().loadManifestFromHistory();
 113  0
       if (currentManclass="keyword">ifest != null) {
 114  0
         SimpleMessage message =
 115  
             new SimpleMessage(getFrontendMessages().getString("message.MANIFEST_ACTIVATED"), new Object[]{currentManifest});
 116  0
         commandResponse.addEvent(new MessageEvent(this, message));
 117  
 
 118  
         // Register the command context with the listener to allow automaic updates of the manifest.
 119  
         //
 120  0
         commandContext.changeCurrentManifest(currentManifest);
 121  0
         commandContext.register();
 122  
       } else {
 123  0
         commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage(getFrontendMessages().getString("message.NO_MANIFEST_IN_HISTORY"))));
 124  
       }
 125  
 
 126  
       try {
 127  0
         Preferences.userRoot().put(WorkingContext.WORKING_CONTEXT_PREFERENCE, commandContext.getWorkingContext().getName());
 128  0
         Preferences.userRoot().flush();
 129  0
       } catch (BackingStoreException e) {
 130  
         // Too bad ...
 131  0
       }
 132  0
     } catch (ManifestException e) {
 133  0
       throw new CommandException(e, e.getErrorCode(), e.getMessageArguments());
 134  0
     } catch (LocationException e) {
 135  0
       throw new CommandException(e, e.getErrorCode(), e.getMessageArguments());
 136  0
     }
 137  0
   }
 138  
 
 139  
   /**
 140  
    * Returns 'init' as the name of this command.
 141  
    *
 142  
    * @return 'init'
 143  
    */
 144  
   public String getName() {
 145  0
     return "init";
 146  
   }
 147  
 
 148  
   /**
 149  
    * Returns 'init' as the alias of this command.
 150  
    *
 151  
    * @return 'init'
 152  
    */
 153  
   public String getAlias() {
 154  0
     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  0
     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  0
     return getDescription();
 174  
   }
 175  
 
 176  
   /**
 177  
    * Empty implementation.
 178  
    */
 179  0
   public void cleanUp() { }
 180  
 
 181  
   public void setContext(CommandContext context) {
 182  0
     this.commandContext = context;
 183  0
   }
 184  
 
 185  
   /**
 186  
    * Empty implementation.
 187  
    *
 188  
    * @param commandLine Unused.
 189  
    */
 190  0
   public void setCommandLine(CommandLine commandLine) {}
 191  
 
 192  
   /**
 193  
    * Returns <code>null</code>
 194  
    *
 195  
    * @return <code>null</code>
 196  
    */
 197  
   public CommandLine getCommandLine() {
 198  0
     return null;
 199  
   }
 200  
 
 201  
   public final void registerCommandResponseListener(CommandResponseListener responseListener) {
 202  0
     getCommandResponse().addCommandResponseListener(responseListener);
 203  0
   }
 204  
 
 205  
   public final void deregisterCommandResponseListener(CommandResponseListener responseListener) {
 206  0
     getCommandResponse().removeCommandReponseListener(responseListener);
 207  0
   }
 208  
 
 209  
   public CommandResponse getCommandResponse() {
 210  0
     return commandResponse;
 211  
   }
 212  
 
 213  
   private ResourceBundle getFrontendMessages() {
 214  0
     return BundleCache.getInstance().getBundle(BundleCache.FRONTEND_MESSAGES_KEY);
 215  
   }
 216  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.