View Javadoc

1   package org.apache.maven.wagon.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * This class is an abstraction of the location from/to resources
31   * can be transfered.
32   *
33   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
34   *
35   * @todo [BP] some things are specific to certain wagons (eg key stuff in authInfo, permissions)
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       * Properties influencing wagon behaviour
60       * which are very specific to particular wagon.
61       */
62      private Properties parameters = new Properties();
63  
64      // Username/password are sometimes encoded in the URL
65      private String username = null;
66  
67      private String password = null;
68  
69      /**
70       * @deprecated use {@link #Repository(String, String)}
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 can not be null for Repository with url=" + url );
82          }
83  
84          setId( id );
85  
86          if ( url == null )
87          {
88              throw new NullPointerException( "url can not be null for Repository with id=" + id );
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      * Retrieve the base directory of the repository. This is derived from the full repository URL, and
106      * contains the entire path component.
107      *
108      * @return the base directory
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         // TODO [BP]: refactor out the PathUtils URL stuff into a class like java.net.URL, so you only parse once
140         //  can't use URL class as is because it won't recognise our protocols, though perhaps we could attempt to
141         //  register handlers for scp, etc?
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         StringBuffer sb = new StringBuffer();
180 
181         sb.append( protocol );
182 
183         sb.append( "://" );
184 
185         sb.append( host );
186 
187         if ( port != WagonConstants.UNKNOWN_PORT )
188         {
189             sb.append( ":" );
190 
191             sb.append( port );
192         }
193 
194         sb.append( basedir );
195 
196         return sb.toString();
197     }
198 
199     public String getHost()
200     {
201         if ( host == null )
202         {
203             return "localhost";
204         }
205         return host;
206     }
207 
208     public String getName()
209     {
210         if ( name == null )
211         {
212             return getId();
213         }
214         return name;
215     }
216 
217     public String toString()
218     {
219         StringBuffer sb = new StringBuffer();
220 
221         sb.append( "Repository[" );
222 
223         if ( StringUtils.isNotEmpty( getName() ) )
224         {
225             sb.append( getName() ).append( "|" );
226         }
227 
228         sb.append( getUrl() );
229         sb.append( "]" );
230 
231         return sb.toString();
232     }
233 
234     public String getProtocol()
235     {
236         return protocol;
237     }
238 
239     public RepositoryPermissions getPermissions()
240     {
241         return permissions;
242     }
243 
244     public void setPermissions( RepositoryPermissions permissions )
245     {
246         this.permissions = permissions;
247     }
248 
249     public String getParameter( String key )
250     {
251         return parameters.getProperty( key );
252     }
253 
254     public void setParameters( Properties parameters )
255     {
256         this.parameters = parameters;
257     }
258 
259     public int hashCode()
260     {
261         final int prime = 31;
262         int result = 1;
263         result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
264         return result;
265     }
266 
267     public boolean equals( Object obj )
268     {
269         if ( this == obj )
270         {
271             return true;
272         }
273         if ( obj == null )
274         {
275             return false;
276         }
277         if ( getClass() != obj.getClass() )
278         {
279             return false;
280         }
281         final Repository other = (Repository) obj;
282         if ( id == null )
283         {
284             if ( other.id != null )
285             {
286                 return false;
287             }
288         }
289         else if ( !id.equals( other.id ) )
290         {
291             return false;
292         }
293         return true;
294     }
295 
296     public String getUsername()
297     {
298         return username;
299     }
300 
301     public String getPassword()
302     {
303         return password;
304     }
305 
306     public void setProtocol( String protocol )
307     {
308         this.protocol = protocol;
309     }
310 
311 }