View Javadoc

1   /* 
2    * Copyright (c) 2012, 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;
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.server.engine.WsagEngine;
45  import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
46  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
47  
48  /**
49   * @author owaeld
50   * 
51   */
52  public class WSAG4JPersistenceFacade
53      implements IAgreementFactoryHome
54  {
55      private static final Logger LOG = Logger.getLogger( WSAG4JPersistenceFacade.class );
56  
57      private WsagEngine[] engines = new WsagEngine[0];
58  
59      /**
60       * A map that contains the available persistent agreement factory which are identified by their factory
61       * id.
62       */
63      protected Map<String, PersistentAgreementFactory> persistentFactories =
64          new HashMap<String, PersistentAgreementFactory>();
65  
66      /**
67       * An ordered list with the available agreement factories. The factories are stored in the order they were
68       * retrieved from the persistence layer.
69       */
70      protected List<PersistentAgreementFactory> factoriesOL = new Vector<PersistentAgreementFactory>();
71  
72      /**
73       * Creates a new facade for the given WSAG4J engines.
74       * 
75       * @param engines
76       *            the available WSAG4J engines
77       */
78      public WSAG4JPersistenceFacade( WsagEngine[] engines )
79      {
80          this.engines = engines;
81      }
82  
83      /**
84       * @see IAgreementFactoryHome#initialize()
85       */
86      public void initialize()
87      {
88          persistentFactories.clear();
89          factoriesOL.clear();
90  
91          for ( int i = 0; i < getEngines().length; i++ )
92          {
93              try
94              {
95                  IAgreementFactoryHome home = getEngines()[i].getPersistenceLayer();
96                  PersistentAgreementFactory[] factories = home.list();
97  
98                  for ( int j = 0; j < factories.length; j++ )
99                  {
100                     if ( persistentFactories.containsKey( factories[j].getResourceId() ) )
101                     {
102                         String message1 =
103                             "[duplicated resource id] "
104                                 + "the agreement factory resource id must be unique in a WSAG4J engine.";
105                         LOG.error( message1 );
106 
107                         String message2 =
108                             "[duplicated resource id] the factory with resource id ''{0}'' was not loaded.";
109                         LOG.error( MessageFormat.format( message2,
110                             new Object[] { factories[j].getResourceId() } ) );
111                     }
112                     else
113                     {
114                         persistentFactories.put( factories[j].getResourceId(), factories[j] );
115                         factoriesOL.add( factories[j] );
116                     }
117                 }
118             }
119             catch ( Exception e )
120             {
121                 LOG.error( "error loading persistence layer", e );
122             }
123         }
124     }
125 
126     /**
127      * looks up a factory with the given id.
128      * 
129      * @param factoryId
130      *            the id of the factory to find
131      * 
132      * @return the factory
133      * 
134      * @throws Exception
135      *             an error occurred while looking up the factory.
136      * 
137      * @see IAgreementFactoryHome#find(String)
138      */
139     public PersistentAgreementFactory find( String factoryId ) throws Exception
140     {
141         if ( persistentFactories.containsKey( factoryId ) )
142         {
143             return persistentFactories.get( factoryId );
144         }
145 
146         return null;
147     }
148 
149     /**
150      * @return all available persistent factories
151      * 
152      * @throws Exception
153      *             indicates an error while generating the factory list
154      * 
155      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#list()
156      */
157     public PersistentAgreementFactory[] list() throws Exception
158     {
159         return factoriesOL.toArray( new PersistentAgreementFactory[persistentFactories.size()] );
160     }
161 
162     /**
163      * @param factoryId
164      *            removes the factory with the given id from the persistence layer
165      * 
166      * @throws Exception
167      *             an error occurred while removing the factory
168      * 
169      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#remove(java.lang.String)
170      */
171     public void remove( String factoryId ) throws Exception
172     {
173         if ( persistentFactories.containsKey( factoryId ) )
174         {
175             factoriesOL.remove( persistentFactories.get( factoryId ) );
176             persistentFactories.remove( factoryId );
177         }
178     }
179 
180     /**
181      * Saves all factories.
182      * 
183      * @throws Exception
184      *             indicates an error while saving the factories
185      */
186     public void save() throws Exception
187     {
188         PersistentAgreementFactory[] factories = list();
189         for ( int i = 0; i < factories.length; i++ )
190         {
191             factories[i].save();
192         }
193     }
194 
195     /**
196      * @param factories
197      *            the factories to save
198      * 
199      * @throws Exception
200      *             indicates an error while saving the factories
201      * 
202      * @see org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome#saveAgreementFactories(org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory[])
203      */
204     public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
205     {
206         for ( int i = 0; i < factories.length; i++ )
207         {
208             factories[i].save();
209         }
210     }
211 
212     /**
213      * Returns the WSAG4J engine instances used by this facade.
214      * 
215      * @return the engine instances
216      */
217     public WsagEngine[] getEngines()
218     {
219         return engines;
220     }
221 }