+ private ParsedPersistenceXmlDescriptor getPersistenceUnitDescriptor(
+ String name,
+ Properties properties,
+ ClassLoaderService loader
+ )
+ throws
+ MojoFailureException
+ {
+ PersistenceXmlParser parser =
+ new PersistenceXmlParser(
+ loader,
+ PersistenceUnitTransactionType.RESOURCE_LOCAL
+ );
+
+ List<ParsedPersistenceXmlDescriptor> units = parser.doResolve(properties);
+
+ if (name == null)
+ {
+ switch (units.size())
+ {
+ case 0:
+ getLog().info("Found no META-INF/persistence.xml.");
+ return null;
+ case 1:
+ getLog().info("Using persistence-unit " + units.get(0).getName());
+ return units.get(0);
+ default:
+ getLog().warn("No name provided and multiple persistence units found:");
+ for (ParsedPersistenceXmlDescriptor unit : units)
+ getLog().warn(" - " + unit.getName());
+ return null;
+ }
+
+ }
+
+ for (ParsedPersistenceXmlDescriptor unit : units)
+ {
+ getLog().debug("Found persistence-unit " + unit.getName());
+ if (!unit.getName().equals(name))
+ continue;
+
+ // See if we (Hibernate) are the persistence provider
+ if (!ProviderChecker.isProvider(unit, properties))
+ {
+ getLog().debug("Wrong provider: " + unit.getProviderClassName());
+ continue;
+ }
+
+ getLog().info("Using persistence-unit " + unit.getName());
+ return unit;
+ }
+
+ throw new MojoFailureException("Could not find persistence-unit " + name);
+ }
+
+
+ static final class MavenProjectClassLoaderService implements ClassLoaderService
+ {
+ final private ClassLoader loader;
+
+
+ public MavenProjectClassLoaderService(ClassLoader loader)
+ {
+ this.loader = loader;
+ }
+
+
+ @Override
+ public <T> Class<T> classForName(String name)
+ {
+ try
+ {
+ return (Class<T>)loader.loadClass(name);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new ClassLoadingException( "Unable to load class [" + name + "]", e );
+ }
+ }
+
+ @Override
+ public URL locateResource(String name)
+ {
+ return loader.getResource(name);
+ }
+
+ @Override
+ public InputStream locateResourceStream(String name)
+ {
+ return loader.getResourceAsStream(name);
+ }
+
+ @Override
+ public List<URL> locateResources(String name)
+ {
+ try
+ {
+ return Collections.list(loader.getResources(name));
+ }
+ catch (IOException e)
+ {
+ return Collections.EMPTY_LIST;
+ }
+ }
+
+ @Override
+ public <S> LinkedHashSet<S> loadJavaServices(Class<S> serviceContract)
+ {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public void stop() { }
+
+ }
+
+