View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf.impl;
36  
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.xml.namespace.QName;
41  
42  import org.apache.log4j.Logger;
43  import org.apache.muse.core.Resource;
44  import org.apache.muse.ws.addressing.EndpointReference;
45  import org.apache.muse.ws.addressing.soap.SoapFault;
46  import org.apache.muse.ws.resource.faults.ResourceUnavailableFault;
47  import org.apache.xmlbeans.XmlObject;
48  import org.ogf.graap.wsag.api.Negotiation;
49  import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
50  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
51  import org.ogf.graap.wsag.wsrf.WSAG4JCapability;
52  import org.ogf.graap.wsag.wsrf.XmlUtils;
53  import org.ogf.graap.wsag.wsrf.faults.AgreementFactoryFault;
54  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
55  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.InitiateNegotiationInputDocument;
56  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.InitiateNegotiationInputType;
57  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.InitiateNegotiationOutputDocument;
58  import org.ogf.schemas.graap.wsAgreement.negotiation.protocol.InitiateNegotiationOutputType;
59  import org.w3.x2005.x08.addressing.EndpointReferenceType;
60  
61  /**
62   * @author hrasheed
63   * 
64   */
65  public class NegotiationFactoryCapability extends WSAG4JCapability
66  {
67  
68      private static final Logger LOG = Logger.getLogger( NegotiationFactoryCapability.class );
69  
70      private PersistentAgreementFactory getFactoryPersistence()
71      {
72          return ( (AgreementFactoryWsResource) getResource() ).getAgreementFactory();
73      }
74  
75      /**
76       * @return the resource property QNames
77       */
78      public QName[] getPropertyNames()
79      {
80          return new QName[0];
81      }
82  
83      /**
84       * Creates a new negotiation instance.
85       * 
86       * @param initiateInput
87       *            the negotiation context + extensions
88       * 
89       * @return the initiate negotiation response document
90       * 
91       * @throws SoapFault
92       *             generic SOAP fault
93       */
94      public InitiateNegotiationOutputType initiateNegotiation( InitiateNegotiationInputDocument initiateInput )
95          throws SoapFault
96      {
97  
98          InitiateNegotiationInputType input = initiateInput.getInitiateNegotiationInput();
99  
100         NegotiationContextType context = input.getNegotiationContext();
101         XmlObject[] ncExtensions = input.getNoncriticalExtensionArray();
102         XmlObject[] cExtensions = new XmlObject[0];
103         Map<String, Object> environment = new HashMap<String, Object>();
104 
105         Negotiation createdNegotiation = null;
106 
107         try
108         {
109             //
110             // initialize the negotiation process
111             //
112             createdNegotiation =
113                 getFactoryPersistence().initiateNegotiation( context, cExtensions, ncExtensions, environment );
114         }
115         catch ( NegotiationFactoryException e )
116         {
117             throw new AgreementFactoryFault( e );
118         }
119         catch ( Exception e )
120         {
121             String message = "Illegal state in initiateNegotiation method. Error: " + e.getMessage();
122             LOG.error( message );
123             if ( LOG.isDebugEnabled() )
124             {
125                 LOG.debug( e );
126             }
127             throw new ResourceUnavailableFault( message, e );
128         }
129 
130         EndpointReferenceType epr = addNewNegotiationWsResource( createdNegotiation );
131 
132         InitiateNegotiationOutputDocument output = InitiateNegotiationOutputDocument.Factory.newInstance();
133 
134         output.addNewInitiateNegotiationOutput().setCreatedNegotiationEPR( epr );
135 
136         return output.getInitiateNegotiationOutput();
137     }
138 
139     /**
140      * Creates a new Negotiation WS-Resource and returns the EPR.
141      * 
142      * @param negotiation
143      *            the negotiation instance for which the WS resource is created
144      * 
145      * @return the endpoint of the negotiation resource
146      * 
147      * @throws AgreementFactoryFault
148      *             an error occurred while creating the WS resource
149      */
150     protected EndpointReferenceType addNewNegotiationWsResource( Negotiation negotiation )
151         throws AgreementFactoryFault
152     {
153 
154         try
155         {
156             //
157             // create a new negotiation WS resource
158             //
159             Resource negotiationInstance = getResource().getResourceManager().createResource( "Negotiation" );
160 
161             //
162             // initialize the negotiation WS resource object, initialize the WS resources,
163             // and add it to the resource manager
164             //
165             EndpointReference museFactoryEPR = getResource().getEndpointReference();
166             EndpointReferenceType factoryEPR = XmlUtils.convertMuseEPRToEndpoint( museFactoryEPR );
167 
168             ( (NegotiationWsResource) negotiationInstance ).setFactoryEPR( factoryEPR );
169             ( (NegotiationWsResource) negotiationInstance ).setNegotiation( negotiation );
170 
171             negotiationInstance.initialize();
172             getResource().getResourceManager().addResource( negotiationInstance.getEndpointReference(),
173                                                             negotiationInstance );
174 
175             //
176             // return the EPR of the new agreement instance
177             //
178             EndpointReferenceType epr =
179                 XmlUtils.convertMuseEPRToEndpoint( negotiationInstance.getEndpointReference() );
180 
181             return epr;
182         }
183         catch ( Exception ex )
184         {
185             throw new AgreementFactoryFault(
186                                              "An internal error occured while adding a new negotiation. Message: "
187                                                  + ex.getMessage() );
188         }
189     }
190 
191 }