001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.server.provider;
026:
027: import com.sun.xml.internal.bind.api.JAXBRIContext;
028: import java.lang.reflect.ParameterizedType;
029: import javax.activation.DataSource;
030: import javax.xml.ws.Binding;
031: import javax.xml.ws.Provider;
032: import com.sun.xml.internal.ws.server.PeptTie;
033: import java.lang.reflect.Type;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.transform.Source;
036: import javax.xml.ws.Service;
037: import javax.xml.ws.ServiceMode;
038: import javax.xml.ws.soap.SOAPBinding;
039:
040: /**
041: * Keeps the runtime information like Service.Mode and erasure of Provider class
042: * about Provider endpoint. It proccess annotations to find about Service.Mode
043: * It also finds about parameterized type(e.g. Source, SOAPMessage, DataSource)
044: * of endpoint class.
045: *
046: */
047: public class ProviderModel {
048:
049: private final boolean isSource;
050: private final Service.Mode mode;
051:
052: public ProviderModel(Class implementorClass, Binding binding) {
053: assert implementorClass != null;
054: assert binding != null;
055:
056: mode = getServiceMode(implementorClass);
057: Class otherClass = (binding instanceof SOAPBinding) ? SOAPMessage.class
058: : DataSource.class;
059: isSource = isSource(implementorClass, otherClass);
060: if (mode == Service.Mode.PAYLOAD && !isSource) {
061: // Illegal to have PAYLOAD && SOAPMessage
062: // Illegal to have PAYLOAD && DataSource
063: throw new IllegalArgumentException(
064: "Illeagal combination - Mode.PAYLOAD and Provider<"
065: + otherClass.getName() + ">");
066: }
067: }
068:
069: public boolean isSource() {
070: return isSource;
071: }
072:
073: public Service.Mode getServiceMode() {
074: return mode;
075: }
076:
077: /**
078: * Is it PAYLOAD or MESSAGE ??
079: */
080: private static Service.Mode getServiceMode(Class c) {
081: ServiceMode mode = (ServiceMode) c
082: .getAnnotation(ServiceMode.class);
083: if (mode == null) {
084: return Service.Mode.PAYLOAD;
085: }
086: return mode.value();
087: }
088:
089: /**
090: * Is it Provider<Source> ? Finds whether the parameterized type is
091: * Source.class or not.
092: *
093: * @param c provider endpoint class
094: * @param otherClass Typically SOAPMessage.class or DataSource.class
095: * @return true if c's parameterized type is Source
096: * false otherwise
097: * @throws IllegalArgumentException if it is not
098: * Provider<Source> or Provider<otherClass>
099: *
100: */
101: private static boolean isSource(Class c, Class otherClass) {
102: Type base = JAXBRIContext.getBaseType(c, Provider.class);
103: assert base != null;
104: if (base instanceof ParameterizedType) {
105: ParameterizedType pt = (ParameterizedType) base;
106: Type[] types = pt.getActualTypeArguments();
107: if (types[0] instanceof Class
108: && Source.class.isAssignableFrom((Class) types[0])) {
109: return true;
110: }
111: if (types[0] instanceof Class
112: && otherClass.isAssignableFrom((Class) types[0])) {
113: return false;
114: }
115: }
116: throw new IllegalArgumentException(
117: "Endpoint should implement Provider<"
118: + Source.class.getName() + "> or Provider<"
119: + otherClass.getName() + ">");
120: }
121:
122: }
|