001package org.apache.maven.scm.provider.cvslib.cvsexe;
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
022import org.apache.maven.scm.command.Command;
023import org.apache.maven.scm.provider.cvslib.AbstractCvsScmProvider;
024import org.apache.maven.scm.provider.cvslib.command.login.CvsLoginCommand;
025import org.apache.maven.scm.provider.cvslib.cvsexe.command.add.CvsExeAddCommand;
026import org.apache.maven.scm.provider.cvslib.cvsexe.command.blame.CvsExeBlameCommand;
027import org.apache.maven.scm.provider.cvslib.cvsexe.command.branch.CvsExeBranchCommand;
028import org.apache.maven.scm.provider.cvslib.cvsexe.command.changelog.CvsExeChangeLogCommand;
029import org.apache.maven.scm.provider.cvslib.cvsexe.command.checkin.CvsExeCheckInCommand;
030import org.apache.maven.scm.provider.cvslib.cvsexe.command.checkout.CvsExeCheckOutCommand;
031import org.apache.maven.scm.provider.cvslib.cvsexe.command.diff.CvsExeDiffCommand;
032import org.apache.maven.scm.provider.cvslib.cvsexe.command.export.CvsExeExportCommand;
033import org.apache.maven.scm.provider.cvslib.cvsexe.command.list.CvsExeListCommand;
034import org.apache.maven.scm.provider.cvslib.cvsexe.command.mkdir.CvsExeMkdirCommand;
035import org.apache.maven.scm.provider.cvslib.cvsexe.command.remove.CvsExeRemoveCommand;
036import org.apache.maven.scm.provider.cvslib.cvsexe.command.status.CvsExeStatusCommand;
037import org.apache.maven.scm.provider.cvslib.cvsexe.command.tag.CvsExeTagCommand;
038import org.apache.maven.scm.provider.cvslib.cvsexe.command.update.CvsExeUpdateCommand;
039import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
040import org.codehaus.plexus.util.StringUtils;
041
042/**
043 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
044 *
045 * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="cvs_native"
046 */
047public class CvsExeScmProvider
048    extends AbstractCvsScmProvider
049{
050    /** sserver transport method */
051    public static final String TRANSPORT_SSERVER = "sserver";
052
053    /** {@inheritDoc} */
054    protected Command getAddCommand()
055    {
056        return new CvsExeAddCommand();
057    }
058
059    /** {@inheritDoc} */
060    protected Command getBranchCommand()
061    {
062        return new CvsExeBranchCommand();
063    }
064
065    /** {@inheritDoc} */
066    protected Command getBlameCommand()
067    {
068        return new CvsExeBlameCommand();
069    }
070
071    /** {@inheritDoc} */
072    protected Command getChangeLogCommand()
073    {
074        return new CvsExeChangeLogCommand();
075    }
076
077    /** {@inheritDoc} */
078    protected Command getCheckInCommand()
079    {
080        return new CvsExeCheckInCommand();
081    }
082
083    /** {@inheritDoc} */
084    protected Command getCheckOutCommand()
085    {
086        return new CvsExeCheckOutCommand();
087    }
088
089    /** {@inheritDoc} */
090    protected Command getDiffCommand()
091    {
092        return new CvsExeDiffCommand();
093    }
094
095    /** {@inheritDoc} */
096    protected Command getExportCommand()
097    {
098        return new CvsExeExportCommand();
099    }
100
101    /** {@inheritDoc} */
102    protected Command getListCommand()
103    {
104        return new CvsExeListCommand();
105    }
106
107    /** {@inheritDoc} */
108    protected Command getLoginCommand()
109    {
110        return new CvsLoginCommand();
111    }
112
113    /** {@inheritDoc} */
114    protected Command getRemoveCommand()
115    {
116        return new CvsExeRemoveCommand();
117    }
118
119    /** {@inheritDoc} */
120    protected Command getStatusCommand()
121    {
122        return new CvsExeStatusCommand();
123    }
124
125    /** {@inheritDoc} */
126    protected Command getTagCommand()
127    {
128        return new CvsExeTagCommand();
129    }
130
131    /** {@inheritDoc} */
132    protected Command getUpdateCommand()
133    {
134        return new CvsExeUpdateCommand();
135    }
136    
137    /** {@inheritDoc} */
138    protected Command getMkdirCommand()
139    {
140        return new CvsExeMkdirCommand();
141    }    
142
143    /** {@inheritDoc} */
144    protected ScmUrlParserResult parseScmUrl( String scmSpecificUrl, char delimiter )
145    {
146        ScmUrlParserResult result = super.parseScmUrl( scmSpecificUrl, delimiter );
147        if ( result.getMessages().isEmpty() )
148        {
149            return result;
150        }
151
152        result.resetMessages();
153
154        String[] tokens = StringUtils.split( scmSpecificUrl, Character.toString( delimiter ) );
155
156        String cvsroot;
157
158        String transport = tokens[0];
159
160        // support sserver
161        if ( transport.equalsIgnoreCase( TRANSPORT_SSERVER ) )
162        {
163            if ( tokens.length < 4 || tokens.length > 5 && transport.equalsIgnoreCase( TRANSPORT_SSERVER ) )
164            {
165                result.getMessages().add( "The connection string contains too few tokens." );
166
167                return result;
168            }
169
170            //create the cvsroot as the remote cvsroot
171            if ( tokens.length == 4 )
172            {
173                cvsroot = ":" + transport + ":" + tokens[1] + ":" + tokens[2];
174            }
175            else
176            {
177                cvsroot = ":" + transport + ":" + tokens[1] + ":" + tokens[2] + ":" + tokens[3];
178            }
179        }
180        else
181        {
182            result.getMessages().add( "Unknown transport: " + transport );
183
184            return result;
185        }
186
187        String user = null;
188
189        String host = null;
190
191        String path = null;
192
193        String module = null;
194
195        int port = -1;
196
197        if ( transport.equalsIgnoreCase( TRANSPORT_SSERVER ) )
198        {
199            //sspi:[username@]host:[port]path:module
200            String userhost = tokens[1];
201
202            int index = userhost.indexOf( '@' );
203
204            if ( index == -1 )
205            {
206                user = "";
207
208                host = userhost;
209            }
210            else
211            {
212                user = userhost.substring( 0, index );
213
214                host = userhost.substring( index + 1 );
215            }
216
217            // no port specified
218            if ( tokens.length == 4 )
219            {
220                path = tokens[2];
221                module = tokens[3];
222            }
223            else
224            {
225                // getting port
226                try
227                {
228                    port = Integer.valueOf( tokens[2] ).intValue();
229                    path = tokens[3];
230                    module = tokens[4];
231                }
232                catch ( Exception e )
233                {
234                    //incorrect
235                    result.getMessages().add( "Your scm url is invalid, could not get port value." );
236
237                    return result;
238                }
239            }
240
241            // cvsroot format is :sspi:host:path
242            cvsroot = ":" + transport + ":" + host + ":";
243
244            if ( port != -1 )
245            {
246                cvsroot += port;
247            }
248
249            cvsroot += path;
250        }
251
252        if ( port == -1 )
253        {
254            result.setRepository( new CvsScmProviderRepository( cvsroot, transport, user, null, host, path,
255                                                                module ) );
256        }
257        else
258        {
259            result.setRepository( new CvsScmProviderRepository( cvsroot, transport, user, null, host, port,
260                                                                path, module ) );
261        }
262
263        return result;
264    }
265}