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.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    private Map locations = null;
49    private WorkingContext workingContext = null;
50  
51    /***
52     * Constructs a LocationLoader for the current <code>workingContext</code>.
53     * @param workingContext
54     */
55    public LocationLoader(WorkingContext workingContext) {
56      this.workingContext = workingContext;
57    }
58  
59  
60  
61    /***
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      locations = new Hashtable();
72  
73      // todo replace by LocationModule stuff.
74  
75      // Recurse over all xml files in the locations directory.
76      //
77      DirectoryScanner scanner = new DirectoryScanner();
78      scanner.setBasedir(workingContext.getLocationStoreBasedir());
79      scanner.setIncludes(new String[]{"**/*.xml"});
80      scanner.scan();
81  
82      String[] files = scanner.getIncludedFiles();
83  
84      if (files == null || files.length <= 0) {
85        throw new LocationException(LocationException.NO_LOCATION_DATA_FOUND);
86      }
87  
88      for (int i = 0; i < files.length; i++) {
89  
90        Digester digester = LocationDescriptor.getDigester();
91  
92        List subList = null;
93        try {
94          subList = (List) digester.parse(new File(workingContext.getLocationStoreBasedir(), files[i]).getPath());
95        } catch (IOException e) {
96          throw new LocationException(e, LocationException.LOCATION_LOAD_ERROR);
97        } catch (SAXException e) {
98          e.printStackTrace();
99          throw new LocationException(e, LocationException.LOCATION_LOAD_ERROR);
100       }
101 
102       if (subList != null) {
103         for (Iterator j = subList.iterator(); j.hasNext();) {
104 
105           LocationDescriptor d = (LocationDescriptor) j.next();
106 
107           if (locations.containsKey(d.getId())) {
108             locations.remove(d.getId());
109             throw new LocationException(
110                 LocationException.DUPLICATE_LOCATION_KEY,
111                 new Object[] {d.getId(), workingContext.getLocationStoreBasedir().getPath()}
112             );
113           }
114 
115           Location location = LocationFactory.getInstance().createLocation(d);
116           location.setWorkingContext(workingContext);
117           
118           locations.put(location.getId(), location);
119         }
120       }
121     }
122   }
123 
124   /***
125    * Returns all locations that have been loaded by the loader.
126    *
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     return locations;
131   }
132 
133   /***
134    * 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     if (locations.containsKey(locationAlias)) {
146       return (Location) locations.get(locationAlias);
147     }
148     throw new LocationException(LocationException.LOCATION_NOT_FOUND, new Object[]{locationAlias});
149   }
150 
151   /***
152    * String representation of all locations.
153    *
154    * @return <code>null</code> until implemented.
155    */
156   public String toString() {
157     return locations.toString();
158   }
159 
160 }