FileDocCategorySizeDatePackage
RMConstants.javaAPI DocExample12536Tue May 29 16:56:40 BST 2007com.sun.xml.ws.rm

RMConstants.java

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

/*
 * RMConstants.java
 *
 * @author Mike Grogan
 * Created on October 9, 2005, 9:14 PM
 *
 */

package com.sun.xml.ws.rm;
import com.sun.xml.ws.api.addressing.AddressingVersion;
import com.sun.xml.ws.developer.MemberSubmissionEndpointReference;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.sun.xml.bind.api.JAXBRIContext; 
import javax.xml.namespace.QName;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Class contains accessors for constants defined by the 02/2005 version of the 
 * WS-RM specification.
 */
public enum RMConstants {


    W3C(AddressingVersion.W3C) {
        @Override
        public Class getAcksToClass (){
            return W3CEndpointReference.class;
        }
    },
    MEMBER(AddressingVersion.MEMBER)  {
        @Override
        public Class getAcksToClass (){
            return MemberSubmissionEndpointReference.class;
        }
    };




    private final AddressingVersion addressingVersion ;
    // TODO FIX ME ADDRESSING_FIXME
    // private static final JAXBContext jc;
    private JAXBRIContext jc;
    
    public JAXBRIContext jaxbricontext;


    private RMConstants(AddressingVersion addVersion) {
        this.addressingVersion = addVersion;
        init();

    }

    public static RMConstants getRMConstants(AddressingVersion version) {
        if(version == AddressingVersion.W3C) {
            return RMConstants.W3C;
        } else {
            return RMConstants.MEMBER;
        }
    }

    private void init(){
        try {

            List<Class> classes = getClassesToBeBound();
            jc = JAXBRIContext.newInstance(classes.toArray(new Class[0]),null,null,null,false,null);
            
            Class[] clazzes = getHeaderClassesToBeBound().toArray(new Class[0]);
            jaxbricontext = JAXBRIContext.newInstance(clazzes,
                                                        null, 
                                                        null, null,false, null);
            
        } catch (JAXBException e) {
            throw new Error(e);
        } catch(RMException e ) {
            throw new Error(e);
        }

    }


    /**
     * Returns the package name for protocol classes.  Used to initialize a JAXBContext.
     *
     * @return the package name (com.sun.xml.ws.rm.protocol)
     */
    private  String getPackageName() {
        return Constants.PROTOCOL_PACKAGE_NAME;
    }


    /**
     * Returns the namespace URI for the WS-RM spec.
     *
     * @return The URI (http://schemas.xmlsoap.org/ws/2005/02/rm)
     */
    public String getNamespaceURI() {
        return Constants.WS_RM_NAMESPACE;
    }



    /**
     * Returns the QName of the CreateSequence element defined by the WS-RM schema.
     *
     * @return The QName.
     */
    public  QName getCreateSequenceQName() {
        return new QName(getNamespaceURI(), "CreateSequence");
    }




    /**
     * Returns the QName of the TerminateSequence element defined by the WS-RM schema.
     *
     * @return The QName.
     */
    public  QName getTerminateSequenceQName() {
        return new QName(getNamespaceURI(), "TerminateSequence");
    }



    /**
     * Returns the QName of the AcksTo element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public  QName getAcksToQName() {
        return new QName(getNamespaceURI(), "AcksTo");
    }


    /**
     * Returns the value of the WS-Addressing Action property stand alone Sequence
     * messages with Last child.
     *
     * @return The Action value (http://schemas.xmlsoap.org/ws/2005/02/rm/Last)
     */
    public String getLastAction() {
        return "http://schemas.xmlsoap.org/ws/2005/02/rm/LastMessage";
    }

    /**
     * Returns the WS-Adressing Action property for stand-alone 
     * SequenceAcknowledgement messages.
     */
     public String getSequenceAcknowledgementAction() {
 	 return "http://schemas.xmlsoap.org/ws/2005/02/rm/SequenceAcknowledgement";
     }

    

    /**
     * Returns the QName of the Address element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public  QName getAddressQName() {
        return new QName(getNamespaceURI(), "Address");
    }

    /**
     * Returns the QName of the Offer element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public QName getOfferQName() {
        return new QName(getNamespaceURI(), "Offer");
    }

    /**
     * Returns the QName of the Identifier element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public QName getIdentifierQName() {
        return new QName(getNamespaceURI(), "Identifier");
    }

    /**
     * Returns the QName of the AckRequested element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public QName getAckRequestedQName() {
        return new QName(getNamespaceURI(), "AckRequested");
    }


    /**
     * Returns the QName of the Seqence element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public  QName getSequenceQName() {
        return new QName(getNamespaceURI(), "Sequence");
    }

    /**
     * Returns the QName of the SequenceAcknowledgement element defined by the WS-RM spec.
     *
     * @return The QName.
     */
    public QName getSequenceAcknowledgementQName() {
        return new QName(getNamespaceURI(),
                "SequenceAcknowledgement");
    }

    //Policy Assertiion QNames
    public QName getRMAssertionQName() {
        return new QName(Constants.version, "RMAssertion");
    }

    public QName getOrderedQName() {
        return new QName(Constants.sunVersion, "Ordered");
    }
    
    public QName getAllowDuplicatesQName() {
        return new QName(Constants.sunVersion, "AllowDuplicates");
    }

    public QName getResendIntervalQName() {
        return new QName(Constants.sunClientVersion, "ResendInterval");
    }

    public QName getAckRequestIntervalQName() {
        return new QName(Constants.sunClientVersion, "AckRequestInterval");
    }
    
    public QName getCloseTimeoutQName() {
        return new QName(Constants.sunClientVersion, "CloseTimeout");
    }

    public QName getInactivityTimeoutQName() {
        return new QName(Constants.version, "InactivityTimeout");
    }

    public QName getAcknowledgementIntervalQName() {
        return new QName(Constants.version, "AcknowledgementInterval");
    }

    public QName getMillisecondsQName() {
        return new QName(Constants.version, "Milliseconds");
    }

    public QName getRMFlowControlQName() {
        return new QName(Constants.microsoftVersion, "RmFlowControl");
    }

    public QName getMaxReceiveBufferSizeQName() {
        return new QName(Constants.microsoftVersion, "MaxReceiveBufferSize");
    }

    public  JAXBRIContext getJAXBContext() {
        return jc;
    }
    
    public JAXBRIContext getJAXBRIContextHeaders() {
        return jaxbricontext;
    }

    public  Marshaller createMarshaller() {
        try {
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            return marshaller;
        } catch (JAXBException e) {
            return null;
        }
    }

    public Unmarshaller createUnmarshaller()  {
        try {
            return jc.createUnmarshaller();
        } catch (JAXBException e) {
            return null;
        }
    }


    private  List<Class> getClassesToBeBound() throws RMException{
        final Class[] classes;
        final ArrayList<Class> classList;
        try {
            classes = new Class[]{
                    Class.forName(getPackageName()+ ".AckRequestedElement"),
                    Class.forName(getPackageName()+ ".SequenceElement"),
                     Class.forName(getPackageName()+ ".SequenceAcknowledgementElement"),
                    Class.forName(getPackageName()+ ".Identifier"),
                    Class.forName(getPackageName()+ ".CreateSequenceElement"),
                    Class.forName(getPackageName()+ ".CreateSequenceResponseElement"),                  
                    Class.forName(getPackageName()+ ".SequenceFaultElement"),
                    Class.forName(getPackageName()+ ".TerminateSequenceElement"),
                    Class.forName(getPackageName()+ ".AcceptType"),                    
                    Class.forName(getPackageName()+ ".OfferType"),
                    Class.forName(getPackageName()+ ".Expires")
            };
            classList = new ArrayList<Class>(Arrays.asList(classes));
            classList.add(getAcksToClass());
            return classList;
        } catch (ClassNotFoundException e) {
            throw new RMException("Cannot bind the following class with JAXBContext" + e);
        }
    }
    
     private  List<Class> getHeaderClassesToBeBound() throws RMException{
        final Class[] classes;
        final ArrayList<Class> classList;
        try {
            classes = new Class[]{
                    Class.forName(getPackageName()+ ".AckRequestedElement"),
                    Class.forName(getPackageName()+ ".SequenceElement"),
                     Class.forName(getPackageName()+ ".SequenceAcknowledgementElement"),
                    Class.forName(getPackageName()+ ".Identifier"),
                    
            };
            classList = new ArrayList<Class>(Arrays.asList(classes));
            classList.add(getAcksToClass());
            return classList;
        } catch (ClassNotFoundException e) {
            throw new RMException("Cannot bind the following class with JAXBContext" + e);
        }
    }

    public abstract Class getAcksToClass();

    public  AddressingVersion getAddressingVersion() {
        return addressingVersion;
    }



    public URI getAnonymousURI() {
        try {

            return new URI(getAddressingVersion().anonymousUri);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Error(e);
        }

    }
}