View Javadoc

1   /* 
2    * Copyright (c) 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.samples;
36  
37  import java.util.Map;
38  
39  import org.apache.log4j.Logger;
40  import org.apache.xmlbeans.XmlObject;
41  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionType;
42  import org.ogf.graap.wsag.api.Agreement;
43  import org.ogf.graap.wsag.api.AgreementFactory;
44  import org.ogf.graap.wsag.api.AgreementOffer;
45  import org.ogf.graap.wsag.api.Negotiation;
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.types.AbstractAgreementFactoryType;
49  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
50  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
51  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
52  import org.w3c.dom.Node;
53  
54  /**
55   * Sample implementation of an agreement factory.
56   * 
57   * @author Oliver Waeldrich
58   * 
59   * @deprecated the framework is customized by agreement actions only
60   */
61  @Deprecated
62  public class SampleAgreementFactory extends AbstractAgreementFactoryType
63      implements AgreementFactory
64  {
65  
66      private static Logger log = Logger.getLogger( SampleAgreementFactory.class );
67  
68      /**
69       * default constructor
70       */
71      public SampleAgreementFactory()
72      {
73          super();
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public AgreementTemplateType[] getTemplates()
81      {
82          // by overwriting this method you can include custom code to be executed
83          // when a WSRF#GetResourceProperty request occurs
84          AgreementTemplateType template1 = AgreementTemplateType.Factory.newInstance();
85          template1.setTemplateId( "SampleComputeResources" );
86          template1.setAgreementId( "SampleAgreementId_1" );
87          template1.addNewContext();
88          template1.addNewCreationConstraints();
89          ServiceDescriptionTermType sdt = template1.addNewTerms().addNewAll().addNewServiceDescriptionTerm();
90  
91          // we set name and service name of the SDT
92          sdt.setName( "sample_service_description_term" );
93          sdt.setServiceName( "sample_service" );
94  
95          // we create the SDT content (must not be null, see schema)
96          JobDefinitionType jdef = JobDefinitionType.Factory.newInstance();
97          jdef.addNewJobDescription().addNewResources().addNewCandidateHosts().addHostName( "some_host" );
98  
99          Node sdtDoc = sdt.getDomNode();
100         Node imported = sdtDoc.getOwnerDocument().importNode( jdef.getDomNode(), true );
101         sdtDoc.appendChild( imported );
102 
103         AgreementTemplateType template2 = AgreementTemplateType.Factory.newInstance();
104         template2.setTemplateId( "SampleOtherResources" );
105         template2.setAgreementId( "SampleAgreementId_2" );
106         template2.addNewContext();
107         template2.addNewCreationConstraints();
108         sdt = template2.addNewTerms().addNewAll().addNewServiceDescriptionTerm();
109 
110         // just import the JSDL document to the second SDT as well
111         sdtDoc = sdt.getDomNode();
112         imported = sdtDoc.getOwnerDocument().importNode( jdef.getDomNode(), true );
113         sdtDoc.appendChild( imported );
114 
115         return new AgreementTemplateType[] { template1, template2 };
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
122     {
123 
124         SampleAgreement agreement = new SampleAgreement( offer );
125 
126         // we have to check if terms are set
127         if ( ( offer.getTerms() != null ) && ( offer.getTerms().getAll() != null ) )
128         {
129 
130             agreement.setTerms( offer.getTerms() );
131 
132             // now we get the SDTs
133             ServiceDescriptionTermType[] sdts = offer.getTerms().getAll().getServiceDescriptionTermArray();
134             if ( ( sdts != null ) && ( sdts.length == 1 ) && ( sdts[0].getName().equals( "TEST_EXCEPTION" ) ) )
135             {
136                 String message = "No ServiceDescriptionTerms defined.";
137                 log.error( message );
138 
139                 // if found a SDT named TEST_EXCEPTION, we fire the Exception
140                 throw new AgreementFactoryException( message );
141             }
142 
143         }
144 
145         if ( offer.getContext() != null )
146         {
147             agreement.setContext( offer.getContext() );
148         }
149 
150         return agreement;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     public Negotiation
157         initiateNegotiation( NegotiationContextType context, XmlObject[] criticalExtensions,
158                              XmlObject[] nonCriticalExtensions, Map<String, Object> environment )
159             throws NegotiationFactoryException
160     {
161 
162         /*
163          * Include example for instantiating a new negotiation. This is supported by default by the
164          * GenericAgreementFactory implementation.
165          */
166         throw new UnsupportedOperationException();
167     }
168 
169 }