View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, 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.text.MessageFormat;
38  import java.util.UUID;
39  
40  import org.apache.log4j.Logger;
41  import org.ogf.graap.wsag.api.logging.LogMessage;
42  import org.ogf.graap.wsag.server.api.IAgreementFactory;
43  import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
44  import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
45  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
46  import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
47  
48  /**
49   * @author T.Weuffel
50   */
51  public class DatabaseWSAG4JPersistence extends AbstractWSAG4JPersistence
52  {
53  
54      private static final Logger LOG = Logger.getLogger( DatabaseWSAG4JPersistence.class );
55  
56      /**
57       * The wsag4j engine configuration.
58       */
59      protected WSAG4JEngineConfigurationType[] config;
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.ogf.graap.wsag.server.persistence.impl.AbstractWSAG4JPersistence#doLoad()
65       */
66      @Override
67      protected PersistentAgreementFactory[] doLoad() throws PersistedResourceException
68      {
69  
70          if ( LOG.isDebugEnabled() )
71          {
72              LOG.debug( "Loading DatabaseWSAG4JPersistence instance" );
73          }
74  
75          AbstractPersistentAgreementFactory persistentFactory = null;
76  
77          //
78          // load the implementation specified in the wsag4j.properties file here
79          // initialize it, and if feasible, set the configuration data
80          //
81  
82          try
83          {
84              //
85              // initialize the factory prototype based on the configuration
86              //
87              IAgreementFactory factory = getAgreementFactoryPrototype( wsag4jConfiguration );
88              factory.initialize();
89  
90              //
91              // create the persistent factory
92              //
93              persistentFactory = new DatabasePersistentAgreementFactory( factory );
94  
95              //
96              // check if the factory has a valid resource id
97              //
98              if ( wsag4jConfiguration.isSetResourceId() )
99              {
100                 persistentFactory.setResourceId( wsag4jConfiguration.getResourceId() );
101             }
102             else
103             {
104                 persistentFactory.setResourceId( UUID.randomUUID().toString() );
105                 LOG.error( "agreement factory id not set in configuration file" );
106                 LOG.error( "generate random agreement factory id " );
107                 LOG.error( "agreement persistence will be disabled" );
108             }
109 
110             //
111             // load the active agreements for this factory
112             //
113             persistentFactory.load();
114 
115             if ( LOG.isDebugEnabled() )
116             {
117                 LOG.debug( "wsag4jConfiguration.getResourceId(): " + wsag4jConfiguration.getResourceId() );
118             }
119 
120         }
121         catch ( Exception ex )
122         {
123             String message =
124                 MessageFormat.format(
125                     "Could not load WSAG4J factory instance ''{0}''. Ignoring this instance.",
126                     new Object[] { wsag4jConfiguration.getResourceId() } );
127             throw new PersistedResourceException( message, ex );
128         }
129 
130         if ( LOG.isDebugEnabled() )
131         {
132             LOG.debug( "DatabaseWSAG4JPersistence initialized." );
133         }
134 
135         return new PersistentAgreementFactory[] { persistentFactory };
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public boolean doRemove( PersistentAgreementFactory factory ) throws PersistedResourceException
143     {
144         throw new PersistedResourceException( "Operation not supported for WSAG4J database persistence." );
145     }
146 
147     /**
148      * {@inheritDoc}
149      * 
150      * @deprecated
151      */
152     @Deprecated
153     public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
154     {
155         if ( LOG.isDebugEnabled() )
156         {
157             LOG.debug( LogMessage.getMessage(
158                 "Try to save all agreements for the {0} specified agreement factories.", factories.length ) );
159         }
160 
161         //
162         // iterate over all agreement factories and try to persist the separate agreements (per factory)
163         //
164         // TODO: use agreement home to list existing agreements for a factory
165         //
166         for ( PersistentAgreementFactory persistentAgreementFactory : factories )
167         {
168             for ( PersistentAgreement persistentAgreement : persistentAgreementFactory.list() )
169             {
170                 try
171                 {
172                     // delegate the save operation to the agreement itself
173                     persistentAgreement.save();
174                 }
175                 catch ( Exception ex )
176                 {
177                     LOG.error( LogMessage.getMessage(
178                         "Could not save agreement ''{0}'' for agreement factory ''{1}''.",
179                         persistentAgreement, persistentAgreementFactory.getResourceId() ), ex );
180                 }
181             }
182         }
183 
184         if ( LOG.isDebugEnabled() )
185         {
186             LOG.debug( "Saved all agreements." );
187         }
188     }
189 
190 }