/** * Registers a class consumer that registers only those annotated with {@link DbTable}. * Because of performance purposes, classes are not dynamically loaded; instead, their * file content is examined. */ public void registerAsConsumer(final ClassScanner classScanner) { classScanner.registerEntryConsumer(classPathEntry -> { if (!classPathEntry.isTypeSignatureInUse(DB_TABLE_ANNOTATION_BYTES)) { return; } final Class<?> beanClass; try { beanClass = classPathEntry.loadClass(); } catch (ClassNotFoundException cnfex) { throw new DbOomException("Entry class not found: " + classPathEntry.name(), cnfex); } if (beanClass == null) { return; } final DbTable dbTable = beanClass.getAnnotation(DbTable.class); if (dbTable == null) { return; } if (registerAsEntities) { dbEntityManager.registerEntity(beanClass); } else { dbEntityManager.registerType(beanClass); } }); }
/** * Loads properties from classpath file(s). Properties are specified using wildcards. */ public static Properties loadFromClasspath(final Properties p, final String... rootTemplate) { ClassScanner.create() .registerEntryConsumer(entryData -> UncheckedException.runAndWrapException(() -> p.load(entryData.openInputStream()))) .includeResources(true) .ignoreException(true) .excludeAllEntries(true) .includeEntries(rootTemplate) .scanDefaultClasspath(); return p; }
if (StringUtil.endsWithIgnoreCase(zipEntryName, CLASS_FILE_EXT)) { final String entryName = prepareEntryName(zipEntryName, true); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, zipFile, zipEntry); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream(); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, zipFile, zipEntry); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream();
if (StringUtil.endsWithIgnoreCase(zipEntryName, CLASS_FILE_EXT)) { final String entryName = prepareEntryName(zipEntryName, true); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, zipFile, zipEntry); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream(); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, zipFile, zipEntry); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream();
/** * Registers a class consumer that registers only those annotated with {@link DbTable}. * Because of performance purposes, classes are not dynamically loaded; instead, their * file content is examined. */ public void registerAsConsumer(final ClassScanner classScanner) { classScanner.registerEntryConsumer(classPathEntry -> { if (!classPathEntry.isTypeSignatureInUse(DB_TABLE_ANNOTATION_BYTES)) { return; } final Class<?> beanClass; try { beanClass = classPathEntry.loadClass(); } catch (ClassNotFoundException cnfex) { throw new DbOomException("Entry class not found: " + classPathEntry.name(), cnfex); } if (beanClass == null) { return; } final DbTable dbTable = beanClass.getAnnotation(DbTable.class); if (dbTable == null) { return; } if (registerAsEntities) { dbEntityManager.registerEntity(beanClass); } else { dbEntityManager.registerType(beanClass); } }); }
/** * Opens zip entry or plain file and returns its input stream. */ public InputStream openInputStream() { if (inputStream != null) { return inputStream; } if (zipFile != null && zipEntry != null) { try { inputStream = zipFile.getInputStream(zipEntry); return inputStream; } catch (IOException ioex) { throw new FindFileException("Input stream error: '" + zipFile.getName() + "', entry: '" + zipEntry.getName() + "'." , ioex); } } if (file != null) { try { inputStream = new FileInputStream(file); return inputStream; } catch (FileNotFoundException fnfex) { throw new FindFileException("Unable to open: " + file.getAbsolutePath(), fnfex); } } throw new FindFileException("Unable to open stream: " + name()); }
if (!classPathEntry.isTypeSignatureInUse(PETITE_BEAN_ANNOTATION_BYTES)) { return; beanClass = classPathEntry.loadClass(); } catch (ClassNotFoundException cnfex) { throw new PetiteException("Unable to load class: " + cnfex, cnfex);
/** * Loads props and properties from the classpath. */ public Props loadFromClasspath(final String... patterns) { ClassScanner.create() .registerEntryConsumer(entryData -> { String usedEncoding = JoddCore.encoding; if (StringUtil.endsWithIgnoreCase(entryData.name(), ".properties")) { usedEncoding = StringPool.ISO_8859_1; } final String encoding = usedEncoding; UncheckedException.runAndWrapException(() -> load(entryData.openInputStream(), encoding)); }) .includeResources(true) .ignoreException(true) .excludeCommonJars() .excludeAllEntries(true) .includeEntries(patterns) .scanDefaultClasspath() .start(); return this; }
/** * Loads properties from classpath file(s). Properties are specified using wildcards. */ public static Properties loadFromClasspath(final Properties p, final String... rootTemplate) { ClassScanner.create() .registerEntryConsumer(entryData -> UncheckedException.runAndWrapException(() -> p.load(entryData.openInputStream()))) .includeResources(true) .ignoreException(true) .excludeAllEntries(true) .includeEntries(rootTemplate) .scanDefaultClasspath(); return p; }
/** * Returns <code>true</code> if class contains {@link #bytecodeSignatureOfType(Class) type signature}. * It searches the class content for bytecode signature. This is the fastest way of finding if come * class uses some type. Please note that if signature exists it still doesn't means that class uses * it in expected way, therefore, class should be loaded to complete the scan. */ public boolean isTypeSignatureInUse(final byte[] bytes) { try { final byte[] data = readBytes(); final int index = ArraysUtil.indexOf(data, bytes); return index != -1; } catch (IOException ioex) { throw new FindFileException("Read error", ioex); } }
protected void scanClassFile(final String filePath, final String rootPath, final File file, final boolean isClass) { if (StringUtil.startsWithIgnoreCase(filePath, rootPath)) { final String entryName = prepareEntryName(filePath.substring(rootPath.length()), isClass); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, file); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream(); } } }
/** * Reads stream bytes. Since stream can be read only once, the byte content * is cached. */ public byte[] readBytes() throws IOException { openInputStream(); if (inputStreamBytes == null) { inputStreamBytes = StreamUtil.readBytes(inputStream); } return inputStreamBytes; }
/** * If entry name is {@link #acceptEntry(String) accepted} invokes {@link #onEntry(ClassPathEntry)} a callback}. */ protected void scanEntry(final ClassPathEntry classPathEntry) { if (!acceptEntry(classPathEntry.name())) { return; } try { onEntry(classPathEntry); } catch (Exception ex) { throw new FindFileException("Scan entry error: " + classPathEntry, ex); } }
/** * Opens zip entry or plain file and returns its input stream. */ public InputStream openInputStream() { if (inputStream != null) { return inputStream; } if (zipFile != null && zipEntry != null) { try { inputStream = zipFile.getInputStream(zipEntry); return inputStream; } catch (IOException ioex) { throw new FindFileException("Input stream error: '" + zipFile.getName() + "', entry: '" + zipEntry.getName() + "'." , ioex); } } if (file != null) { try { inputStream = new FileInputStream(file); return inputStream; } catch (FileNotFoundException fnfex) { throw new FindFileException("Unable to open: " + file.getAbsolutePath(), fnfex); } } throw new FindFileException("Unable to open stream: " + name()); }
/** * Parses class name that matches madvoc-related names. */ protected void registerAsConsumer(final ClassScanner classScanner) { classScanner.registerEntryConsumer(classPathEntry -> { final String entryName = classPathEntry.name(); if (entryName.endsWith(actionClassSuffix)) { try { acceptActionClass(classPathEntry.loadClass()); } catch (Exception ex) { log.debug("Invalid Madvoc action, ignoring: " + entryName); } } else if (classPathEntry.isTypeSignatureInUse(MADVOC_COMPONENT_ANNOTATION)) { try { acceptMadvocComponentClass(classPathEntry.loadClass()); } catch (Exception ex) { log.debug("Invalid Madvoc component ignoring: {}" + entryName); } } }); }
if (!classPathEntry.isTypeSignatureInUse(PETITE_BEAN_ANNOTATION_BYTES)) { return; beanClass = classPathEntry.loadClass(); } catch (ClassNotFoundException cnfex) { throw new PetiteException("Unable to load class: " + cnfex, cnfex);
/** * Loads props and properties from the classpath. */ public Props loadFromClasspath(final String... patterns) { ClassScanner.create() .registerEntryConsumer(entryData -> { String usedEncoding = JoddCore.encoding; if (StringUtil.endsWithIgnoreCase(entryData.name(), ".properties")) { usedEncoding = StringPool.ISO_8859_1; } final String encoding = usedEncoding; UncheckedException.runAndWrapException(() -> load(entryData.openInputStream(), encoding)); }) .includeResources(true) .ignoreException(true) .excludeCommonJars() .excludeAllEntries(true) .includeEntries(patterns) .scanDefaultClasspath() .start(); return this; }
/** * Returns <code>true</code> if class contains {@link #bytecodeSignatureOfType(Class) type signature}. * It searches the class content for bytecode signature. This is the fastest way of finding if come * class uses some type. Please note that if signature exists it still doesn't means that class uses * it in expected way, therefore, class should be loaded to complete the scan. */ public boolean isTypeSignatureInUse(final byte[] bytes) { try { final byte[] data = readBytes(); final int index = ArraysUtil.indexOf(data, bytes); return index != -1; } catch (IOException ioex) { throw new FindFileException("Read error", ioex); } }
protected void scanClassFile(final String filePath, final String rootPath, final File file, final boolean isClass) { if (StringUtil.startsWithIgnoreCase(filePath, rootPath)) { final String entryName = prepareEntryName(filePath.substring(rootPath.length()), isClass); final ClassPathEntry classPathEntry = new ClassPathEntry(entryName, file); try { scanEntry(classPathEntry); } finally { classPathEntry.closeInputStream(); } } }
/** * Reads stream bytes. Since stream can be read only once, the byte content * is cached. */ public byte[] readBytes() throws IOException { openInputStream(); if (inputStreamBytes == null) { inputStreamBytes = StreamUtil.readBytes(inputStream); } return inputStreamBytes; }