Example: Simple In-Memory Producer
Add odata4j Library
Add the odata4j-<version>-nojpabundle.jar
file to WEB-INF/lib
. When using CXF, odata4j-<version>-cxfbundle.jar
has to be used.
Create Producer Factory
This sample factory creates an in-memory producer that exposes thread information of the running JVM. Put the compiled class into WEB-INF/classes
.
```
package org.odata4j.tomcat;
import java.util.Properties;
import org.core4j.Enumerable; import org.core4j.Func; import org.core4j.Funcs; import org.odata4j.producer.ODataProducer; import org.odata4j.producer.ODataProducerFactory; import org.odata4j.producer.inmemory.InMemoryProducer;
public class ExampleProducerFactory implements ODataProducerFactory {
@Override public ODataProducer create(Properties properties) { InMemoryProducer producer = new InMemoryProducer("example");
// expose this jvm's thread information (Thread instances) as an entity-set called "Threads"
producer.register(Thread.class, Long.class, "Threads", new Func<Iterable<Thread>>() {
public Iterable<Thread> apply() {
ThreadGroup tg = Thread.currentThread().getThreadGroup();
while (tg.getParent() != null)
tg = tg.getParent();
Thread[] threads = new Thread[1000];
int count = tg.enumerate(threads, true);
return Enumerable.create(threads).take(count);
}
}, Funcs.method(Thread.class, Long.class, "getId"));
return producer;
} } ```
Edit Deployment Descriptor
Edit the WEB-INF/web.xml
file.
``` http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
OData com.sun.jersey.spi.container.servlet.ServletContainer javax.ws.rs.Application org.odata4j.jersey.producer.resources.ODataApplication 1 OData /example.svc/*
CrossDomain com.sun.jersey.spi.container.servlet.ServletContainer javax.ws.rs.Application org.odata4j.producer.resources.RootApplication 1 CrossDomain /*
``` * Instead of specifying parameter javax.ws.rs.Application it is still possible to use com.sun.jersey.config.property.resourceConfigClass. * When not using Jersey-specifc configuration (see below), the generic org.odata4j.producer.resources.DefaultODataApplication can be set.
Using CXF``` http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
OData org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet javax.ws.rs.Application org.odata4j.producer.resources.DefaultODataApplication 1 OData /example.svc/*
```
Specify Producer Factory
There are two possibilities to specify the OData producer factory. It can be either set using a system property or added as an additional initialization parameter to the deployment descriptor.
The system property has to be set before starting Tomcat (e.g. using the CATALINA_OPTS environment variable):
-Dodata4j.producerfactory=org.odata4j.tomcat.ExampleProducerFactory
The initialization parameter requires the Jersey-specifc application org.odata4j.jersey.producer.resources.ODataApplication and is added to the corresponding<servlet>
tag:<init-param> <param-name>odata4j.producerfactory</param-name> <param-value>org.odata4j.tomcat.ExampleProducerFactory</param-value> </init-param>
Run the Example
Start Tomcat and point your client (browser) to the following URL:
http://<tomcat-server>:<port>/<context-name>/example.svc/