View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.local;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.command.add.AddScmResult;
31  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
32  import org.apache.maven.scm.command.checkin.CheckInScmResult;
33  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
34  import org.apache.maven.scm.command.list.ListScmResult;
35  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
36  import org.apache.maven.scm.command.status.StatusScmResult;
37  import org.apache.maven.scm.command.tag.TagScmResult;
38  import org.apache.maven.scm.command.update.UpdateScmResult;
39  import org.apache.maven.scm.provider.AbstractScmProvider;
40  import org.apache.maven.scm.provider.ScmProviderRepository;
41  import org.apache.maven.scm.provider.local.command.add.LocalAddCommand;
42  import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand;
43  import org.apache.maven.scm.provider.local.command.checkin.LocalCheckInCommand;
44  import org.apache.maven.scm.provider.local.command.checkout.LocalCheckOutCommand;
45  import org.apache.maven.scm.provider.local.command.list.LocalListCommand;
46  import org.apache.maven.scm.provider.local.command.mkdir.LocalMkdirCommand;
47  import org.apache.maven.scm.provider.local.command.status.LocalStatusCommand;
48  import org.apache.maven.scm.provider.local.command.tag.LocalTagCommand;
49  import org.apache.maven.scm.provider.local.command.update.LocalUpdateCommand;
50  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
51  import org.apache.maven.scm.repository.ScmRepositoryException;
52  
53  /**
54   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
55   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
56   */
57  @Singleton
58  @Named("local")
59  public class LocalScmProvider extends AbstractScmProvider {
60      /** {@inheritDoc} */
61      @Override
62      public String getScmType() {
63          return "local";
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
69              throws ScmRepositoryException {
70          String[] tokens = StringUtils.split(scmSpecificUrl, Character.toString(delimiter));
71  
72          if (tokens.length != 2) {
73              throw new ScmRepositoryException("The connection string didn't contain the expected number of tokens. "
74                      + "Expected 2 tokens but got " + tokens.length + " tokens.");
75          }
76  
77          // ----------------------------------------------------------------------
78          //
79          // ----------------------------------------------------------------------
80  
81          String root = tokens[0];
82  
83          File rootFile = new File(root);
84  
85          if (!rootFile.isAbsolute()) {
86              String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
87  
88              rootFile = new File(basedir, root);
89          }
90  
91          if (!rootFile.exists()) {
92              throw new ScmRepositoryException("The root doesn't exists (" + rootFile.getAbsolutePath() + ").");
93          }
94  
95          if (!rootFile.isDirectory()) {
96              throw new ScmRepositoryException("The root isn't a directory (" + rootFile.getAbsolutePath() + ").");
97          }
98  
99          // ----------------------------------------------------------------------
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 }