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.wsrf.persistence;
36  
37  import java.text.MessageFormat;
38  import java.util.List;
39  import java.util.Vector;
40  
41  import org.apache.log4j.Logger;
42  import org.ogf.graap.wsag.api.WsagConstants;
43  import org.ogf.graap.wsag.server.api.IEngineComponent;
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  import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
48  import org.w3.x2005.x08.addressing.EndpointReferenceType;
49  import org.w3.x2005.x08.addressing.ReferenceParametersType;
50  import org.w3c.dom.Document;
51  import org.w3c.dom.Element;
52  import org.w3c.dom.Node;
53  
54  /**
55   * @author thomasw
56   */
57  public class WsDatabaseWSAG4JPersistence implements IEngineComponent, IWsAgreementFactoryHome
58  {
59  
60      private final Logger log = Logger.getLogger( WsDatabaseWSAG4JPersistence.class );
61  
62      // default resource-id counter
63      private static int counter = 0;
64  
65      private final IAgreementFactoryHome agreementFactoryHome;
66  
67      private List<WsPersistentAgreementFactory> wsFactories;
68  
69      private WSAG4JEngineConfigurationType wsagconfig;
70  
71      /**
72       * 
73       * @param agreementFactoryHome
74       *            the home implementation of the agreement factory
75       * 
76       * @throws Exception
77       */
78      public WsDatabaseWSAG4JPersistence( IAgreementFactoryHome agreementFactoryHome )
79      {
80          super();
81          this.agreementFactoryHome = agreementFactoryHome;
82          wsFactories = new Vector<WsPersistentAgreementFactory>();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public void initialize() throws Exception
89      {
90          wsFactories.clear();
91  
92          // delegate method call to agreement factory home instance
93          PersistentAgreementFactory[] persistentAgreementFactories = agreementFactoryHome.list();
94  
95          // loop over all agreement factories and load the corresponding eprs
96          wsFactories = new Vector<WsPersistentAgreementFactory>();
97  
98          for ( PersistentAgreementFactory persistentAgreementFactory : persistentAgreementFactories )
99          {
100             // wrap agreement factory
101             WsPersistentAgreementFactory wsPersistentAgreementFactory =
102                 new WsDatabaseAgreementFactory( persistentAgreementFactory, getNextEPR() );
103             wsFactories.add( wsPersistentAgreementFactory );
104         }
105 
106         //
107         // log the initialized factories
108         //
109         if ( log.isDebugEnabled() )
110         {
111             log.debug( "Initialize WsDatabaseWSAG4JPersistence instance." );
112 
113             WsPersistentAgreementFactory[] wsPersistentAgreementFactories = list();
114             log.debug( wsPersistentAgreementFactories.length
115                 + " WsPersistentAgreementFactory instances available." );
116 
117             for ( WsPersistentAgreementFactory wsPersistentAgreementFactory : wsPersistentAgreementFactories )
118             {
119                 WsDatabasePersistentAgreement[] wsDatabasePersistentAgreements =
120                     wsPersistentAgreementFactory.list();
121                 log.debug( "    " + wsDatabasePersistentAgreements.length
122                     + " persisted WsDatabasePersistentAgreement instances found." );
123 
124                 for ( WsDatabasePersistentAgreement wsDatabasePersistentAgreement : wsDatabasePersistentAgreements )
125                 {
126                     log.debug( "        " + wsDatabasePersistentAgreement.toString() );
127                 }
128             }
129         }
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public WsPersistentAgreementFactory find( String agreementFactoryId ) throws Exception
136     {
137 
138         log.debug( "WsDatabaseWSAG4JPersistence -> find(String agreementFactoryId)" );
139 
140         // delegate method call to agreement factory home instance
141         PersistentAgreementFactory persistentAgreementFactory =
142             agreementFactoryHome.find( agreementFactoryId );
143 
144         // try to find the corresponding epr
145         EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
146 
147         // wrap agreement factory
148         WsPersistentAgreementFactory wsPersistentAgreementFactory =
149             new WsDatabaseAgreementFactory( persistentAgreementFactory, epr );
150 
151         return wsPersistentAgreementFactory;
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     public WsPersistentAgreementFactory[] list() throws Exception
158     {
159         log.debug( "WsDatabaseWSAG4JPersistence -> list()" );
160 
161         return wsFactories.toArray( new WsPersistentAgreementFactory[wsFactories.size()] );
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     public void saveAgreementFactories( WsPersistentAgreementFactory[] wsPersistentAgreementFactories )
168         throws Exception
169     {
170         log.debug( "WsDatabaseWSAG4JPersistence -> saveAgreementFactories(WsPersistentAgreementFactory[])" );
171 
172         // loop over all ws agreement factories and call the save operation
173         for ( WsPersistentAgreementFactory wsPersistentAgreementFactory : wsPersistentAgreementFactories )
174         {
175             wsPersistentAgreementFactory.save();
176         }
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     public void remove( String agreementFactoryId ) throws Exception
183     {
184         String message = "remove() operation not supported for {0}.";
185         throw new UnsupportedOperationException( MessageFormat.format( message,
186                                                                        new Object[] { getClass().getName() } ) );
187     }
188 
189     /**
190      * @return the next EPR for a factory resource
191      */
192     public EndpointReferenceType getNextEPR()
193     {
194         EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
195         epr.addNewAddress().setStringValue( WsagEngine.getGatewayURL()
196                                                 + WsagConstants.AGREEMENT_FACTORY_SERVICE_URI );
197 
198         ReferenceParametersType ref = epr.addNewReferenceParameters();
199 
200         String value = "WSAG4J_ResourceId_" + counter++;
201 
202         Document doc = ref.getDomNode().getOwnerDocument();
203         Element element =
204             doc.createElementNS( WsagConstants.WSAG4J_RESOURCE_ID_QNAME.getNamespaceURI(),
205                                  WsagConstants.WSAG4J_RESOURCE_ID_QNAME.getLocalPart() );
206 
207         element.appendChild( element.getOwnerDocument().createTextNode( value ) );
208 
209         Node imported = doc.importNode( element, true );
210         ref.getDomNode().appendChild( imported );
211 
212         return epr;
213     }
214 
215     /**
216      * {@inheritDoc}
217      * 
218      * @see org.ogf.graap.wsag.server.api.IEngineComponent#setEngineConfiguration(WSAG4JEngineConfigurationType)
219      */
220     public void setEngineConfiguration( WSAG4JEngineConfigurationType config )
221     {
222         wsagconfig = config;
223     }
224 
225     /**
226      * {@inheritDoc}
227      * 
228      * @see org.ogf.graap.wsag.server.api.IEngineComponent#getEngineConfiguration()
229      */
230     public WSAG4JEngineConfigurationType getEngineConfiguration()
231     {
232         return wsagconfig;
233     }
234 }