001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.connector.svn;
023    
024    import java.util.UUID;
025    import java.util.concurrent.TimeUnit;
026    import javax.transaction.xa.XAResource;
027    import org.jboss.dna.graph.ExecutionContext;
028    import org.jboss.dna.graph.cache.CachePolicy;
029    import org.jboss.dna.graph.connectors.RepositoryConnection;
030    import org.jboss.dna.graph.connectors.RepositorySourceException;
031    import org.jboss.dna.graph.connectors.RepositorySourceListener;
032    import org.jboss.dna.graph.properties.Name;
033    import org.jboss.dna.graph.properties.NameFactory;
034    import org.jboss.dna.graph.requests.Request;
035    import org.tmatesoft.svn.core.SVNException;
036    import org.tmatesoft.svn.core.io.SVNRepository;
037    
038    /**
039     * The repository connection to a SVN Repository instance.
040     * 
041     * @author Serge Pagop
042     */
043    public class SVNRepositoryConnection implements RepositoryConnection {
044    
045        protected static final RepositorySourceListener NO_OP_LISTENER = new RepositorySourceListener() {
046    
047            /**
048             * {@inheritDoc}
049             */
050            public void notify( String sourceName,
051                                Object... events ) {
052                // do nothing
053            }
054        };
055    
056        private Name uuidPropertyName;
057        private final String sourceName;
058        private final String uuidPropertyNameString;
059        private final CachePolicy cachePolicy;
060        private final SVNRepository repository;
061        private RepositorySourceListener listener = NO_OP_LISTENER;
062    
063        public SVNRepositoryConnection( String sourceName,
064                                        CachePolicy cachePolicy,
065                                        String uuidPropertyName,
066                                        SVNRepository repository ) {
067            assert (sourceName != null);
068            assert (repository != null);
069            assert (uuidPropertyName != null);
070            this.sourceName = sourceName;
071            this.cachePolicy = cachePolicy;
072            this.uuidPropertyNameString = uuidPropertyName;
073            this.repository = repository;
074        }
075    
076        SVNRepository getRepository() {
077            return repository;
078        }
079    
080        /**
081         * {@inheritDoc}
082         */
083        public String getSourceName() {
084            return sourceName;
085        }
086    
087        /**
088         * {@inheritDoc}
089         */
090        public CachePolicy getDefaultCachePolicy() {
091            return cachePolicy;
092        }
093    
094        /**
095         * {@inheritDoc}
096         */
097        public XAResource getXAResource() {
098            return null;
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public boolean ping( long time,
105                             TimeUnit unit ) {
106            try {
107                this.repository.getRepositoryRoot(true);
108            } catch (SVNException e) {
109                return false;
110            }
111            return true;
112        }
113    
114        /**
115         * {@inheritDoc}
116         */
117        public void setListener( RepositorySourceListener listener ) {
118            this.listener = listener != null ? listener : NO_OP_LISTENER;
119        }
120    
121        /**
122         * {@inheritDoc}
123         * 
124         * @see org.jboss.dna.graph.connectors.RepositoryConnection#close()
125         */
126        public void close() {
127            // do not care about.
128        }
129    
130        /**
131         * {@inheritDoc}
132         * 
133         * @see org.jboss.dna.graph.connectors.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
134         *      org.jboss.dna.graph.requests.Request)
135         */
136        public void execute( ExecutionContext context,
137                             Request request ) throws RepositorySourceException {
138            // TODO
139        }
140    
141        /**
142         * @return listener
143         */
144        protected RepositorySourceListener getListener() {
145            return this.listener;
146        }
147    
148        /**
149         * Utility method to calculate (if required) and obtain the name that should be used to store the UUID values for each node.
150         * This method may be called without regard to synchronization, since it should return the same value if it happens to be
151         * called concurrently while not yet initialized.
152         * 
153         * @param context the execution context
154         * @return the name, or null if the UUID should not be stored
155         */
156        protected Name getUuidPropertyName( ExecutionContext context ) {
157            if (uuidPropertyName == null) {
158                NameFactory nameFactory = context.getValueFactories().getNameFactory();
159                uuidPropertyName = nameFactory.create(this.uuidPropertyNameString);
160            }
161            return this.uuidPropertyName;
162        }
163    
164        protected UUID generateUuid() {
165            return UUID.randomUUID();
166        }
167    
168    }