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    /** {@inheritDoc} */
061    @Override
062    public String getScmType() {
063        return "local";
064    }
065
066    /** {@inheritDoc} */
067    @Override
068    public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
069            throws ScmRepositoryException {
070        String[] tokens = StringUtils.split(scmSpecificUrl, Character.toString(delimiter));
071
072        if (tokens.length != 2) {
073            throw new ScmRepositoryException("The connection string didn't contain the expected number of tokens. "
074                    + "Expected 2 tokens but got " + tokens.length + " tokens.");
075        }
076
077        // ----------------------------------------------------------------------
078        //
079        // ----------------------------------------------------------------------
080
081        String root = tokens[0];
082
083        File rootFile = new File(root);
084
085        if (!rootFile.isAbsolute()) {
086            String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
087
088            rootFile = new File(basedir, root);
089        }
090
091        if (!rootFile.exists()) {
092            throw new ScmRepositoryException("The root doesn't exists (" + rootFile.getAbsolutePath() + ").");
093        }
094
095        if (!rootFile.isDirectory()) {
096            throw new ScmRepositoryException("The root isn't a directory (" + rootFile.getAbsolutePath() + ").");
097        }
098
099        // ----------------------------------------------------------------------
100        //
101        // ----------------------------------------------------------------------
102
103        String module = tokens[1];
104
105        File moduleFile = new File(rootFile, module);
106
107        if (!moduleFile.exists()) {
108            throw new ScmRepositoryException(
109                    "The module doesn't exist (root: " + rootFile.getAbsolutePath() + ", module: " + module + ").");
110        }
111
112        if (!moduleFile.isDirectory()) {
113            throw new ScmRepositoryException("The module isn't a directory.");
114        }
115
116        return new LocalScmProviderRepository(rootFile.getAbsolutePath(), module);
117    }
118
119    // ----------------------------------------------------------------------
120    // Utility methods
121    // ----------------------------------------------------------------------
122
123    public static String fixModuleName(String module) {
124        if (module.endsWith("/")) {
125            module = module.substring(0, module.length() - 1);
126        }
127
128        if (module.startsWith("/")) {
129            module = module.substring(1);
130        }
131
132        return module;
133    }
134
135    /** {@inheritDoc} */
136    @Override
137    public StatusScmResult status(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
138            throws ScmException {
139        LocalStatusCommand command = new LocalStatusCommand();
140
141        return (StatusScmResult) command.execute(repository, fileSet, parameters);
142    }
143
144    /** {@inheritDoc} */
145    @Override
146    public TagScmResult tag(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
147            throws ScmException {
148        LocalTagCommand command = new LocalTagCommand();
149
150        return (TagScmResult) command.execute(repository, fileSet, parameters);
151    }
152
153    /** {@inheritDoc} */
154    @Override
155    public AddScmResult add(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
156            throws ScmException {
157        LocalAddCommand command = new LocalAddCommand();
158
159        return (AddScmResult) command.execute(repository, fileSet, parameters);
160    }
161
162    /** {@inheritDoc} */
163    @Override
164    protected ChangeLogScmResult changelog(
165            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
166        LocalChangeLogCommand command = new LocalChangeLogCommand();
167
168        return (ChangeLogScmResult) command.execute(repository, fileSet, parameters);
169    }
170
171    /** {@inheritDoc} */
172    @Override
173    public CheckInScmResult checkin(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
174            throws ScmException {
175        LocalCheckInCommand command = new LocalCheckInCommand();
176
177        return (CheckInScmResult) command.execute(repository, fileSet, parameters);
178    }
179
180    /** {@inheritDoc} */
181    @Override
182    public CheckOutScmResult checkout(
183            ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
184        LocalCheckOutCommand command = new LocalCheckOutCommand();
185
186        return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
187    }
188
189    /** {@inheritDoc} */
190    @Override
191    protected ListScmResult list(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
192            throws ScmException {
193        LocalListCommand command = new LocalListCommand();
194
195        return (ListScmResult) command.execute(repository, fileSet, parameters);
196    }
197
198    /** {@inheritDoc} */
199    @Override
200    protected MkdirScmResult mkdir(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
201            throws ScmException {
202        LocalMkdirCommand command = new LocalMkdirCommand();
203
204        return (MkdirScmResult) command.execute(repository, fileSet, parameters);
205    }
206
207    /** {@inheritDoc} */
208    @Override
209    public UpdateScmResult update(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
210            throws ScmException {
211        LocalUpdateCommand command = new LocalUpdateCommand();
212
213        return (UpdateScmResult) command.execute(repository, fileSet, parameters);
214    }
215}