001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.connector.filesystem;
025    
026    import java.io.File;
027    import java.io.FilenameFilter;
028    import java.util.Set;
029    import java.util.concurrent.CopyOnWriteArrayList;
030    import java.util.concurrent.TimeUnit;
031    import javax.transaction.xa.XAResource;
032    import org.jboss.dna.graph.ExecutionContext;
033    import org.jboss.dna.graph.cache.CachePolicy;
034    import org.jboss.dna.graph.connector.RepositoryConnection;
035    import org.jboss.dna.graph.connector.RepositorySourceException;
036    import org.jboss.dna.graph.connector.RepositorySourceListener;
037    import org.jboss.dna.graph.request.Request;
038    import org.jboss.dna.graph.request.processor.RequestProcessor;
039    
040    /**
041     * The {@link RepositoryConnection} implementation for the file system connector. The bulk of the work is performed by the
042     * {@link FileSystemRequestProcessor}.
043     * 
044     * @author Randall Hauch
045     */
046    public class FileSystemConnection implements RepositoryConnection {
047    
048        private final String sourceName;
049        private final File defaultWorkspace;
050        private final CachePolicy cachePolicy;
051        private final Set<String> availableWorkspaceNames;
052        private final boolean creatingWorkspacesAllowed;
053        private final CopyOnWriteArrayList<RepositorySourceListener> listeners = new CopyOnWriteArrayList<RepositorySourceListener>();
054        private final FilenameFilter filenameFilter;
055        private final boolean updatesAllowed;
056    
057        FileSystemConnection( String sourceName,
058                              File defaultWorkspace,
059                              Set<String> availableWorkspaceNames,
060                              boolean creatingWorkspacesAllowed,
061                              CachePolicy cachePolicy,
062                              FilenameFilter filenameFilter,
063                              boolean updatesAllowed ) {
064            assert sourceName != null;
065            assert sourceName.trim().length() != 0;
066            assert availableWorkspaceNames != null;
067            this.sourceName = sourceName;
068            this.defaultWorkspace = defaultWorkspace;
069            this.availableWorkspaceNames = availableWorkspaceNames;
070            this.creatingWorkspacesAllowed = creatingWorkspacesAllowed;
071            this.cachePolicy = cachePolicy;
072            this.filenameFilter = filenameFilter;
073            this.updatesAllowed = updatesAllowed;
074        }
075    
076        /**
077         * {@inheritDoc}
078         * 
079         * @see org.jboss.dna.graph.connector.RepositoryConnection#getSourceName()
080         */
081        public String getSourceName() {
082            return sourceName;
083        }
084    
085        /**
086         * {@inheritDoc}
087         * 
088         * @see org.jboss.dna.graph.connector.RepositoryConnection#getDefaultCachePolicy()
089         */
090        public CachePolicy getDefaultCachePolicy() {
091            return cachePolicy;
092        }
093    
094        /**
095         * {@inheritDoc}
096         * 
097         * @see org.jboss.dna.graph.connector.RepositoryConnection#getXAResource()
098         */
099        public XAResource getXAResource() {
100            return null;
101        }
102    
103        /**
104         * {@inheritDoc}
105         * 
106         * @see org.jboss.dna.graph.connector.RepositoryConnection#setListener(org.jboss.dna.graph.connector.RepositorySourceListener)
107         */
108        public void setListener( RepositorySourceListener listener ) {
109            if (listener != null) {
110                listeners.addIfAbsent(listener);
111            }
112        }
113    
114        /**
115         * {@inheritDoc}
116         * 
117         * @see org.jboss.dna.graph.connector.RepositoryConnection#ping(long, java.util.concurrent.TimeUnit)
118         */
119        public boolean ping( long time,
120                             TimeUnit unit ) {
121            return true;
122        }
123    
124        /**
125         * {@inheritDoc}
126         * 
127         * @see org.jboss.dna.graph.connector.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
128         *      org.jboss.dna.graph.request.Request)
129         */
130        public void execute( ExecutionContext context,
131                             Request request ) throws RepositorySourceException {
132            RequestProcessor proc = new FileSystemRequestProcessor(sourceName, defaultWorkspace, availableWorkspaceNames,
133                                                                   creatingWorkspacesAllowed, context, filenameFilter, updatesAllowed);
134            try {
135                proc.process(request);
136            } finally {
137                proc.close();
138            }
139        }
140    
141        /**
142         * {@inheritDoc}
143         * 
144         * @see org.jboss.dna.graph.connector.RepositoryConnection#close()
145         */
146        public void close() {
147        }
148    }