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.store.jpa;
025    
026    import java.util.UUID;
027    import java.util.concurrent.CopyOnWriteArrayList;
028    import java.util.concurrent.TimeUnit;
029    import javax.persistence.EntityManager;
030    import javax.transaction.xa.XAResource;
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 JpaConnection implements RepositoryConnection {
043    
044        private final String name;
045        private final CachePolicy cachePolicy;
046        private final CopyOnWriteArrayList<RepositorySourceListener> listeners = new CopyOnWriteArrayList<RepositorySourceListener>();
047        private final EntityManager entityManager;
048        private final Model model;
049        private final UUID rootNodeUuid;
050        private final String nameOfDefaultWorkspace;
051        private final String[] predefinedWorkspaceNames;
052        private final boolean creatingWorkspacesAllowed;
053        private final long largeValueMinimumSizeInBytes;
054        private final boolean compressData;
055        private final boolean enforceReferentialIntegrity;
056    
057        /*package*/JpaConnection( String sourceName,
058                                   CachePolicy cachePolicy,
059                                   EntityManager entityManager,
060                                   Model model,
061                                   UUID rootNodeUuid,
062                                   String nameOfDefaultWorkspace,
063                                   String[] predefinedWorkspaceNames,
064                                   long largeValueMinimumSizeInBytes,
065                                   boolean creatingWorkspacesAllowed,
066                                   boolean compressData,
067                                   boolean enforceReferentialIntegrity ) {
068            assert sourceName != null;
069            assert entityManager != null;
070            assert model != null;
071            assert rootNodeUuid != null;
072            this.name = sourceName;
073            this.cachePolicy = cachePolicy; // may be null
074            this.entityManager = entityManager;
075            this.model = model;
076            this.rootNodeUuid = rootNodeUuid;
077            this.largeValueMinimumSizeInBytes = largeValueMinimumSizeInBytes;
078            this.compressData = compressData;
079            this.enforceReferentialIntegrity = enforceReferentialIntegrity;
080            this.nameOfDefaultWorkspace = nameOfDefaultWorkspace;
081            this.predefinedWorkspaceNames = predefinedWorkspaceNames != null ? predefinedWorkspaceNames : new String[] {};
082            this.creatingWorkspacesAllowed = creatingWorkspacesAllowed;
083        }
084    
085        /**
086         * {@inheritDoc}
087         * 
088         * @see org.jboss.dna.graph.connector.RepositoryConnection#getSourceName()
089         */
090        public String getSourceName() {
091            return name;
092        }
093    
094        /**
095         * {@inheritDoc}
096         * 
097         * @see org.jboss.dna.graph.connector.RepositoryConnection#setListener(org.jboss.dna.graph.connector.RepositorySourceListener)
098         */
099        public void setListener( RepositorySourceListener listener ) {
100            if (listener != null) {
101                listeners.addIfAbsent(listener);
102            }
103        }
104    
105        /**
106         * {@inheritDoc}
107         * 
108         * @see org.jboss.dna.graph.connector.RepositoryConnection#getDefaultCachePolicy()
109         */
110        public CachePolicy getDefaultCachePolicy() {
111            return cachePolicy;
112        }
113    
114        /**
115         * {@inheritDoc}
116         * 
117         * @see org.jboss.dna.graph.connector.RepositoryConnection#getXAResource()
118         */
119        public XAResource getXAResource() {
120            return null;
121        }
122    
123        /**
124         * {@inheritDoc}
125         * 
126         * @see org.jboss.dna.graph.connector.RepositoryConnection#ping(long, java.util.concurrent.TimeUnit)
127         */
128        public boolean ping( long time,
129                             TimeUnit unit ) {
130            return entityManager.isOpen();
131        }
132    
133        /**
134         * {@inheritDoc}
135         * 
136         * @see org.jboss.dna.graph.connector.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
137         *      org.jboss.dna.graph.request.Request)
138         */
139        public void execute( ExecutionContext context,
140                             Request request ) throws RepositorySourceException {
141            long size = largeValueMinimumSizeInBytes;
142            RequestProcessor proc = model.createRequestProcessor(name,
143                                                                 context,
144                                                                 entityManager,
145                                                                 rootNodeUuid,
146                                                                 nameOfDefaultWorkspace,
147                                                                 predefinedWorkspaceNames,
148                                                                 size,
149                                                                 creatingWorkspacesAllowed,
150                                                                 compressData,
151                                                                 enforceReferentialIntegrity);
152            try {
153                proc.process(request);
154            } finally {
155                proc.close();
156            }
157        }
158    
159        /**
160         * {@inheritDoc}
161         * 
162         * @see org.jboss.dna.graph.connector.RepositoryConnection#close()
163         */
164        public void close() {
165        }
166    
167    }