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.client.impl;
36  
37  import java.util.Properties;
38  
39  import org.ogf.graap.wsag.api.AgreementOffer;
40  import org.ogf.graap.wsag.api.client.AgreementClient;
41  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
42  import org.ogf.graap.wsag.api.client.AgreementFactoryService;
43  import org.ogf.graap.wsag.api.client.AgreementRegistryService;
44  import org.ogf.graap.wsag.api.client.NegotiationClient;
45  import org.ogf.graap.wsag.api.client.WsClient;
46  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
47  import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
48  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
49  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
50  import org.ogf.graap.wsag.api.security.ISecurityProperties;
51  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
52  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
53  import org.w3.x2005.x08.addressing.EndpointReferenceType;
54  
55  /**
56   * A client side facade to an agreement factory service and the associated agreement registry.
57   * 
58   * @author Oliver Waeldrich
59   * 
60   */
61  public class AgreementFactoryFacade
62      implements AgreementFactoryClient
63  {
64  
65      //
66      // client for invoking WS-Agreement factory methods
67      //
68      private final AgreementFactoryService factoryClient;
69  
70      //
71      // client for invoking WSSRF service group methods
72      //
73      private final AgreementRegistryService registryClient;
74  
75      //
76      // client properties to store security processing results
77      //
78      private Properties properties = new Properties();
79  
80      //
81      // client security properties
82      //
83      private final ISecurityProperties securityProperties;
84  
85      /**
86       * Creates a new instance of the facade.
87       * 
88       * @param factoryClient
89       *            the factory client instance
90       * 
91       * @param registryClient
92       *            the registry client instance
93       * 
94       * @param securityProperties
95       *            the security properties to use
96       */
97      public AgreementFactoryFacade( AgreementFactoryService factoryClient,
98                                     AgreementRegistryService registryClient,
99                                     ISecurityProperties securityProperties )
100     {
101         this.factoryClient = factoryClient;
102         this.registryClient = registryClient;
103         this.securityProperties = securityProperties;
104 
105         factoryClient.getWebServiceClient().setProperties( properties );
106         registryClient.getWebServiceClient().setProperties( properties );
107     }
108 
109     /**
110      * Returns the templates supported by this factory.
111      * 
112      * {@inheritDoc}
113      */
114     public AgreementTemplateType[] getTemplates()
115         throws ResourceUnknownException, ResourceUnavailableException
116     {
117         return factoryClient.getTemplates();
118     }
119 
120     /**
121      * Creates a new agreement instance.
122      * 
123      * @param offer
124      *            the offer to create the agreement for
125      * 
126      * @return the created agreement
127      * 
128      * @throws AgreementFactoryException
129      *             the agreement offer was rejected by the factory
130      * 
131      * @throws ResourceUnavailableException
132      *             the requested agreement factory resource is unavailable
133      * 
134      * @throws ResourceUnknownException
135      *             the requested agreement factory resource is unknown
136      */
137     public AgreementClient createAgreement( AgreementOffer offer )
138         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
139     {
140         return factoryClient.createAgreement( offer );
141     }
142 
143     /**
144      * Creates a new pending agreement instance.
145      * 
146      * {@inheritDoc}
147      */
148     public AgreementClient createPendingAgreement( AgreementOffer offer )
149         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
150     {
151         return factoryClient.createPendingAgreement( offer );
152     }
153 
154     /**
155      * Creates a new pending agreement instance.
156      * 
157      * {@inheritDoc}
158      */
159     public AgreementClient createPendingAgreement( AgreementOffer offer, EndpointReferenceType acceptanceEPR )
160         throws AgreementFactoryException, ResourceUnknownException, ResourceUnavailableException
161     {
162         return factoryClient.createPendingAgreement( offer, acceptanceEPR );
163     }
164 
165     /**
166      * Initiates a Negotiation.
167      * 
168      * {@inheritDoc}
169      */
170     public NegotiationClient initiateNegotiation( NegotiationContextType context )
171         throws NegotiationFactoryException, ResourceUnknownException, ResourceUnavailableException
172     {
173         return factoryClient.initiateNegotiation( context );
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     public String getResourceId() throws ResourceUnknownException, ResourceUnavailableException
180     {
181         return factoryClient.getResourceId();
182     }
183 
184     /**
185      * 
186      * @return a template with the given name and id, or null if no appropriate template was found.
187      * 
188      *         {@inheritDoc}
189      */
190     public AgreementTemplateType getTemplate( String name, String id )
191         throws ResourceUnknownException, ResourceUnavailableException
192     {
193         if ( name == null )
194         {
195             return null;
196         }
197 
198         id = ( id != null ) ? id : "";
199 
200         AgreementTemplateType[] templates = getTemplates();
201 
202         for ( int i = 0; i < templates.length; i++ )
203         {
204             if ( name.equalsIgnoreCase( templates[i].getName() ) )
205             {
206 
207                 String actualId =
208                     ( templates[i].getTemplateId() != null ) ? templates[i].getTemplateId() : "";
209 
210                 if ( id.equalsIgnoreCase( actualId ) )
211                 {
212                     return templates[i];
213                 }
214             }
215         }
216 
217         return null;
218     }
219 
220     /**
221      * Creates s copy of this AgreementFactoryClient.
222      * 
223      * @return the cloned agreement factory client
224      * 
225      * @throws CloneNotSupportedException
226      *             this particular client implementation does not support cloning
227      */
228     @Override
229     public AgreementFactoryClient clone() throws CloneNotSupportedException
230     {
231         return new AgreementFactoryFacade( factoryClient, registryClient, securityProperties.clone() );
232     }
233 
234     /**
235      * Lists all agreements of this factory.
236      * 
237      * @return all agreement clients known by this factory
238      * 
239      * @throws ResourceUnavailableException
240      *             the requested agreement factory resource is unavailable
241      * 
242      * @throws ResourceUnknownException
243      *             the requested agreement factory resource is unknown
244      */
245     public AgreementClient[] listAgreements() throws ResourceUnknownException, ResourceUnavailableException
246     {
247         return registryClient.listAgreements();
248     }
249 
250     /**
251      * Turns trace on/off.
252      * 
253      * @param trace
254      *            turns trace on/off.
255      */
256     public void setTrace( boolean trace )
257     {
258         factoryClient.getWebServiceClient().setTrace( trace );
259         registryClient.getWebServiceClient().setTrace( trace );
260     }
261 
262     /**
263      * @return the trace level
264      */
265     public boolean isUsingTrace()
266     {
267         return factoryClient.getWebServiceClient().isUsingTrace()
268             && registryClient.getWebServiceClient().isUsingTrace();
269     }
270 
271     /**
272      * @return Returns the properties.
273      */
274     public Properties getProperties()
275     {
276         return properties;
277     }
278 
279     /**
280      * @param properties
281      *            The properties to set.
282      */
283     public void setProperties( Properties properties )
284     {
285         this.properties = properties;
286         factoryClient.getWebServiceClient().setProperties( properties );
287         registryClient.getWebServiceClient().setProperties( properties );
288     }
289 
290     /**
291      * @return Returns the endpoint reference for the service.
292      */
293     public EndpointReferenceType getEndpoint()
294     {
295         return factoryClient.getWebServiceClient().getEndpoint();
296     }
297 
298     /**
299      * @return the securityProperties
300      */
301     public ISecurityProperties getSecurityProperties()
302     {
303         return securityProperties;
304     }
305 
306     /**
307      * @return the webservice client.
308      */
309     public WsClient getWebServiceClient()
310     {
311         return this;
312     }
313 
314     /**
315      * @return Returns the agreement factory client.
316      */
317     public AgreementFactoryService getAgreementFactoryClient()
318     {
319         return factoryClient;
320     }
321 
322     /**
323      * @return Returns the agreement registry client.
324      */
325     public AgreementRegistryService getAgreementRegistryClient()
326     {
327         return registryClient;
328     }
329 }