001    package org.apache.maven.scm.provider.local;
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.CommandParameters;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.command.add.AddScmResult;
026    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
027    import org.apache.maven.scm.command.checkin.CheckInScmResult;
028    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
029    import org.apache.maven.scm.command.list.ListScmResult;
030    import org.apache.maven.scm.command.mkdir.MkdirScmResult;
031    import org.apache.maven.scm.command.update.UpdateScmResult;
032    import org.apache.maven.scm.command.status.StatusScmResult;
033    import org.apache.maven.scm.command.tag.TagScmResult;
034    import org.apache.maven.scm.provider.AbstractScmProvider;
035    import org.apache.maven.scm.provider.ScmProviderRepository;
036    import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
037    import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand;
038    import org.apache.maven.scm.provider.local.command.checkin.LocalCheckInCommand;
039    import org.apache.maven.scm.provider.local.command.checkout.LocalCheckOutCommand;
040    import org.apache.maven.scm.provider.local.command.list.LocalListCommand;
041    import org.apache.maven.scm.provider.local.command.mkdir.LocalMkdirCommand;
042    import org.apache.maven.scm.provider.local.command.update.LocalUpdateCommand;
043    import org.apache.maven.scm.provider.local.command.status.LocalStatusCommand;
044    import org.apache.maven.scm.provider.local.command.tag.LocalTagCommand;
045    import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
046    import org.apache.maven.scm.repository.ScmRepositoryException;
047    import org.codehaus.plexus.util.StringUtils;
048    
049    import java.io.File;
050    
051    /**
052     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
053     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
054     *
055     * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="local"
056     */
057    public class LocalScmProvider
058        extends AbstractScmProvider
059    {
060        /** {@inheritDoc} */
061        public String getScmType()
062        {
063            return "local";
064        }
065    
066        /** {@inheritDoc} */
067        public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter )
068            throws ScmRepositoryException
069        {
070            String[] tokens = StringUtils.split( scmSpecificUrl, Character.toString( delimiter ) );
071    
072            if ( tokens.length != 2 )
073            {
074                throw new ScmRepositoryException( "The connection string didn't contain the expected number of tokens. "
075                    + "Expected 2 tokens but got " + tokens.length + " tokens." );
076            }
077    
078            // ----------------------------------------------------------------------
079            //
080            // ----------------------------------------------------------------------
081    
082            String root = tokens[0];
083    
084            File rootFile = new File( root );
085    
086            if ( !rootFile.isAbsolute() )
087            {
088                String basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
089    
090                rootFile = new File( basedir, root );
091            }
092    
093            if ( !rootFile.exists() )
094            {
095                throw new ScmRepositoryException( "The root doesn't exists (" + rootFile.getAbsolutePath() + ")." );
096            }
097    
098            if ( !rootFile.isDirectory() )
099            {
100                throw new ScmRepositoryException( "The root isn't a directory (" + rootFile.getAbsolutePath() + ")." );
101            }
102    
103            // ----------------------------------------------------------------------
104            //
105            // ----------------------------------------------------------------------
106    
107            String module = tokens[1];
108    
109            File moduleFile = new File( rootFile, module );
110    
111            if ( !moduleFile.exists() )
112            {
113                throw new ScmRepositoryException(
114                    "The module doesn't exist (root: " + rootFile.getAbsolutePath() + ", module: " + module + ")." );
115            }
116    
117            if ( !moduleFile.isDirectory() )
118            {
119                throw new ScmRepositoryException( "The module isn't a directory." );
120            }
121    
122            return new LocalScmProviderRepository( rootFile.getAbsolutePath(), module );
123        }
124    
125        // ----------------------------------------------------------------------
126        // Utility methods
127        // ----------------------------------------------------------------------
128    
129        public static String fixModuleName( String module )
130        {
131            if ( module.endsWith( "/" ) )
132            {
133                module = module.substring( 0, module.length() - 1 );
134            }
135    
136            if ( module.startsWith( "/" ) )
137            {
138                module = module.substring( 1 );
139            }
140    
141            return module;
142        }
143    
144        /** {@inheritDoc} */
145        public StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
146            throws ScmException
147        {
148            LocalStatusCommand command = new LocalStatusCommand();
149    
150            command.setLogger( getLogger() );
151    
152            return (StatusScmResult) command.execute( repository, fileSet, parameters );
153        }
154    
155        /** {@inheritDoc} */
156        public TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
157            throws ScmException
158        {
159            LocalTagCommand command = new LocalTagCommand();
160    
161            command.setLogger( getLogger() );
162    
163            return (TagScmResult) command.execute( repository, fileSet, parameters );
164        }
165    
166        /** {@inheritDoc} */
167        public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
168            throws ScmException
169        {
170            LocalAddCommand command = new LocalAddCommand();
171    
172            command.setLogger( getLogger() );
173    
174            return (AddScmResult) command.execute( repository, fileSet, parameters );
175        }
176    
177        /** {@inheritDoc} */
178        protected ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet,
179                                                CommandParameters parameters )
180            throws ScmException
181        {
182            LocalChangeLogCommand command = new LocalChangeLogCommand();
183    
184            command.setLogger( getLogger() );
185    
186            return (ChangeLogScmResult) command.execute( repository, fileSet, parameters );
187        }
188    
189        /** {@inheritDoc} */
190        public CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet,
191                                         CommandParameters parameters )
192            throws ScmException
193        {
194            LocalCheckInCommand command = new LocalCheckInCommand();
195    
196            command.setLogger( getLogger() );
197    
198            return (CheckInScmResult) command.execute( repository, fileSet, parameters );
199        }
200    
201        /** {@inheritDoc} */
202        public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
203                                           CommandParameters parameters )
204            throws ScmException
205        {
206            LocalCheckOutCommand command = new LocalCheckOutCommand();
207    
208            command.setLogger( getLogger() );
209    
210            return (CheckOutScmResult) command.execute( repository, fileSet, parameters );
211        }
212    
213        /** {@inheritDoc} */
214        protected ListScmResult list( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
215            throws ScmException
216        {
217            LocalListCommand command = new LocalListCommand();
218    
219            command.setLogger( getLogger() );
220    
221            return (ListScmResult) command.execute( repository, fileSet, parameters );
222        }
223        
224        /** {@inheritDoc} */
225        protected MkdirScmResult mkdir( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
226            throws ScmException
227        {
228            LocalMkdirCommand command = new LocalMkdirCommand();
229    
230            command.setLogger( getLogger() );
231    
232            return (MkdirScmResult) command.execute( repository, fileSet, parameters );
233        }
234    
235        /** {@inheritDoc} */
236        public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
237            throws ScmException
238        {
239            LocalUpdateCommand command = new LocalUpdateCommand();
240    
241            command.setLogger( getLogger() );
242    
243            return (UpdateScmResult) command.execute( repository, fileSet, parameters );
244        }
245    }