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