This example illustrates how a JAXRS-POJO deployed in the Fuji Framwork should work. The sequence flow is as follows:
bundle: this is the Rest Binding Component
bundle: this is the Service Assembly containing the JAXRS-POJO
bundle: this is a Java class implementing the Fuji Simple Service Provider API
: this is the URL exposed by the JAXRS-POJO
In addition to jbi.xml and Manifest.mf, the Service Unit for JAXRS-POJO contains:
Configuration for JAXRS-POJO
# optional property, if not exist, defaults to default-listener listener-name=default-listener # required property, all JAXRS-POJOs in this service-unit will be accessible under this context-root context-root=/jaxrsapp
This is the JAXRS-POJO class. It looks similar to this:
package jaxrs;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import org.glassfish.openesb.api.service.ServiceConsumer;
import org.glassfish.openesb.api.service.ServiceMessage;
import org.glassfish.openesb.api.service.ServiceProps;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
/**
* HelloResource.java
*
* @author Edward Chou
*/
@Path("/hello")
public class HelloResource {
private static final Logger logger = Logger.getLogger(HelloResource.class.getName());
@Context
BundleContext bundleContext;
@GET
public String get() {
String serviceFilter = "(" + ServiceProps.SERVICE_NAME + "=myService)";
try {
ServiceReference[] refs = bundleContext.getServiceReferences(ServiceConsumer.class.getName(), serviceFilter);
if (refs.length > 0 && refs[0] != null) {
ServiceReference serviceRef = refs[0];
logger.log(Level.INFO, "looked up service reference: " + serviceRef);
ServiceConsumer consumer = (ServiceConsumer) bundleContext.getService(serviceRef);
ServiceMessage message = consumer.getServiceMessageFactory().createMessage();
message.setPayload("request from consumer");
ServiceMessage replyMessage = consumer.invoke(message);
String reply = (String) replyMessage.getPayload();
logger.log(Level.INFO, "got reply from provider: " + reply);
return reply;
}
} catch (Exception e) {
logger.log(Level.SEVERE, "exception when invoking ServiceProvider", e);
}
return "failed to invoke ServiceProvider";
}
}
Pretty much everything is standard for JAXRS, except for the injected "@Context BundleContext bundleContext". This is the OSGI's BundleContext, and it is injected by RestBC upon deployment. This give the JAXRS-POJO access to the OSGI environment, which allows it to lookup the Fuji Simple Service API. The rest of the codes just sends a request message to the service provider "myService".