@Override public Vfs.Dir createDir(URL url) throws Exception { return new Vfs.Dir() { @Override public String getPath() { return ""; } @Override public Iterable<Vfs.File> getFiles() { return new ArrayList<Vfs.File>(); } @Override public void close() { //do nothing } }; }
private static Vfs.Dir emptyVfsDir(final URL url) { return new Vfs.Dir() { @Override public String getPath() { return url.toExternalForm(); } @Override public Iterable<Vfs.File> getFiles() { return Collections.emptyList(); } @Override public void close() { } }; }
@Override public Vfs.Dir createDir(URL url) throws Exception { return new Vfs.Dir() { @Override public String getPath() { return url.toExternalForm(); } @Override public Iterable<Vfs.File> getFiles() { return Collections.emptyList(); } @Override public void close() { } }; }
@Override public Vfs.Dir createDir(URL url) { try { File tmpFile = File.createTempFile("vfs-jndi-bundle", ".jar"); // we need to load this resource from the server context using the server ClassLoader // we use reflections to call the service exposed by the server BundleContext bundleContext = FrameworkUtil.getBundle(JndiUrlType.class).getBundleContext(); Object jndiService = OSGiServiceUtils.findService(bundleContext, JNDI_SERVICE); // copy to the resource to the temp file MethodUtils.invokeMethod(jndiService, WRITE_TO_FILE_METHOD, new Object[] {url.toString(), tmpFile.getAbsolutePath()}); return new TmpDir(tmpFile); } catch (IOException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Unable to create mvn url for " + url, e); } }
@Test public void vfsFromDirWithJarInName() throws MalformedURLException { String tmpFolder = System.getProperty("java.io.tmpdir"); tmpFolder = tmpFolder.endsWith(File.separator) ? tmpFolder : tmpFolder + File.separator; String dirWithJarInName = tmpFolder + "tony.jarvis"; File newDir = new File(dirWithJarInName); newDir.mkdir(); try { Vfs.Dir dir = Vfs.fromURL(new URL(format("file:{0}", dirWithJarInName))); assertEquals(dirWithJarInName, dir.getPath()); assertEquals(SystemDir.class, dir.getClass()); } finally { newDir.delete(); } }
@Test public void vfsFromDirWithinAJarUrl() throws MalformedURLException { URL directoryInJarUrl = ClasspathHelper.forClass(String.class); assertTrue(directoryInJarUrl.toString().startsWith("jar:file:")); assertTrue(directoryInJarUrl.toString().contains(".jar!")); String directoryInJarPath = directoryInJarUrl.toExternalForm().replaceFirst("jar:", ""); int start = directoryInJarPath.indexOf(":") + 1; int end = directoryInJarPath.indexOf(".jar!") + 4; String expectedJarFile = directoryInJarPath.substring(start, end); Vfs.Dir dir = Vfs.fromURL(new URL(directoryInJarPath)); assertEquals(ZipDir.class, dir.getClass()); assertEquals(expectedJarFile, dir.getPath()); }
private void testVfsDir(URL url) { System.out.println("testVfsDir(" + url + ")"); assertNotNull(url); Vfs.Dir dir = Vfs.fromURL(url); assertNotNull(dir); Iterable<Vfs.File> files = dir.getFiles(); Vfs.File first = files.iterator().next(); assertNotNull(first); first.getName(); try { first.openInputStream(); } catch (IOException e) { throw new RuntimeException(e); } dir.close(); }
@Test public void jarInputStream() { JavassistAdapter javassistAdapter = new JavassistAdapter(); for (URL jar : ClasspathHelper.forClassLoader()) { try { for (Vfs.File file : Iterables.limit(new JarInputDir(jar).getFiles(), 5)) { if (file.getName().endsWith(".class")) { String className = javassistAdapter.getClassName(javassistAdapter.getOrCreateClassObject(file)); } } } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public Object scan(Vfs.File file, Object classObject) { if (classObject == null) { try { classObject = configuration.getMetadataAdapter().getOfCreateClassObject(file); } catch (Exception e) { throw new ReflectionsException("could not create class object from file " + file.getRelativePath()); } } scan(classObject); return classObject; }
@Override public Dir createDir(URL url) throws Exception { File file = Vfs.getFile(url); if (file == null || file.exists() == false) { s_logger.warn("URL could not be resolved to a file: " + url); return new EmptyDir(file); } else { return new SystemDir(file); } }
@Override public Vfs.Dir createDir(URL url) { try (InputStream in = url.openStream()) { //copy to a temporary file File tmpFile = File.createTempFile("vfs-mvn-bundle", ".jar"); try (OutputStream out = new FileOutputStream(tmpFile)) { IOUtils.copy(in, out); } // return a dir implementation which removes the file when its done return new TmpDir(tmpFile); } catch (IOException e) { throw new IllegalArgumentException("Unable to create mvn url for " + url, e); } }
protected void scan(URL url) { Vfs.Dir dir = Vfs.fromURL(url); try { for (final Vfs.File file : dir.getFiles()) { // scan if inputs filter accepts file relative path or fqn Predicate<String> inputsFilter = configuration.getInputsFilter(); String path = file.getRelativePath(); String fqn = path.replace('/', '.'); if (inputsFilter == null || inputsFilter.apply(path) || inputsFilter.apply(fqn)) { Object classObject = null; for (Scanner scanner : configuration.getScanners()) { try { if (scanner.acceptsInput(path) || scanner.acceptsInput(fqn)) { classObject = scanner.scan(file, classObject); } } catch (Exception e) { if (log != null) { // SLF4J will filter out Throwables from the format string arguments. log.debug("could not scan file {} in url {} with scanner {}", file.getRelativePath(), url.toExternalForm(), scanner.getClass().getSimpleName(), e); } } } } } } finally { dir.close(); } }
/** * collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter * and de-serializes them using the default serializer {@link org.reflections.serializers.XmlSerializer} or using the optionally supplied optionalSerializer * <p> * it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), * so that relevant urls could be found much faster * @param optionalSerializer - optionally supply one serializer instance. if not specified or null, {@link org.reflections.serializers.XmlSerializer} will be used */ public static Reflections collect(final String packagePrefix, final Predicate<String> resourceNameFilter, @Nullable Serializer... optionalSerializer) { Serializer serializer = optionalSerializer != null && optionalSerializer.length == 1 ? optionalSerializer[0] : new XmlSerializer(); Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix); if (urls.isEmpty()) return null; long start = System.currentTimeMillis(); final Reflections reflections = new Reflections(); Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter); for (final Vfs.File file : files) { InputStream inputStream = null; try { inputStream = file.openInputStream(); reflections.merge(serializer.read(inputStream)); } catch (IOException e) { throw new ReflectionsException("could not merge " + file, e); } finally { close(inputStream); } } if (log != null) { Store store = reflections.getStore(); int keys = 0; int values = 0; for (String index : store.keySet()) { keys += store.get(index).keySet().size(); values += store.get(index).size(); } log.info(format("Reflections took %d ms to collect %d url%s, producing %d keys and %d values [%s]", System.currentTimeMillis() - start, urls.size(), urls.size() > 1 ? "s" : "", keys, values, Joiner.on(", ").join(urls))); } return reflections; }
@Override public Object scan(Vfs.File file, Object classObject) { classObject = super.scan(file, classObject); String className = getMetadataAdapter().getClassName(classObject); getStore().put(className, className); return classObject; }
public Object scan(Vfs.File file, Object classObject) { if (classObject == null) { try { classObject = configuration.getMetadataAdapter().getOrCreateClassObject(file); } catch (Exception e) { throw new ReflectionsException("could not create class object from file " + file.getRelativePath(), e); } } scan(classObject); return classObject; }
public ClassFile getOrCreateClassObject(final Vfs.File file) { InputStream inputStream = null; try { inputStream = file.openInputStream(); DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream)); return new ClassFile(dis); } catch (IOException e) { throw new ReflectionsException("could not create class file from " + file.getName(), e); } finally { Utils.close(inputStream); } }
@Test public void findFilesFromEmptyMatch() throws MalformedURLException { final URL jar = getSomeJar(); final Iterable<Vfs.File> files = Vfs.findFiles(java.util.Arrays.asList(jar), Predicates.<Vfs.File>alwaysTrue()); assertNotNull(files); assertTrue(files.iterator().hasNext()); }
/** * Prepares an Eclipse plugin to be used with LOGICOBJECTS * @param plugin */ public static void configure(Plugin plugin) { //enabling javassist to work correctly in Eclipse ClassPool classPool = ClassPool.getDefault(); classPool.appendClassPath(new LoaderClassPath(plugin.getClass().getClassLoader())); LogicObjects.getDefault().getContext().getLogicObjectFactory().setClassPool(classPool); Vfs.addDefaultURLTypes(new BundleUrlType(plugin.getBundle())); //enabling the Reflections filters to work in Eclipse String pathLogicFiles = null; URL urlPlugin = ClasspathHelper.forClass(plugin.getClass()); /** * adding all the classes in the plugin to the search path * This line has to be after the call to Vfs.addDefaultURLTypes(...) */ LogicObjects.getDefault().getContext().addSearchUrl(urlPlugin); try { pathLogicFiles = FileLocator.toFileURL(urlPlugin).getPath(); } catch (IOException e) { throw new RuntimeException(e); } /** * move to the directory where the plugin is deployed * This code uses the bootstrap engine since this engine will not trigger the loading of Logtalk * After we have moved to the location of the plugin files we can load logtalk afterwards */ //LogicEngine.getBootstrapEngine().cd(pathLogicFiles); TODO verify that this is not needed anymore (it should work without this since all the logic files are copied to a tmp directory) }
public static void registerUrlTypes() { final List<UrlType> urlTypes = new LinkedList<>(); urlTypes.add(new EmptyUrlType(ENDINGS)); urlTypes.addAll(Arrays.asList(Vfs.DefaultUrlTypes.values())); Vfs.setDefaultURLTypes(urlTypes); }
public static void init() { Vfs.addDefaultURLTypes(new JnilibIgnoringUrlType()); }
private static FudgeContext constructContext() { FudgeContext fudgeContext = new FudgeContext(); ExtendedFudgeBuilderFactory.init(fudgeContext.getObjectDictionary()); InnerClassFudgeBuilderFactory.init(fudgeContext.getObjectDictionary()); // hack to handle non-existent classpath directory entries List<UrlType> urlTypes = Lists.newArrayList(Vfs.getDefaultUrlTypes()); urlTypes.add(0, new OGFileUrlType()); Vfs.setDefaultURLTypes(urlTypes); // init annotation reflector, which needs this class loader Set<ClassLoader> loaders = new HashSet<>(); loaders.add(OpenGammaFudgeContext.class.getClassLoader()); try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader != null) { loaders.add(loader); } } catch (Exception ex) { // ignore } int availableProcessors = Runtime.getRuntime().availableProcessors(); ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("Reflections-scan-%d").build(); ExecutorService executorService = Executors.newFixedThreadPool(availableProcessors, factory); try { Configuration config = new ConfigurationBuilder() .setUrls(ClasspathHelper.forManifest(ClasspathHelper.forJavaClassPath())) .setScanners(new TypeAnnotationsScanner(), new FieldAnnotationsScanner(), new SubTypesScanner(false)) .filterInputsBy(FilterBuilder.parse(AnnotationReflector.DEFAULT_ANNOTATION_REFLECTOR_FILTER)) .addClassLoaders(loaders) .setExecutorService(executorService); AnnotationReflector.initDefaultReflector(new AnnotationReflector(config)); AnnotationReflector reflector = AnnotationReflector.getDefaultReflector(); fudgeContext.getObjectDictionary().addAllAnnotatedBuilders(reflector); fudgeContext.getTypeDictionary().addAllAnnotatedSecondaryTypes(reflector); } finally { executorService.shutdown(); } FudgeTypeDictionary td = fudgeContext.getTypeDictionary(); td.registerClassRename("com.opengamma.util.timeseries.zoneddatetime.ArrayZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.zoneddatetime.ArrayZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.zoneddatetime.ListZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.zoneddatetime.ListZonedDateTimeDoubleTimeSeries", ImmutableZonedDateTimeDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.ArrayLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.ArrayLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.ListLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.ListLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.util.timeseries.localdate.MapLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.timeseries.localdate.MapLocalDateDoubleTimeSeries", ImmutableLocalDateDoubleTimeSeries.class); td.registerClassRename("com.opengamma.id.Identifier", ExternalId.class); td.registerClassRename("com.opengamma.id.IdentifierBundleWithDates", ExternalIdBundleWithDates.class); td.registerClassRename("com.opengamma.id.IdentifierBundle", ExternalIdBundle.class); td.registerClassRename("com.opengamma.id.IdentifierWithDates", ExternalIdWithDates.class); td.registerClassRename("com.opengamma.id.ObjectIdentifier", ObjectId.class); td.registerClassRename("com.opengamma.id.UniqueIdentifier", UniqueId.class); return fudgeContext; }
@Override public Iterable<Vfs.File> getFiles() { return Collections.emptyList(); // just return no files }
private void init() { final List<Vfs.UrlType> urlTypes = new ArrayList<>(); urlTypes.add(new EmptyFileUrlTypes(excludedFileExtensions)); urlTypes.addAll(Arrays.asList(Vfs.DefaultUrlTypes.values())); Vfs.setDefaultURLTypes(urlTypes); }
@Override public Iterable<Vfs.File> getFiles() { return zipDir.getFiles(); }
@Override public Vfs.Dir createDir(URL url) { return new SystemDir(FileUtils.toFile(fixURL(url))); }
private void initResources(FilterConfig config) { try { if (CONFIG.getContent().getAssetsUrl() != null) { LOGGER.log(Level.INFO, "Using external assets, please provide the jsmart assets content at [" + CONFIG.getContent().getAssetsUrl() + "]"); } ServletContext context = config.getServletContext(); Set<String> libs = context.getResourcePaths(LIB_FILE_PATH); if (libs == null || libs.isEmpty()) { LOGGER.log(Level.SEVERE, "Could not find the JSmart library JAR file. Empty [" + LIB_FILE_PATH + "] resource folder."); return; } String libFilePath = null; for (String lib : libs) { Matcher matcher = JAR_FILE_PATTERN.matcher(lib); if (matcher.find()) { libFilePath = matcher.group(); break; } } if (libFilePath == null) { LOGGER.log(Level.SEVERE, "Could not find the JSmart library JAR file inside [" + LIB_FILE_PATH + "]"); return; } Resources jsonResources = EXPRESSIONS.GSON.fromJson(convertResourceToString(FILTER_RESOURCES), Resources.class); File libFile = new File(context.getRealPath(libFilePath)); Dir content = Vfs.fromURL(libFile.toURI().toURL()); Iterator<Vfs.File> files = content.getFiles().iterator(); while (files.hasNext()) { Vfs.File file = files.next(); // Copy index.jsp and replace content to redirect to welcome-url case configured if (file.getRelativePath().startsWith(INDEX_JSP)) { if (CONFIG.getContent().getWelcomeUrl() != null) { StringWriter writer = new StringWriter(); IOUtils.copy(file.openInputStream(), writer); String index = writer.toString().replace("{0}", CONFIG.getContent().getWelcomeUrl()); copyFileResource(new ByteArrayInputStream(index.getBytes(ENCODING)), file.getRelativePath(), context); } } // Do not copy anything if assets-url was provided if (CONFIG.getContent().getAssetsUrl() != null) { continue; } // Copy js, css and font resources to specific location for (String resource : jsonResources.getResources()) { String resourcePath = resource.replace("*", ""); if (file.getRelativePath().startsWith(resourcePath)) { initDirResources(context.getRealPath(PATH_SEPARATOR), file.getRelativePath()); copyFileResource(file.openInputStream(), file.getRelativePath(), context); break; } } } } catch (Exception ex) { LOGGER.log(Level.SEVERE, ex.getMessage()); } }
@Override public Object scan(Vfs.File file, Object classObject) { getStore().put(file.getName(), file.getRelativePath()); return classObject; }
public Class getOrCreateClassObject(Vfs.File file) throws Exception { return getOrCreateClassObject(file, null); }
public Class getOrCreateClassObject(Vfs.File file, @Nullable ClassLoader... loaders) throws Exception { String name = file.getRelativePath().replace("/", ".").replace(".class", ""); return forName(name, loaders); }
/** * Registers unrecognized URLs with reflections. * Reflections use of Vfs doesn't recognize the above URLs such as .jnilib and logs warns when it sees them. By registering those file endings, we suppress the warns. */ public static void registerUrlTypes() { final List<Vfs.UrlType> urlTypes = Lists.newArrayList(); // include a list of file extensions / filenames to be recognized urlTypes.add(new EmptyIfFileEndingsUrlType(".jnilib")); urlTypes.addAll(Arrays.asList(Vfs.DefaultUrlTypes.values())); Vfs.setDefaultURLTypes(urlTypes); }
/** * OSX contains file:// resources on the classpath including .mar and .jnilib files. * * Reflections use of Vfs doesn't recognize these URLs and logs warns when it sees them. By registering those file endings, we suppress the warns. */ public static void registerUrlTypes() { final List<Vfs.UrlType> urlTypes = Lists.newArrayList(); // include a list of file extensions / filenames to be recognized urlTypes.add(new EmptyIfFileEndingsUrlType(".mar", ".jnilib", ".so", ".dll")); urlTypes.addAll(Arrays.asList(Vfs.DefaultUrlTypes.values())); Vfs.setDefaultURLTypes(urlTypes); }
public Vfs.Dir createDir(final URL url) throws Exception { return emptyVfsDir(url); }
Object scan(Vfs.File file, @Nullable Object classObject);
C getOrCreateClassObject(Vfs.File file) throws Exception;
public Iterable<Vfs.File> getFiles() {return zipDir.getFiles();}