Java Agent Development Framework - Eclipse and Maven integration - java

I created a maven project with JADE framework as a dependency but this framework requires different commands to execute the jar than ordinary java applications.
Build:
javac –classpath <JADE-classes> Class_name.java
Run:
java –classpath <JADE-classes> jade.Boot <agent-local-name>:<fully-qualified-agent-class>
Where <fully-qualified-agent-class> is package_name.Class_name
or
java –cp lib\jade.jar jade.Boot [options] [AgentSpecifierlist]
Is it possible to build a runnable jar using maven plugins so I just type java -jar myjar.jar instead of the command above?
Would mvn eclipse:eclipse command change build parameters of the eclipse project after editing the pom.xml file?

There isn't any such plugin available for JADE because it is not widely used framework and anyone hasn't bothered to develop a plugin for it. But there is a workaround to run it the conventional way, but this would only work if you already know your <fully-qualified-agent-class> names. what you can do is write a class that extends Thread and from that Thread's run() method invoke the JADE framework agent by passing the <fully-qualified-agent-class> as arguments. Refer to an example below.
jadeBootThread.java
public class jadeBootThread extends Thread {
private final String jadeBoot_CLASS_NAME = "jade.Boot";
private final String MAIN_METHOD_NAME = "main";
//add the <agent-local-name>:<fully-qualified-agent-class> name here;
// you can add more than one by semicolon separated values.
private final String ACTOR_NAMES_args = "Agent1:com.myagents.agent1";
private final String GUI_args = "-gui";
private final Class<?> secondClass;
private final Method main;
private final String[] params;
public jadeBootThread() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
secondClass = Class.forName(jadeBoot_CLASS_NAME);
main = secondClass.getMethod(MAIN_METHOD_NAME, String[].class);
params = new String[]{GUI_args, ACTOR_NAMES_args};
}
#Override
public void run() {
try {
main.invoke(null, new Object[]{params});
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
ex.printStacktrace();
}
}
}
Now you can invoke this thread from your main method or any other way by creating runnable jar file with eclipse plugin etc.

A better solution for this :
public class Example03 {
public static void main(String[] args){
String host;
int port;
String platform = null; //default name
boolean main = true;
host = "localhost";
port = -1; //default-port 1099
Runtime runtime = Runtime.instance();
Profile profile = null;
AgentContainer container = null;
profile = new ProfileImpl(host, port, platform, main);
// create container
container = runtime.createMainContainer(profile);
// create the agents
try {
AgentController agent1 = container.createNewAgent(
"Agent1",
routing.TransportAgent.class.getName(),
args);
AgentController agent2 = container.createNewAgent(
"Agent2",
routing.TransportAgent.class.getName(),
args);
// create the GUI
AgentController rma = container.createNewAgent("rma", "jade.tools.rma.rma", null);
// start the agents
agent1.start();
agent2.start();
rma.start();
} catch(StaleProxyException e) {
throw new RuntimeException(e);
}
}
}

Related

How to set/get a value from another JVM

I have a class Normal with the following code:
public class Normal {
private static String myStr = "Not working...";
private static boolean running = true;
public static void main(String[] args) {
while(running) {
System.out.println(myStr);
}
}
}
And I have another class named Injector in another project. Its purpose is to change the values of Normal even though they are not in the same JVM:
public class Injector {
public static void main(String[] args) {
String PID = //Gets PID, which works fine
VirtualMachine vm = VirtualMachine.attach(PID);
/*
Set/Get field values for classes in vm?
*/
}
}
What I want to do is change the values myStr and running in the class Normal to "Working!" and false respectively without changing the code in Normal (Only in Injector).
Thanks in advance
You'll need two JARs:
One is Java Agent that uses Reflection to change the field value. Java Agent's main class should have agentmain entry point.
public static void agentmain(String args, Instrumentation instr) throws Exception {
Class normalClass = Class.forName("Normal");
Field myStrField = normalClass.getDeclaredField("myStr");
myStrField.setAccessible(true);
myStrField.set(null, "Working!");
}
You'll have to add MANIFEST.MF with Agent-Class attribute and pack the agent into a jar file.
The second one is a utility that uses Dynamic Attach to inject the agent jar into the running VM. Let pid be the target Java process ID.
import com.sun.tools.attach.VirtualMachine;
...
VirtualMachine vm = VirtualMachine.attach(pid);
try {
vm.loadAgent(agentJarPath, "");
} finally {
vm.detach();
}
A bit more details in the article.

Passing OSGi bundles for Jython Interpreter on-the-fly

we would like to integrate the Jython interpreter into our Eclipse RCP based solution and we need to access the OSGi bundles (e.g. everything from Activator.getContext().getBundles() ) from there.
How could I pass these bundles to a Jython PythonInterpreter object python.path property, so I can import these classes from the Jython code?
(I get similar error messages when I try to import packages e.g.:
from org.eclipse.chemclipse.msd.converter.chromatogram import ChromatogramConverterMSD)
ImportError: cannot import name ChromatogramConverterMSD
I managed to find a solution after working on this issue for days.
First we need a ClassLoader which can load OSGi bundles if necessary:
public class JythonClassLoader extends ClassLoader {
private final Bundle bundle;
public JythonClassLoader(ClassLoader parent, Bundle bundle) {
super(parent);
this.bundle = bundle;
}
#Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// System.out.println("findClass " + name);
try {
return super.findClass(name);
} catch(ClassNotFoundException e) {
Class<?> loadClass = bundle.loadClass(name);
if(loadClass != null)
return loadClass;
throw e;
}
}}
Then the PythonInterpreter needs to know about this ClassLoader. The best is to set up the environment for the PythonInterpreter before. The following class does the job:
public class JythonEnvironment {
private final Bundle bundle;
public JythonEnvironment(Bundle bundle) {
this.bundle = bundle;
}
public PySystemState getPySystemState() {
Properties properties = new Properties();
String jythonPath = net.openchrom.thirdpartylibraries.jython.Activator.getJythonPath();
properties.setProperty("python.home", jythonPath);
properties.setProperty("python.cachedir.skip", "true");
Properties systemProperties = System.getProperties();
PySystemState.initialize(systemProperties, properties, new String[]{""});
PySystemState pySystemState = new PySystemState();
JythonClassLoader classLoader = new JythonClassLoader(getClass().getClassLoader(), bundle);
pySystemState.setClassLoader(classLoader);
return pySystemState;
}
public PythonInterpreter getInterpreter() {
return new PythonInterpreter(null, getPySystemState());
}
public PythonInterpreter getInterpreter(OutputStream outStream, OutputStream errStream) {
PythonInterpreter interpreter = new PythonInterpreter(null, getPySystemState());
interpreter.setErr(outStream);
interpreter.setOut(errStream);
return interpreter;
}}
The JythonEnvironment class needs to know about the bundle. The best if that one can be received through the Activator.
JythonEnvironment environment = new JythonEnvironment(Activator.getDefault().getBundle());
Hope this answer will save time, if someone else needs to integrate Jython into an Eclipse RCP based solution.

How to block access to some classes, when executing groovy scripts from java?

I'm pretty new to groovy, and scripting in java generally, and I really
hope there is a simple solution for my problem.
In our application, the users can execute groovy scripts which they write
themselves, and we need to control what those scripts can and can not do.
I read a lot of stuff about sandboxing groovy, but either I am looking at
wrong places or I am overlooking the obvious.
To make it simple, I have a small example which demonstrates the problem.
This is my class loader which should prevent java.lang.System from being
loaded and available to scripts:
public class MyClassLoader extends ClassLoader {
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.startsWith("java.lang.System")) {
throw new ClassNotFoundException("Class not found: " + name);
}
return super.loadClass(name);
}
}
And this is a simple program that tries to call System.currentTimeMillis():
public static void main(String[] args) {
String code = "java.lang.System.currentTimeMillis();";
ClassLoader classLoader = new MyClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
GroovyShell shell = new GroovyShell();
Script script = shell.parse(code);
Object result = script.run();
log.debug(result);
}
MyClassLoader throws exceptions for java.lang.SystemBeanInfo
and java.lang.SystemCustomizer, but the code executes.
Same thing happens if I use javax.script classes:
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("Groovy");
Object o = engine.eval(code);
log.debug(o);
And if I try it with JavaScript engine, it works as expected (just replace
"Groovy" with "JavaScript" in the above example).
Can anyone help me with this? BTW, I'm using groovy-all-1.8.8.jar, with
jdk1.7.0_55.
Thanks
I can recommend Groovy Sandbox for this purpose. In contrast to SecureASTCustomizer it will check if an execution is allowed dynamically at runtime. It intercepts every method call, object allocations, property/attribute access, array access, and so on - and you thus have a very fine grained control on what you allow (white-listing).
Naturally the configuration on what is allowed is very important. For example you may want to allow using Strings and use methods like substring, but probably not the execute method on String, which could be exploited with something like 'rm -R ~/*'.execute().
Creating a configuration that is really safe is a challenge, and it is more difficult the more you allow.
Downside of the Groovy Sandbox is that the code must run with the interceptor registered and you will have a performance penalty during execution.
This image [1] shows an example from a project where we used Groovy Sandbox for Groovy code entered by the user. The code is run to valide the script - so if the statement there would actually be executed as part of it, the application would have exited before I could do the screenshot ;)
Perhaps you'd be interested in using a SecureASTCustomizer in conjunction with a CompilerConfiguration. If you are concerned with security, an explicit white list might be better than a black list.
def s = new SecureASTCustomizer()
s.importsWhiteList = [ 'a.legal.Klass', 'other.legal.Klass' ]
def c = new CompilerConfiguration()
c.addCompilationCustomizers(s)
def sh = new GroovyShell(c)
Take a look at that class, it contains a lot of options that are ready to use.
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
public class SandboxGroovyClassLoader extends ClassLoader {
public SandboxGroovyClassLoader(ClassLoader parent) {
super(parent);
}
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.startsWith("java.lang.System"))
return null;
return super.loadClass(name);
}
#Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("java.lang.System"))
return null;
return super.loadClass(name, resolve);
}
static void runWithGroovyClassLoader() throws Exception {
System.out.println("Begin runWithGroovyClassLoader");
String code = "def hello_world() { java.lang.System.currentTimeMillis(); };";
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<?> scriptClass = groovyClassLoader.parseClass(code);
Object scriptInstance = scriptClass.newInstance();
Object result = scriptClass.getDeclaredMethod("hello_world", new Class[] {}).invoke(scriptInstance, new Object[] {});
System.out.println(result);
groovyClassLoader.close();
System.out.println("End runWithGroovyClassLoader");
}
static void runWithSandboxGroovyClassLoader() throws Exception {
System.out.println("Begin runWithSandboxGroovyClassLoader");
ClassLoader parentClassLoader = SandboxGroovyClassLoader.class.getClassLoader();
SandboxGroovyClassLoader classLoader = new SandboxGroovyClassLoader(parentClassLoader);
String code = "def hello_world() { java.lang.System.currentTimeMillis(); };";
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader);
Class<?> scriptClass = groovyClassLoader.parseClass(code);
Object scriptInstance = scriptClass.newInstance();
Object result = scriptClass.getDeclaredMethod("hello_world", new Class[] {}).invoke(scriptInstance, new Object[] {});
System.out.println(result);
groovyClassLoader.close();
System.out.println("End runWithSandboxGroovyClassLoader");
}
static void runWithSandboxGroovyShellClassLoader() throws Exception {
System.out.println("Begin runWithSandboxGroovyShellClassLoader");
String code = "java.lang.System.currentTimeMillis();";
ClassLoader parentClassLoader = SandboxGroovyClassLoader.class.getClassLoader();
SandboxGroovyClassLoader classLoader = new SandboxGroovyClassLoader(parentClassLoader);
Thread.currentThread().setContextClassLoader(classLoader);
GroovyShell shell = new GroovyShell();
Script script = shell.parse(code);
Object result = script.run();
System.out.println(result);
System.out.println("End runWithSandboxGroovyShellClassLoader");
}
public static void main(String[] args) throws Exception {
runWithGroovyClassLoader();
runWithSandboxGroovyClassLoader();
runWithSandboxGroovyShellClassLoader();
}
}
Is it what you want ?

How do I load and use native library in java?

I've got a java class, calling a native method and trying to load library:
import java.io.UnsupportedEncodingException;
public class Main {
public static native String getMyString(String s);
/**
* #param args
* #throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
// System.out.println("here!");
String s2 = getMyString("string text");
for (Byte b : s2.getBytes("UTF-8")) {
System.out.print(b);
System.out.print(",");
}
}
static {
System.loadLibrary("mylib.so");
}
}
The "mylib.so" is in the directory, where Main.class is located.
When I run java Main I get following exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no mylib.so in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at Main.<clinit>(Main.java:24)
What should I change for this to wark?
I've tried setting library full path without success
Do the following:
Use System.loadLibrary("mylib");
Copy mylib.so to libmylib.so
Run java -Djava.library.path=/root/ Main
"How to load native library"
public final class NativeLibsLoaderUtil {
private static final String JAVA_LIBRARY_PATH = "java.library.path";
private static final String SYS_PATHS = "sys_paths";
private NativeLibsLoaderUtil() {
}
private static void addLibsToJavaLibraryPath(final String tmpDirName) {
try {
System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
/* Optionally add these two lines */
System.setProperty("jna.library.path", tmpDirName);
System.setProperty("jni.library.path", tmpDirName);
final Field fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (IllegalAccessException | NoSuchFieldException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
Where tmpDirName is a directory where you store your library.
Or you can modify above class and use temp directory from your system property, like this:
/**
* Temporary directory system property name
*/
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
/**
*
* #return
*/
private static File getTempDir() {
final String tmpDirName = System.getProperty(JAVA_IO_TMPDIR);
final File tmpDir = new File(tmpDirName);
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
return tmpDir;
}
!But first you have to copy there your native lib :)
Then to load native library call "addLibsToJavaLibraryPath" method in static block in "most root" class before any class constructor was executed.
static {
NativeLibsLoaderUtil.addLibsToJavaLibraryPath("/tmp");
}
You should add the so to library path:
-Djava.libarary.path= (this is in the java command).
if you run from eclipse:
How to add native library to "java.library.path" with Eclipse launch (instead of overriding it)
If you compiled opencv, on installation you should have seen something like:
make install:
-- Up-to-date: /usr/local/share/java/opencv4/libopencv_java460.so
-- Up-to-date: /usr/local/share/java/opencv4/opencv-460.jar
make a hard link to the /usr/lib/ folder:
$ sudo ln /usr/local/share/java/opencv4/libopencv_java460.so /usr/lib/libopencv_java460.so
And then just run:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
that he will get the *.so
As Reimeus answered.
Or you can use
System.load("/Library/Path/libsample.so");

"Hello World" with Java annotations

Problem description: Compile 2 jar files independently, without having to include each other on classpath; and at runtime, include both and invoke main() in one jar to print a string on stdout. Parts of the output string have to come from the 2nd jar.
Constraints: The solution cannot be IDE-dependent or use any other jar files.
Answering what have I done so far: My Solution is described below.
Reason to ask the question: I am trying to figure out if/how to use annotations to solve this problem (hopefully in a more elegant manner), but cannot find any suitable documentation or tutorial. I appreciate any pointer(s) for that also.
My Solution: A batch file (on Unix, please change the backslash to forward slash, semicolon to colon and the rem's to #) as follows:
rem Compile and package the testing class (Main) without any library
javac -d target core\*.java
cd target
jar cvf ..\main.jar .\core
cd ..
rem Compile and package the Greeting and Greeted classes as a library
javac -d lib impl\*.java
cd lib
jar cvf ..\mylib.jar .\impl
cd ..
rem Use the two parts above at runtime and execute main() in Main class
java -cp main.jar;mylib.jar core.Main
There are 2 files in the impl directory and 2 in the core, as follows:
/* File: impl/Greeting.java */
package impl;
public class Greeting {
public String getGreeting () {
return "Hello";
}}
/* File: impl/Greeted.java */
package impl;
public class Greeted {
public String getGreeted () {
return "world";
}
/* File: core/Main.java */
package core;
public class Main {
private String greeting = "Learn annotations", greeted = "keep using Java",
// Can read the following 4 values from a configuration file, too
// Trying to see if we can get these using Java annotations
greetingClassName = "impl.Greeting", greetingMethod = "getGreeting",
greetedClassName = "impl.Greeted", greetedMethod = "getGreeted";
public Main () {
try {
MyRunTime runTime = new MyRunTime();
Object gting = runTime.getInstance(greetingClassName),
gted = runTime.getInstance(greetedClassName),
g1Str = runTime.getResponseNoArg (gting, greetingMethod),
g2Str = runTime.getResponseNoArg (gted, greetedMethod);
if (g1Str instanceof String) greeting = (String) g1Str;
if (g2Str instanceof String) greeted = (String) g2Str;
} catch (Exception ex) {
System.err.println ("Error in Library loading: " + ex.getMessage());
}}
public void greet () {
System.out.println (greeting + ", " + greeted + "!");
}
public static void main (String[] args) {
new Main().greet();
}}
/* File: core/MyRunTime.java */
package core;
import java.lang.reflect.*;
public class MyRunTime {
public Object getResponseNoArg (Object anInstance, String methodName) throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
Method method = anInstance.getClass().getMethod (methodName);
return method.invoke (anInstance);
}
public Object getInstance (String className) throws
ClassNotFoundException,
InstantiationException,
IllegalAccessException {
Class c = Class.forName (className);
return c.newInstance();
}
}
That's it. I would also like not to mess with the ClassLoader unless absolutely necessary. Once I get a handle on how to do this with annotations, I can look into passing arguments and go forward. Thank you for your help.
I wouldn't use Annotations for this - it's probably worth covering what Annotations are and where they are best used.
Instead I would do something like:
Place different files in each jar
A main class to each jar file, which does the same thing: list the files on the classpath
I think that would meet the project requirements.
To list the files on the classpath you could so something like:
public class Main {
public static void main(final String[] args) throws java.lang.Throwable {
final String list = System.getProperty( "java.class.path" );
for (String path : list.split( ";" )) {
java.io.File object = new java.io.File( path );
if ( object.isDirectory() )
for ( String entry : object.list() ) {
java.io.File thing = new java.io.File( entry );
if ( thing.isFile() )
System.out.println( thing );
else if( object.isFile() )
System.out.println( object );
}
}
}
}

Categories