package org.jboss.fresh.shell.commands;
import org.jboss.fresh.ctx.Context;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.events.EventCentral;
import org.jboss.fresh.events.EventListener;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.LinkedList;
public class EventsExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(EventsExe.class);
static final int LIST = 2;
static final int ADD = 3;
static final int DELETE = 4;
public void process(String exepath, String[] params) throws Exception {
log.debug("entered");
PrintWriter err = new PrintWriter(new BufferWriter(getStdOut()));
if (helpRequested()) {
printUsage();
log.debug("done");
return;
}
if (params.length == 0) {
if (canThrowEx()) {
throw new RuntimeException("Wrong number of arguments. No action specified.");
} else {
printUsage();
log.debug("done");
return;
}
}
int action = -1;
String aclazz = null;
String pattern = null;
String dlisStr = null;
String compName = null;
LinkedList patterns = new LinkedList();
for (int i = 0; i < params.length; i++) {
String tmp = params[i];
if (tmp.equals("-a")) {
action = ADD;
if (i < params.length-1) {
aclazz = params[++i];
} else {
error("Listener class specification missing (-a)");
return;
}
//aclazz = params[++i];
//pattern = params[++i];
//for (int j = ++i, k = 0; j < params.length; j++, k++) {
// if (k > 0) exbuf.append(" ");
// exbuf.append(params[j]);
//}
//break;
} else if (tmp.equals("-r")) {
action = DELETE;
if (i < params.length-1) {
dlisStr = params[++i];
} else {
error("Parameter missing: listener name");
return;
}
//if (++i < params.length) {
// pattern = params[i];
//}
break;
} else if (tmp.equals("-l")) {
action = LIST;
break;
} else if (tmp.equals("-n")) {
if (i < params.length-1) {
compName = params[++i];
} else {
error("Name specification missing (-n)");
return;
}
} else if (tmp.equals("-p")) {
if (i < params.length-1) {
patterns.add(params[++i]);
} else {
error("Pattern specification missing (-p)");
return;
}
}
}
Context c = getShell().getContext();
EventCentral ec = (EventCentral) c.get("EventCentral");
if (ec == null) {
error("EventCentral not bound in context.");
return;
}
switch (action) {
case ADD:
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class liscls = null;
try {
liscls = cl.loadClass(aclazz);
} catch (ClassNotFoundException e) {
error("No such class: " + aclazz);
return;
}
EventListener lis = null;
// if extra params
// load class and find constructor that takes one String as parameter
try {
// create Listener
lis = (EventListener) liscls.newInstance();
//} catch (NoSuchMethodException e) {
// error("No constructor for extra parameters found: " + liscls);
// return;
} catch (InstantiationException e) {
error("Could not instantiate class: " + liscls);
return;
} catch (IllegalAccessException e) {
error("Not allowed to instantiate class: " + liscls);
return;
}
String origin = ec.produceOrigin(compName);
// register it with the pattern
ec.registerEventListener(origin, lis, patterns);
BufferObjectWriter boo = new BufferObjectWriter(getStdOut());
boo.writeObject(origin);
boo.flush();
}
break;
case LIST:
{
Iterator it = ec.listenersIterator();
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
Object key = ent.getKey();
Object val = ent.getValue();
err.println(key + " = " + val);
}
}
break;
case DELETE:
{
// find listener whose string representation equals
// then call unregister
Iterator it = ec.listenersIterator();
EventListener lis = null;
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
lis = (EventListener) ent.getKey();
if (String.valueOf(lis).equals(dlisStr)) break;
}
if (lis == null) {
if (canThrowEx()) {
throw new Exception("Event listener not registered: " + dlisStr);
} else {
err.println("Event listener not registered: " + dlisStr);
log.debug("done");
return;
}
}
ec.unregisterEventListener(lis);
}
break;
default:
}
}
public void printUsage() {
PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
out.println("Usage: events [--help] [-ex] <params> ...");
out.println();
out.println(" Instantiate new event listener and register it. The parameters after class are optional. If passed the constructor that takes one String parameter is used. If not present exception is thrown.");
out.println(" events -a <class> -n <component_name> -p <pattern1> <pattern2> <pattern3> ...");
out.println(" example: events -a org.jboss.fresh.parsek.workflow.Conditon org.jboss.fresh.shell.commands.cms.PersistenceFilter conf1");
out.println();
out.println(" List all the event listeners and their patterns");
out.println(" events -l");
out.println();
out.println(" Unregister the specified event listener. The parameters after class are optional. If passed the constructor that takes one String parameter is used. If not present exception is thrown.");
out.println(" events -r org.jboss.fresh.parsek.workflow.Conditon@54a693 org.jboss.fresh.shell.cms.PersistenceFilter");
out.println();
out.println(" event --help : this help\n");
out.close();
}
}