Author: tchemit Date: 2008-04-04 21:43:01 +0000 (Fri, 04 Apr 2008) New Revision: 351 Added: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSProvider.java Modified: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSProvider.java trunk/lutinvcs/lutinvcs-provider-cvs/src/main/java/org/codelutin/vcs/CVSProvider.java trunk/lutinvcs/lutinvcs-provider-mock/src/main/java/org/codelutin/vcs/MockProvider.java trunk/lutinvcs/lutinvcs-provider-svn/src/main/java/org/codelutin/vcs/SVNProvider.java Log: refactor provider to act as handler and connexion factory Modified: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSProvider.java =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSProvider.java 2008-04-04 21:26:19 UTC (rev 350) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSProvider.java 2008-04-04 21:43:01 UTC (rev 351) @@ -14,6 +14,8 @@ */ package org.codelutin.vcs; +import java.util.List; + /** * The contract to be realized to provide vcs communication. * <p/> @@ -28,13 +30,17 @@ /** @return the identifier of the vcs provider (eg SVN, CVS, MOCK) */ String getName(); + /** @return list of known connexions. */ + List<C> getConnexions(); + /** * return a new VCSHandler instance for given config. * * @param config svn handler config * @return the new instance + * @throws IllegalStateException if any pb while init handler */ - H newInstance(VCSConfig config); + H newInstance(VCSConfig config) throws IllegalStateException; /** * @param mode the mode required Added: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSProvider.java =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSProvider.java (rev 0) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSProvider.java 2008-04-04 21:43:01 UTC (rev 351) @@ -0,0 +1,78 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, 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.util; + +import org.codelutin.vcs.VCSConfig; +import org.codelutin.vcs.VCSConnexion; +import org.codelutin.vcs.VCSConnexionMode; +import org.codelutin.vcs.VCSHandler; +import org.codelutin.vcs.VCSProvider; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; + +/** + * base implementation of provider with a cached handler and list of connexions + * + * @author chemit + */ +public abstract class AbstractVCSProvider<H extends VCSHandler, C extends VCSConnexion> implements VCSProvider<H, C> { + + protected H handler; + + protected List<C> connexions; + + protected final String name; + + private final Class<H> handlerImpl; + + protected AbstractVCSProvider(String name, Class<H> handlerImpl) { + this.name = name; + this.handlerImpl = handlerImpl; + } + + public final String getName() { + return name; + } + + public List<C> getConnexions() { + if (connexions == null) { + connexions = new ArrayList<C>(); + } + return connexions; + } + + public H newInstance(VCSConfig config) throws IllegalStateException { + if (handler != null) { + return handler; + } + try { + return handlerImpl.getConstructor(VCSConfig.class).newInstance(config); + } catch (InstantiationException e) { + throw new IllegalStateException("could not instanciate the handler " + handlerImpl + " for reason " + e.getMessage()); + } catch (IllegalAccessException e) { + throw new IllegalStateException("could not instanciate the handler " + handlerImpl + " for reason " + e.getMessage()); + } catch (InvocationTargetException e) { + throw new IllegalStateException("could not instanciate the handler " + handlerImpl + " for reason " + e.getMessage()); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("could not instanciate the handler " + handlerImpl + " for reason " + e.getMessage()); + } + } + + public C newConnection(VCSConnexionMode mode, VCSConfig config) { + return null; + } +} Modified: trunk/lutinvcs/lutinvcs-provider-cvs/src/main/java/org/codelutin/vcs/CVSProvider.java =================================================================== --- trunk/lutinvcs/lutinvcs-provider-cvs/src/main/java/org/codelutin/vcs/CVSProvider.java 2008-04-04 21:26:19 UTC (rev 350) +++ trunk/lutinvcs/lutinvcs-provider-cvs/src/main/java/org/codelutin/vcs/CVSProvider.java 2008-04-04 21:43:01 UTC (rev 351) @@ -14,20 +14,21 @@ */ package org.codelutin.vcs; +import org.codelutin.vcs.impl.cvs.CVSConnexion; import org.codelutin.vcs.impl.cvs.CVSHandler; import org.codelutin.vcs.impl.cvs.CVSHelper; -import org.codelutin.vcs.impl.cvs.CVSConnexion; +import org.codelutin.vcs.util.AbstractVCSProvider; /** @author chemit */ -public class CVSProvider implements VCSProvider<CVSHandler, CVSConnexion> { +public class CVSProvider extends AbstractVCSProvider<CVSHandler, CVSConnexion> { - public String getName() { - return "CVS"; + public CVSProvider() { + super("CVS", CVSHandler.class); } public CVSHandler newInstance(VCSConfig config) { CVSHelper.setConfig(config); - return new CVSHandler(config); + return super.newInstance(config); } public CVSConnexion newConnection(VCSConnexionMode mode, VCSConfig config) { Modified: trunk/lutinvcs/lutinvcs-provider-mock/src/main/java/org/codelutin/vcs/MockProvider.java =================================================================== --- trunk/lutinvcs/lutinvcs-provider-mock/src/main/java/org/codelutin/vcs/MockProvider.java 2008-04-04 21:26:19 UTC (rev 350) +++ trunk/lutinvcs/lutinvcs-provider-mock/src/main/java/org/codelutin/vcs/MockProvider.java 2008-04-04 21:43:01 UTC (rev 351) @@ -14,20 +14,17 @@ */ package org.codelutin.vcs; -import org.codelutin.vcs.impl.mock.MockVCSHandler; import org.codelutin.vcs.impl.mock.MockConnexion; +import org.codelutin.vcs.impl.mock.MockVCSHandler; +import org.codelutin.vcs.util.AbstractVCSProvider; /** @author chemit */ -public class MockProvider implements VCSProvider<MockVCSHandler, MockConnexion> { +public class MockProvider extends AbstractVCSProvider<MockVCSHandler, MockConnexion> { - public String getName() { - return "MOCK"; + public MockProvider() { + super("MOCK", MockVCSHandler.class); } - public MockVCSHandler newInstance(VCSConfig config) { - return new MockVCSHandler(config); - } - public MockConnexion newConnection(VCSConnexionMode mode, VCSConfig config) { return null; } Modified: trunk/lutinvcs/lutinvcs-provider-svn/src/main/java/org/codelutin/vcs/SVNProvider.java =================================================================== --- trunk/lutinvcs/lutinvcs-provider-svn/src/main/java/org/codelutin/vcs/SVNProvider.java 2008-04-04 21:26:19 UTC (rev 350) +++ trunk/lutinvcs/lutinvcs-provider-svn/src/main/java/org/codelutin/vcs/SVNProvider.java 2008-04-04 21:43:01 UTC (rev 351) @@ -16,15 +16,13 @@ import org.codelutin.vcs.impl.svn.SVNHandler; import org.codelutin.vcs.util.AbstractVCSConnexion; +import org.codelutin.vcs.util.AbstractVCSProvider; /** @author chemit */ -public class SVNProvider implements VCSProvider<SVNHandler, AbstractVCSConnexion> { - public String getName() { - return "SVN"; - } +public class SVNProvider extends AbstractVCSProvider<SVNHandler, AbstractVCSConnexion> { - public SVNHandler newInstance(VCSConfig config) { - return new SVNHandler(config); + public SVNProvider() { + super("SVN", SVNHandler.class); } public AbstractVCSConnexion newConnection(VCSConnexionMode mode, VCSConfig config) {