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      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public String getScmType() {
65          return "local";
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
73              throws ScmRepositoryException {
74          String[] tokens = StringUtils.split(scmSpecificUrl, Character.toString(delimiter));
75  
76          if (tokens.length != 2) {
77              throw new ScmRepositoryException("The connection string didn't contain the expected number of tokens. "
78                      + "Expected 2 tokens but got " + tokens.length + " tokens.");
79          }
80  
81          // ----------------------------------------------------------------------
82          //
83          // ----------------------------------------------------------------------
84  
85          String root = tokens[0];
86  
87          File rootFile = new File(root);
88  
89          if (!rootFile.isAbsolute()) {
90              String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
91  
92              rootFile = new File(basedir, root);
93          }
94  
95          if (!rootFile.exists()) {
96              throw new ScmRepositoryException("The root doesn't exists (" + rootFile.getAbsolutePath() + ").");
97          }
98  
99          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 }