Source code: org/repoweb/model/ManagerConfig.java
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 String _repositoryType = "org.repoweb.model.file.FileRepository";
22 private String _repositoryLocation =
23 new File(System.getProperty("user.home"), ".maven/repository").getAbsolutePath();
24
25 public String getRepositoryType() {
26 return _repositoryType;
27 }
28
29
30 public void setRepositoryType(String repositoryType) {
31 this._repositoryType = repositoryType;
32 }
33
34
35 public String getRepositoryLocation() {
36 return _repositoryLocation;
37 }
38
39
40 public void setRepositoryLocation(String repositoryLocation) {
41 this._repositoryLocation = repositoryLocation;
42 }
43
44
45 public void save(Writer writer) throws IOException {
46 PrintWriter out = new PrintWriter(writer);
47
48 out.print(REPO_TYPE);
49 out.print("=");
50 out.println(getRepositoryType());
51 out.print(REPO_LOCATION);
52 out.print("=");
53 out.println(getRepositoryLocation());
54 }
55
56
57 public void load(Reader reader) throws IOException {
58 Map props = new HashMap();
59
60 loadProperty(props, loadFile(reader));
61 setRepositoryType((String)props.get(REPO_TYPE));
62 setRepositoryLocation((String)props.get(REPO_LOCATION));
63 }
64
65
66 public boolean equals(Object o) {
67 if (this == o) {
68 return true;
69 }
70 if (!(o instanceof ManagerConfig)) {
71 return false;
72 }
73
74 final ManagerConfig managerConfig = (ManagerConfig)o;
75
76 if (_repositoryLocation != null
77 ? !_repositoryLocation.equals(managerConfig._repositoryLocation)
78 : managerConfig._repositoryLocation != null) {
79 return false;
80 }
81 if (_repositoryType != null
82 ? !_repositoryType.equals(managerConfig._repositoryType)
83 : managerConfig._repositoryType != null) {
84 return false;
85 }
86
87 return true;
88 }
89
90
91 public int hashCode() {
92 int result;
93 result = (_repositoryType != null ? _repositoryType.hashCode() : 0);
94 result =
95 29 * result
96 + (_repositoryLocation != null ? _repositoryLocation.hashCode() : 0);
97 return result;
98 }
99
100
101 private String loadFile(Reader r) throws IOException {
102 BufferedReader reader = new BufferedReader(r);
103 StringBuffer buffer = new StringBuffer();
104 String line;
105
106 while ((line = reader.readLine()) != null) {
107 if (!line.startsWith("//")) {
108 buffer.append(line);
109
110 if (reader.ready()) {
111 buffer.append("\n");
112 }
113 }
114 }
115
116 return buffer.toString();
117 }
118
119
120 private void loadProperty(Map props, String file) {
121 if (file.length() == 0) {
122 return;
123 }
124
125 int eolIdx = findEndOfProperty(file, 0);
126
127 if (eolIdx == -1) {
128 eolIdx = file.length();
129 }
130
131 int equalIdx = file.indexOf('=');
132
133 String name;
134 String value = "";
135
136 if ((equalIdx != -1) && (eolIdx > equalIdx)) {
137 name = file.substring(0, equalIdx).trim();
138 value = file.substring(equalIdx + 1, eolIdx).trim();
139 }
140 else {
141 name = file.substring(0, eolIdx).trim();
142 }
143
144 if (name.length() > 0) {
145 props.put(name, value);
146 }
147
148 loadProperty(props, file.substring(Math.min(eolIdx + 1, file.length())));
149 }
150
151
152 private int findEndOfProperty(String file, int fromIndex) {
153 int idx = file.indexOf('\n', fromIndex);
154
155 if (idx == -1) {
156 return -1;
157 }
158
159 if ((idx == 0) || ('\\' != file.charAt(idx - 1))) {
160 return idx;
161 }
162
163 return findEndOfProperty(file, idx + 1);
164 }
165 }