Coverage report

  %line %branch
nl.toolforge.karma.core.location.LocationLoader
82% 
96% 

 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.location;
 20  
 
 21  
 import nl.toolforge.karma.core.boot.WorkingContext;
 22  
 import org.apache.commons.digester.Digester;
 23  
 import org.apache.tools.ant.DirectoryScanner;
 24  
 import org.xml.sax.SAXException;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.IOException;
 28  
 import java.util.Hashtable;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 
 33  
 /**
 34  
  * <p>Loader class to load all gain access to <code>Location</code> instances. Location instances are created based on XML
 35  
  * definitions in the location store. The location store is a directory on the user's local harddisk, which is a
 36  
  * mandatory property in <code>karma.properties</code>.
 37  
  *
 38  
  * <p>A location can be of different types. Some locations require authenticator data, which is mapped from an XML file
 39  
  * in the Karma configuration directory (<code>uthenticator.xml</code>). A location with id <code>cvs-1</code> will be
 40  
  *  mapped to an authenticator with the same id.
 41  
  *
 42  
  * @author D.A. Smedes
 43  
  * @author W.M. Oosterom
 44  
  * @version $Id: LocationLoader.java,v 1.17 2004/11/02 23:57:06 asmedes Exp $
 45  
  */
 46  
 public final class LocationLoader {
 47  
 
 48  45
   private Map locations = null;
 49  45
   private WorkingContext workingContext = null;
 50  
 
 51  
   /**
 52  63
    * Constructs a LocationLoader for the current <code>workingContext</code>.
 53  63
    * @param workingContext
 54  
    */
 55  45
   public LocationLoader(WorkingContext workingContext) {
 56  45
     this.workingContext = workingContext;
 57  45
   }
 58  
 
 59  63
 
 60  63
 
 61  63
   /**
 62  
    * Loads all location xml files from the path specified by the {@link WorkingContext#getLocationStore()}
 63  
    * property. Location objects are matched against authenticator objects, which should be available in the Karma
 64  
    * configuration directory {@link nl.toolforge.karma.core.boot.Karma#getConfigurationBaseDir()} and should be named
 65  
    * '<code>authenticators.xml</code>'.
 66  
    *
 67  
    * @throws LocationException
 68  
    */
 69  
   public synchronized void load() throws LocationException {
 70  
 
 71  60
     locations = new Hashtable();
 72  
 
 73  
     // todo replace by LocationModule stuff.
 74  
 
 75  74
     // Recurse over all xml files in the locations directory.
 76  
     //
 77  60
     DirectoryScanner scanner = new DirectoryScanner();
 78  60
     scanner.setBasedir(workingContext.getLocationStoreBasedir());
 79  60
     scanner.setIncludes(new String[]{"**/*.xml"});
 80  60
     scanner.scan();
 81  74
 
 82  134
     String[] files = scanner.getIncludedFiles();
 83  74
 
 84  134
     if (files == null || files.length <= 0) {
 85  0
       throw new LocationException(LocationException.NO_LOCATION_DATA_FOUND);
 86  74
     }
 87  
 
 88  254
     for (int i = 0; i < files.length; i++) {
 89  
 
 90  120
       Digester digester = LocationDescriptor.getDigester();
 91  
 
 92  342
       List subList = null;
 93  
       try {
 94  268
         subList = (List) digester.parse(new File(workingContext.getLocationStoreBasedir(), files[i]).getPath());
 95  0
       } catch (IOException e) {
 96  148
         throw new LocationException(e, LocationException.LOCATION_LOAD_ERROR);
 97  0
       } catch (SAXException e) {
 98  148
         e.printStackTrace();
 99  0
         throw new LocationException(e, LocationException.LOCATION_LOAD_ERROR);
 100  120
       }
 101  
 
 102  120
       if (subList != null) {
 103  120
         for (Iterator j = subList.iterator(); j.hasNext();) {
 104  148
 
 105  360
           LocationDescriptor d = (LocationDescriptor) j.next();
 106  148
 
 107  508
           if (locations.containsKey(d.getId())) {
 108  0
             locations.remove(d.getId());
 109  444
             throw new LocationException(
 110  
                 LocationException.DUPLICATE_LOCATION_KEY,
 111  444
                 new Object[] {d.getId(), workingContext.getLocationStoreBasedir().getPath()}
 112  
             );
 113  
           }
 114  
 
 115  360
           Location location = LocationFactory.getInstance().createLocation(d);
 116  360
           location.setWorkingContext(workingContext);
 117  
           
 118  360
           locations.put(location.getId(), location);
 119  444
         }
 120  444
       }
 121  
     }
 122  504
   }
 123  
 
 124  
   /**
 125  
    * Returns all locations that have been loaded by the loader.
 126  74
    *
 127  
    * @return A map containing <code>Location</code>-objects, accessible by their <code>id</code> as a key.
 128  
    */
 129  
   public final Map getLocations() {
 130  15
     return locations;
 131  
   }
 132  
 
 133  
   /**
 134  11
    * Gets a <code>Location</code> instance by its <code>locationAlias</code>. This method checks the availability of the
 135  
    * location as well throwing the corresponding errors when this did not succeed.
 136  
    *
 137  
    * @param  locationAlias      The <code>location</code>-attribute from the <code>module</code>-element
 138  
    *                            in the manifest.
 139  
    * @return A <code>Location</code> instance, representing e.g. a CVS repository or a Maven repository.
 140  
    * @throws LocationException See {@link LocationException#LOCATION_NOT_FOUND}.
 141  
    * @throws LocationException See {@link LocationException#CONNECTION_EXCEPTION}.
 142  
    */
 143  
   public final Location get(String locationAlias) throws LocationException {
 144  
 
 145  165
     if (locations.containsKey(locationAlias)) {
 146  150
       return (Location) locations.get(locationAlias);
 147  
     }
 148  15
     throw new LocationException(LocationException.LOCATION_NOT_FOUND, class="keyword">new Object[]{locationAlias});
 149  131
   }
 150  120
 
 151  
   /**
 152  11
    * String representation of all locations.
 153  
    *
 154  
    * @return <code>null</code> until implemented.
 155  
    */
 156  
   public String toString() {
 157  0
     return locations.toString();
 158  
   }
 159  
 
 160  
 }

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