Search in sources :

Example 6 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor in project jqa-java-plugin by buschmais.

the class ServiceLoaderIT method invalidDescriptor.

/**
     * Verifies that any files not representing service descriptors are ignored.
     *
     * @throws java.io.IOException
     *             If the test fails.
     */
@Test
public void invalidDescriptor() throws IOException {
    File file = getClassesDirectory(ServiceLoaderIT.class);
    final File propsFile = new File(file, "META-INF/test.properties");
    final String path = "META-INF/services/test.properties";
    store.beginTransaction();
    JavaClassesDirectoryDescriptor artifactDescriptor = getArtifactDescriptor("a1");
    execute(artifactDescriptor, new ScanClassPathOperation() {

        @Override
        public List<FileDescriptor> scan(JavaArtifactFileDescriptor artifact, Scanner scanner) {
            return singletonList(scanner.<File, FileDescriptor>scan(propsFile, path, JavaScope.CLASSPATH));
        }
    }, getScanner());
    List<ServiceLoaderDescriptor> s = query("MATCH (s:ServiceLoader:Properties:File) RETURN s").getColumn("s");
    assertThat(s.size(), equalTo(1));
    ServiceLoaderDescriptor serviceLoaderDescriptor = s.get(0);
    assertThat(serviceLoaderDescriptor.getFileName(), equalTo(path));
    store.commitTransaction();
}
Also used : Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) JavaClassesDirectoryDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaClassesDirectoryDescriptor) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) ServiceLoaderDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor) File(java.io.File) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor) Test(org.junit.Test)

Example 7 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor in project jqa-java-plugin by buschmais.

the class PropertyFileScannerPlugin method scan.

@Override
public PropertyFileDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    ScannerContext context = scanner.getContext();
    Store store = context.getStore();
    FileDescriptor fileDescriptor = context.getCurrentDescriptor();
    PropertyFileDescriptor propertyFileDescriptor = store.addDescriptorType(fileDescriptor, PropertyFileDescriptor.class);
    Properties properties = new Properties();
    try (InputStream stream = item.createStream()) {
        properties.load(stream);
    } catch (IllegalArgumentException e) {
        LOGGER.warn("Cannot load properties from '" + path + "': " + e.getMessage());
    }
    for (String name : properties.stringPropertyNames()) {
        String value = properties.getProperty(name);
        PropertyDescriptor propertyDescriptor = store.create(PropertyDescriptor.class);
        propertyDescriptor.setName(name);
        propertyDescriptor.setValue(value);
        propertyFileDescriptor.getProperties().add(propertyDescriptor);
    }
    return propertyFileDescriptor;
}
Also used : PropertyFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PropertyFileDescriptor) PropertyDescriptor(com.buschmais.jqassistant.plugin.common.api.model.PropertyDescriptor) InputStream(java.io.InputStream) Store(com.buschmais.jqassistant.core.store.api.Store) Properties(java.util.Properties) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) PropertyFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PropertyFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Example 8 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor in project jqa-java-plugin by buschmais.

the class ServiceLoaderFileScannerPlugin method scan.

@Override
public ServiceLoaderDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    Matcher matcher = PATTERN.matcher(path);
    if (!matcher.matches()) {
        throw new IOException("Cannot match path name: " + path);
    }
    String serviceInterface = matcher.group(2);
    ScannerContext context = scanner.getContext();
    FileDescriptor fileDescriptor = context.getCurrentDescriptor();
    ServiceLoaderDescriptor serviceLoaderDescriptor = context.getStore().addDescriptorType(fileDescriptor, ServiceLoaderDescriptor.class);
    TypeDescriptor interfaceTypeDescriptor = getTypeDescriptor(serviceInterface, context);
    serviceLoaderDescriptor.setType(interfaceTypeDescriptor);
    try (InputStream stream = item.createStream()) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String serviceImplementation;
        while ((serviceImplementation = reader.readLine()) != null) {
            if (!isEmptyOrComment(serviceImplementation)) {
                TypeDescriptor implementationTypeDescriptor = getTypeDescriptor(serviceImplementation, context);
                serviceLoaderDescriptor.getContains().add(implementationTypeDescriptor);
            }
        }
    }
    return serviceLoaderDescriptor;
}
Also used : TypeDescriptor(com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ServiceLoaderDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Example 9 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor in project jqa-java-plugin by buschmais.

the class ManifestFileScannerPlugin method scan.

@Override
public ManifestFileDescriptor scan(FileResource item, String path, Scope scope, Scanner scanner) throws IOException {
    try (InputStream stream = item.createStream()) {
        Manifest manifest = new Manifest(stream);
        ScannerContext context = scanner.getContext();
        Store store = context.getStore();
        FileDescriptor fileDescriptor = context.getCurrentDescriptor();
        ManifestFileDescriptor manifestFileDescriptor = store.addDescriptorType(fileDescriptor, ManifestFileDescriptor.class);
        ManifestSectionDescriptor mainSectionDescriptor = store.create(ManifestSectionDescriptor.class);
        mainSectionDescriptor.setName(SECTION_MAIN);
        manifestFileDescriptor.setMainSection(mainSectionDescriptor);
        readSection(manifest.getMainAttributes(), mainSectionDescriptor, store);
        for (Map.Entry<String, Attributes> sectionEntry : manifest.getEntries().entrySet()) {
            ManifestSectionDescriptor sectionDescriptor = store.create(ManifestSectionDescriptor.class);
            sectionDescriptor.setName(sectionEntry.getKey());
            readSection(sectionEntry.getValue(), sectionDescriptor, store);
            manifestFileDescriptor.getManifestSections().add(sectionDescriptor);
        }
        return manifestFileDescriptor;
    }
}
Also used : ManifestSectionDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestSectionDescriptor) InputStream(java.io.InputStream) Attributes(java.util.jar.Attributes) Store(com.buschmais.jqassistant.core.store.api.Store) Manifest(java.util.jar.Manifest) ScannerContext(com.buschmais.jqassistant.core.scanner.api.ScannerContext) ManifestFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor) Map(java.util.Map) ManifestFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Example 10 with FileDescriptor

use of com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor in project jqa-java-plugin by buschmais.

the class AbstractJavaPluginIT method scanClassPathResources.

protected void scanClassPathResources(final Scope scope, String artifactId, final String... resources) throws IOException {
    final File directory = getClassesDirectory(this.getClass());
    execute(artifactId, new ScanClassPathOperation() {

        @Override
        public List<FileDescriptor> scan(JavaArtifactFileDescriptor artifact, Scanner scanner) {
            List<FileDescriptor> result = new ArrayList<>();
            for (String resource : resources) {
                File file = new File(directory, resource);
                FileDescriptor fileDescriptor = scanner.scan(file, resource, scope);
                result.add(fileDescriptor);
            }
            return result;
        }
    });
}
Also used : Scanner(com.buschmais.jqassistant.core.scanner.api.Scanner) ArrayList(java.util.ArrayList) List(java.util.List) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) File(java.io.File) JavaArtifactFileDescriptor(com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor) FileDescriptor(com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)

Aggregations

FileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.FileDescriptor)11 JavaArtifactFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaArtifactFileDescriptor)7 Scanner (com.buschmais.jqassistant.core.scanner.api.Scanner)5 ScannerContext (com.buschmais.jqassistant.core.scanner.api.ScannerContext)4 InputStream (java.io.InputStream)4 List (java.util.List)4 Test (org.junit.Test)4 ClassFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ClassFileDescriptor)3 TypeDescriptor (com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor)3 File (java.io.File)3 Store (com.buschmais.jqassistant.core.store.api.Store)2 ServiceLoaderDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ServiceLoaderDescriptor)2 ArrayList (java.util.ArrayList)2 ArtifactFileDescriptor (com.buschmais.jqassistant.plugin.common.api.model.ArtifactFileDescriptor)1 PropertyDescriptor (com.buschmais.jqassistant.plugin.common.api.model.PropertyDescriptor)1 ContainerFileResolver (com.buschmais.jqassistant.plugin.common.api.scanner.ContainerFileResolver)1 AbstractFileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.AbstractFileResource)1 FileResource (com.buschmais.jqassistant.plugin.common.api.scanner.filesystem.FileResource)1 JavaClassesDirectoryDescriptor (com.buschmais.jqassistant.plugin.java.api.model.JavaClassesDirectoryDescriptor)1 ManifestFileDescriptor (com.buschmais.jqassistant.plugin.java.api.model.ManifestFileDescriptor)1