Skip to content

Instantly share code, notes, and snippets.

@milendyankov
Last active August 29, 2015 14:04
Show Gist options
  • Save milendyankov/97aa088ace695669dee8 to your computer and use it in GitHub Desktop.
Save milendyankov/97aa088ace695669dee8 to your computer and use it in GitHub Desktop.
OSGI DS test
public interface HelloService {
String hello ();
}
import java.util.Date;
import org.osgi.service.component.annotations.Component;
import com.commsen.test.osgi.ds.HelloService;
@Component(immediate = true)
public class HelloWithTime implements HelloService {
public String hello() {
return "Hello at " + new Date();
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import com.commsen.test.osgi.ds.HelloService;
@Component(immediate = true)
public class JFrameStaticPrinter {
private static int n = 1;
final JFrame jFrame = new JFrame("JFrame STATIC Printer [" + n++ + "]");
boolean init = true;
HelloService _helloSerivice;
/**
* This simulates a method that uses the referenced service a number of
* times for quite some time. The only purpose of the delays is buy you time
* to stop the referenced service while the method is executing.
*/
public void longRunnigMethod() {
for (int i = 0; i < 20; i++) {
if (_helloSerivice == null) {
System.out.println("[JFrame static] Serivice is NULL !!!");
} else {
System.out.println("[JFrame static] " + _helloSerivice.hello());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
@Reference
protected void bindHelloService(HelloService helloSerivice) {
this._helloSerivice = helloSerivice;
}
@Activate
protected void start() {
if (init) {
JButton button = new JButton("Call a long runnig method");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
longRunnigMethod();
}
});
jFrame.setLayout(new BorderLayout());
jFrame.setBounds(10, 10, 300, 100);
jFrame.getContentPane().add(button, BorderLayout.SOUTH);
jFrame.setVisible(true);
init = false;
}
}
@Deactivate
protected void stop() {
jFrame.setVisible(false);
}
protected void unbindHelloService(HelloService helloSerivice) {
this._helloSerivice = null;
}
}
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import com.commsen.test.osgi.ds.HelloService;
@Component(immediate = true)
public class Printer {
HelloService _helloSerivice;
/**
* This simulates a method that uses the referenced service a number of
* times for quite some time. The only purpose of it is to see what is going
* to happen when the service goes away while it is being executed!
*/
public void longRunnigMethod() {
for (int i = 0; i < 20; i++) {
if (_helloSerivice == null) {
System.out.println("Service is NULL !!!");
} else {
System.out.println(_helloSerivice.hello());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
@Reference
protected void bindHelloService(HelloService helloSerivice) {
this._helloSerivice = helloSerivice;
}
@Activate
protected void start() {
/*
* Here we start a thread to simulate and external call to the long
* running method!
*/
Thread T = new Thread(new Runnable() {
public void run() {
longRunnigMethod();
}
}, "Simulate a call thread");
T.start();
}
@Deactivate
protected void stop() {
}
protected void unbindHelloService(HelloService helloSerivice) {
this._helloSerivice = null;
}
}
import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.BindingPolicy;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import com.commsen.test.osgi.ds.HelloServiceIPOJO;
@Component(immediate = true)
@Instantiate
public class PrinterIPOJO {
static int n = 0;
// @Requires(proxy = false, policy=BindingPolicy.STATIC)
HelloServiceIPOJO _helloSerivice;
/**
* This simulates a method that uses the referenced service a number of
* times for quite some time. The only purpose of the delays is buy you time
* to stop the referenced service while the method is executing.
*/
public void longRunnigMethod() {
for (int i = 0; i < 20; i++) {
if (_helloSerivice == null) {
System.out.println("[iPojo Thread " + n
+ "] Serivice is NULL !!!");
} else {
System.out.println("[iPojo Thread " + n + "] "
+ _helloSerivice.hello());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
@Validate
protected void start() {
n++;
/*
* Here we start a thread to simulate and external call to the long
* running service!
*/
Thread T = new Thread(new Runnable() {
public void run() {
longRunnigMethod();
}
}, "Simulate a call thread (iPojo)");
T.start();
}
@Invalidate
protected void stop() {
}
@Bind(policy = BindingPolicy.STATIC, proxy = false)
public void bindHelloSerivice(HelloServiceIPOJO helloSerivice) {
this._helloSerivice = helloSerivice;
}
@Unbind
public void unbindHelloSerivice() {
this._helloSerivice = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment