1 package org.apache.maven.wagon.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.wagon.PathUtils;
23 import org.apache.maven.wagon.WagonConstants;
24 import org.codehaus.plexus.util.StringUtils;
25
26 import java.io.Serializable;
27 import java.util.Properties;
28
29
30
31
32
33
34
35
36
37 public class Repository
38 implements Serializable
39 {
40 private static final long serialVersionUID = 1312227676322136247L;
41
42 private String id;
43
44 private String name;
45
46 private String host;
47
48 private int port = WagonConstants.UNKNOWN_PORT;
49
50 private String basedir;
51
52 private String protocol;
53
54 private String url;
55
56 private RepositoryPermissions permissions;
57
58
59
60
61
62 private Properties parameters = new Properties();
63
64
65 private String username = null;
66
67 private String password = null;
68
69
70
71
72 public Repository()
73 {
74
75 }
76
77 public Repository( String id, String url )
78 {
79 if ( id == null )
80 {
81 throw new NullPointerException( "id cannot be null" );
82 }
83
84 setId( id );
85
86 if ( url == null )
87 {
88 throw new NullPointerException( "url cannot be null" );
89 }
90
91 setUrl( url );
92 }
93
94 public String getId()
95 {
96 return id;
97 }
98
99 public void setId( String id )
100 {
101 this.id = id;
102 }
103
104
105
106
107
108
109
110 public String getBasedir()
111 {
112 return basedir;
113 }
114
115 public void setBasedir( String basedir )
116 {
117 this.basedir = basedir;
118 }
119
120 public void setName( String name )
121 {
122 this.name = name;
123 }
124
125 public int getPort()
126 {
127 return port;
128 }
129
130 public void setPort( int port )
131 {
132 this.port = port;
133 }
134
135 public void setUrl( String url )
136 {
137 this.url = url;
138
139
140
141
142
143 this.protocol = PathUtils.protocol( url );
144
145 this.host = PathUtils.host( url );
146
147 this.port = PathUtils.port( url );
148
149 this.basedir = PathUtils.basedir( url );
150
151 String username = PathUtils.user( url );
152 this.username = username;
153
154 if ( username != null )
155 {
156 String password = PathUtils.password( url );
157
158 if ( password != null )
159 {
160 this.password = password;
161
162 username += ":" + password;
163 }
164
165 username += "@";
166
167 int index = url.indexOf( username );
168 this.url = url.substring( 0, index ) + url.substring( index + username.length() );
169 }
170 }
171
172 public String getUrl()
173 {
174 if ( url != null )
175 {
176 return url;
177 }
178
179 StringBuilder sb = new StringBuilder();
180
181 sb.append( protocol );
182
183 sb.append( "://" );
184
185 if ( isIPv6Address() )
186 {
187
188
189 sb.append( "[" ).append( getHost() ).append( "]" );
190 }
191 else
192 {
193 sb.append( getHost() );
194 }
195
196 if ( port != WagonConstants.UNKNOWN_PORT )
197 {
198 sb.append( ":" );
199
200 sb.append( port );
201 }
202
203 sb.append( basedir );
204
205 return sb.toString();
206 }
207
208
209
210
211
212
213 private boolean isIPv6Address()
214 {
215 return getHost().contains( ":" );
216 }
217
218 public String getHost()
219 {
220 if ( host == null )
221 {
222 return "localhost";
223 }
224 return host;
225 }
226
227 public String getName()
228 {
229 if ( name == null )
230 {
231 return getId();
232 }
233 return name;
234 }
235
236 public String toString()
237 {
238 StringBuilder sb = new StringBuilder();
239
240 sb.append( "Repository[" );
241
242 if ( StringUtils.isNotEmpty( getName() ) )
243 {
244 sb.append( getName() ).append( "|" );
245 }
246
247 sb.append( getUrl() );
248 sb.append( "]" );
249
250 return sb.toString();
251 }
252
253 public String getProtocol()
254 {
255 return protocol;
256 }
257
258 public RepositoryPermissions getPermissions()
259 {
260 return permissions;
261 }
262
263 public void setPermissions( RepositoryPermissions permissions )
264 {
265 this.permissions = permissions;
266 }
267
268 public String getParameter( String key )
269 {
270 return parameters.getProperty( key );
271 }
272
273 public void setParameters( Properties parameters )
274 {
275 this.parameters = parameters;
276 }
277
278 public int hashCode()
279 {
280 final int prime = 31;
281 int result = 1;
282 result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
283 return result;
284 }
285
286 public boolean equals( Object obj )
287 {
288 if ( this == obj )
289 {
290 return true;
291 }
292 if ( obj == null )
293 {
294 return false;
295 }
296 if ( getClass() != obj.getClass() )
297 {
298 return false;
299 }
300 final Repository other = (Repository) obj;
301 if ( id == null )
302 {
303 if ( other.id != null )
304 {
305 return false;
306 }
307 }
308 else if ( !id.equals( other.id ) )
309 {
310 return false;
311 }
312 return true;
313 }
314
315 public String getUsername()
316 {
317 return username;
318 }
319
320 public String getPassword()
321 {
322 return password;
323 }
324
325 public void setProtocol( String protocol )
326 {
327 this.protocol = protocol;
328 }
329
330 }