Search in sources :

Example 1 with ClassInspectionService

use of io.atlasmap.java.inspect.ClassInspectionService in project atlasmap by atlasmap.

the class GenerateInspectionsMojo method generateInspection.

private void generateInspection(List<String> artifacts, Collection<String> classNames) throws MojoFailureException, MojoExecutionException {
    List<URL> urls = artifacts == null ? Collections.emptyList() : resolveClasspath(artifacts);
    ClassLoader origTccl = Thread.currentThread().getContextClassLoader();
    for (String className : classNames) {
        Class<?> clazz = null;
        JavaClass c = null;
        // Not even this plugin will be available on this new URLClassLoader
        try (URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), origTccl)) {
            clazz = loader.loadClass(className);
            ClassInspectionService classInspectionService = new ClassInspectionService();
            classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
            c = classInspectionService.inspectClass(loader, clazz);
        } catch (ClassNotFoundException | IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        try {
            ObjectMapper objectMapper = Json.mapper();
            File target = outputFile;
            if (target == null) {
                target = new File(outputDir, "atlasmap-inpection-" + className + ".json");
            }
            objectMapper.writeValue(target, c);
            getLog().info("Created: " + target);
        } catch (JsonProcessingException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) URL(java.net.URL) JavaClass(io.atlasmap.java.v2.JavaClass) URLClassLoader(java.net.URLClassLoader) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) URLClassLoader(java.net.URLClassLoader) File(java.io.File) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ClassInspectionService

use of io.atlasmap.java.inspect.ClassInspectionService in project atlasmap by atlasmap.

the class JavaModuleTest method testGetSetJavaInspectionService.

@Test
public void testGetSetJavaInspectionService() {
    ClassInspectionService cis = new ClassInspectionService();
    assertNotNull(module);
    assertNull(module.getJavaInspectionService());
    module.setJavaInspectionService(cis);
    assertNotNull(module.getJavaInspectionService());
    assertSame(cis, module.getJavaInspectionService());
}
Also used : ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) Test(org.junit.Test)

Example 3 with ClassInspectionService

use of io.atlasmap.java.inspect.ClassInspectionService in project atlasmap by atlasmap.

the class JavaService method getClass.

// example from:
// https://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
@GET
@Path("/class")
@Produces(MediaType.APPLICATION_JSON)
public Response getClass(@QueryParam("className") String className) {
    ClassInspectionService classInspectionService = new ClassInspectionService();
    classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
    JavaClass c = classInspectionService.inspectClass(className);
    classInspectionService = null;
    return Response.ok().entity(toJson(c)).build();
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with ClassInspectionService

use of io.atlasmap.java.inspect.ClassInspectionService in project syndesis by syndesisio.

the class GenerateConnectorInspectionsMojo method generateInspections.

// ****************************************
// Inspections
// ****************************************
@SuppressWarnings("PMD.CyclomaticComplexity")
private Optional<DataShape> generateInspections(Connector connector, Optional<DataShape> shape) throws IOException, DependencyResolutionRequiredException, ClassNotFoundException {
    if (!shape.isPresent() || !connector.getId().isPresent()) {
        return Optional.empty();
    }
    final DataShape given = shape.get();
    DataShape ret = given;
    if (DataShapeKinds.JAVA == given.getKind() && StringUtils.isNotEmpty(given.getType())) {
        final String type = given.getType();
        File outputFile = new File(inspectionsOutputDir, String.format("%s/%s/%s.json", inspectionsResourceDir, connector.getId().get(), type));
        if (!outputFile.getParentFile().exists() && outputFile.getParentFile().mkdirs()) {
            getLog().debug("Directory created:" + outputFile.getParentFile());
        }
        getLog().info("Generating for connector: " + connector.getId().get() + ", and type: " + type);
        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        final List<String> elements = project.getCompileClasspathElements();
        final URL[] classpath = new URL[elements.size()];
        for (int i = 0; i < elements.size(); i++) {
            classpath[i] = new File(elements.get(i)).toURI().toURL();
            getLog().debug("Add element to classpath: " + classpath[i]);
        }
        try (URLClassLoader loader = new URLClassLoader(classpath, tccl)) {
            ClassInspectionService classInspectionService = new ClassInspectionService();
            classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
            final Class<?> clazz = loader.loadClass(type);
            final JavaClass c = classInspectionService.inspectClass(loader, clazz);
            final ObjectMapper mapper = io.atlasmap.v2.Json.mapper();
            if (inspectionMode == InspectionMode.SPECIFICATION || inspectionMode == InspectionMode.RESOURCE_AND_SPECIFICATION) {
                getLog().info("Specification for type: " + type + " created");
                ret = new DataShape.Builder().createFrom(given).specification(mapper.writeValueAsString(c)).build();
            }
            if (inspectionMode == InspectionMode.RESOURCE || inspectionMode == InspectionMode.RESOURCE_AND_SPECIFICATION) {
                mapper.writeValue(outputFile, c);
                getLog().info("Created: " + outputFile);
            }
        }
    }
    return Optional.of(ret);
}
Also used : DataShape(io.syndesis.common.model.DataShape) URL(java.net.URL) JavaClass(io.atlasmap.java.v2.JavaClass) URLClassLoader(java.net.URLClassLoader) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) URLClassLoader(java.net.URLClassLoader) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with ClassInspectionService

use of io.atlasmap.java.inspect.ClassInspectionService in project atlasmap by atlasmap.

the class GenerateInspectionsMojo method generateJavaInspection.

private void generateJavaInspection(List<String> artifacts, Collection<String> classNames, CollectionType collectionType, String collectionClassName) throws MojoFailureException, MojoExecutionException {
    List<URL> urls = artifacts == null ? Collections.emptyList() : resolveClasspath(artifacts);
    ClassLoader origTccl = Thread.currentThread().getContextClassLoader();
    for (String className : classNames) {
        Class<?> clazz = null;
        JavaClass c = null;
        // Not even this plugin will be available on this new URLClassLoader
        try (URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), origTccl)) {
            clazz = loader.loadClass(className);
            ClassInspectionService classInspectionService = new ClassInspectionService();
            classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
            c = classInspectionService.inspectClass(loader, clazz, collectionType, collectionClassName);
        } catch (ClassNotFoundException | IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        writeToJsonFile(DEFAULT_OUTPUT_FILE_PREFIX + "-" + className, c);
    }
}
Also used : JavaClass(io.atlasmap.java.v2.JavaClass) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) URLClassLoader(java.net.URLClassLoader) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ClassInspectionService (io.atlasmap.java.inspect.ClassInspectionService)7 JavaClass (io.atlasmap.java.v2.JavaClass)6 URL (java.net.URL)4 URLClassLoader (java.net.URLClassLoader)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 File (java.io.File)3 IOException (java.io.IOException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ClassInspectionRequest (io.atlasmap.java.v2.ClassInspectionRequest)1 ClassInspectionResponse (io.atlasmap.java.v2.ClassInspectionResponse)1 AtlasService (io.atlasmap.service.AtlasService)1 Operation (io.swagger.v3.oas.annotations.Operation)1 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)1 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)1 DataShape (io.syndesis.common.model.DataShape)1 ApplicationPath (javax.ws.rs.ApplicationPath)1 Consumes (javax.ws.rs.Consumes)1