Created
October 3, 2011 14:01
-
-
Save mathieuancelin/1259160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cx.ath.mancel01.osgi.simple.api.Event; | |
import cx.ath.mancel01.osgi.simple.api.F.Action; | |
import cx.ath.mancel01.osgi.simple.api.F.Callable; | |
import cx.ath.mancel01.osgi.simple.api.F.Function; | |
import cx.ath.mancel01.osgi.simple.api.F.Option; | |
import cx.ath.mancel01.osgi.simple.api.ListenerRegistration; | |
import cx.ath.mancel01.osgi.simple.api.ServiceRegistration; | |
import static cx.ath.mancel01.osgi.simple.api.ListenerTypes.*; | |
import org.junit.Test; | |
import org.osgi.framework.BundleContext; | |
public class OSGiTest { | |
@Test | |
public void testApp(BundleContext ctx) { | |
ServiceRegistration<MyService> reg = | |
SimpleOSGi.forCtx(ctx).forContract(MyService.class).publish(new MyService() { | |
@Override | |
public String doSomething() { | |
return "Hello"; | |
} | |
}); | |
ListenerRegistration l1 = | |
SimpleOSGi.forCtx(ctx).when(SERVICE_ARRIVAL).perform(new Action<Event>() { | |
@Override | |
public void apply(Event t) { | |
System.out.println("Service arrived ..."); | |
} | |
}); | |
ListenerRegistration l2 = | |
SimpleOSGi.forCtx(ctx).when(SERVICE_CHANGES).perform(new Action<Event>() { | |
@Override | |
public void apply(Event t) { | |
System.out.println("Service changed ..."); | |
} | |
}); | |
ListenerRegistration l3 = | |
SimpleOSGi.forCtx(ctx).when(SERVICE_DEPARTURE).perform(new Action<Event>() { | |
@Override | |
public void apply(Event t) { | |
System.out.println("Service departure ..."); | |
} | |
}); | |
SimpleOSGi.forCtx(ctx).ref(MyService.class).perform(new Function<MyService, String>() { | |
@Override | |
public String apply(MyService t) { | |
return t.doSomething().toUpperCase(); | |
} | |
}).toRight("Error during service lookup ...").fold(new Action<String>() { | |
@Override | |
public void apply(String t) { | |
System.err.println(t); | |
} | |
}, new Action<String>() { | |
@Override | |
public void apply(String t) { | |
System.out.println(t); | |
} | |
}); | |
Option<String> result = SimpleOSGi.forCtx(ctx).ref(MyService.class) | |
.perform(new Function<MyService, String>() { | |
@Override | |
public String apply(MyService t) { | |
return t.doSomething().toUpperCase(); | |
} | |
}, "Error during service lookup ..."); | |
Option<String> result2 = SimpleOSGi.forCtx(ctx).ref(MyService.class) | |
.performIfAvailable(new Function<MyService, String>() { | |
@Override | |
public String apply(MyService t) { | |
return t.doSomething().toUpperCase(); | |
} | |
}).or(new Callable<String>() { | |
@Override | |
public String apply() { | |
return "Error during service lookup ..."; | |
} | |
}).apply(); | |
System.out.println(result); | |
System.out.println(result2); | |
reg.unregister(); | |
l1.unregister(); | |
l2.unregister(); | |
l3.unregister(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment