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 public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException 71 { 72 try 73 { 74 // 75 // Creates a SampleAgreementOffer from a offer. 76 // getting Resources and Time Constraints information for a domain specific processing 77 // and decides upon accepting/rejecting the agreement offer based on the 78 // current availability of the resources within a required time frame 79 // 80 SampleAgreementOffer negotiatedOffer = new SampleAgreementOffer( offer.getXMLObject() ); 81 if ( log.isTraceEnabled() ) 82 { 83 log.trace( "resource requirements in offer: " 84 + negotiatedOffer.getResourceDefinition().xmlText() ); 85 log.trace( "time constraint in offer: " + negotiatedOffer.getTimeConstraint().xmlText() ); 86 } 87 88 // 89 // create a new instance of a (negotiated) domain-specific agreement implementation 90 // 91 SampleAgreement negotiatedAgreement = new SampleAgreement( offer ); 92 93 // 94 // create an instance of a monitored agreement 95 // 96 MonitorableAgreement monitorAgreement = new MonitorableAgreement( negotiatedAgreement ); 97 98 // 99 // set monitoring interval to 30 seconds, monitoring handler(s) are invoked once each monitoring 100 // cycle 101 // 102 monitorAgreement.setCronExpression( "0/30 * * * * ?" ); 103 104 // 105 // add a custom monitoring handler 106 // 107 monitorAgreement.addMonitoringHandler( new SampleSDTMonitor() ); 108 109 // 110 // add the agreement offer to the execution context so that monitoring handler can access it later 111 // on 112 // 113 Map<String, XmlObject> executionProperties = 114 monitorAgreement.getExecutionContext().getExecutionProperties(); 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 public void initialize() throws ActionInitializationException 139 { 140 // nothing to do here 141 } 142 143 }