Search in sources :

Example 1 with ConfigurationParser

use of com.axway.ats.agent.core.ConfigurationParser in project ats-framework by Axway.

the class ActionClassGenerator method generate.

/**
     * Generate the action class client stub
     *
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     * @throws ClassNotFoundException
     * @throws AgentException
     */
public void generate() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException, AgentException {
    File descriptorFile = new File(descriptorFileName);
    if (!descriptorFile.exists()) {
        throw new BuildException("Descriptor file " + descriptorFile + " does not exist. Absolute path of searched file: " + descriptorFile.getAbsolutePath());
    }
    log.info("Parsing configuration file " + descriptorFile.getAbsoluteFile());
    ConfigurationParser configParser = new ConfigurationParser();
    configParser.parse(new FileInputStream(descriptorFile), descriptorFile.getAbsolutePath());
    //first generate the cleanup client class
    generateCleanupHandlerStub(configParser);
    //get the javadocs for all actions in the action class
    ActionJavadocExtractor javadocExtractor = new ActionJavadocExtractor(sourceDir);
    Map<String, String> actionJavadocMap = javadocExtractor.extractJavaDocs();
    boolean errorsWhileProcessing = false;
    //next we need to generate the action classes
    Set<String> actionClassNames = configParser.getActionClassNames();
    for (String actionClassName : actionClassNames) {
        Class<?> actionClass;
        try {
            actionClass = Class.forName(actionClassName);
        } catch (ClassNotFoundException cnfe) {
            log.error("Could not find class '" + actionClassName + "' or a referenced class", cnfe);
            errorsWhileProcessing = true;
            continue;
        } catch (ExceptionInInitializerError eiie) {
            log.error("Could not initialize a static constant or execute a static section in '" + actionClassName + "' or a referenced class", eiie);
            errorsWhileProcessing = true;
            continue;
        }
        //check if stub generation should be skipped
        if (!needStubGeneration(actionClass)) {
            log.info("Skipping client stub generation for action class '" + actionClass.getName() + "'");
            continue;
        }
        String targetActionClassPackage = originalTargetPackage;
        log.info("Source package: " + originalSourcePackage);
        //replace it with the target package, otherwise use only the target package
        if (originalSourcePackage != null && actionClass.getName().contains(originalSourcePackage)) {
            targetActionClassPackage = actionClass.getPackage().getName().replace(originalSourcePackage, originalTargetPackage);
        }
        //create the package folder structure
        String targetPath = createPackageIfDoesNotExist(targetActionClassPackage);
        String destFileName = targetPath + "/" + actionClass.getSimpleName() + ".java";
        log.info("Writing generated stub to '" + destFileName + "'");
        PrintWriter fileWriter = null;
        try {
            fileWriter = new PrintWriter(new FileOutputStream(new File(destFileName)));
            fileWriter.write(generateStub(configParser.getComponentName(), actionClass, targetActionClassPackage, originalTargetPackage, actionJavadocMap));
            fileWriter.flush();
        } finally {
            IoUtils.closeStream(fileWriter);
        }
    }
    if (errorsWhileProcessing) {
        throw new RuntimeException("There were some errors while creating the Agent action client and server jars. Please review the console output above.");
    }
}
Also used : FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) ConfigurationParser(com.axway.ats.agent.core.ConfigurationParser) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with ConfigurationParser

use of com.axway.ats.agent.core.ConfigurationParser in project ats-framework by Axway.

the class DynamicComponentLoader method loadComponents.

@Override
protected void loadComponents(ComponentRepository componentRepository) throws AgentException {
    if (componentsLocation == null || !componentsLocation.isDirectory() || !componentsLocation.exists()) {
        throw new AgentException("Components location not set, does not exist or is not a directory");
    }
    log.debug("Unloading any existing components");
    //clear the action map before registering
    componentRepository.clear();
    //init the class loader for loading the action classes
    ClassLoader currentClassLoader = DynamicComponentLoader.class.getClassLoader();
    actionClassesClassLoader = new ReversedDelegationClassLoader(new URL[] {}, currentClassLoader);
    File[] files = componentsLocation.listFiles();
    // java.io.File constructor accepts URIs where ClassLoader requires URLs
    List<URI> jarFileURIs = new ArrayList<URI>();
    if (files != null) {
        //get only the jar files
        for (File file : files) {
            if (file.getAbsolutePath().endsWith(".jar")) {
                try {
                    // file.toURL() is deprecated and in case with spaces in directories there were errors
                    URI jarFileURI = file.toURI();
                    jarFileURIs.add(jarFileURI);
                    //add the jar file to our custom class loader
                    //it may not be an Agent component (i.e. no descriptor) but
                    //an Agent component might depend on it anyway
                    actionClassesClassLoader.addURL(jarFileURI.toURL());
                } catch (MalformedURLException mue) {
                    log.warn("Malformed URL", mue);
                }
            }
        }
    }
    for (URI uri : jarFileURIs) {
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(new File(uri));
            JarEntry entry = jarFile.getJarEntry(COMPONENT_DESCRIPTOR_NAME);
            if (entry != null) {
                log.info("Found component library in '" + uri.toURL().getFile() + "', starting registration process");
                InputStream descriptorStream = jarFile.getInputStream(entry);
                ConfigurationParser configParser = new ConfigurationParser();
                try {
                    configParser.parse(descriptorStream, (new File(uri)).getAbsolutePath());
                    registerComponent(configParser, componentRepository);
                    log.info("Successfully registered component library with name '" + configParser.getComponentName() + "'");
                } catch (Exception e) {
                    log.warn("Exception thrown while loading library '" + uri.toURL().getFile() + "', skipping it", e);
                    continue;
                }
            }
        } catch (IOException ioe) {
            try {
                log.warn("Could not open library '" + uri.toURL().getFile() + "', skipping it");
            } catch (MalformedURLException e) {
                log.error(e);
            }
        } finally {
            IoUtils.closeStream(jarFile);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) AgentException(com.axway.ats.agent.core.exceptions.AgentException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) AgentException(com.axway.ats.agent.core.exceptions.AgentException) IOException(java.io.IOException) ConfigurationParser(com.axway.ats.agent.core.ConfigurationParser) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

ConfigurationParser (com.axway.ats.agent.core.ConfigurationParser)2 File (java.io.File)2 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 BuildException (org.apache.tools.ant.BuildException)1