Search in sources :

Example 1 with ApiMethodHelper

use of org.apache.camel.util.component.ApiMethodHelper in project camel by apache.

the class DocumentGeneratorMojo method getDocumentContext.

private VelocityContext getDocumentContext() throws MavenReportException {
    final VelocityContext context = new VelocityContext();
    context.put("helper", this);
    // project GAV
    context.put("groupId", project.getGroupId());
    context.put("artifactId", project.getArtifactId());
    context.put("version", project.getVersion());
    // component URI format
    // look for single API, no endpoint-prefix
    @SuppressWarnings("unchecked") final Set<String> apiNames = new TreeSet<String>(collection.getApiNames());
    context.put("apiNames", apiNames);
    String suffix;
    if (apiNames.size() == 1 && ((Set) apiNames).contains("")) {
        suffix = "://endpoint?[options]";
    } else {
        suffix = "://endpoint-prefix/endpoint?[options]";
    }
    context.put("uriFormat", scheme + suffix);
    // API helpers
    final Map<String, ApiMethodHelper> apiHelpers = new TreeMap<String, ApiMethodHelper>();
    for (Object element : collection.getApiHelpers().entrySet()) {
        Map.Entry entry = (Map.Entry) element;
        apiHelpers.put(((ApiName) entry.getKey()).getName(), (ApiMethodHelper) entry.getValue());
    }
    context.put("apiHelpers", apiHelpers);
    // API methods and endpoint configurations
    final Map<String, Class<? extends ApiMethod>> apiMethods = new TreeMap<String, Class<? extends ApiMethod>>();
    final Map<String, Class<?>> apiConfigs = new TreeMap<String, Class<?>>();
    for (Object element : collection.getApiMethods().entrySet()) {
        Map.Entry entry = (Map.Entry) element;
        final String name = ((ApiName) entry.getValue()).getName();
        @SuppressWarnings("unchecked") Class<? extends ApiMethod> apiMethod = (Class<? extends ApiMethod>) entry.getKey();
        apiMethods.put(name, apiMethod);
        Class<?> configClass;
        try {
            configClass = getProjectClassLoader().loadClass(getEndpointConfigName(apiMethod));
        } catch (ClassNotFoundException e) {
            throw new MavenReportException(e.getMessage(), e);
        } catch (MojoExecutionException e) {
            throw new MavenReportException(e.getMessage(), e);
        }
        apiConfigs.put(name, configClass);
    }
    context.put("apiMethods", apiMethods);
    context.put("apiConfigs", apiConfigs);
    // API component properties
    context.put("scheme", this.scheme);
    context.put("componentName", this.componentName);
    Class<?> configClass;
    try {
        configClass = getProjectClassLoader().loadClass(getComponentConfig());
    } catch (ClassNotFoundException e) {
        throw new MavenReportException(e.getMessage(), e);
    } catch (MojoExecutionException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    context.put("componentConfig", configClass);
    // get declared and derived fields for component config
    // use get/set methods instead of fields, since this class could inherit others, that have private fields
    // so getDeclaredFields() won't work, like it does for generated endpoint config classes!!!
    final Map<String, String> configFields = new TreeMap<String, String>();
    do {
        IntrospectionSupport.ClassInfo classInfo = IntrospectionSupport.cacheClass(configClass);
        for (IntrospectionSupport.MethodInfo method : classInfo.methods) {
            if (method.isSetter) {
                configFields.put(method.getterOrSetterShorthandName, getCanonicalName(method.method.getParameterTypes()[0]));
            }
        }
        configClass = configClass.getSuperclass();
    } while (configClass != null && !configClass.equals(Object.class));
    context.put("componentConfigFields", configFields);
    return context;
}
Also used : IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) VelocityContext(org.apache.velocity.VelocityContext) ApiMethod(org.apache.camel.util.component.ApiMethod) TreeSet(java.util.TreeSet) ApiMethodHelper(org.apache.camel.util.component.ApiMethodHelper) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ApiName(org.apache.camel.util.component.ApiName) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 IntrospectionSupport (org.apache.camel.util.IntrospectionSupport)1 ApiMethod (org.apache.camel.util.component.ApiMethod)1 ApiMethodHelper (org.apache.camel.util.component.ApiMethodHelper)1 ApiName (org.apache.camel.util.component.ApiName)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1 VelocityContext (org.apache.velocity.VelocityContext)1