/** * Read the deployment XML for the given resources. */ public static List<XmEbean> readByResourceList(List<Resource> resourceList){ try { List<XmEbean> mappings = new ArrayList<>(); for (Resource xmlMappingRes : resourceList) { try (InputStream is = new FileInputStream(xmlMappingRes.getLocationOnDisk())) { mappings.add(XmlMappingReader.read(is)); } } return mappings; } catch (IOException e) { throw new RuntimeException("Error reading ebean xml mapping", e); } } }
/** * Search the classPath for the classes we are interested in. */ private BootupClasses getBootupClasses() { try { BootupClasses bc = new BootupClasses(); long st = System.currentTimeMillis(); for (ClassPathScanner finder : this.scanners) { if (packages != null && !packages.isEmpty()) { for (String packageName : packages) { finder.scanForClasses(packageName, bc); } } else { // scan locally finder.scanForClasses("", bc); } } long searchTime = System.currentTimeMillis() - st; logger.debug("Classpath search entities[{}] searchTime[{}] in packages[{}]", bc.getEntities().size(), searchTime, packages); return bc; } catch (Exception ex) { throw new RuntimeException("Error in classpath search (looking for entities etc)", ex); } }
/** * Return the list of ClassPathScanner services using serverConfig service loader. */ public static List<ClassPathScanner> find(ServerConfig serverConfig) { List<ClassPathScanner> scanners = new ArrayList<>(); for (ClassPathScannerFactory factory : serverConfig.serviceLoad(ClassPathScannerFactory.class)) { scanners.add(factory.createScanner(serverConfig.getClassLoadConfig().getClassLoader())); } return scanners; } }
/** * Read all the migration resources (SQL scripts) returning true if there are versions. */ public boolean readResources() { String migrationPath = migrationConfig.getMigrationPath(); ClassLoader classLoader = migrationConfig.getClassLoader(); Scanner scanner = new Scanner(classLoader); List<Resource> resourceList = scanner.scanForResources(migrationPath, new Match(migrationConfig)); logger.debug("resources: {}", resourceList); for (Resource resource : resourceList) { String filename = resource.getFilename(); if (filename.endsWith(migrationConfig.getApplySuffix())) { int pos = filename.lastIndexOf(migrationConfig.getApplySuffix()); String mainName = filename.substring(0, pos); MigrationVersion migrationVersion = MigrationVersion.parse(mainName); LocalMigrationResource res = new LocalMigrationResource(migrationVersion, resource.getLocation(), resource); versions.add(res); } } Collections.sort(versions); return !versions.isEmpty(); }
@Override public List<Class<?>> scanForClasses(Location location, ClassFilter predicate) { try { List<Class<?>> classes = new ArrayList<Class<?>>(); Set<String> resourceNames = findResourceNames(location, FilterResource.bySuffix(".class")); LOG.debug("scanning for classes at {} found {} resources to check", location, resourceNames.size()); for (String resourceName : resourceNames) { String className = toClassName(resourceName); try { Class<?> clazz = classLoader.loadClass(className); if (predicate.isMatch(clazz)) { classes.add(clazz); LOG.trace("... matched class: {} ", className); } } catch (NoClassDefFoundError err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } catch (ClassNotFoundException err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } } return classes; } catch (IOException e) { throw new ClassPathScanException(e); } }
/** * Return the content for the migration apply ddl script. */ public String getContent() { return resource.loadAsString("UTF-8"); }
/** * Return a programmatic JDBC migration. */ private LocalMigrationResource createJdbcMigration(Resource resource, String filename) { int pos = filename.lastIndexOf(".class"); String mainName = filename.substring(0, pos); MigrationVersion migrationVersion = MigrationVersion.parse(mainName); String className = resource.getLocation().replace('/', '.'); className = className.substring(0, className.length()-6); JdbcMigration instance = migrationConfig.getJdbcMigrationFactory().createInstance(className); return new LocalJdbcMigrationResource(migrationVersion, resource.getLocation(), instance); }
/** * Filters this list of resource names to only include the ones whose filename matches this prefix and this suffix. */ private Set<String> filterResourceNames(Set<String> resourceNames, ResourceFilter predicate) { Set<String> filteredResourceNames = new TreeSet<String>(); for (String resourceName : resourceNames) { if (predicate.isMatch(resourceName)) { filteredResourceNames.add(resourceName); } } return filteredResourceNames; } }
private List<Resource> xmlMappingResources() { List<ClassPathScanner> scanners = ClassPathScanners.find(serverConfig); List<String> mappingLocations = serverConfig.getMappingLocations(); List<Resource> resourceList = new ArrayList<>(); long st = System.currentTimeMillis(); if (mappingLocations != null && !mappingLocations.isEmpty()) { for (ClassPathScanner finder : scanners) { for (String mappingLocation : mappingLocations) { resourceList.addAll(finder.scanForResources(mappingLocation, resourceName -> resourceName.endsWith(".xml"))); } } } long searchTime = System.currentTimeMillis() - st; log.debug("Classpath search mappings[{}] searchTime[{}]", resourceList.size(), searchTime); return resourceList; }
/** * Return a resource matcher that matches by suffix. */ public static ResourceFilter bySuffix(String suffix) { return new BySuffix(suffix); }
/** * Return a resource matcher that matches by both prefix and suffix. */ public static ResourceFilter byPrefixSuffix(String prefix, String suffix) { return new ByPrefixSuffix(prefix, suffix); }
/** * Return a resource matcher that matches by prefix. */ public static ResourceFilter byPrefix(String prefix) { return new ByPrefix(prefix); }
/** * Read all the migration resources (SQL scripts) returning true if there are versions. */ public boolean readResources() { String migrationPath = migrationConfig.getMigrationPath(); ClassLoader classLoader = migrationConfig.getClassLoader(); Scanner scanner = new Scanner(classLoader); List<Resource> resourceList = scanner.scanForResources(migrationPath, new Match(migrationConfig)); logger.debug("resources: {}", resourceList); for (Resource resource : resourceList) { String filename = resource.getFilename(); if (filename.endsWith(migrationConfig.getApplySuffix())) { int pos = filename.lastIndexOf(migrationConfig.getApplySuffix()); String mainName = filename.substring(0, pos); MigrationVersion migrationVersion = MigrationVersion.parse(mainName); LocalMigrationResource res = new LocalMigrationResource(migrationVersion, resource.getLocation(), resource); versions.add(res); } } Collections.sort(versions); return !versions.isEmpty(); }
/** * Return the content for the migration apply ddl script. */ public String getContent() { return resource.loadAsString("UTF-8"); }
/** * Create a script based migration. */ private LocalMigrationResource createScriptMigration(Resource resource, String filename) { int pos = filename.lastIndexOf(migrationConfig.getApplySuffix()); String mainName = filename.substring(0, pos); MigrationVersion migrationVersion = MigrationVersion.parse(mainName); return new LocalDdlMigrationResource(migrationVersion, resource.getLocation(), resource); }
/** * Return the list of ClassPathScanner services using serverConfig service loader. */ public static List<ClassPathScanner> find(ServerConfig serverConfig) { List<ClassPathScanner> scanners = new ArrayList<>(); for (ClassPathScannerFactory factory : serverConfig.serviceLoad(ClassPathScannerFactory.class)) { scanners.add(factory.createScanner(serverConfig.getClassLoadConfig().getClassLoader())); } return scanners; } }
/** * Read the deployment XML for the given resources. */ public static List<XmEbean> readByResourceList(List<Resource> resourceList){ try { List<XmEbean> mappings = new ArrayList<>(); for (Resource xmlMappingRes : resourceList) { try (InputStream is = new FileInputStream(xmlMappingRes.getLocationOnDisk())) { mappings.add(XmlMappingReader.read(is)); } } return mappings; } catch (IOException e) { throw new RuntimeException("Error reading ebean xml mapping", e); } } }
/** * Filters this list of resource names to only include the ones whose filename matches this prefix and this suffix. */ private Set<String> filterResourceNames(Set<String> resourceNames, ResourceFilter predicate) { Set<String> filteredResourceNames = new TreeSet<String>(); for (String resourceName : resourceNames) { if (predicate.isMatch(resourceName)) { filteredResourceNames.add(resourceName); } } return filteredResourceNames; } }
/** * Return the content for the migration apply ddl script. */ public String getContent() { return resource.loadAsString("UTF-8"); }
/** * Return the list of ClassPathScanner services using serverConfig service loader. */ public static List<ClassPathScanner> find(ServerConfig serverConfig) { List<ClassPathScanner> scanners = new ArrayList<>(); ServiceLoader<ClassPathScannerFactory> scannerLoader = serverConfig.serviceLoad(ClassPathScannerFactory.class); for (ClassPathScannerFactory factory : scannerLoader) { ClassPathScanner scanner = factory.createScanner(serverConfig.getClassLoadConfig().getClassLoader()); scanners.add(scanner); } return scanners; } }