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.server.persistence.impl;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Vector;
41  
42  import org.apache.log4j.Logger;
43  import org.apache.xmlbeans.XmlObject;
44  import org.ogf.graap.wsag.api.Agreement;
45  import org.ogf.graap.wsag.api.AgreementFactory;
46  import org.ogf.graap.wsag.api.AgreementOffer;
47  import org.ogf.graap.wsag.api.Negotiation;
48  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
49  import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
50  import org.ogf.graap.wsag.api.logging.LogMessage;
51  import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
52  import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
53  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
54  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
55  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
56  import org.w3.x2005.x08.addressing.EndpointReferenceType;
57  
58  /**
59   * A persistent factory can list all agreements that it has created, even after system restart. Agreement
60   * creation itself is handles by a delegate, i.e. the factory class that is configured in the wsag4j config
61   * file.
62   * 
63   * @author owaeld
64   */
65  public abstract class AbstractPersistentAgreementFactory
66      implements PersistentAgreementFactory
67  {
68  
69      private static final Logger LOG = Logger.getLogger( SimplePersistentAgreementFactory.class );
70  
71      /**
72       * The factory implementation that handles agreement creation.
73       */
74      protected AgreementFactory factory;
75  
76      /**
77       * The resource id of the factory. This id uniquely identifies the factory.
78       */
79      protected String resourceId;
80  
81      private final List<PersistentAgreement> activeAgreements = new Vector<PersistentAgreement>();
82  
83      /**
84       * @param factory
85       *            the delegation target for the {@link AgreementFactory} calls. This is typically an instance
86       *            of {@link org.ogf.graap.wsag.server.engine.GenericAgreementFactory}.
87       */
88      public AbstractPersistentAgreementFactory( AgreementFactory factory )
89      {
90          this.factory = factory;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      public AgreementFactory getAgreementFactory()
97      {
98          return factory;
99      }
100 
101     /**
102      * Persists the given agreement instance.
103      * 
104      * @param agreement
105      *            the agreement to persist
106      * @return the persisted agreement
107      * 
108      * @throws PersistedResourceException
109      *             indicates that an error occurred while persisting the agreement
110      */
111     protected abstract PersistentAgreement persistAgreement( Agreement agreement )
112         throws PersistedResourceException;
113 
114     /**
115      * Loads all agreements that were created by this factory.
116      * 
117      * @return the loaded agreements
118      * 
119      * @throws PersistedResourceException
120      *             indicates an error while loading the agreements from the persistence layer
121      */
122     protected abstract PersistentAgreement[] doLoad() throws PersistedResourceException;
123 
124     /**
125      * Removes an agreement from the persistence layer
126      * 
127      * @param toRemove
128      *            the agreement to remove
129      * 
130      * @throws PersistedResourceException
131      *             indicates that the agreement could not be removed from the persistence layer
132      */
133     protected abstract void doRemove( PersistentAgreement toRemove ) throws PersistedResourceException;
134 
135     /**
136      * Creates a new agreement instance.
137      * 
138      * @param offer
139      *            the agreement offer
140      * 
141      * @return the new agreement
142      * 
143      * @throws AgreementFactoryException
144      *             indicates that the agreement offer was rejected or any other error while creating the
145      *             agreement.
146      * 
147      * @see org.ogf.graap.wsag.api.AgreementFactory#createAgreement(org.ogf.graap.wsag.api.AgreementOffer)
148      */
149     public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
150     {
151         Agreement agreement = factory.createAgreement( offer );
152 
153         // create the agreement (with the persisted PersistentAgreementContainer
154         PersistentAgreement persistentAgreement;
155 
156         try
157         {
158             persistentAgreement = persistAgreement( agreement );
159             activeAgreements.add( persistentAgreement );
160         }
161         catch ( PersistedResourceException e )
162         {
163             throw new AgreementFactoryException( "Failed to persist agreement.", e );
164         }
165 
166         // try to save the agreement
167         String agreementId = agreement.getAgreementId();
168 
169         try
170         {
171             persistentAgreement.save();
172         }
173         catch ( Exception ex )
174         {
175             LOG.error( LogMessage.getMessage( "Could not save the new agreement ''{0}''.", agreementId ), ex );
176         }
177 
178         return agreement;
179     }
180 
181     /**
182      * {@inheritDoc}
183      * 
184      * @see org.ogf.graap.wsag.server.persistence.impl.SimplePersistentAgreementFactory#addAgreement(org.ogf.graap.wsag.api.Agreement,
185      *      org.w3.x2005.x08.addressing.EndpointReferenceType)
186      */
187     public void addAgreement( Agreement agreement, EndpointReferenceType agreementEpr )
188     {
189         // create the agreement (with the persisted PersistentAgreementContainer
190 
191         try
192         {
193             PersistentAgreement persisted = persistAgreement( agreement );
194             activeAgreements.add( persisted );
195         }
196         catch ( Exception ex )
197         {
198             LOG.error(
199                 LogMessage.getMessage( "Could not persist agreement ''{0}''.", agreement.getAgreementId() ),
200                 ex );
201         }
202     }
203 
204     /**
205      * {@inheritDoc}
206      * 
207      * @see org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory#load()
208      */
209     public void load() throws Exception
210     {
211         activeAgreements.clear();
212 
213         PersistentAgreement[] agreements = doLoad();
214 
215         // extract all agreements
216         for ( int i = 0; i < agreements.length; i++ )
217         {
218             activeAgreements.add( agreements[i] );
219         }
220     }
221 
222     /**
223      * {@inheritDoc}
224      * 
225      * @see PersistentAgreementFactory#save()
226      */
227     public void save() throws Exception
228     {
229         Iterator<PersistentAgreement> it = activeAgreements.iterator();
230         while ( it.hasNext() )
231         {
232             PersistentAgreement persistentAgreement = it.next();
233             persistentAgreement.save();
234         }
235     }
236 
237     /**
238      * {@inheritDoc}
239      * 
240      * @see org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory#list()
241      */
242     public PersistentAgreement[] list()
243     {
244         // convert to array of persistent agreements
245         return activeAgreements.toArray( new PersistentAgreement[activeAgreements.size()] );
246     }
247 
248     /**
249      * {@inheritDoc}
250      * 
251      * @see org.ogf.graap.wsag.server.persistence.IAgreementHome#list(java.lang.String)
252      */
253     public PersistentAgreement[] list( String agreementFactoryId ) throws Exception
254     {
255         if ( resourceId.equals( agreementFactoryId ) )
256         {
257             return list();
258         }
259         else
260         {
261             return new PersistentAgreement[0];
262         }
263     }
264 
265     /**
266      * {@inheritDoc}
267      * 
268      * @see org.ogf.graap.wsag.server.persistence.IAgreementHome#find(java.lang.String)
269      */
270     public PersistentAgreement find( String agreementId ) throws PersistedResourceException
271     {
272         LOG.debug( LogMessage.getMessage( "Try to find a agreement with id ''{0}''.", agreementId ) );
273 
274         synchronized ( activeAgreements )
275         {
276             for ( Iterator<PersistentAgreement> iterator = activeAgreements.iterator(); iterator.hasNext(); )
277             {
278                 PersistentAgreement actual = iterator.next();
279 
280                 if ( actual.getAgreement().getAgreementId().equals( agreementId ) )
281                 {
282                     return actual;
283                 }
284             }
285         }
286 
287         String msgText = "agreement with id ''{0}'' was not found at factory ''{1}''.";
288         String error = LogMessage.format( msgText, agreementId, getResourceId() );
289         throw new PersistedResourceException( error );
290     }
291 
292     /**
293      * {@inheritDoc}
294      * 
295      * @see org.ogf.graap.wsag.server.persistence.IAgreementHome#remove(java.lang.String)
296      */
297     public void remove( String agreementId ) throws PersistedResourceException
298     {
299         LOG.debug( LogMessage.getMessage( "Remove agreement with id''{0}''.", agreementId ) );
300 
301         //
302         // find the agreement instance in the active list
303         //
304         PersistentAgreement toRemove = null;
305         Iterator<PersistentAgreement> it = activeAgreements.iterator();
306         while ( it.hasNext() )
307         {
308             PersistentAgreement persistentAgreement = it.next();
309             if ( persistentAgreement.getAgreement().getAgreementId().equals( agreementId ) )
310             {
311                 toRemove = persistentAgreement;
312                 break;
313             }
314         }
315 
316         if ( toRemove == null )
317         {
318             //
319             // no agreement with the given id in this factory
320             //
321             String msgText = "The agreement with id ''{0}'' does not exist at factory ''{1}''";
322             LOG.debug( LogMessage.getMessage( msgText, agreementId, getResourceId() ) );
323 
324             String removeTxt = "Agreement ''{0}'' was not removed. Agreement does not exist in factory.";
325             throw new PersistedResourceException( LogMessage.format( removeTxt, agreementId ) );
326         }
327 
328         try
329         {
330             synchronized ( activeAgreements )
331             {
332                 doRemove( toRemove );
333                 activeAgreements.remove( toRemove );
334             }
335         }
336         catch ( Exception e )
337         {
338             LOG.error( LogMessage.getMessage( "Agreement ''{0}'' could not be removed.", agreementId ), e );
339         }
340 
341         LOG.debug( LogMessage.getMessage( "Agreement ''{0}'' removed.", agreementId ) );
342     }
343 
344     /**
345      * @return the resourceId
346      */
347     public String getResourceId()
348     {
349         return resourceId;
350     }
351 
352     /**
353      * @param resourceId
354      *            the resourceId to set
355      */
356     public void setResourceId( String resourceId )
357     {
358         this.resourceId = resourceId;
359 
360         String message = "Replaced generated unique resource id with set resource id ''{0}''.";
361         LOG.debug( LogMessage.getMessage( message, resourceId ) );
362     }
363 
364     /**
365      * @return the supported templates
366      * 
367      * @see org.ogf.graap.wsag.api.AgreementFactory#getTemplates()
368      */
369     public AgreementTemplateType[] getTemplates()
370     {
371         return factory.getTemplates();
372     }
373 
374     /**
375      * {@inheritDoc}
376      */
377     public Negotiation
378         initiateNegotiation( NegotiationContextType context, XmlObject[] criticalExtensions,
379                              XmlObject[] nonCriticalExtensions, Map<String, Object> environment )
380             throws NegotiationFactoryException
381     {
382 
383         Negotiation negotiation =
384             factory.initiateNegotiation( context, criticalExtensions, nonCriticalExtensions, environment );
385 
386         return negotiation;
387     }
388 
389 }