View Javadoc

1   package org.apache.maven.scm.provider.cvslib.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.scm.provider.ScmProviderRepositoryWithHost;
23  import org.apache.maven.scm.provider.cvslib.AbstractCvsScmProvider;
24  
25  /**
26   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
27   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
28   *
29   */
30  public class CvsScmProviderRepository
31      extends ScmProviderRepositoryWithHost
32  {
33      /** */
34      private String cvsroot;
35  
36      /** */
37      private String transport;
38  
39      /** */
40      private String path;
41  
42      /** */
43      private String module;
44  
45      public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
46                                       String path, String module )
47      {
48          this( cvsroot, transport, user, password, host, -1, path, module );
49      }
50  
51      public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
52                                       int port, String path, String module )
53      {
54          this.cvsroot = cvsroot;
55  
56          this.transport = transport;
57  
58          if ( user == null && AbstractCvsScmProvider.TRANSPORT_EXT.equals( transport ) )
59          {
60              user = System.getProperty( "user.name" );
61          }
62  
63          setUser( user );
64  
65          setPassword( password );
66  
67          setHost( host );
68  
69          setPort( port );
70  
71          this.path = path;
72  
73          this.module = module;
74      }
75  
76      /**
77       * @return The cvs root
78       */
79      public String getCvsRoot()
80      {
81          String root = getCvsRootForCvsPass();
82  
83          return removeDefaultPortFromCvsRoot( root );
84      }
85  
86      private String removeDefaultPortFromCvsRoot( String root )
87      {
88          if ( root != null && root.indexOf( ":2401" ) > 0 )
89          {
90              root = root.substring( 0, root.indexOf( ":2401" ) ) + ":" + root.substring( root.indexOf( ":2401" ) + 5 );
91          }
92  
93          return root;
94      }
95  
96      /**
97       * @return The cvs root stored in .cvspass
98       */
99      public String getCvsRootForCvsPass()
100     {
101         String result;
102         String transport = getTransport();
103         if ( AbstractCvsScmProvider.TRANSPORT_LOCAL.equals( transport ) )
104         {
105             result = ":" + transport + ":" + cvsroot;
106         }
107         else if ( getUser() != null )
108         {
109             result = getCvsRootWithCorrectUser( getUser() );
110         }
111         else
112         {
113             throw new IllegalArgumentException( "Username isn't defined." );
114         }
115         return result;
116     }
117 
118     /**
119      * @return The subtype (like pserver).
120      */
121     public String getTransport()
122     {
123         return transport;
124     }
125 
126     /**
127      * @return The path.
128      */
129     public String getPath()
130     {
131         return path;
132     }
133 
134     /**
135      * @return The module name.
136      */
137     public String getModule()
138     {
139         return module;
140     }
141 
142     private String getCvsRootWithCorrectUser()
143     {
144         return getCvsRootWithCorrectUser( null );
145     }
146 
147     /**
148      * @param user user name
149      * @return
150      */
151     private String getCvsRootWithCorrectUser( String user )
152     {
153         //:transport:rest_of_cvsroot
154         int indexOfUsername = getTransport().length() + 2;
155 
156         int indexOfAt = cvsroot.indexOf( '@' );
157 
158         String userString = user == null ? "" : ":" + user;
159 
160         if ( indexOfAt > 0 )
161         {
162             cvsroot = ":" + getTransport() + userString + cvsroot.substring( indexOfAt );
163         }
164         else
165         {
166             cvsroot = ":" + getTransport() + userString + "@" + cvsroot.substring( indexOfUsername );
167         }
168 
169         return cvsroot;
170     }
171 
172     /** {@inheritDoc} */
173     public String toString()
174     {
175         StringBuilder sb = new StringBuilder();
176 
177         if ( getUser() == null )
178         {
179             if ( AbstractCvsScmProvider.TRANSPORT_LOCAL.equals( getTransport() ) )
180             {
181                 sb.append( getCvsRoot() );
182             }
183             else
184             {
185                 sb.append( removeDefaultPortFromCvsRoot( getCvsRootWithCorrectUser() ) );
186             }
187         }
188         else
189         {
190             sb.append( getCvsRoot() );
191         }
192         sb.append( ":" );
193         sb.append( getModule() );
194 
195         /* remove first colon */
196         if ( sb.charAt( 0 ) == ':' )
197         {
198             sb.deleteCharAt( 0 );
199         }
200         return sb.toString();
201     }
202 
203 }