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.manifest;
20  
21  import nl.toolforge.core.util.file.XMLFilenameFilter;
22  import nl.toolforge.karma.core.boot.WorkingContext;
23  import nl.toolforge.karma.core.location.LocationException;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.HashSet;
32  import java.util.prefs.Preferences;
33  
34  /***
35   * Class that collects all manifests for a <code>WorkingContext</code>.
36   *
37   * @author D.A. Smedes
38   * @version $Id: ManifestCollector.java,v 1.17 2004/11/03 20:54:15 asmedes Exp $
39   */
40  public class ManifestCollector {
41  
42    private static Log logger = LogFactory.getLog(ManifestCollector.class);
43  
44    private Collection manifests = new ArrayList();
45    private WorkingContext workingContext = null;
46  
47    public ManifestCollector(WorkingContext workingContext) {
48      this.workingContext = workingContext;
49    }
50  
51    /***
52     * Returns a collection of manifest file names, located in the the manifest store.
53     * 
54     * @return
55     */
56    public Collection getAllManifests() {
57      File manifestStore = workingContext.getConfiguration().getManifestStore().getModule().getBaseDir();
58  
59      Object[] mList = manifestStore.list(new XMLFilenameFilter());
60  
61      if (mList == null) {
62        return new ArrayList();
63      }
64      manifests = Arrays.asList(mList);
65      if (manifests.size() == 0) {
66        return new ArrayList();
67      }
68  
69      return manifests;
70    }
71  
72    public synchronized void refresh() {
73      init();
74    }
75  
76    /***
77     * Retrieves the last used manifest or <code>null</code> when none was found.
78     *
79     * @return The last used manifest.
80     * @throws ManifestException When the manifest referred to by {@link Manifest.HISTORY_KEY} could not be loaded.
81     */
82    public Manifest loadManifestFromHistory() throws LocationException, ManifestException {
83  
84      String contextManifest = workingContext.getContextManifestPreference();
85  
86      String manifestId = Preferences.userRoot().get(contextManifest, null);
87      if (manifestId != null) {
88  
89        ManifestFactory manifestFactory = new ManifestFactory();
90        ManifestLoader loader = new ManifestLoader(workingContext);
91        Manifest manifest = manifestFactory.create(workingContext, loader.load(manifestId));
92  
93        return manifest;
94      }
95  
96      return null;
97    }
98  
99    private synchronized void init() {
100     manifests = new HashSet();
101   }
102 }