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.samples.actions;
36  
37  import java.util.Map;
38  
39  import org.apache.log4j.Logger;
40  import org.apache.xmlbeans.XmlObject;
41  import org.ogf.graap.wsag.api.Agreement;
42  import org.ogf.graap.wsag.api.AgreementOffer;
43  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
44  import org.ogf.graap.wsag.server.actions.AbstractCreateAgreementAction;
45  import org.ogf.graap.wsag.server.actions.ActionInitializationException;
46  import org.ogf.graap.wsag.server.monitoring.MonitorableAgreement;
47  
48  /**
49   * SampleCreateAgreementAction
50   * 
51   * @author hrasheed
52   * 
53   */
54  public class SampleCreateAgreementAction extends AbstractCreateAgreementAction
55  {
56  
57      private static Logger log = Logger.getLogger( SampleCreateAgreementAction.class );
58  
59      /**
60       * Key to identify an offer in the agreement execution properties.
61       * 
62       * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#getProperties()
63       * @see org.ogf.graap.wsag.api.types.AbstractAgreementType#getExecutionContext()
64       */
65      public static final String SAMPLE_OFFER = "SAMPLE_OFFER";
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
72      {
73          try
74          {
75              //
76              // Creates a SampleAgreementOffer from a offer.
77              // getting Resources and Time Constraints information for a domain specific processing
78              // and decides upon accepting/rejecting the agreement offer based on the
79              // current availability of the resources within a required time frame
80              //
81              SampleAgreementOffer negotiatedOffer = new SampleAgreementOffer( offer.getXMLObject() );
82              if ( log.isTraceEnabled() )
83              {
84                  log.trace( "resource requirements in offer: "
85                      + negotiatedOffer.getResourceDefinition().xmlText() );
86                  log.trace( "time constraint in offer: " + negotiatedOffer.getTimeConstraint().xmlText() );
87              }
88  
89              //
90              // create a new instance of a (negotiated) domain-specific agreement implementation
91              //
92              SampleAgreement negotiatedAgreement = new SampleAgreement( offer );
93  
94              //
95              // create an instance of a monitored agreement
96              //
97              MonitorableAgreement monitorAgreement = new MonitorableAgreement( negotiatedAgreement );
98  
99              //
100             // set monitoring interval to 30 seconds, monitoring handler(s) are invoked once each monitoring
101             // cycle
102             //
103             monitorAgreement.setCronExpression( "0/30 * * * * ?" );
104 
105             //
106             // add a custom monitoring handler
107             //
108             monitorAgreement.addMonitoringHandler( new SampleSDTMonitor() );
109 
110             //
111             // add the agreement offer to the execution context so that monitoring handler can access it later
112             // on
113             //
114             Map<String, XmlObject> executionProperties = monitorAgreement.getExecutionContext();
115             executionProperties.put( SampleCreateAgreementAction.SAMPLE_OFFER, negotiatedOffer.getXMLObject() );
116 
117             //
118             // start the monitoring process
119             //
120             monitorAgreement.startMonitoring();
121 
122             //
123             // return the monitored agreement
124             //
125             return monitorAgreement;
126 
127         }
128         catch ( Exception e )
129         {
130             log.error( e );
131             throw new AgreementFactoryException( e );
132         }
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public void initialize() throws ActionInitializationException
140     {
141         // nothing to do here
142     }
143 
144 }