in.cleartax.dropwizard.sharding.hibernate.ScanningMultiTenantHibernateBundle Maven / Gradle / Ivy
/*
* Copyright 2018 Saurabh Agrawal (Cleartax)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package in.cleartax.dropwizard.sharding.hibernate;
import com.google.common.collect.ImmutableList;
import io.dropwizard.Configuration;
import org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener;
import org.glassfish.jersey.server.internal.scanning.PackageNamesScanner;
import javax.persistence.Entity;
import java.io.IOException;
import java.io.InputStream;
public abstract class ScanningMultiTenantHibernateBundle extends MultiTenantHibernateBundle {
protected ScanningMultiTenantHibernateBundle(String pckg) {
this(pckg, new MultiTenantSessionFactoryFactory());
}
protected ScanningMultiTenantHibernateBundle(String pckg, MultiTenantSessionFactoryFactory sessionFactoryFactory) {
this(new String[]{pckg}, sessionFactoryFactory);
}
protected ScanningMultiTenantHibernateBundle(String[] pckgs, MultiTenantSessionFactoryFactory sessionFactoryFactory) {
super(findEntityClassesFromDirectory(pckgs), sessionFactoryFactory);
}
protected ScanningMultiTenantHibernateBundle(ImmutableList> entities,
MultiTenantSessionFactoryFactory sessionFactoryFactory) {
super(entities, sessionFactoryFactory);
}
/**
* Method scanning given directory for classes containing Hibernate @Entity annotation
*
* @param pckgs string array with packages containing Hibernate entities (classes annotated with @Entity annotation)
* e.g. com.codahale.fake.db.directory.entities
* @return ImmutableList with classes from given directory annotated with Hibernate @Entity annotation
*/
public static ImmutableList> findEntityClassesFromDirectory(String[] pckgs) {
@SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class);
final PackageNamesScanner scanner = new PackageNamesScanner(pckgs, true);
while (scanner.hasNext()) {
final String next = scanner.next();
if (asl.accept(next)) {
try (final InputStream in = scanner.open()) {
asl.process(next, in);
} catch (IOException e) {
throw new RuntimeException("AnnotationAcceptingListener failed to process scanned resource: " + next);
}
}
}
final ImmutableList.Builder> builder = ImmutableList.builder();
for (Class> clazz : asl.getAnnotatedClasses()) {
builder.add(clazz);
}
return builder.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy