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 org.apache.log4j.Logger;
38  import org.ogf.graap.wsag.api.Agreement;
39  import org.ogf.graap.wsag.api.AgreementFactory;
40  import org.ogf.graap.wsag.api.AgreementOffer;
41  import org.ogf.graap.wsag.api.types.AbstractAgreementType;
42  import org.ogf.graap.wsag.wsrf.AgreementAcceptanceClient;
43  import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
44  import org.ogf.schemas.graap.wsAgreement.AgreementStateDefinition;
45  import org.ogf.schemas.graap.wsAgreement.AgreementStateType;
46  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
47  import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
48  import org.ogf.schemas.graap.wsAgreement.TermTreeType;
49  import org.ogf.schemas.graap.wsAgreement.TerminateInputType;
50  
51  /**
52   * PendingAgreementFacade
53   * 
54   * @author Oliver Waeldrich
55   * 
56   */
57  public class PendingAgreementFacade implements Agreement, Runnable
58  {
59  
60      private static final Logger LOG = Logger.getLogger( PendingAgreementFacade.class );
61  
62      private Agreement agreement;
63  
64      private AgreementFactory factory;
65  
66      private AgreementOffer offer;
67  
68      private AgreementAcceptanceClient client;
69  
70      private TerminateInputType reason;
71  
72      /**
73       * Implementation of a pending agreement facade. The agreement creation is started in a separate process.
74       * As long as the agreement creation is in progress, the facades represents the pending agreement. If the
75       * agreement creation is successful, the pending agreement is replaced with the real one created by the
76       * WSAG4J engine. This is an implementation of the state pattern (GOF).
77       * 
78       * @param offer
79       *            the agreement offer
80       * @param factory
81       *            the agreement factory
82       * @param client
83       *            the agreement acceptance client
84       */
85      public PendingAgreementFacade( AgreementOffer offer, AgreementFactory factory,
86                                     AgreementAcceptanceClient client )
87      {
88          this.factory = factory;
89          this.offer = offer;
90          this.client = client;
91          this.agreement = new PendingAgreementImpl( offer );
92  
93          this.agreement.getState().setState( AgreementStateDefinition.PENDING );
94      }
95  
96      /**
97       * {@inheritDoc}
98       * 
99       * @see java.lang.Runnable#run()
100      */
101     public void run()
102     {
103         try
104         {
105             Agreement newAgreement = factory.createAgreement( offer );
106 
107             if ( agreement.getState().getState() == AgreementStateDefinition.PENDING_AND_TERMINATING )
108             {
109                 try
110                 {
111                     agreement = newAgreement;
112                     agreement.terminate( reason );
113                 }
114                 catch ( Exception e )
115                 {
116                     // must not throw any exception
117                     LOG.error( e.getMessage() );
118                 }
119             }
120             else
121             {
122                 agreement = newAgreement;
123                 acceptAgreement();
124             }
125         }
126         catch ( Exception e )
127         {
128             if ( LOG.isInfoEnabled() )
129             {
130                 LOG.info( "Error while creating pending agreement. Cause: " + e.getMessage() );
131             }
132 
133             rejectAgreement();
134         }
135     }
136 
137     private void rejectAgreement()
138     {
139         try
140         {
141             agreement.getState().setState( AgreementStateDefinition.REJECTED );
142 
143             if ( client != null )
144             {
145                 client.reject();
146             }
147         }
148         catch ( Exception ex )
149         {
150             // local call to the pending agreement
151             LOG.error( ex.getMessage() );
152         }
153     }
154 
155     private void acceptAgreement()
156     {
157         try
158         {
159             if ( client != null )
160             {
161                 client.accept();
162             }
163         }
164         catch ( Exception ex )
165         {
166             // local call to the pending agreement
167             LOG.error( ex.getMessage() );
168         }
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     public String getAgreementId()
175     {
176         return agreement.getAgreementId();
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     public AgreementContextType getContext()
183     {
184         return agreement.getContext();
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     public GuaranteeTermStateType[] getGuaranteeTermStates()
191     {
192         return agreement.getGuaranteeTermStates();
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     public String getName()
199     {
200         return agreement.getName();
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     public ServiceTermStateType[] getServiceTermStates()
207     {
208         return agreement.getServiceTermStates();
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     public AgreementStateType getState()
215     {
216         return agreement.getState();
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     public TermTreeType getTerms()
223     {
224         return agreement.getTerms();
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     public void terminate( TerminateInputType terminateReason )
231     {
232         this.reason = terminateReason;
233         agreement.terminate( terminateReason );
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     public AbstractAgreementType getAgreementInstance()
240     {
241         return agreement.getAgreementInstance();
242     }
243 }