Commit 96d0092d authored by Nicolas Dolet's avatar Nicolas Dolet

Add a generic launcher.

Use this launcher in FactoryCommandLine.
Set project.build.sourceEncoding to remove Maven warning.
parent ca835c86
<?xml version="1.0"?>
<!--
* OW2 FraSCAti : Assembly Factory tools
*
* Copyright (c) 2007-2009 INRIA
* Copyright (c) 2007-2009 INRIA, USTL
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -18,65 +17,69 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Damien Fournier
*
* Contributor(s): Nicolas Dolet
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>org.ow2.frascati.factory</groupId>
<version>0.6-SNAPSHOT</version>
</parent>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-factory-tools</artifactId>
<name>FraSCAti Assembly Factory: Tools Module</name>
<version>0.6-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
<cxf.version>2.2.1</cxf.version>
</properties>
<!-- ======= -->
<!-- Build -->
<!-- ======= -->
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>org.ow2.frascati.factory</groupId>
<version>0.6-SNAPSHOT</version>
</parent>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-factory-tools</artifactId>
<name>FraSCAti Assembly Factory: Tools Module</name>
<version>0.6-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<properties>
<cxf.version>2.2.1</cxf.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- ============ -->
<!-- Dependencies -->
<!-- ============ -->
<dependencies>
<dependency>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-af-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Apache commons CLI required library to use FraSCAti factories. -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.1</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-af-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Apache commons CLI required library to use FraSCAti factories. -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.1</version>
</dependency>
<!-- Apache CXF tools required libraries to use FraSCAti wsdl2java. -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Apache CXF tools required libraries to use FraSCAti wsdl2java. -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
/***
* OW2 FraSCAti Assembly Factory
* OW2 FraSCAti Assembly Factory Tools
* Copyright (C) 2008-2009 INRIA, USTL
*
* This library is free software; you can redistribute it and/or
......@@ -51,6 +51,7 @@ public class FactoryCommandLine {
private static final String PATH_OPT = "libpath";
private static final String SERVICE_NAME_OPT = "service-name";
private static final String METHOD_NAME_OPT = "method-name";
private static final String PARAMS_OPT = "p";
private static final String USE_ASSEMBLY_FACTORY_OPT = "use-assembly-factory";
private static final String USE_GENERATE_FACTORY_OPT = "use-generate-factory";
private static final String USE_RUNTIME_FACTORY_OPT = "use-runtime-factory";
......@@ -59,19 +60,17 @@ public class FactoryCommandLine {
/** Create Options object. */
Options options;
/** The name of the executable. */
String commandName;
String commandName = "frascati";
/** The factory command line */
CommandLine cmd;
/**
* Default constructor.
*/
public FactoryCommandLine(String commandName) {
public FactoryCommandLine() {
OptionGroup factoryGroup = new OptionGroup();
options = new Options();
this.commandName = commandName;
// add options
options.addOption("h", HELP_OPT, false, "print this message.");
......@@ -85,6 +84,13 @@ public class FactoryCommandLine {
+ " If not specified, the composite is run in a (standalone) server mode.");
options.addOption("m", METHOD_NAME_OPT, true,
"the name of the method to invoke. Can be only used if the -s option is specified.");
OptionBuilder.withArgName("param1 param2...");
OptionBuilder.hasArgs();
OptionBuilder.withValueSeparator(' ');
OptionBuilder.withDescription("a list of parameters for the method. Can be only used if the -m option is specified.");
options.addOption(OptionBuilder.create(PARAMS_OPT));
options.addOption("o", GF_OUTPUT_OPT, true, "the path of the base output directory.\n"
+ " Can only be used with the --use-generate-factory or --use-runtime-factory option.");
......@@ -159,6 +165,19 @@ public class FactoryCommandLine {
return cmd.getOptionValue(METHOD_NAME_OPT);
}
/**
* Get the parameters of the method to invoke.
*
* @return the parameters of the method to invoke.
*/
public String[] getParams() {
String[] params = cmd.getOptionValues(PARAMS_OPT);
if(params != null)
return params;
else
return new String[0];
}
/**
* Get the Classpath used by the factory.
*
......@@ -214,6 +233,7 @@ public class FactoryCommandLine {
* @param args - Command line arguments
*/
public static void main(String[] args) {
// The factory command line used to retrieve options value.
FactoryCommandLine cmd = null;
// List of URLs used to define the classpath
......@@ -222,7 +242,7 @@ public class FactoryCommandLine {
System.out.println("\nFraSCAti Standalone Runtime");
// => Parse arguments
cmd = new FactoryCommandLine(""); // TODO : put the name of the executable here!
cmd = new FactoryCommandLine();
cmd.parse(args);
String compositeName = args[0];
......@@ -238,47 +258,55 @@ public class FactoryCommandLine {
}
}
Factory f = new Factory();
f.setOutput(cmd.getBaseDir());
Launcher launcher = new Launcher(compositeName, f, urls);
Object result = launcher.call(cmd.getServiceName(), cmd.getMethodName(), cmd.getParams());
System.out.println("Call done!");
if(result != null) {
System.out.println("Service response:");
System.out.println(result);
}
// Run the factory
if ( cmd.isGenerateFactory() ) {
GenerateFactory factory = new GenerateFactory();
System.out.println("Running the Generate Factory\n");
System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
// Add urls to current classloader
ClassLoader parent = factory.getClassLoader();
URLClassLoader urlcl = new URLClassLoader(urls, parent);
factory.setClassLoader(urlcl);
GenerateFactory.run(compositeName, urls, cmd.getSrcRoots(), cmd.getBaseDir());
} else {
AbstractFactory factory = null;
if ( cmd.isAssemblyFactory() ) {
factory = new AssemblyFactory();
System.out.print("Running the Assembly Factory ");
System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
// Add urls to current classloader
ClassLoader parent = factory.getClassLoader();
URLClassLoader urlcl = new URLClassLoader(urls, parent);
factory.setClassLoader(urlcl);
// needed when using binding factory
// TODO: manage classloader between Assembly Factory and Binding Factory
Thread.currentThread().setContextClassLoader(urlcl);
Factory.run(factory, compositeName, cmd.getServiceName(), cmd.getMethodName(), urls);
} else {
Factory runtimeFactory = new Factory();
System.out.print("Running the Runtime Factory ");
System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
Factory.run(runtimeFactory, compositeName, cmd.getServiceName(), cmd.getMethodName(), urls, cmd.getBaseDir());
}
}
// if ( cmd.isGenerateFactory() ) {
// AbstractFactory factory = new GenerateFactory();
// System.out.println("Running the Generate Factory\n");
// System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
//
// // Add urls to current classloader
// ClassLoader parent = factory.getClassLoader();
// URLClassLoader urlcl = new URLClassLoader(urls, parent);
// factory.setClassLoader(urlcl);
//
// GenerateFactory.run(compositeName, urls, cmd.getSrcRoots(), cmd.getBaseDir());
//
// } else {
// AbstractFactory factory = null;
// if ( cmd.isAssemblyFactory() ) {
// factory = new AssemblyFactory();
// System.out.print("Running the Assembly Factory ");
// System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
//
// // Add urls to current classloader
// ClassLoader parent = factory.getClassLoader();
// URLClassLoader urlcl = new URLClassLoader(urls, parent);
// factory.setClassLoader(urlcl);
//
// // needed when using binding factory
// // TODO: manage classloader between Assembly Factory and Binding Factory
// Thread.currentThread().setContextClassLoader(urlcl);
//
// AbstractFactory.run(factory, compositeName, cmd.getServiceName(), cmd.getMethodName(), urls);
// } else {
//
// Factory runtimeFactory = new Factory();
// System.out.print("Running the Runtime Factory ");
// System.out.println(" with "+compositeName+'/'+cmd.getServiceName()+'/'+cmd.getMethodName()+"\n");
//
// Factory.run(runtimeFactory, compositeName, cmd.getServiceName(), cmd.getMethodName(), urls, cmd.getBaseDir());
// }
// }
}
}
/***
* OW2 FraSCAti Assembly Factory Tools
* Copyright (C) 2009 INRIA, USTL
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Nicolas Dolet
*
* Contributor(s):
*/
package org.ow2.frascati.factory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.logging.Logger;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.ow2.frascati.tinfi.TinfiDomain;
/**
* Launcher for SCA composite applications
*/
public class Launcher {
/**
* Name of the SCA composite to launch
*/
private String compositeName;
/**
* The composite
*/
private Component scaComposite;
/**
* Logger for this class
*/
private Logger log = Logger.getLogger(this.getClass().getCanonicalName());
//---------------------------------------------------------------------------
// Constructor(s)
// --------------------------------------------------------------------------
/**
* Launcher for <code>compositeName</code>
*/
public Launcher(String compositeName) {
try {
scaComposite = new Factory().getComposite(compositeName);
} catch (RuntimeException e) {
System.err.println("Unable to launch " + compositeName + ": "
+ e.getMessage());
scaComposite = null;
}
}
/**
* Launcher for <code>compositeName</code> which libraries
* are in <code>urls</code>, with the Factory <code>f</code>
*/
public Launcher(String compositeName, Factory f, URL[] urls) {
try {
scaComposite = f.getComposite(compositeName,urls);
} catch (RuntimeException e) {
System.err.println("Unable to launch " + compositeName + ": "
+ e.getMessage());
scaComposite = null;
}
}
//---------------------------------------------------------------------------
// Public methods
// --------------------------------------------------------------------------
/**
* Launch the composite
*/
public void launch() {
if (compositeName != null) {
try {
scaComposite = new Factory().getComposite(compositeName);
} catch (RuntimeException e) {
System.err.println("Unable to launch " + compositeName + ": "
+ e.getMessage());
scaComposite = null;
}
} else {
System.err.println("FraSCAti launcher: composite name must be set!");
}
}
/**
* Launch an SCA composite
* @param comopositeName the composite to launch
*/
public void launch(String compositeName) {
this.compositeName = compositeName;
launch();
}
/**
* Invoke a method on a service
* @param serviceName the service of the SCA composite
* @param methodName the method of <code>service</code> to call
* @return the method result or null
*/
public Object call(String serviceName, String methodName, String[] params) {
try {
Object o = TinfiDomain.getService(scaComposite, Object.class, serviceName);
Class<?> c = o.getClass();
Class<?>[] paramTypes = new Class[params.length];
String paramList = "";
for(int i=0;i<params.length;++i)
{
paramTypes[i] = params[i].getClass();
paramList += params[i] + " ";
}
Method m = c.getMethod(methodName,paramTypes);
log.info("Call '" + serviceName + "' - Method '" + methodName + "' - Params: " + paramList);
return m.invoke(o, (Object[]) params);
} catch (NoSuchInterfaceException e) {
System.err.println("Unable to get the service " + serviceName);
System.err.println(e.getMessage());
} catch (SecurityException e) {
System.err.println("Unable to get the method " + methodName);
System.err.println(e.getMessage());
} catch (NoSuchMethodException e) {
System.err.println("The service " + serviceName
+ " does not provide the method " + methodName + params);
System.err.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
} catch (IllegalAccessException e) {
System.err.println(e.getMessage());
} catch (InvocationTargetException e) {
System.err.println(e.getMessage());
}
return null;
}
//---------------------------------------------------------------------------
// Main method
// --------------------------------------------------------------------------
/**
* Build a launcher for an SCA composite and perform a call on a service
* @param args[0] the SCA composite name
* @param args[1] the service name
* @param args[2] the method name
* @param args[i] the arguments of the methods given as Strings
*/
public static void main(String[] args) {
Launcher l = new Launcher(args[0]);
String p = args[3];
String[] params = p.split(" ");
System.out.println("Calling the '" + args[1] + "' service: ");
System.out.println("\tMethod '" + args[2] + "'"
+ ((params.length == 0) ? "" : " with params: " + p));
Object result = l.call(args[1], args[2], params);
if(result != null) {
System.out.println("Service response:");
System.out.println(result.toString());
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment