001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.local;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.io.File;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.maven.scm.CommandParameters;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.command.add.AddScmResult;
031import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
032import org.apache.maven.scm.command.checkin.CheckInScmResult;
033import org.apache.maven.scm.command.checkout.CheckOutScmResult;
034import org.apache.maven.scm.command.list.ListScmResult;
035import org.apache.maven.scm.command.mkdir.MkdirScmResult;
036import org.apache.maven.scm.command.status.StatusScmResult;
037import org.apache.maven.scm.command.tag.TagScmResult;
038import org.apache.maven.scm.command.update.UpdateScmResult;
039import org.apache.maven.scm.provider.AbstractScmProvider;
040import org.apache.maven.scm.provider.ScmProviderRepository;
041import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
042import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand;
043import org.apache.maven.scm.provider.local.command.checkin.LocalCheckInCommand;
044import org.apache.maven.scm.provider.local.command.checkout.LocalCheckOutCommand;
045import org.apache.maven.scm.provider.local.command.list.LocalListCommand;
046import org.apache.maven.scm.provider.local.command.mkdir.LocalMkdirCommand;
047import org.apache.maven.scm.provider.local.command.status.LocalStatusCommand;
048import org.apache.maven.scm.provider.local.command.tag.LocalTagCommand;
049import org.apache.maven.scm.provider.local.command.update.LocalUpdateCommand;
050import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
051import org.apache.maven.scm.repository.ScmRepositoryException;
052
053/**
054 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
055 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
056 */
057@Singleton
058@Named("local")
059public class LocalScmProvider extends AbstractScmProvider {
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public String getScmType() {
065        return "local";
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
073            throws ScmRepositoryException {
074        String[] tokens = StringUtils.split(scmSpecificUrl, Character.toString(delimiter));
075
076        if (tokens.length != 2) {
077            throw new ScmRepositoryException("The connection string didn't contain the expected number of tokens. "
078                    + "Expected 2 tokens but got " + tokens.length + " tokens.");
079        }
080
081        // ----------------------------------------------------------------------
082        //
083        // ----------------------------------------------------------------------
084
085        String root = tokens[0];
086
087        File rootFile = new File(root);
088
089        if (!rootFile.isAbsolute()) {
090            String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
091
092            rootFile = new File(basedir, root);
093        }
094
095        if (!rootFile.exists()) {
096            throw new ScmRepositoryException("The root doesn't exists (" + rootFile.getAbsolutePath() + ").");
097        }
098
099        if (!rootFile.isDirectory()) {
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            throw new ScmRepositoryException(
113                    "The module doesn't exist (root: " + rootFile.getAbsolutePath() + ", module: " + module + ").");
114        }
115
116        if (!moduleFile.isDirectory()) {
117            throw new ScmRepositoryException("The module isn't a directory.");
118        }
119
120        return new LocalScmProviderRepository(rootFile.getAbsolutePath(), module);
121    }
122
123    // ----------------------------------------------------------------------
124    // Utility methods
125    // ----------------------------------------------------------------------
126
127    public static String fixModuleName(String module) {
128        if (module.endsWith("/")) {
129            module = module.substring(0, module.length() - 1);
130        }
131
132        if (module.startsWith("/")) {
133            module = module.substring(1);
134        }
135
136        return module;
137    }
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public StatusScmResult status(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
144            throws ScmException {
145        LocalStatusCommand command = new LocalStatusCommand();
146
147        return (StatusScmResult) command.execute(repository, fileSet, parameters);
148    }
149
150    /**
151     * {@inheritDoc}
152     */
153    @Override
154    public TagScmResult tag(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
155            throws ScmException {
156        LocalTagCommand command = new LocalTagCommand();
157
158        return (TagScmResult) command.execute(repository, fileSet, parameters);
159    }
160
161    /**
162     * {@inheritDoc}
163     */
164    @Override
165    public AddScmResult add(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
166            throws ScmException {
167        LocalAddCommand command = new LocalAddCommand();
168
169        return (AddScmResult) command.execute(repository, fileSet, parameters);
170    }
171
172    /**
173     * {@inheritDoc}
174     */
175    @Override
176    protected ChangeLogScmResult changelog(
177            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
178        LocalChangeLogCommand command = new LocalChangeLogCommand();
179
180        return (ChangeLogScmResult) command.execute(repository, fileSet, parameters);
181    }
182
183    /**
184     * {@inheritDoc}
185     */
186    @Override
187    public CheckInScmResult checkin(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
188            throws ScmException {
189        LocalCheckInCommand command = new LocalCheckInCommand();
190
191        return (CheckInScmResult) command.execute(repository, fileSet, parameters);
192    }
193
194    /**
195     * {@inheritDoc}
196     */
197    @Override
198    public CheckOutScmResult checkout(
199            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
200        LocalCheckOutCommand command = new LocalCheckOutCommand();
201
202        return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
203    }
204
205    /**
206     * {@inheritDoc}
207     */
208    @Override
209    protected ListScmResult list(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
210            throws ScmException {
211        LocalListCommand command = new LocalListCommand();
212
213        return (ListScmResult) command.execute(repository, fileSet, parameters);
214    }
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    protected MkdirScmResult mkdir(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
221            throws ScmException {
222        LocalMkdirCommand command = new LocalMkdirCommand();
223
224        return (MkdirScmResult) command.execute(repository, fileSet, parameters);
225    }
226
227    /**
228     * {@inheritDoc}
229     */
230    @Override
231    public UpdateScmResult update(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
232            throws ScmException {
233        LocalUpdateCommand command = new LocalUpdateCommand();
234
235        return (UpdateScmResult) command.execute(repository, fileSet, parameters);
236    }
237}