Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2012 Jeanfrancois Arcand
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  * the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.atmosphere.plugin.jaxrs;
import  com.sun.jersey.core.spi.component.ComponentContext;
import  com.sun.jersey.core.spi.component.ComponentScope;
import  com.sun.jersey.spi.inject.Injectable;
Allow injection of ExecutionContext

Author(s):
Jeanfrancois Arcand
abstract public class ExecutionContextInjector extends BaseInjectableProvider {
    boolean isValidType(Type t) {
        return (t instanceof Class) && ExecutionContext.class.isAssignableFrom((Classt);
    }
    public static final class PerRequest extends ExecutionContextInjector {
        @Override
        public ComponentScope getScope() {
            return ComponentScope.PerRequest;
        }
        @Override
        public Injectable getInjectable(ComponentContext icContext aType t) {
            if (!isValidType(t))
                return null;
            return new Injectable<ExecutionContext>() {
                @Override
                public ExecutionContext getValue() {
                    return new AtmosphereExecutionContext(AtmosphereResourceImpl.class.cast(getAtmosphereResource(AtmosphereResource.classfalse)));
                }
            };
        }
    }
    public static final class Singleton extends ExecutionContextInjector {
        @Override
        public ComponentScope getScope() {
            return ComponentScope.Singleton;
        }
        @Override
        public Injectable getInjectable(ComponentContext icContext aType t) {
            if (!isValidType(t))
                return null;
            return new Injectable<ExecutionContext>() {
                @Override
                public ExecutionContext getValue() {
                    return (ExecutionContext) Proxy.newProxyInstance(this.getClass().getClassLoader(),
                            new Class[]{ExecutionContext.class},
                            new InvocationHandler() {
                                @Override
                                public Object invoke(Object proxyMethod methodObject[] argsthrows Throwable {
                                    return method.invoke(new AtmosphereExecutionContext(AtmosphereResourceImpl.class.cast(getAtmosphereResource(AtmosphereResource.classfalse))),
                                            args);
                                }
                            });
                }
            };
        }
    }
New to GrepCode? Check out our FAQ X