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.text.MessageFormat;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Vector;
42  
43  import org.apache.log4j.Logger;
44  import org.ogf.graap.wsag.api.logging.LogMessage;
45  import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
46  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
47  import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
48  
49  /**
50   * This class serves as a facade for multiple wsag4j engine instances that are deployed in a server. Since
51   * multiple multiple wsag4j engine instances can be deployed in a server this facade allows to list, save and
52   * remove agreement factories via the {@link IAgreementFactoryHome} interface as there would be only one
53   * engine instance.
54   * 
55   * @author owaeld
56   */
57  public class WSAG4JPersistenceFacade implements IAgreementFactoryHome
58  {
59  
60      private static final Logger LOG = Logger.getLogger( WSAG4JPersistenceFacade.class );
61  
62      private WSAG4JEngineConfigurationType[] config = new WSAG4JEngineConfigurationType[] { null };;
63  
64      /**
65       * A map that contains the available persistent agreement factory which are identified by their factory
66       * id.
67       */
68      protected Map<String, PersistentAgreementFactory> persistentFactories =
69          new HashMap<String, PersistentAgreementFactory>();
70  
71      /**
72       * An ordered list with the available agreement factories. The factories are stored in the order they were
73       * retrieved from the persistence layer.
74       */
75      protected List<PersistentAgreementFactory> factoriesOL = new Vector<PersistentAgreementFactory>();
76  
77      /**
78       * Creates a new facade for the given engine configurations.
79       * 
80       * @param config
81       *            the configurations of the available WSAG4J engines
82       */
83      public WSAG4JPersistenceFacade( WSAG4JEngineConfigurationType[] config )
84      {
85          this.config = config;
86      }
87  
88      /**
89       * @see IAgreementFactoryHome#initialize()
90       */
91      public void initialize()
92      {
93          persistentFactories.clear();
94          factoriesOL.clear();
95  
96          for ( int i = 0; i < config.length; i++ )
97          {
98              try
99              {
100                 IAgreementFactoryHome home = loadPersistenceLayer( config[i] );
101                 home.setEngineConfiguration( config[i] );
102                 home.initialize();
103 
104                 PersistentAgreementFactory[] factories = home.list();
105                 for ( int j = 0; j < factories.length; j++ )
106                 {
107                     if ( persistentFactories.containsKey( factories[j].getResourceId() ) )
108                     {
109                         String message1 =
110                             "[duplicated resource id] "
111                                 + "the agreement factory resource id must be unique in a WSAG4J engine.";
112                         LOG.error( message1 );
113 
114                         String message2 =
115                             "[duplicated resource id] the factory with resource id ''{0}'' was not loaded.";
116                         LOG.error( MessageFormat.format( message2,
117                                                          new Object[] { factories[j].getResourceId() } ) );
118                     }
119                     else
120                     {
121                         persistentFactories.put( factories[j].getResourceId(), factories[j] );
122                         factoriesOL.add( factories[j] );
123                     }
124                 }
125             }
126             catch ( Exception e )
127             {
128                 LOG.error( "error loading persistence layer", e );
129             }
130         }
131     }
132 
133     private IAgreementFactoryHome loadPersistenceLayer( WSAG4JEngineConfigurationType configuration )
134         throws Exception
135     {
136         String implementationClass =
137             configuration.getFactory().getPersistenceImplementation().getImplementationClass();
138 
139         if ( LOG.isDebugEnabled() )
140         {
141             LOG.debug( LogMessage.getMessage( "Create ''{0}'' instance as persistence layer.",
142                 implementationClass ) );
143         }
144 
145         try
146         {
147             Object instance = Class.forName( implementationClass ).newInstance();
148 
149             if ( instance instanceof IAgreementFactoryHome )
150             {
151                 if ( LOG.isDebugEnabled() )
152                 {
153                     LOG.debug( "Persistence layer instance created and loaded." );
154                 }
155 
156                 return (IAgreementFactoryHome) instance;
157             }
158             else
159             {
160                 throw new Exception(
161                     MessageFormat.format(
162                         "Error loading WSAG4J persistence layer. Class [{0}] does not implement interface [{1}].",
163                         implementationClass, IAgreementFactoryHome.class.getName() ) );
164             }
165         }
166         catch ( ClassNotFoundException e )
167         {
168             throw new Exception( MessageFormat.format(
169                 "Error loading WSAG4J persistence layer. Class [{0}] not found. Error: {1}",
170                 implementationClass, e.getMessage() ) );
171         }
172         catch ( InstantiationException e )
173         {
174             throw new Exception( MessageFormat.format(
175                 "Error loading WSAG4J persistence layer. Class [{0}] could not be instantiated. Error: {1}",
176                 implementationClass, e.getMessage() ) );
177         }
178         catch ( IllegalAccessException e )
179         {
180             throw new Exception( MessageFormat.format(
181                 "Error loading WSAG4J persistence layer. Class [{0}] could not be accessed. Error: {1}",
182                 implementationClass, e.getMessage() ) );
183         }
184     }
185 
186     /**
187      * looks up a factory with the given id.
188      * 
189      * @param factoryId
190      *            the id of the factory to find
191      * 
192      * @return the factory
193      * 
194      * @throws Exception
195      *             an error occurred while looking up the factory.
196      * 
197      * @see IAgreementFactoryHome#find(String)
198      */
199     public PersistentAgreementFactory find( String factoryId ) throws Exception
200     {
201         if ( persistentFactories.containsKey( factoryId ) )
202         {
203             return persistentFactories.get( factoryId );
204         }
205 
206         return null;
207     }
208 
209     /**
210      * @return all available persistent factories
211      * 
212      * @throws Exception
213      *             indicates an error while generating the factory list
214      * 
215      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#list()
216      */
217     public PersistentAgreementFactory[] list() throws Exception
218     {
219         return factoriesOL.toArray( new PersistentAgreementFactory[persistentFactories.size()] );
220     }
221 
222     /**
223      * @param factoryId
224      *            removes the factory with the given id from the persistence layer
225      * 
226      * @throws Exception
227      *             an error occurred while removing the factory
228      * 
229      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#remove(java.lang.String)
230      */
231     public void remove( String factoryId ) throws Exception
232     {
233         if ( persistentFactories.containsKey( factoryId ) )
234         {
235             factoriesOL.remove( persistentFactories.get( factoryId ) );
236             persistentFactories.remove( factoryId );
237         }
238     }
239 
240     /**
241      * Saves all factories.
242      * 
243      * @throws Exception
244      *             indicates an error while saving the factories
245      */
246     public void save() throws Exception
247     {
248         PersistentAgreementFactory[] factories = list();
249         for ( int i = 0; i < factories.length; i++ )
250         {
251             factories[i].save();
252         }
253     }
254 
255     /**
256      * @param configs
257      *            the engine configuration to set
258      * 
259      * @see org.ogf.graap.wsag.server.api.IEngineComponent#setEngineConfiguration(org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType)
260      */
261     public void setEngineConfiguration( WSAG4JEngineConfigurationType configs )
262     {
263         this.config = new WSAG4JEngineConfigurationType[] { configs };
264     }
265 
266     /**
267      * @return the engine configuration
268      * @see org.ogf.graap.wsag.server.api.IEngineComponent#getEngineConfiguration()
269      */
270     public WSAG4JEngineConfigurationType getEngineConfiguration()
271     {
272         return config[0];
273     }
274 
275     /**
276      * @param factories
277      *            the factories to save
278      * 
279      * @throws Exception
280      *             indicates an error while saving the factories
281      * 
282      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#saveAgreementFactories(org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory[])
283      */
284     public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
285     {
286         for ( int i = 0; i < factories.length; i++ )
287         {
288             factories[i].save();
289         }
290     }
291 
292 }