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:
037: package com.sun.xml.ws.client.sei;
038:
039: import com.sun.xml.ws.api.model.Parameter;
040: import com.sun.xml.ws.model.ParameterImpl;
041: import com.sun.xml.bind.api.RawAccessor;
042:
043: import javax.xml.ws.Holder;
044: import javax.xml.ws.WebServiceException;
045: import javax.xml.namespace.QName;
046: import javax.xml.bind.JAXBException;
047:
048: /**
049: * Moves a Java value unmarshalled from a response message
050: * to the right place.
051: *
052: * <p>
053: * Sometimes values are returned as a return value, and
054: * others are returned in the {@link Holder} value. Instances
055: * of this interface abstracts this detail.
056: *
057: * <p>
058: * {@link ValueSetter} is a stateless behavior encapsulation.
059: *
060: * @author Kohsuke Kawaguchi
061: */
062: abstract class ValueSetter {
063: private ValueSetter() {
064: }
065:
066: /**
067: * Moves the value to the expected place.
068: *
069: * @param obj
070: * The unmarshalled object.
071: * @param args
072: * The arguments given to the Java method invocation. If <tt>obj</tt>
073: * is supposed to be returned as a {@link Holder} value, a suitable
074: * {@link Holder} is obtained from this argument list and <tt>obj</tt>
075: * is set.
076: *
077: * @return
078: * if <tt>obj</tt> is supposed to be returned as a return value
079: * from the method, this method returns <tt>obj</tt>. Otherwise null.
080: */
081: abstract Object put(Object obj, Object[] args);
082:
083: /**
084: * Singleton instance.
085: */
086: private static final ValueSetter RETURN_VALUE = new ReturnValue();
087: /**
088: * {@link Param}s with small index numbers are used often,
089: * so we pool them to reduce the footprint.
090: */
091: private static final ValueSetter[] POOL = new ValueSetter[16];
092:
093: static {
094: for (int i = 0; i < POOL.length; i++)
095: POOL[i] = new Param(i);
096: }
097:
098: /**
099: * Returns a {@link ValueSetter} suitable for the given {@link Parameter}.
100: */
101: static ValueSetter getSync(ParameterImpl p) {
102: int idx = p.getIndex();
103:
104: if (idx == -1)
105: return RETURN_VALUE;
106: if (idx < POOL.length)
107: return POOL[idx];
108: else
109: return new Param(idx);
110: }
111:
112: private static final class ReturnValue extends ValueSetter {
113: Object put(Object obj, Object[] args) {
114: return obj;
115: }
116: }
117:
118: static final class Param extends ValueSetter {
119: /**
120: * Index of the argument to put the value to.
121: */
122: private final int idx;
123:
124: public Param(int idx) {
125: this .idx = idx;
126: }
127:
128: Object put(Object obj, Object[] args) {
129: Object arg = args[idx];
130: if (arg != null) {
131: // we build model in such a way that this is guaranteed
132: assert arg instanceof Holder;
133: ((Holder) arg).value = obj;
134: }
135: // else {
136: // if null is given as a Holder, there's no place to return
137: // the value, so just ignore.
138: // }
139:
140: // no value to return
141: return null;
142: }
143: }
144:
145: /**
146: * Singleton instance.
147: */
148: static final ValueSetter SINGLE_VALUE = new SingleValue();
149:
150: /**
151: * Used in case of async invocation, where there is only one OUT parameter
152: */
153: private static final class SingleValue extends ValueSetter {
154: /**
155: * Set args[0] as the value
156: */
157: Object put(Object obj, Object[] args) {
158: args[0] = obj;
159: return null;
160: }
161: }
162:
163: /**
164: * OUT parameters are set in async bean
165: */
166: static final class AsyncBeanValueSetter extends ValueSetter {
167:
168: private final RawAccessor accessor;
169:
170: AsyncBeanValueSetter(ParameterImpl p, Class wrapper) {
171: QName name = p.getName();
172: try {
173: accessor = p.getOwner().getJAXBContext()
174: .getElementPropertyAccessor(wrapper,
175: name.getNamespaceURI(),
176: name.getLocalPart());
177: } catch (JAXBException e) {
178: throw new WebServiceException( // TODO: i18n
179: wrapper
180: + " do not have a property of the name "
181: + name, e);
182: }
183: }
184:
185: /**
186: * Sets the property in async bean instance
187: *
188: * @param obj property in async bean
189: * @param args args[0] contains async bean instance
190: * @return null always
191: */
192: Object put(Object obj, Object[] args) {
193: assert args != null;
194: assert args.length == 1;
195: assert args[0] != null;
196:
197: Object bean = args[0];
198: try {
199: accessor.set(bean, obj);
200: } catch (Exception e) {
201: throw new WebServiceException(e); // TODO:i18n
202: }
203: return null;
204: }
205:
206: }
207:
208: }
|