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.api.model;
037:
038: /**
039: * Denotes the binding of a parameter.
040: *
041: * <p>
042: * This is somewhat like an enumeration (but it is <b>NOT</b> an enumeration.)
043: *
044: * <p>
045: * The possible values are
046: * BODY, HEADER, UNBOUND, and ATTACHMENT. BODY, HEADER, and UNBOUND
047: * has a singleton semantics, but there are multiple ATTACHMENT instances
048: * as it carries additional MIME type parameter.
049: *
050: * <p>
051: * So don't use '==' for testing the equality.
052: */
053: public final class ParameterBinding {
054: /**
055: * Singleton instance that represents 'BODY'
056: */
057: public static final ParameterBinding BODY = new ParameterBinding(
058: Kind.BODY, null);
059: /**
060: * Singleton instance that represents 'HEADER'
061: */
062: public static final ParameterBinding HEADER = new ParameterBinding(
063: Kind.HEADER, null);
064: /**
065: * Singleton instance that represents 'UNBOUND',
066: * meaning the parameter doesn't have a representation in a SOAP message.
067: */
068: public static final ParameterBinding UNBOUND = new ParameterBinding(
069: Kind.UNBOUND, null);
070:
071: /**
072: * Creates an instance that represents the attachment
073: * with a given MIME type.
074: *
075: * <p>
076: * TODO: shall we consider givint the singleton semantics by using
077: * a cache? It's more elegant to do so, but
078: * no where in JAX-WS RI two {@link ParameterBinding}s are compared today,
079: */
080: public static ParameterBinding createAttachment(String mimeType) {
081: return new ParameterBinding(Kind.ATTACHMENT, mimeType);
082: }
083:
084: /**
085: * Represents 4 kinds of binding.
086: */
087: public static enum Kind {
088: BODY, HEADER, UNBOUND, ATTACHMENT;
089: }
090:
091: /**
092: * Represents the kind of {@link ParameterBinding}.
093: * Always non-null.
094: */
095: public final Kind kind;
096:
097: /**
098: * Only used with attachment binding.
099: */
100: private String mimeType;
101:
102: private ParameterBinding(Kind kind, String mimeType) {
103: this .kind = kind;
104: this .mimeType = mimeType;
105: }
106:
107: public String toString() {
108: return kind.toString();
109: }
110:
111: /**
112: * Returns the MIME type associated with this binding.
113: *
114: * @throws IllegalStateException
115: * if this binding doesn't represent an attachment.
116: * IOW, if {@link #isAttachment()} returns false.
117: * @return
118: * Can be null, if the MIME type is not known.
119: */
120: public String getMimeType() {
121: if (!isAttachment())
122: throw new IllegalStateException();
123: return mimeType;
124: }
125:
126: public boolean isBody() {
127: return this == BODY;
128: }
129:
130: public boolean isHeader() {
131: return this == HEADER;
132: }
133:
134: public boolean isUnbound() {
135: return this == UNBOUND;
136: }
137:
138: public boolean isAttachment() {
139: return kind == Kind.ATTACHMENT;
140: }
141: }
|