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.samples.pendingagreement;
36  
37  import javax.xml.namespace.QName;
38  
39  import org.apache.log4j.Logger;
40  import org.ogf.graap.wsag.api.Agreement;
41  import org.ogf.graap.wsag.api.AgreementOffer;
42  import org.ogf.graap.wsag.api.client.AgreementClient;
43  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
44  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
45  import org.ogf.graap.wsag.api.security.SecurityProperties;
46  import org.ogf.graap.wsag.api.types.AgreementOfferType;
47  import org.ogf.graap.wsag.client.remote.RemoteAgreementFactoryClientImpl;
48  import org.ogf.graap.wsag.server.actions.AbstractCreateAgreementAction;
49  import org.ogf.graap.wsag.server.actions.ActionInitializationException;
50  import org.ogf.graap.wsag.server.api.AgreementAcceptanceFactory;
51  import org.ogf.graap.wsag.server.api.AgreementAcceptanceListener;
52  import org.ogf.graap.wsag.server.api.IAgreementFactoryContext;
53  import org.ogf.graap.wsag.server.engine.WsagEngine;
54  import org.ogf.schemas.graap.wsAgreement.AgreementStateDefinition;
55  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
56  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
57  import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
58  import org.w3.x2005.x08.addressing.EndpointReferenceType;
59  
60  /**
61   * CreatePendingAgreementAction
62   * 
63   * This class shows an example for a long lasting agreement creation process.
64   * 
65   * @author Oliver Waeldrich
66   * 
67   */
68  public class CreatePendingAgreementWithNotificationAction extends AbstractCreateAgreementAction
69  {
70  
71      private static final Logger LOG = Logger.getLogger( CreatePendingAgreementWithNotificationAction.class );
72  
73      /**
74       * Example for a long lasting agreement creation process.
75       * 
76       * {@inheritDoc}
77       */
78      public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
79      {
80          IAgreementFactoryContext factoryContext = getHandlerContext().getFactoryContext();
81          AgreementAcceptanceFactory acceptanceFactory =
82              (AgreementAcceptanceFactory) factoryContext.get( AgreementAcceptanceFactory.AGREEMENT_ACCEPTANCE_FACTORY );
83  
84          if ( acceptanceFactory == null )
85          {
86              throw new AgreementFactoryException( "Acceptance not supported." );
87          }
88          else
89          {
90              //
91              // create an acceptance instance and register the acceptance handler
92              //
93              final PendingAgreement agreement = new PendingAgreement( offer );
94  
95              AgreementAcceptanceListener acceptanceListener = new AgreementAcceptanceListener()
96              {
97  
98                  public void reject()
99                  {
100                     try
101                     {
102                         agreement.getState().setState( AgreementStateDefinition.REJECTED );
103                     }
104                     catch ( Exception e )
105                     {
106                         LOG.error( "Error while setting agreement state." );
107                     }
108                 }
109 
110                 public void accept()
111                 {
112                     try
113                     {
114                         agreement.getState().setState( AgreementStateDefinition.COMPLETE );
115                     }
116                     catch ( Exception e )
117                     {
118                         LOG.error( "Error while setting agreement state." );
119                     }
120                 }
121             };
122 
123             EndpointReferenceType acceptanceEPR =
124                 acceptanceFactory.registerAgreementAccetanceListener( acceptanceListener );
125 
126             //
127             // now create an pending agreement with the factory specified
128             // in the agreement offer
129             //
130             final QName eprQName = EndpointReferenceDocument.type.getDocumentElementName();
131             ServiceDescriptionTermType sdt = offer.getTerms().getAll().getServiceDescriptionTermArray( 0 );
132             EndpointReferenceType epr = (EndpointReferenceType) sdt.selectChildren( eprQName )[0];
133 
134             try
135             {
136                 SecurityProperties securityProperties = new SecurityProperties( WsagEngine.getLoginContext() );
137 
138                 AgreementFactoryClient factory =
139                     new RemoteAgreementFactoryClientImpl( epr, securityProperties );
140 
141                 AgreementTemplateType template = factory.getTemplate( "SAMPLE4-PENDING-AGREEMENT", "1" );
142                 AgreementClient agreementClient =
143                     factory.createPendingAgreement( new AgreementOfferType( template ), acceptanceEPR );
144 
145                 if ( LOG.isInfoEnabled() )
146                 {
147                     LOG.info( "Created agreement client: " + agreementClient.getEndpoint().xmlText() );
148                 }
149             }
150             catch ( Exception e )
151             {
152                 LOG.error( "Failed to create the pending agreement. Reason: " + e.getMessage() );
153 
154                 if ( LOG.isDebugEnabled() )
155                 {
156                     LOG.debug( e );
157                 }
158 
159                 throw new AgreementFactoryException( e );
160             }
161 
162             //
163             // Now, since the work is done and we return the
164             // pending, subcontracted agreement...
165             //
166             return agreement;
167 
168         }
169     }
170 
171     /**
172      * Initializes the action.
173      * 
174      * {@inheritDoc}
175      */
176     public void initialize() throws ActionInitializationException
177     {
178         // nothing to do here
179     }
180 
181 }