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.module;
20  
21  import nl.toolforge.karma.core.KarmaRuntimeException;
22  import nl.toolforge.karma.core.Version;
23  import nl.toolforge.karma.core.boot.WorkingContext;
24  import nl.toolforge.karma.core.location.Location;
25  import nl.toolforge.karma.core.location.LocationException;
26  
27  /***
28   * <p>Factory class to create modules based on a {@link nl.toolforge.karma.core.module.ModuleDigester}.</p>
29   *
30   * @author D.A. Smedes
31   * @version $Id: ModuleFactory.java,v 1.2 2004/11/10 23:53:09 asmedes Exp $
32   */
33  public final class ModuleFactory {
34  
35    private WorkingContext workingContext = null;
36  
37    public ModuleFactory(WorkingContext workingContext) {
38      this.workingContext = workingContext;
39    }
40  
41    public Module create(ModuleDigester digester, Module.Type moduleType) throws LocationException {
42  
43      if (digester == null) {
44        throw new IllegalArgumentException("Descriptor cannot be null.");
45      }
46      if (moduleType == null) {
47        throw new IllegalArgumentException("Module type cannot be null.");
48      }
49  
50      Location location = workingContext.getLocationLoader().get(digester.getLocation());
51  
52      Version version = null;
53      if (digester.getVersion() != null) {
54        version = new Version(digester.getVersion());
55      }
56  
57      if (moduleType.equals(Module.JAVA_SOURCE_MODULE)) {
58        return new SourceModule(digester.getName(), location, version);
59      } else if (moduleType.equals(Module.JAVA_ENTERPRISE_APPLICATION)) {
60        return new JavaEnterpriseApplicationModule(digester.getName(), location, version);
61      } else if (moduleType.equals(Module.JAVA_WEB_APPLICATION)) {
62        return new JavaWebApplicationModule(digester.getName(), location, version);
63      } else if (moduleType.equals(Module.LIBRARY_MODULE)) {
64        return new LibModule(digester.getName(), location, version);
65      } else if (moduleType.equals(Module.OTHER_MODULE)) {
66        return new OtherModule(digester.getName(), location, version);
67      } else if (moduleType.equals(Module.UNKNOWN)) {
68        return new UntypedModule(digester.getName(), location, version);
69      }
70  
71      throw new KarmaRuntimeException("Type mismatch. Could not create module.");
72    }
73  }