View Javadoc

1   /*
2   * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3   */
4   package nl.toolforge.karma.core.location;
5   
6   import nl.toolforge.karma.core.KarmaRuntimeException;
7   import nl.toolforge.karma.core.vc.cvsimpl.CVSRepository;
8   import nl.toolforge.karma.core.vc.svnimpl.SubversionRepository;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  public class LocationFactory {
13  
14    private static Log logger = LogFactory.getLog(LocationFactory.class);
15    private static LocationFactory instance = null;
16  
17    /***
18     * Gets the singleton instance of this factory.
19     */
20    public static LocationFactory getInstance() {
21  
22      if (instance == null) {
23        instance = new LocationFactory();
24      }
25      return instance;
26    }
27  
28    private LocationFactory() { }
29  
30    public Location createLocation(LocationDescriptor descriptor) throws LocationException {
31  
32      LocationType type = null;
33      try {
34        type = LocationType.getTypeInstance(descriptor.getType());
35      } catch (IllegalArgumentException e) {
36        throw new LocationException(LocationException.INVALID_LOCATION_TYPE, new Object[]{descriptor.getType(), descriptor.getId()});
37      }
38  
39      try {
40        if (type.equals(LocationType.CVS)) {
41  
42          CVSRepository location = new CVSRepository(descriptor.getId());
43  
44          try {
45            location.setProtocol(descriptor.getProtocol());
46          } catch (RuntimeException r) {
47            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"protocol"});
48          }
49          try {
50            location.setHost(descriptor.getHost());
51          } catch (RuntimeException r) {
52            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"host"});
53          }
54          try {
55            location.setPort(descriptor.getPort());
56          } catch (RuntimeException r) {
57            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"port"});
58          }
59          try {
60            location.setRepository(descriptor.getRepository());
61          } catch (RuntimeException r) {
62            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"repository"});
63          }
64          try {
65            location.setOffset(descriptor.getModuleOffset());
66          } catch (RuntimeException r) {
67            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"module-offset"});
68          }
69          return location;
70  
71        } else if (type.equals(LocationType.SUBVERSION)) {
72  
73          SubversionRepository location = new SubversionRepository(descriptor.getId());
74  
75          try {
76            location.setProtocol(descriptor.getProtocol());
77          } catch (RuntimeException r) {
78            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"protocol"});
79          }
80          try {
81            location.setHost(descriptor.getHost());
82          } catch (RuntimeException r) {
83            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"host"});
84          }
85          try {
86            location.setPort(descriptor.getPort());
87          } catch (RuntimeException r) {
88            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"port"});
89          }
90          try {
91            location.setRepository(descriptor.getRepository());
92          } catch (RuntimeException r) {
93            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"repository"});
94          }
95          try {
96            location.setOffset(descriptor.getModuleOffset());
97          } catch (RuntimeException r) {
98            throw new LocationException(LocationException.INVALID_ELEMENT_VALUE, new Object[]{"module-offset"});
99          }
100         return location;
101       }
102     } catch (RuntimeException r) {
103       logger.error(r);
104       throw new LocationException(LocationException.INVALID_ELEMENT_VALUE);
105     }
106     throw new KarmaRuntimeException("Not implemented for other types than '"+LocationType.CVS+"' and '"+LocationType.SUBVERSION+"' ");
107   }
108 }