webapplicationimpl.java
来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 922 行 · 第 1/3 页
JAVA
922 行
/* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html * or jersey/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at jersey/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */package com.sun.jersey.impl.application;import com.sun.jersey.api.NotFoundException;import com.sun.jersey.spi.resource.InjectableProviderContext;import com.sun.jersey.spi.inject.Injectable;import com.sun.jersey.spi.service.ComponentContext;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationHandler;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.Proxy;import java.lang.reflect.Type;import java.net.URI;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ConcurrentMap;import java.util.logging.Level;import java.util.logging.Logger;import javax.ws.rs.WebApplicationException;import javax.ws.rs.core.Context;import javax.ws.rs.core.HttpHeaders;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.MultivaluedMap;import javax.ws.rs.core.Request;import javax.ws.rs.core.Response;import javax.ws.rs.core.SecurityContext;import javax.ws.rs.core.UriInfo;import javax.ws.rs.ext.MessageBodyWorkers;import javax.ws.rs.ext.Provider;import com.sun.jersey.api.container.ContainerException;import com.sun.jersey.api.core.HttpContext;import com.sun.jersey.api.core.HttpResponseContext;import com.sun.jersey.api.core.ResourceConfig;import com.sun.jersey.api.core.ResourceContext;import com.sun.jersey.api.model.AbstractResource;import com.sun.jersey.api.model.ResourceModelIssue;import com.sun.jersey.api.uri.ExtendedUriInfo;import com.sun.jersey.api.uri.UriTemplate;import com.sun.jersey.impl.ImplMessages;import com.sun.jersey.impl.ThreadLocalHttpContext;import com.sun.jersey.impl.model.ResourceClass;import com.sun.jersey.impl.model.RulesMap;import com.sun.jersey.api.container.ContainerCheckedException;import com.sun.jersey.impl.model.parameter.CookieParamInjectableProvider;import com.sun.jersey.impl.model.parameter.HeaderParamInjectableProvider;import com.sun.jersey.impl.model.parameter.HttpContextInjectableProvider;import com.sun.jersey.impl.model.parameter.MatrixParamInjectableProvider;import com.sun.jersey.impl.model.parameter.PathParamInjectableProvider;import com.sun.jersey.impl.model.parameter.QueryParamInjectableProvider;import com.sun.jersey.impl.modelapi.annotation.IntrospectionModeller;import com.sun.jersey.impl.modelapi.validation.BasicValidator;import com.sun.jersey.impl.template.TemplateFactory;import com.sun.jersey.impl.uri.PathPattern;import com.sun.jersey.impl.uri.PathTemplate;import com.sun.jersey.impl.uri.UriHelper;import com.sun.jersey.impl.uri.rules.ResourceClassRule;import com.sun.jersey.impl.uri.rules.ResourceObjectRule;import com.sun.jersey.impl.uri.rules.RightHandPathRule;import com.sun.jersey.impl.uri.rules.RootResourceClassesRule;import com.sun.jersey.impl.wadl.WadlFactory;import com.sun.jersey.impl.wadl.WadlResource;import com.sun.jersey.spi.container.ExtendedMessageBodyWorkers;import com.sun.jersey.spi.container.ContainerRequest;import com.sun.jersey.spi.container.ContainerResponse;import com.sun.jersey.spi.container.ContainerResponseWriter;import com.sun.jersey.spi.container.WebApplication;import com.sun.jersey.spi.inject.InjectableProvider;import com.sun.jersey.spi.inject.SingletonTypeInjectableProvider;import com.sun.jersey.spi.inject.Inject;import com.sun.jersey.spi.resource.ResourceProviderFactory;import com.sun.jersey.spi.service.ComponentProvider;import com.sun.jersey.spi.service.ComponentProvider.Scope;import com.sun.jersey.spi.template.TemplateContext;import com.sun.jersey.spi.uri.rules.UriRule;import java.util.HashMap;import javax.ws.rs.ext.ExceptionMapper;/** * A Web application that contains a set of resources, each referenced by * an absolute URI template. * * @author Paul.Sandoz@Sun.Com */public final class WebApplicationImpl implements WebApplication { private static final Logger LOGGER = Logger.getLogger(WebApplicationImpl.class.getName()); private final ConcurrentMap<Class, ResourceClass> metaClassMap = new ConcurrentHashMap<Class, ResourceClass>(); private final ResourceProviderFactory resolverFactory; private final ThreadLocalHttpContext context; private boolean initiated; private ResourceConfig resourceConfig; private RootResourceClassesRule rootsRule; private InjectableProviderFactory injectableFactory; private MessageBodyFactory bodyFactory; private ComponentProvider provider; private ComponentProvider resourceProvider; private TemplateContext templateContext; private ExceptionMapperFactory exceptionFactory; private ResourceMethodDispatcherFactory dispatcherFactory; private ResourceContext resourceContext; public WebApplicationImpl() { this.resolverFactory = ResourceProviderFactory.getInstance(); this.context = new ThreadLocalHttpContext(); InvocationHandler requestHandler = new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(context.getRequest(), args); } }; InvocationHandler uriInfoHandler = new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(context.getUriInfo(), args); } }; // Create injectable provider factory this.injectableFactory = new InjectableProviderFactory(); injectableFactory.add(new ContextInjectableProvider<InjectableProviderContext>( InjectableProviderContext.class, injectableFactory)); // Add proxied injectables final Map<Type, Object> m = new HashMap<Type, Object>(); m.put(HttpContext.class, context); m.put(HttpHeaders.class, createProxy(HttpHeaders.class, requestHandler)); m.put(UriInfo.class, createProxy(UriInfo.class, uriInfoHandler)); m.put(ExtendedUriInfo.class, createProxy(ExtendedUriInfo.class, uriInfoHandler)); m.put(Request.class, createProxy(Request.class, requestHandler)); m.put(SecurityContext.class, createProxy(SecurityContext.class, requestHandler)); injectableFactory.add(new InjectableProvider<Context, Type>() { public Scope getScope() { return Scope.Singleton; } public Injectable getInjectable(ComponentContext ic, Context a, Type c) { final Object o = m.get(c); if (o != null) { return new Injectable() { public Object getValue(HttpContext c) { return o; } }; } else return null; } }); } @Override public WebApplication clone() { WebApplicationImpl wa = new WebApplicationImpl(); if (provider instanceof DefaultComponentProvider) { wa.initiate(resourceConfig, null); } else { AdaptingComponentProvider acp = (AdaptingComponentProvider) provider; wa.initiate(resourceConfig, acp.getAdaptedComponentProvider()); } return wa; } public ResourceClass getResourceClass(Class c) { assert c != null; // Try the non-blocking read, the most common opertaion ResourceClass rc = metaClassMap.get(c); if (rc != null) { return rc; } // ResourceClass is not present use a synchronized block // to ensure that only one ResourceClass instance is created // and put to the map synchronized (metaClassMap) { // One or more threads may have been blocking on the synchronized // block, re-check the map rc = metaClassMap.get(c); if (rc != null) { return rc; } rc = newResourceClass(getAbstractResource(c)); metaClassMap.put(c, rc); } rc.init(getComponentProvider(), getResourceComponentProvider(), resolverFactory); return rc; } private ResourceClass getResourceClass(AbstractResource ar) { ResourceClass rc = newResourceClass(ar); metaClassMap.put(ar.getResourceClass(), rc); rc.init(getComponentProvider(), getResourceComponentProvider(), resolverFactory); return rc; } private ResourceClass newResourceClass(final AbstractResource ar) { assert null != ar; BasicValidator validator = new BasicValidator(); validator.validate(ar); boolean fatalIssueFound = false; for (ResourceModelIssue issue : validator.getIssueList()) { if (issue.isFatal()) { fatalIssueFound = true; if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.severe(issue.getMessage()); } } else { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(issue.getMessage()); } } } // eof model validation if (fatalIssueFound) { LOGGER.severe(ImplMessages.FATAL_ISSUES_FOUND_AT_RES_CLASS(ar.getResourceClass().getName())); throw new ContainerException(ImplMessages.FATAL_ISSUES_FOUND_AT_RES_CLASS(ar.getResourceClass().getName())); } return new ResourceClass( resourceConfig, getComponentProvider(), dispatcherFactory, injectableFactory, ar); } private AbstractResource getAbstractResource(Class c) { return IntrospectionModeller.createResource(c); } /** * Inject resources onto fields of an object. * @param o the object */ private void injectResources(Object o) { injectableFactory.injectResources(o); } private final class AdaptingComponentProvider implements ComponentProvider { private final ComponentProvider cp; AdaptingComponentProvider(ComponentProvider cp) { this.cp = cp; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?