Author: tchemit Date: 2008-05-13 14:14:09 +0000 (Tue, 13 May 2008) New Revision: 650 Added: trunk/lutinvcs/core/src/main/java/org/codelutin/vcs/VCSConfigFactory.java Log: refactor config + introduction factory de config Copied: trunk/lutinvcs/core/src/main/java/org/codelutin/vcs/VCSConfigFactory.java (from rev 630, trunk/lutinvcs/core/src/main/java/org/codelutin/vcs/VCSFactory.java) =================================================================== --- trunk/lutinvcs/core/src/main/java/org/codelutin/vcs/VCSConfigFactory.java (rev 0) +++ trunk/lutinvcs/core/src/main/java/org/codelutin/vcs/VCSConfigFactory.java 2008-05-13 14:14:09 UTC (rev 650) @@ -0,0 +1,116 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ +package org.codelutin.vcs; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.config.IdentityConfig; +import org.codelutin.util.config.SimpleIdentityConfig; +import org.codelutin.vcs.util.SimpleVCSConnexionConfig; +import org.codelutin.vcs.util.SimpleVCSRootConfig; +import org.codelutin.vcs.type.VCSConnexionConfigProperty; + +import java.util.ArrayList; +import java.util.List; +import java.util.EnumMap; + +/** + * This classes acts as a factory of {@link org.codelutin.vcs.VCSProvider}, and {@link org.codelutin.vcs.VCSConnexion}. + * + * @author chemit + * @see VCSConnexionConfig + * @see VCSRootConfig + */ +@org.codelutin.i18n.I18nable +public class VCSConfigFactory { + + static protected final Log log = LogFactory.getLog(VCSConfigFactory.class); + + /** + * shared instance + */ + static protected VCSConfigFactory instance; + + /** + * root configs indexed by their name + */ + protected final java.util.Map<String, VCSRootConfig> roots; + + /** + * connexion configs indexed by their rootConfig name + */ + protected final List<VCSConnexionConfig> connexions; + + + public static VCSConfigFactory getInstance() { + if (instance == null) { + instance = new VCSConfigFactory(); + } + return instance; + } + + public static IdentityConfig newIdentity(String firstName, String lastName, String email) { + IdentityConfig config = new SimpleIdentityConfig(firstName, lastName, email); + log.info(config); + return config; + } + + public List<VCSRootConfig> getRoots() { + return new ArrayList<VCSRootConfig>(roots.values()); + } + + public List<VCSConnexionConfig> getConnexions() { + return connexions; + } + + public static VCSRootConfig newRoot(String name, String type, String host, String repository, Integer port) { + if (getInstance().roots.containsKey(name)) { + // can not + throw new IllegalStateException("can not create roots " + name + ", already existing "); + } + VCSRootConfig config = new SimpleVCSRootConfig(name, type, host, repository, port); + log.info(config); + getInstance().roots.put(name, config); + return config; + } + + public static VCSConnexionConfig newConnexion(String rootName, EnumMap<VCSConnexionConfigProperty, Object> props) { + if (!getInstance().roots.containsKey(rootName)) { + // can not + throw new IllegalStateException("unfound root " + rootName); + } + + VCSConnexionConfig config = new SimpleVCSConnexionConfig(props); + if (props==null || !props.containsKey(VCSConnexionConfigProperty.rootConfig)) { + // force to add rootConfig + config.setRootConfig(getInstance().roots.get(rootName)); + } + log.info(config); + getInstance().connexions.add(config); + return config; + } + + + protected VCSConfigFactory() { + roots = new java.util.HashMap<String, VCSRootConfig>(); + connexions = new ArrayList<VCSConnexionConfig>(); + } + +} \ No newline at end of file