[ 
https://issues.apache.org/jira/browse/WINK-243?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12803172#action_12803172
 ] 

Bryant Luk commented on WINK-243:
---------------------------------

I don't think there's a strict guarantee that the type we inject into a 
@Context type will be the same exact object that's set on the message context.  
Proxies are already injected during some lifecycles (i.e. singletons) so we 
only have to inject a singleton once yet it still remains thread safe.  
Personally, I would be cautious of type casting any injected instance 
regardless if it's in JAX-RS or a general injection framework.

However, if you really want the real object set on the message context, after 
applying the patch, I believe you could add a user handler to inject the 
RuntimeContext itself like:

{code}
    public void handleRequest(MessageContext context, HandlersChain chain) 
throws Throwable {
        context.setAttribute(RuntimeContext.class, context);
        ...
{code}

Then you could call:

{code}
public Resource {
  @Context
  private RuntimeContext runtimeContext;

  @GET
   public String getHelloWorld() {
     MySubType m = (MySubType)runtimeContext.getAttribute(MyType.class, new 
MySubType());
   }
}
{code}

assuming MySubType is a child class of MyType.

> Unable to inject fields once ResourceInstance is initialized
> ------------------------------------------------------------
>
>                 Key: WINK-243
>                 URL: https://issues.apache.org/jira/browse/WINK-243
>             Project: Wink
>          Issue Type: Bug
>          Components: Server
>    Affects Versions: 1.0
>         Environment: Wink 1.0 / JRE 1.6
>            Reporter: bharath chinnadurai
>         Attachments: WINK-243.patch
>
>
> Unable to inject fields once ResourceInstance is initialized. 
> I get a handle to the the resource instance which is about to be invoked 
> using UriInfo.getMatchedResources,  which will initialize a new 
> ResourceInstance using ObjectFactory which would in-turn inject all 
> fields/methods to the resource instance and the resource instance is returned 
> back. After this process any sort of field injection to the context is not 
> getting reflected in the resource instance. This is because in 
> ResourceInstance.getInstanceMethod,
> {code:title=ResourceInstance.java|borderStyle=solid}
> ...
>     public Object getInstance(RuntimeContext context) {
>         if (instance != null) {
>             return instance;
>         }
>         instance = record.getObjectFactory().getInstance(context);
>         return instance;
>     }
> ...
> {code} 
> As we see if the instance is not null the instance is returned as it is.
> A probable fix could be to call wink common's CreationUtils.injectFields() to 
> re-inject the fields which would inject any field that was set to the context 
> after the resource class was initialized. The code above would look like,
> {code:title=ResourceInstance.java|borderStyle=solid}
> ...
> public Object getInstance(RuntimeContext context) {
>         if (instance != null) {
>               try {
>                       CreationUtils.injectFields(instance, 
> record.getMetadata(), context);
>               }  catch (Exception e) {
>                       throw new ObjectCreationException(e);
>               }
>             return instance;
>         }
>         instance = record.getObjectFactory().getInstance(context);
>         return instance;
>     }
> ...
> {code} 
> However this would re-inject fields that was already injected to the instance 
> before while creation of the instance. If that can be lived with, this could 
> possibly be a patch for this bug.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to