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.federation;
023    
024    import java.util.concurrent.TimeUnit;
025    import java.util.concurrent.atomic.AtomicReference;
026    import javax.transaction.xa.XAResource;
027    import net.jcip.annotations.ThreadSafe;
028    import org.jboss.dna.graph.ExecutionContext;
029    import org.jboss.dna.graph.cache.CachePolicy;
030    import org.jboss.dna.graph.connectors.RepositoryConnection;
031    import org.jboss.dna.graph.connectors.RepositorySourceException;
032    import org.jboss.dna.graph.connectors.RepositorySourceListener;
033    import org.jboss.dna.graph.requests.Request;
034    import org.jboss.dna.graph.requests.processor.RequestProcessor;
035    
036    /**
037     * @author Randall Hauch
038     */
039    @ThreadSafe
040    public class FederatedRepositoryConnection implements RepositoryConnection {
041    
042        protected static final RepositorySourceListener NO_OP_LISTENER = new RepositorySourceListener() {
043            public void notify( String sourceName,
044                                Object... events ) {
045                // do nothing
046            }
047        };
048    
049        private final FederatedRepository repository;
050        private final String sourceName;
051        private final AtomicReference<RepositorySourceListener> listener;
052    
053        protected FederatedRepositoryConnection( FederatedRepository repository,
054                                                 String sourceName ) {
055            assert sourceName != null;
056            assert repository != null;
057            this.sourceName = sourceName;
058            this.repository = repository;
059            this.listener = new AtomicReference<RepositorySourceListener>(NO_OP_LISTENER);
060            this.repository.register(this);
061        }
062    
063        /**
064         * @return repository
065         */
066        protected FederatedRepository getRepository() {
067            return this.repository;
068        }
069    
070        /**
071         * {@inheritDoc}
072         */
073        public String getSourceName() {
074            return this.sourceName;
075        }
076    
077        /**
078         * {@inheritDoc}
079         */
080        public CachePolicy getDefaultCachePolicy() {
081            return this.repository.getConfiguration().getDefaultCachePolicy();
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public XAResource getXAResource() {
088            return null;
089        }
090    
091        /**
092         * {@inheritDoc}
093         */
094        public void setListener( RepositorySourceListener listener ) {
095            if (listener == null) listener = NO_OP_LISTENER;
096            RepositorySourceListener oldListener = this.listener.getAndSet(listener);
097            this.repository.addListener(listener);
098            if (oldListener != NO_OP_LISTENER) {
099                this.repository.removeListener(oldListener);
100            }
101        }
102    
103        /**
104         * {@inheritDoc}
105         */
106        public boolean ping( long time,
107                             TimeUnit unit ) {
108            return this.repository.isRunning();
109        }
110    
111        /**
112         * {@inheritDoc}
113         * 
114         * @see org.jboss.dna.graph.connectors.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
115         *      org.jboss.dna.graph.requests.Request)
116         */
117        public void execute( ExecutionContext context,
118                             Request request ) throws RepositorySourceException {
119            if (!this.repository.isRunning()) {
120                throw new RepositorySourceException(FederationI18n.repositoryHasBeenShutDown.text(this.repository.getName()));
121            }
122            if (request == null) return;
123    
124            RequestProcessor processor = this.repository.getProcessor(context, sourceName);
125            assert processor != null;
126            try {
127                processor.process(request);
128            } finally {
129                processor.close();
130            }
131        }
132    
133        /**
134         * {@inheritDoc}
135         */
136        public void close() {
137            try {
138                this.repository.removeListener(this.listener.get());
139            } finally {
140                this.repository.unregister(this);
141            }
142        }
143    
144    }