1 /*** 2 * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package net.sf.vcaperture.services; 18 19 import java.io.File; 20 import java.util.Collection; 21 import java.util.Collections; 22 23 import net.sf.vcaperture.model.AbstractRepository; 24 import net.sf.vcaperture.util.BeanUtil; 25 26 /*** 27 * The default repository service with a working general implementation. 28 * 29 * @author Jeremy Thomerson (jthomerson@users.sourceforge.net) 30 */ 31 public class DefaultRepositoryService implements IRepositoryService { 32 33 public static final String REPO_CONFIG_FILE = "repositories.yml"; 34 35 private RepositoryConfiguration mConfiguration; 36 private File mWorkingDirectory; 37 38 /*** 39 * This method should be called after the working directory is set. This will load 40 * the repositories from the configuration file. 41 */ 42 protected void initialize() { 43 if (mWorkingDirectory == null || !mWorkingDirectory.exists() || !mWorkingDirectory.isDirectory()) { 44 throw new IllegalStateException("Working directory must exist as a directory. [Current location: " + mWorkingDirectory.getAbsolutePath() + "]"); 45 } 46 File configFile = new File(mWorkingDirectory, REPO_CONFIG_FILE); 47 if (configFile == null || !configFile.exists() || !configFile.isFile()) { 48 throw new IllegalStateException("Repository configuration file must exist as a file. [Current location: " + configFile.getAbsolutePath() + "]"); 49 } 50 mConfiguration = BeanUtil.loadType(configFile, RepositoryConfiguration.class); 51 } 52 53 public AbstractRepository getRepository(String repositoryName) { 54 for (AbstractRepository repo : getRepositories()) { 55 if (repositoryName.equals(repo.getName())) { 56 return repo; 57 } 58 } 59 return null; 60 } 61 public Collection<AbstractRepository> getRepositories() { 62 return mConfiguration.getRepositories(); 63 } 64 65 public void setRepositories(Collection<AbstractRepository> repos) { 66 if (repos == null) { 67 repos = Collections.emptyList(); 68 } 69 mConfiguration.setRepositories(repos); 70 } 71 72 public File getWorkingDirectory() { 73 return mWorkingDirectory; 74 } 75 76 public void setWorkingDirectory(File directory) { 77 mWorkingDirectory = directory; 78 } 79 80 }