001    package org.apache.maven.scm.provider.cvslib.repository;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
023    import org.apache.maven.scm.provider.cvslib.AbstractCvsScmProvider;
024    
025    /**
026     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
027     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
028     * @version $Id: CvsScmProviderRepository.java 1306867 2012-03-29 13:45:10Z olamy $
029     */
030    public class CvsScmProviderRepository
031        extends ScmProviderRepositoryWithHost
032    {
033        /** */
034        private String cvsroot;
035    
036        /** */
037        private String transport;
038    
039        /** */
040        private String path;
041    
042        /** */
043        private String module;
044    
045        public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
046                                         String path, String module )
047        {
048            this( cvsroot, transport, user, password, host, -1, path, module );
049        }
050    
051        public CvsScmProviderRepository( String cvsroot, String transport, String user, String password, String host,
052                                         int port, String path, String module )
053        {
054            this.cvsroot = cvsroot;
055    
056            this.transport = transport;
057    
058            if ( user == null && AbstractCvsScmProvider.TRANSPORT_EXT.equals( transport ) )
059            {
060                user = System.getProperty( "user.name" );
061            }
062    
063            setUser( user );
064    
065            setPassword( password );
066    
067            setHost( host );
068    
069            setPort( port );
070    
071            this.path = path;
072    
073            this.module = module;
074        }
075    
076        /**
077         * @return The cvs root
078         */
079        public String getCvsRoot()
080        {
081            String root = getCvsRootForCvsPass();
082    
083            return removeDefaultPortFromCvsRoot( root );
084        }
085    
086        private String removeDefaultPortFromCvsRoot( String root )
087        {
088            if ( root != null && root.indexOf( ":2401" ) > 0 )
089            {
090                root = root.substring( 0, root.indexOf( ":2401" ) ) + ":" + root.substring( root.indexOf( ":2401" ) + 5 );
091            }
092    
093            return root;
094        }
095    
096        /**
097         * @return The cvs root stored in .cvspass
098         */
099        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    }