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