se.jguru.nazgul.test.bundles.hello.client.blueprint.HelloClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nazgul-test-hello-client-blueprint Show documentation
Show all versions of nazgul-test-hello-client-blueprint Show documentation
Nazgul Framework: Test client bundle using the Hello API service APIs.
The newest version!
/*
* Copyright (c) jGuru Europe AB
* All rights reserved.
*/
package se.jguru.nazgul.test.bundles.hello.client.blueprint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.jguru.nazgul.core.test.bundles.hello.api.Hello;
import se.jguru.nazgul.core.test.bundles.hello.api.calltrace.CallTrace;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author Lennart Jörelid, jGuru Europe AB
*/
public class HelloClient extends Thread implements CallTrace {
// Our log
private static final Logger log = LoggerFactory.getLogger(HelloClient.class);
// Internal state
private int numLogs = 5;
private List helloServiceProxyList;
private List callTrace;
/**
* Creates a new HelloClient instance with the supplied
*
* @param name the name of the new thread
* @param numLogs the number of logs which should be emitted before stopping this thread.
*/
public HelloClient(final String name, final Integer numLogs) {
super(name);
if(numLogs > 0) {
this.numLogs = numLogs;
}
helloServiceProxyList = new ArrayList();
callTrace = new ArrayList();
}
/**
* Method called when a Hello service is added to this HelloClient.
*
* @param helloService The Hello service to add.
*/
public void addHelloService(final Hello helloService, final Map serviceProperties) {
final String serviceId = "" + serviceProperties.get("service.id");
final List objectClasses = new ArrayList();
for(String current : (String[]) serviceProperties.get("objectClass")) {
objectClasses.add(current);
}
log("Added helloservice [Service ID:" + serviceId + ", objectClasses: " + objectClasses + "]");
helloServiceProxyList.add(helloService);
}
/**
* Method called when a Hello service is removed from this HelloClient.
*
* @param helloService The Hello service to remove.
*/
public void removeHelloService(final Hello helloService) {
log("Removed helloservice [" + helloService + "]");
helloServiceProxyList.remove(helloService);
}
/**
* @return A debug call trace.
*/
@Override
public List getCallTrace() {
return callTrace;
}
/**
* If this thread was constructed using a separate
* Runnable
run object, then that
* Runnable
object's run
method is called;
* otherwise, this method does nothing and returns.
*
* Subclasses of Thread
should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
for(int i = 0; i < numLogs; i++) {
final String prefix = "[Call " + (i+1) + "/" + numLogs ;
if(helloServiceProxyList == null || helloServiceProxyList.size() == 0) {
log(prefix + "]: No Hello services injected.");
} else {
for(int j = 0; j < helloServiceProxyList.size(); j++) {
log(prefix + ", Service " + (j + 1) + "/" + helloServiceProxyList.size() + "]: "
+ helloServiceProxyList.get(j).sayHello());
}
}
try {
Thread.sleep(3000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//
// Printout the calltrace
//
System.out.println("\n == Call Trace ==");
for(String current : callTrace) {
System.out.println(current);
}
}
//
// Private helpers
//
private void log(final String toLog) {
log.info("\n ===> " + toLog);
callTrace.add(toLog);
}
}