Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do @Context objects come from

I've been searching everywhere, but can't seem to find a clear answer...

What is the mechanism whereby a server (glassfish for my problem) injects actual objets that are annotated with @Context? More specifically, if I wanted to write a class that did something like:

@Path("/")
public class MyResource {
  @GET
  public String doSomething(@Context MyObject obj) {
    // ...
  }
}

then how would I do it? Where is it that the MyObject is instanciated, who does it, and how?

Edit: I've seen stuff like the following:

Using @Context, @Provider and ContextResolver in JAX-RS

http://jersey.576304.n2.nabble.com/ContextResolver-confusion-td5654154.html

However, this doesn't square with what I've seen, e.g. in the constructor of org.neo4j.server.rest.web.RestfulGraphDatabase, which has the following signature:

public RestfulGraphDatabase(
  @Context UriInfo uriInfo,
  @Context Database database,
  @Context InputFormat input,
  @Context OutputFormat output,
  @Context LeaseManager leaseManager )
like image 513
Kricket Avatar asked Jun 05 '12 13:06

Kricket


People also ask

What is a context object?

The context object is an internal JSON structure that is available during an execution, and contains information about your state machine and execution. This allows your workflows access to information about their specific execution. You can access the context object from the following fields: InputPath.

What is a context object in Java?

A Context object contains a list of properties in the form of NamedValue objects. These properties represent information about the client, the environment, or the circumstances of a request and generally are properties that might be inconvenient to pass as parameters.

What is context pattern?

Context object Design Pattern is one of the J2EE design patterns used to store references and pointers to configuration information and services needed by other objects. It allows different objects to interact with each other without every object needing to maintain all the information.

What is context in Android stackoverflow?

An Android app has activities. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.


1 Answers

You can write your own injection provider and plug that into Jersey - look at SingletonTypeInjectableProvider and PerRequestTypeInjectableProvider - extend one of these classes (depending on the lifecycle you want for the injectable object) and register your implementation as a provider in your web app.

For example, something like this:

@Provider
public class MyObjectProvider extends SingletonTypeInjectableProvider<Context, MyObject> {
    public MyObjectProvider() {
        // binds MyObject.class to a single MyObject instance
        // i.e. the instance of MyObject created bellow will be injected if you use
        // @Context MyObject myObject
        super(MyObject.class, new MyObject());
    }
}

To include the provider in your web app you have several options:

  1. if your app uses classpath scanning (or package scanning) just make sure the provider is in the right package / on the classpath
  2. or you can simply register it using META-INF/services entry (add META-INF/services/com.sun.jersey.spi.inject.InjectableProvider file having the name of your provider class in it's contents)
like image 70
Martin Matula Avatar answered Oct 04 '22 19:10

Martin Matula