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.");
}
}
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);
}
}
}
Aggregations