001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jpa.entities;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import javax.persistence.EntityManager;
022: import javax.persistence.EntityManagerFactory;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.openjpa.meta.ClassMetaData;
027: import org.apache.openjpa.persistence.EntityManagerFactoryImpl;
028: import org.apache.openjpa.persistence.OpenJPAPersistence;
029: import org.compass.core.mapping.ResourceMapping;
030: import org.compass.gps.device.jpa.JpaGpsDevice;
031: import org.compass.gps.device.jpa.JpaGpsDeviceException;
032: import org.compass.gps.spi.CompassGpsInterfaceDevice;
033:
034: /**
035: * A specilized version that works with OpenJPA. This class should be used instead of
036: * {@link org.compass.gps.device.jpa.entities.DefaultJpaEntitiesLocator} since it works with both openjpa xml files and annotatios.
037: *
038: * @author kimchy
039: */
040: public class OpenJPAJpaEntitiesLocator implements JpaEntitiesLocator {
041:
042: protected Log log = LogFactory.getLog(getClass());
043:
044: private ClassLoader classLoader;
045:
046: public void setClassLoader(ClassLoader classLoader) {
047: this .classLoader = classLoader;
048: }
049:
050: public EntityInformation[] locate(
051: EntityManagerFactory entityManagerFactory,
052: JpaGpsDevice device) throws JpaGpsDeviceException {
053:
054: CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device
055: .getGps();
056:
057: // TODO this should use OpenJPAEnitiyManagerFactorySPI, here for backward compatability with pre 1.0
058: EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) OpenJPAPersistence
059: .cast(entityManagerFactory);
060:
061: ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>();
062:
063: // pre initalize the class meta data
064: EntityManager entityManager = emf.createEntityManager();
065: entityManager.close();
066:
067: Collection<Class> classes = emf.getConfiguration()
068: .getMetaDataRepositoryInstance().loadPersistentTypes(
069: true, classLoader);
070: for (Class clazz : classes) {
071: ClassMetaData classMetaData = emf.getConfiguration()
072: .getMetaDataRepositoryInstance().getMetaData(clazz,
073: classLoader, true);
074: String entityname = classMetaData.getDescribedType()
075: .getName();
076: if (!gps.hasMappingForEntityForIndex((entityname))) {
077: if (log.isDebugEnabled()) {
078: log
079: .debug("Entity ["
080: + entityname
081: + "] does not have compass mapping, filtering it out");
082: }
083: continue;
084: }
085:
086: if (shouldFilter(entityname, classMetaData, device)) {
087: continue;
088: }
089: ResourceMapping resourceMapping = gps
090: .getMappingForEntityForIndex(entityname);
091: EntityInformation entityInformation = new EntityInformation(
092: clazz, entityname, resourceMapping
093: .getSubIndexHash().getSubIndexes());
094: entitiesList.add(entityInformation);
095: if (log.isDebugEnabled()) {
096: log
097: .debug("Entity [" + entityname
098: + "] will be indexed");
099: }
100: }
101: return entitiesList.toArray(new EntityInformation[entitiesList
102: .size()]);
103: }
104:
105: /**
106: * Returns <code>true</code> if the entity name needs to be filtered.
107: *
108: * <p>Implementation filteres out inherited OpenJPA mappings, since the select query
109: * for the base class will cover any inherited classes as well.
110: *
111: * <p>Note, that this method is called after it has been verified that the class has
112: * Compass mappings (either directly, or indirectly by an interface or a super class).
113: *
114: * @param entityname The name of the entity
115: * @param classMetadata The OpenJPA class meta data.
116: * @param device The Jpa Gps device
117: * @return <code>true</code> if the entity should be filtered out, <code>false</code> if not.
118: */
119: protected boolean shouldFilter(String entityname,
120: ClassMetaData classMetadata, JpaGpsDevice device) {
121: Class<?> clazz = classMetadata.getDescribedType();
122: // if it is inherited, do not add it to the classes to index, since the "from [entity]"
123: // query for the base class will return results for this class as well
124: if (classMetadata.getMappedPCSuperclassMetaData() != null) {
125: Class super Class = classMetadata
126: .getMappedPCSuperclassMetaData().getDescribedType();
127: // only filter out classes that their super class has compass mappings
128: if (super Class != null
129: && ((CompassGpsInterfaceDevice) device.getGps())
130: .hasMappingForEntityForIndex(super Class)) {
131: if (log.isDebugEnabled()) {
132: log
133: .debug("Entity ["
134: + entityname
135: + "] is inherited and super class ["
136: + super Class
137: + "] has compass mapping, filtering it out");
138: }
139: return true;
140: }
141: }
142: return false;
143: }
144: }
|