001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.server.provider;
037:
038: import com.sun.istack.NotNull;
039: import com.sun.xml.bind.api.JAXBRIContext;
040: import com.sun.xml.ws.api.WSBinding;
041: import com.sun.xml.ws.api.server.AsyncProvider;
042: import com.sun.xml.ws.resources.ServerMessages;
043:
044: import javax.activation.DataSource;
045: import javax.xml.soap.SOAPMessage;
046: import javax.xml.transform.Source;
047: import javax.xml.ws.Provider;
048: import javax.xml.ws.Service;
049: import javax.xml.ws.ServiceMode;
050: import javax.xml.ws.WebServiceException;
051: import javax.xml.ws.soap.SOAPBinding;
052: import java.lang.reflect.ParameterizedType;
053: import java.lang.reflect.Type;
054:
055: /**
056: * Keeps the runtime information like Service.Mode and erasure of Provider class
057: * about Provider endpoint. It proccess annotations to find about Service.Mode
058: * It also finds about parameterized type(e.g. Source, SOAPMessage, DataSource)
059: * of endpoint class.
060: *
061: * @author Jitendra Kotamraju
062: * @author Kohsuke Kawaguchi
063: */
064: final class ProviderEndpointModel<T> {
065: /**
066: * True if this is {@link AsyncProvider}.
067: */
068: final boolean isAsync;
069:
070: /**
071: * In which mode does this provider operate?
072: */
073: @NotNull
074: final Service.Mode mode;
075: /**
076: * T of {@link Provider}<T>.
077: */
078: @NotNull
079: final Class datatype;
080: /**
081: * User class that extends {@link Provider}.
082: */
083: @NotNull
084: final Class implClass;
085:
086: ProviderEndpointModel(Class<T> implementorClass, WSBinding binding) {
087: assert implementorClass != null;
088: assert binding != null;
089:
090: implClass = implementorClass;
091: mode = getServiceMode(implementorClass);
092: Class otherClass = (binding instanceof SOAPBinding) ? SOAPMessage.class
093: : DataSource.class;
094: isAsync = AsyncProvider.class
095: .isAssignableFrom(implementorClass);
096:
097: Class<? extends Object> baseType = isAsync ? AsyncProvider.class
098: : Provider.class;
099: Type baseParam = JAXBRIContext.getBaseType(implementorClass,
100: baseType);
101: if (baseParam == null)
102: throw new WebServiceException(ServerMessages
103: .NOT_IMPLEMENT_PROVIDER(implementorClass.getName()));
104: if (!(baseParam instanceof ParameterizedType))
105: throw new WebServiceException(ServerMessages
106: .PROVIDER_NOT_PARAMETERIZED(implementorClass
107: .getName()));
108:
109: ParameterizedType pt = (ParameterizedType) baseParam;
110: Type[] types = pt.getActualTypeArguments();
111: if (!(types[0] instanceof Class))
112: throw new WebServiceException(ServerMessages
113: .PROVIDER_INVALID_PARAMETER_TYPE(implementorClass
114: .getName(), types[0]));
115: datatype = (Class) types[0];
116:
117: if (mode == Service.Mode.PAYLOAD && datatype != Source.class) {
118: // Illegal to have PAYLOAD && SOAPMessage
119: // Illegal to have PAYLOAD && DataSource
120: throw new IllegalArgumentException(
121: "Illeagal combination - Mode.PAYLOAD and Provider<"
122: + otherClass.getName() + ">");
123: }
124: }
125:
126: /**
127: * Is it PAYLOAD or MESSAGE ??
128: *
129: * @param c endpoint class
130: * @return Service.Mode.PAYLOAD or Service.Mode.MESSAGE
131: */
132: private static Service.Mode getServiceMode(Class<?> c) {
133: ServiceMode mode = c.getAnnotation(ServiceMode.class);
134: return (mode == null) ? Service.Mode.PAYLOAD : mode.value();
135: }
136: }
|