1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.server.sei;
27
28import com.sun.xml.internal.ws.model.ParameterImpl;
29import com.sun.xml.internal.ws.api.model.Parameter;
30
31import javax.jws.WebParam.Mode;
32import javax.xml.ws.Holder;
33
34/**
35 * Gets a value from an object that represents a parameter passed
36 * as a method argument.
37 *
38 * <p>
39 * This abstraction hides the handling of {@link Holder}.
40 *
41 * <p>
42 * {@link ValueGetter} is a stateless behavior encapsulation.
43 *
44 * @author Kohsuke Kawaguchi
45 */
46public enum ValueGetter {
47    /**
48     * {@link ValueGetter} that works for {@link Mode#IN}  parameter.
49     *
50     * <p>
51     * Since it's the IN mode, the parameter is not a {@link Holder},
52     * therefore the parameter itself is a value.
53     */
54    PLAIN() {
55        public Object get(Object parameter) {
56            return parameter;
57        }
58    },
59    /**
60     * Creates {@link ValueGetter} that works for {@link Holder},
61     * which is  {@link Mode#INOUT} or  {@link Mode#OUT}.
62     *
63     * <p>
64     * In those {@link Mode}s, the parameter is a {@link Holder},
65     * so the value to be sent is obtained by getting the value of the holder.
66     */
67    HOLDER() {
68        public Object get(Object parameter) {
69            if(parameter==null)
70                // the user is allowed to pass in null where a Holder is expected.
71                return null;
72            return ((Holder)parameter).value;
73        }
74    };
75
76    /**
77     * Gets the value to be sent, from a parameter given as a method argument.
78     */
79    public abstract Object get(Object parameter);
80
81    /**
82     * Returns a {@link ValueGetter} suitable for the given {@link Parameter}.
83     */
84    public static ValueGetter get(ParameterImpl p) {
85        // return value is always PLAIN
86        if(p.getMode() == Mode.IN || p.getIndex() == -1) {
87            return PLAIN;
88        } else {
89            return HOLDER;
90        }
91    }
92}
93