View Javadoc

1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.model;
7   import java.io.BufferedReader;
8   import java.io.File;
9   import java.io.IOException;
10  import java.io.PrintWriter;
11  import java.io.Reader;
12  import java.io.Writer;
13  import java.util.HashMap;
14  import java.util.Map;
15  /***
16   * Bean that represent configuration of the repository manager.
17   */
18  public class ManagerConfig {
19      private static final String REPO_TYPE = "repoweb.repository.type";
20      private static final String REPO_LOCATION = "repoweb.repository.location";
21      private RepositoryConfig _mainRepository =
22          new RepositoryConfig("org.repoweb.model.file.FileRepository",
23              new File(System.getProperty("user.home"), ".maven/repository")
24                  .getAbsolutePath());
25      private final RepositoryConfig _defaultPomRepository =
26          new RepositoryConfig("org.repoweb.model.jar.JarRepository", "/default-pom/");
27  
28      public RepositoryConfig getDefaultPomRepository() {
29          return _defaultPomRepository;
30      }
31  
32  
33      public void setMainRepository(RepositoryConfig config) {
34          if (config == null) {
35              throw new NullPointerException();
36          }
37          this._mainRepository = config;
38      }
39  
40  
41      public RepositoryConfig getMainRepository() {
42          return _mainRepository;
43      }
44  
45  
46      public void save(Writer writer) {
47          PrintWriter out = new PrintWriter(writer);
48  
49          out.print(REPO_TYPE);
50          out.print("=");
51          out.println(getMainRepository().getType());
52          out.print(REPO_LOCATION);
53          out.print("=");
54          out.println(getMainRepository().getLocation());
55      }
56  
57  
58      public void load(Reader reader) throws IOException {
59          Map props = new HashMap();
60  
61          loadProperty(props, loadFile(reader));
62          _mainRepository =
63              new RepositoryConfig((String)props.get(REPO_TYPE),
64                  (String)props.get(REPO_LOCATION));
65      }
66  
67  
68      public boolean equals(Object obj) {
69          if (this == obj) {
70              return true;
71          }
72          if (!(obj instanceof ManagerConfig)) {
73              return false;
74          }
75  
76          final ManagerConfig managerConfig = (ManagerConfig)obj;
77  
78          if (!_mainRepository.equals(managerConfig._mainRepository)) {
79              return false;
80          }
81  
82          return true;
83      }
84  
85  
86      public int hashCode() {
87          return (_mainRepository != null ? _mainRepository.hashCode() : 0);
88      }
89  
90  
91      private String loadFile(Reader fileReader) throws IOException {
92          BufferedReader reader = new BufferedReader(fileReader);
93          StringBuffer buffer = new StringBuffer();
94          String line;
95  
96          while ((line = reader.readLine()) != null) {
97              if (!line.startsWith("//")) {
98                  buffer.append(line);
99  
100                 if (reader.ready()) {
101                     buffer.append("\n");
102                 }
103             }
104         }
105 
106         return buffer.toString();
107     }
108 
109 
110     private void loadProperty(Map props, String file) {
111         if (file.length() == 0) {
112             return;
113         }
114 
115         int eolIdx = findEndOfProperty(file, 0);
116 
117         if (eolIdx == -1) {
118             eolIdx = file.length();
119         }
120 
121         int equalIdx = file.indexOf('=');
122 
123         String name;
124         String value = "";
125 
126         if ((equalIdx != -1) && (eolIdx > equalIdx)) {
127             name = file.substring(0, equalIdx).trim();
128             value = file.substring(equalIdx + 1, eolIdx).trim();
129         }
130         else {
131             name = file.substring(0, eolIdx).trim();
132         }
133 
134         if (name.length() > 0) {
135             props.put(name, value);
136         }
137 
138         loadProperty(props, file.substring(Math.min(eolIdx + 1, file.length())));
139     }
140 
141 
142     private int findEndOfProperty(String file, int fromIndex) {
143         int idx = file.indexOf('\n', fromIndex);
144 
145         if (idx == -1) {
146             return -1;
147         }
148 
149         if ((idx == 0) || ('//' != file.charAt(idx - 1))) {
150             return idx;
151         }
152 
153         return findEndOfProperty(file, idx + 1);
154     }
155 
156     /***
157      * Repository configuration.
158      */
159     public static class RepositoryConfig {
160         private String _type;
161         private String _location;
162 
163         public RepositoryConfig(String repositoryType, String repositoryLocation) {
164             if (repositoryType == null || repositoryLocation == null) {
165                 throw new NullPointerException();
166             }
167             _type = repositoryType;
168             _location = repositoryLocation;
169         }
170 
171         public String getType() {
172             return _type;
173         }
174 
175 
176         public String getLocation() {
177             return _location;
178         }
179 
180 
181         public boolean equals(Object obj) {
182             if (this == obj) {
183                 return true;
184             }
185             if (!(obj instanceof RepositoryConfig)) {
186                 return false;
187             }
188 
189             final RepositoryConfig repositoryConfig = (RepositoryConfig)obj;
190 
191             if (!_location.equals(repositoryConfig._location)) {
192                 return false;
193             }
194             if (!_type.equals(repositoryConfig._type)) {
195                 return false;
196             }
197 
198             return true;
199         }
200 
201 
202         public int hashCode() {
203             int result;
204             result = _type.hashCode();
205             result = 29 * result + _location.hashCode();
206             return result;
207         }
208     }
209 }