/*
* Check whether Classes to scan was provided into persistence.xml If
* yes, load them. Otherwise load them from classpath/ context path
*/
Reader reader;
ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
if (classesToScan == null || classesToScan.isEmpty())
{
log.info("No class to scan for persistence unit " + persistenceUnit
+ ". Entities will be loaded from classpath/ context-path");
// Entity metadata is not related to any PU, and hence will be
// stored at common place
// persistenceUnit = Constants.COMMON_ENTITY_METADATAS;
// Check whether all common entity metadata have already been loaded
if (appMetadata.getMetamodelMap().get(persistenceUnit) != null)
{
log.info("All common entitity metadata already loaded, nothing need to be done");
return;
}
reader = new ClasspathReader();
// resources = reader.findResourcesByClasspath();
}
else
{
reader = new ClasspathReader(classesToScan);
// resources = reader.findResourcesByContextLoader();
}
InputStream[] iStreams = null;
PersistenceUnitMetadata puMetadata = persistentUnitMetadataMap.get(persistenceUnit);
if (this.getClass().getClassLoader() instanceof URLClassLoader && !puMetadata.getExcludeUnlistedClasses())
{
URL[] managedClasses = reader.findResources();
if (managedClasses != null)
{
List<URL> managedResources = Arrays.asList(managedClasses);
managedURLs.addAll(managedResources);
}
}
else
{
iStreams = reader.findResourcesAsStream();
}
if (managedURLs != null)
{
resources = managedURLs.toArray(new URL[] {});
}
// All entities to load should be annotated with @Entity
reader.addValidAnnotations(Entity.class.getName());
Metamodel metamodel = appMetadata.getMetamodel(persistenceUnit);
if (metamodel == null)
{
metamodel = new MetamodelImpl();
}
Map<String, EntityMetadata> entityMetadataMap = ((MetamodelImpl) metamodel).getEntityMetadataMap();
Map<String, Class<?>> entityNameToClassMap = ((MetamodelImpl) metamodel).getEntityNameToClassMap();
Map<String, List<String>> puToClazzMap = new HashMap<String, List<String>>();
Map<String, IdDiscriptor> entityNameToKeyDiscriptorMap = new HashMap<String, IdDiscriptor>();
List<Class<?>> classes = new ArrayList<Class<?>>();
if (resources != null && resources.length > 0)
{
for (URL resource : resources)
{
try
{
ResourceIterator itr = reader.getResourceIterator(resource, reader.getFilter());
InputStream is = null;
while ((is = itr.next()) != null)
{
classes.addAll(scanClassAndPutMetadata(is, reader, entityMetadataMap, entityNameToClassMap,
persistenceUnit, client, puToClazzMap, entityNameToKeyDiscriptorMap));
}
}
catch (IOException e)
{
log.error("Error while retreiving and storing entity metadata. Details:", e);
throw new MetamodelLoaderException("Error while retreiving and storing entity metadata");
}
}
}
else if (iStreams != null)
{
try
{
for (InputStream is : iStreams)
{
try
{
classes.addAll(scanClassAndPutMetadata(is, reader, entityMetadataMap, entityNameToClassMap,
persistenceUnit, client, puToClazzMap, entityNameToKeyDiscriptorMap));
}
finally
{
if (is != null)
{
is.close();
}
}
}
}
catch (IOException e)
{
log.error("Error while retreiving and storing entity metadata. Details:", e);
throw new MetamodelLoaderException("Error while retreiving and storing entity metadata, Caused by : .",
e);
}
}
((MetamodelImpl) metamodel).setEntityMetadataMap(entityMetadataMap);
appMetadata.getMetamodelMap().put(persistenceUnit, metamodel);
appMetadata.setClazzToPuMap(puToClazzMap);
((MetamodelImpl) metamodel).addKeyValues(entityNameToKeyDiscriptorMap);
// assign JPA metamodel.
((MetamodelImpl) metamodel).assignEmbeddables(kunderaMetadata.getApplicationMetadata()
.getMetaModelBuilder(persistenceUnit).getEmbeddables());
((MetamodelImpl) metamodel).assignManagedTypes(kunderaMetadata.getApplicationMetadata()