webapplicationimpl.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 922 行 · 第 1/3 页

JAVA
922
字号
        public ComponentProvider getAdaptedComponentProvider() {            return cp;        }        //        public <T> T getInstance(Scope scope, Class<T> c)                throws InstantiationException, IllegalAccessException {            T o = cp.getInstance(scope, c);            if (o == null) {                o = c.newInstance();                injectResources(o);            } else {                injectResources(cp.getInjectableInstance(o));            }            return o;        }        public <T> T getInstance(Scope scope, Constructor<T> contructor, Object[] parameters)                throws InstantiationException, IllegalArgumentException,                IllegalAccessException, InvocationTargetException {            T o = cp.getInstance(scope, contructor, parameters);            if (o == null) {                o = contructor.newInstance(parameters);                injectResources(o);            } else {                injectResources(cp.getInjectableInstance(o));            }            return o;        }        public <T> T getInstance(ComponentContext cc, Scope scope, Class<T> c)                 throws InstantiationException, IllegalAccessException {            T o = cp.getInstance(cc, scope, c);            if (o == null) {                o = c.newInstance();                injectResources(o);            } else {                injectResources(cp.getInjectableInstance(o));            }            return o;        }                public <T> T getInjectableInstance(T instance) {            return cp.getInjectableInstance(instance);        }        public void inject(Object instance) {            cp.inject(instance);            injectResources(cp.getInjectableInstance(instance));        }    }    private final static class AdaptingResourceComponentProvider implements ComponentProvider {        private final ComponentProvider cp;        AdaptingResourceComponentProvider(ComponentProvider cp) {            this.cp = cp;        }        public ComponentProvider getAdaptedComponentProvider() {            return cp;        }        //        public <T> T getInstance(Scope scope, Class<T> c)                throws InstantiationException, IllegalAccessException {            T o = cp.getInstance(scope, c);            if (o == null)                o = c.newInstance();            return o;        }        public <T> T getInstance(Scope scope, Constructor<T> contructor, Object[] parameters)                throws InstantiationException, IllegalArgumentException,                IllegalAccessException, InvocationTargetException {            T o = cp.getInstance(scope, contructor, parameters);            if (o == null) {                o = contructor.newInstance(parameters);            }            return o;        }        public <T> T getInstance(ComponentContext cc, Scope scope, Class<T> c)                 throws InstantiationException, IllegalAccessException {            T o = cp. getInstance(cc, scope, c);            if (o == null)                o = c.newInstance();                        return o;        }                public <T> T getInjectableInstance(T instance) {            return cp.getInjectableInstance(instance);        }        public void inject(Object instance) {            cp.inject(instance);        }    }        private final class DefaultComponentProvider implements ComponentProvider {        public <T> T getInstance(Scope scope, Class<T> c)                throws InstantiationException, IllegalAccessException {            final T o = c.newInstance();            injectResources(o);            return o;        }        public <T> T getInstance(Scope scope, Constructor<T> contructor, Object[] parameters)                throws InstantiationException, IllegalArgumentException,                IllegalAccessException, InvocationTargetException {            final T o = contructor.newInstance(parameters);            injectResources(o);            return o;        }        public <T> T getInstance(ComponentContext cc, Scope scope, Class<T> c)                 throws InstantiationException, IllegalAccessException {            return getInstance(scope, c);        }                public <T> T getInjectableInstance(T instance) {            return instance;        }        public void inject(Object instance) {            injectResources(instance);        }    }    private final static class DefaultResourceComponentProvider implements ComponentProvider {        public <T> T getInstance(Scope scope, Class<T> c)                throws InstantiationException, IllegalAccessException {            final T o = c.newInstance();            return o;        }        public <T> T getInstance(Scope scope, Constructor<T> contructor, Object[] parameters)                throws InstantiationException, IllegalArgumentException,                IllegalAccessException, InvocationTargetException {            final T o = contructor.newInstance(parameters);            return o;        }        public <T> T getInstance(ComponentContext cc, Scope scope, Class<T> c)                 throws InstantiationException, IllegalAccessException {            return getInstance(scope, c);        }                public <T> T getInjectableInstance(T instance) {            return instance;        }        public void inject(Object instance) {        }    }        // Singleton        public void initiate(ResourceConfig resourceConfig) {        initiate(resourceConfig, null);    }    private static class ContextInjectableProvider<T> extends            SingletonTypeInjectableProvider<Context, T> {        ContextInjectableProvider(Type type, T instance) {            super(type, instance);        }    }    public void initiate(ResourceConfig resourceConfig, ComponentProvider _provider) {        if (resourceConfig == null) {            throw new IllegalArgumentException("ResourceConfig instance MUST NOT be null");        }        if (initiated) {            throw new ContainerException(ImplMessages.WEB_APP_ALREADY_INITIATED());        }        this.initiated = true;        // Set up the component provider to be        // used with non-resource class components        this.provider = (_provider == null)                ? new DefaultComponentProvider()                : new AdaptingComponentProvider(_provider);                // Set up the resource component provider to be        // used with resource class components        this.resourceProvider = (_provider == null)                ? new DefaultResourceComponentProvider()                : new AdaptingResourceComponentProvider(_provider);                // Check the resource configuration        this.resourceConfig = resourceConfig;        verifyResourceConfig();        this.resourceContext = new ResourceContext() {            public <T> T getResource(Class<T> c) {                final ResourceClass rc = getResourceClass(c);                if (rc == null) {                    LOGGER.severe("No resource class found for class " + c.getName());                    throw new ContainerException("No resource class found for class " + c.getName());                }                final Object instance = rc.provider.getInstance(resourceProvider, context);                return instance != null ? c.cast(instance) : null;            }        };        ComponentProviderCache cpc = new ComponentProviderCache(                    this.injectableFactory,                    this.provider,                    resourceConfig.getProviderClasses(),                    resourceConfig.getProviderInstances());        // Add injectable provider for @Inject        injectableFactory.add(            new InjectableProvider<Inject, Type>() {                    public Scope getScope() {                        return Scope.Undefined;                    }                    @SuppressWarnings("unchecked")                    public Injectable<Object> getInjectable(ComponentContext ic, Inject a, final Type c) {                        if (!(c instanceof Class))                            return null;                                                final InjectableProviderFactory.AccessibleObjectContext aic =                                 new InjectableProviderFactory.AccessibleObjectContext(                                ic.getAccesibleObject(), ic.getAnnotations());                        return new Injectable<Object>() {                            public Object getValue(HttpContext context) {                                try {                                    return provider.getInstance(aic, Scope.Undefined, (Class)c);                                } catch (Exception e) {                                    LOGGER.log(Level.SEVERE, "Could not get instance from component provider for type " +                                            c, e);                                    throw new ContainerException("Could not get instance from component provider for type " +                                            c, e);                                }                            }                        };                    }                });                // Allow injection of resource config        injectableFactory.add(new ContextInjectableProvider<ResourceConfig>(                ResourceConfig.class, resourceConfig));        // Allow injection of resource context        injectableFactory.add(new ContextInjectableProvider<ResourceContext>(                ResourceContext.class, resourceContext));                injectableFactory.configure(cpc);                // Add per-request-based injectable providers        injectableFactory.add(new CookieParamInjectableProvider());        injectableFactory.add(new HeaderParamInjectableProvider());        injectableFactory.add(new HttpContextInjectableProvider());        injectableFactory.add(new MatrixParamInjectableProvider());        injectableFactory.add(new PathParamInjectableProvider());        injectableFactory.add(new QueryParamInjectableProvider());        // Obtain all context resolvers        new ContextResolverFactory(cpc, injectableFactory);        // Obtain all the templates        this.templateContext = new TemplateFactory(cpc);        // Allow injection of template context        injectableFactory.add(new ContextInjectableProvider<TemplateContext>(                TemplateContext.class, templateContext));        // Obtain all the exception mappers        this.exceptionFactory = new ExceptionMapperFactory(cpc);                // Obtain all resource method dispatchers        this.dispatcherFactory = new ResourceMethodDispatcherFactory(cpc);                // Obtain all message body readers/writers        this.bodyFactory = new MessageBodyFactory(cpc);        injectableFactory.add(                new ContextInjectableProvider<MessageBodyWorkers>(                MessageBodyWorkers.class, bodyFactory));        bodyFactory.init();                // Add per-request-based injectable providers        injectableFactory.add(new CookieParamInjectableProvider());        injectableFactory.add(new HeaderParamInjectableProvider());        injectableFactory.add(new HttpContextInjectableProvider());        injectableFactory.add(new MatrixParamInjectableProvider());        injectableFactory.add(new PathParamInjectableProvider());        injectableFactory.add(new QueryParamInjectableProvider());                // Inject on all components        cpc.injectOnComponents();                // Obtain all root resources        this.rootsRule = new RootResourceClassesRule(                processRootResources(resourceConfig.getResourceClasses()));           }    public ExtendedMessageBodyWorkers getMessageBodyWorkers() {        return bodyFactory;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?