/** * 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 List<Class<?>> findEntityClassesFromDirectory(String[] pckgs) { @SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class); try (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); } } } } return new ArrayList<>(asl.getAnnotatedClasses()); } }
private void init() { compositeResourceFinder = new CompositeResourceFinder(); for (final String p : packages) { try { final Enumeration<URL> urls = ResourcesProvider.getInstance().getResources(p.replace('.', '/'), classloader); while (urls.hasMoreElements()) { try { addResourceFinder(toURI(urls.nextElement())); } catch (final URISyntaxException e) { throw new ResourceFinderException("Error when converting a URL to a URI", e); } } } catch (final IOException e) { throw new ResourceFinderException("IO error when package scanning jar", e); } } }
/** * Adds array of package names which will be used to scan for components. * <p/> * Package scanning ignores an inheritance and therefore {@link Path} annotation * on parent classes and interfaces will be ignored. * <p/> * @param recursive defines whether any nested packages in the collection of specified * package names should be recursively scanned (value of {@code true}) * as part of the package scanning or not (value of {@code false}). * @param packages array of package names. * @return updated resource configuration instance. * @see #packages(String...) */ public final ResourceConfig packages(final boolean recursive, final String... packages) { if (packages == null || packages.length == 0) { return this; } return registerFinder(new PackageNamesScanner(packages, recursive)); }
final PackageNamesScanner scanner = new PackageNamesScanner( scanPkgs.toArray(new String[scanPkgs.size()]), true); while (scanner.hasNext()) { String fileName = scanner.next(); if (!fileName.endsWith(".class")) continue; ClassInfo info = new ClassInfo(fileName) {
private URI toURI(final URL url) throws URISyntaxException { try { return url.toURI(); } catch (final URISyntaxException e) { // Work around bug where some URLs are incorrectly encoded. // This can occur when certain class loaders are utilized // to obtain URLs for resources. return URI.create(toExternalForm(url)); } }
@Override public InputStream getFileStream() { if (in == null) { in = scanner.open(); } return in; }
/** * Adds array of package names which will be used to scan for components. * <p/> * Package scanning ignores an inheritance and therefore {@link Path} annotation * on parent classes and interfaces will be ignored. * <p/> * @param recursive defines whether any nested packages in the collection of specified * package names should be recursively scanned (value of {@code true}) * as part of the package scanning or not (value of {@code false}). * @param packages array of package names. * @return updated resource configuration instance. * @see #packages(String...) */ public final ResourceConfig packages(final boolean recursive, final String... packages) { if (packages == null || packages.length == 0) { return this; } return registerFinder(new PackageNamesScanner(packages, recursive)); }
private URI toURI(final URL url) throws URISyntaxException { try { return url.toURI(); } catch (final URISyntaxException e) { // Work around bug where some URLs are incorrectly encoded. // This can occur when certain class loaders are utilized // to obtain URLs for resources. return URI.create(toExternalForm(url)); } }
/** * 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<Class<?>> 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<Class<?>> builder = ImmutableList.builder(); for (Class<?> clazz : asl.getAnnotatedClasses()) { builder.add(clazz); } return builder.build(); } }
final Object p = getProperty(ServerProperties.PROVIDER_SCANNING_RECURSIVE); final boolean recursive = p == null || PropertiesHelper.isProperty(p); rfs.add(new PackageNamesScanner(packageNames, recursive));
private void init() { compositeResourceFinder = new CompositeResourceFinder(); for (final String p : packages) { try { final Enumeration<URL> urls = ResourcesProvider.getInstance().getResources(p.replace('.', '/'), classloader); while (urls.hasMoreElements()) { try { addResourceFinder(toURI(urls.nextElement())); } catch (final URISyntaxException e) { throw new ResourceFinderException("Error when converting a URL to a URI", e); } } } catch (final IOException e) { throw new ResourceFinderException("IO error when package scanning jar", e); } } }
private URI toURI(final URL url) throws URISyntaxException { try { return url.toURI(); } catch (final URISyntaxException e) { // Work around bug where some URLs are incorrectly encoded. // This can occur when certain class loaders are utilized // to obtain URLs for resources. return URI.create(toExternalForm(url)); } }
@SuppressWarnings("unchecked") final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class); try (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) {
final Object p = getProperty(ServerProperties.PROVIDER_SCANNING_RECURSIVE); final boolean recursive = p == null || PropertiesHelper.isProperty(p); rfs.add(new PackageNamesScanner(packageNames, recursive));