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