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.graph.connector.inmemory;
025    
026    import java.util.concurrent.TimeUnit;
027    import java.util.concurrent.locks.Lock;
028    import javax.transaction.xa.XAResource;
029    import org.jboss.dna.common.statistic.Stopwatch;
030    import org.jboss.dna.common.util.Logger;
031    import org.jboss.dna.graph.ExecutionContext;
032    import org.jboss.dna.graph.cache.CachePolicy;
033    import org.jboss.dna.graph.connector.RepositoryConnection;
034    import org.jboss.dna.graph.connector.RepositorySourceException;
035    import org.jboss.dna.graph.connector.RepositorySourceListener;
036    import org.jboss.dna.graph.request.Request;
037    import org.jboss.dna.graph.request.processor.RequestProcessor;
038    
039    /**
040     * @author Randall Hauch
041     */
042    public class InMemoryRepositoryConnection implements RepositoryConnection {
043    
044        protected static final RepositorySourceListener NO_OP_LISTENER = new RepositorySourceListener() {
045    
046            /**
047             * {@inheritDoc}
048             */
049            public void notify( String sourceName,
050                                Object... events ) {
051                // do nothing
052            }
053        };
054    
055        private final InMemoryRepositorySource source;
056        private final InMemoryRepository repository;
057        private RepositorySourceListener listener = NO_OP_LISTENER;
058    
059        InMemoryRepositoryConnection( InMemoryRepositorySource source,
060                                      InMemoryRepository repository ) {
061            assert source != null;
062            assert repository != null;
063            this.source = source;
064            this.repository = repository;
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public String getSourceName() {
071            return source.getName();
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        public CachePolicy getDefaultCachePolicy() {
078            return source.getDefaultCachePolicy();
079        }
080    
081        /**
082         * {@inheritDoc}
083         */
084        public XAResource getXAResource() {
085            return null;
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        public boolean ping( long time,
092                             TimeUnit unit ) {
093            return true;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public void setListener( RepositorySourceListener listener ) {
100            this.listener = listener != null ? listener : NO_OP_LISTENER;
101        }
102    
103        /**
104         * {@inheritDoc}
105         */
106        public void close() {
107            // do nothing
108        }
109    
110        /**
111         * {@inheritDoc}
112         * 
113         * @see org.jboss.dna.graph.connector.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
114         *      org.jboss.dna.graph.request.Request)
115         */
116        public void execute( ExecutionContext context,
117                             Request request ) throws RepositorySourceException {
118            Logger logger = context.getLogger(getClass());
119            Stopwatch sw = null;
120            if (logger.isTraceEnabled()) {
121                sw = new Stopwatch();
122                sw.start();
123            }
124            // Do any commands update/write?
125            RequestProcessor processor = new InMemoryRequestProcessor(context, this.repository);
126    
127            Lock lock = request.isReadOnly() ? repository.getLock().readLock() : repository.getLock().writeLock();
128            lock.lock();
129            try {
130                // Obtain the lock and execute the commands ...
131                processor.process(request);
132            } finally {
133                try {
134                    processor.close();
135                } finally {
136                    lock.unlock();
137                }
138            }
139            if (logger.isTraceEnabled()) {
140                assert sw != null;
141                sw.stop();
142                logger.trace("InMemoryRepositoryConnection.execute(...) took " + sw.getTotalDuration());
143            }
144        }
145    
146        /**
147         * @return listener
148         */
149        protected RepositorySourceListener getListener() {
150            return this.listener;
151        }
152    
153        /**
154         * {@inheritDoc}
155         * 
156         * @see java.lang.Object#toString()
157         */
158        @Override
159        public String toString() {
160            return "Connection to the \"" + getSourceName() + "\" in-memory repository";
161        }
162    }