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.server.monitoring;
36
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.Vector;
40
41 import org.apache.xmlbeans.XmlObject;
42 import org.ogf.graap.wsag.server.accounting.IAccountingSystem;
43 import org.ogf.graap.wsag.server.accounting.SimpleAccountingSystemLogger;
44 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateDefinition;
45 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateDocument;
46 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
47
48 /**
49 * MonitoringContext
50 *
51 * @author Oliver Waeldrich
52 *
53 */
54 public class MonitoringContext implements IMonitoringContext
55 {
56
57 private Vector<ServiceTermStateType> serviceTermStates = new Vector<ServiceTermStateType>();
58
59 private Vector<IServiceTermMonitoringHandler> monitoringHandler =
60 new Vector<IServiceTermMonitoringHandler>();
61
62 private IAccountingSystem accountingSystem = new SimpleAccountingSystemLogger();
63
64 /**
65 * Maps to {@link org.ogf.graap.wsag.api.types.AbstractAgreementType#getExecutionContext()} .
66 */
67 private HashMap<String, XmlObject> executionProperties = new HashMap<String, XmlObject>();
68
69 /**
70 * Maps to {@link org.ogf.graap.wsag.api.types.AbstractAgreementType#getTransientExecutionContext()} .
71 */
72 private Map<String, Object> transientExecutionProperties = new HashMap<String, Object>();
73
74 /**
75 * Adds a new service term state with the given name to the state monitor. The state is initialized as
76 * NOT_READY.
77 *
78 * @param name
79 * the name of the state the state to add
80 */
81 public void addServiceTemState( String name )
82 {
83 ServiceTermStateType state = ServiceTermStateDocument.Factory.newInstance().addNewServiceTermState();
84 state.setTermName( name );
85 state.setState( ServiceTermStateDefinition.NOT_READY );
86 addServiceTemState( state );
87 }
88
89 /**
90 * Adds a new service term state to the state monitor.
91 *
92 * @param state
93 * the state to add
94 */
95 public void addServiceTemState( ServiceTermStateType state )
96 {
97 serviceTermStates.add( state );
98 }
99
100 /**
101 * Returns an array of the registered service term states
102 *
103 * @return the registered service term states
104 */
105 public ServiceTermStateType[] getServiceTermStates()
106 {
107 return serviceTermStates.toArray( new ServiceTermStateType[serviceTermStates.size()] );
108 }
109
110 /**
111 * Removes a service term state from the state monitor.
112 *
113 * @param state
114 * the state to remove
115 */
116 public void removeServiceTemState( ServiceTermStateType state )
117 {
118 serviceTermStates.remove( state );
119 }
120
121 /**
122 * {@inheritDoc}
123 *
124 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#setServiceTemState(ServiceTermStateType[])
125 */
126 public void setServiceTemState( ServiceTermStateType[] states )
127 {
128 serviceTermStates.clear();
129 for ( int i = 0; i < states.length; i++ )
130 {
131 serviceTermStates.add( states[i] );
132 }
133 }
134
135 /**
136 * Returns the service term state with the given name, or null if no term state with this name is
137 * registered.
138 *
139 * @param name
140 * the name of the service term state
141 * @return the service term state with the given name
142 */
143 public ServiceTermStateType getServiceTermStateByName( String name )
144 {
145 for ( int i = 0; i < serviceTermStates.size(); i++ )
146 {
147 ServiceTermStateType termState = serviceTermStates.get( i );
148
149 if ( name.equals( termState.getTermName() ) )
150 {
151 return termState;
152 }
153 }
154
155 return null;
156 }
157
158 /**
159 * {@inheritDoc}
160 *
161 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#getProperties()
162 */
163 public Map<String, XmlObject> getProperties()
164 {
165 return executionProperties;
166 }
167
168 /**
169 * {@inheritDoc}
170 *
171 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#setProperties(java.util.Map)
172 */
173 public void setProperties( Map<String, XmlObject> properties )
174 {
175 executionProperties.clear();
176 executionProperties.putAll( properties );
177 }
178
179 /**
180 * {@inheritDoc}
181 *
182 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#getTransientProperties()
183 */
184 public Map<String, Object> getTransientProperties()
185 {
186 return transientExecutionProperties;
187 }
188
189 /**
190 * {@inheritDoc}
191 *
192 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#setTransientProperties(java.util.Map)
193 */
194 public void setTransientProperties( Map<String, Object> properties )
195 {
196 transientExecutionProperties = properties;
197 }
198
199 /**
200 * {@inheritDoc}
201 *
202 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#addMonitoringHandler(IServiceTermMonitoringHandler)
203 */
204 public void addMonitoringHandler( IServiceTermMonitoringHandler handler )
205 {
206 monitoringHandler.add( handler );
207 }
208
209 /**
210 * {@inheritDoc}
211 *
212 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#getMonitoringHandler()
213 */
214 public IServiceTermMonitoringHandler[] getMonitoringHandler()
215 {
216 return monitoringHandler.toArray( new IServiceTermMonitoringHandler[monitoringHandler.size()] );
217 }
218
219 /**
220 * {@inheritDoc}
221 *
222 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#removeMonitoringHandler(IServiceTermMonitoringHandler)
223 */
224 public void removeMonitoringHandler( IServiceTermMonitoringHandler handler )
225 {
226 monitoringHandler.remove( handler );
227 }
228
229 /**
230 * {@inheritDoc}
231 *
232 * @see org.ogf.graap.wsag.server.monitoring.IMonitoringContext#setMonitoringHandler(IServiceTermMonitoringHandler[])
233 */
234 public void setMonitoringHandler( IServiceTermMonitoringHandler[] handler )
235 {
236 monitoringHandler.clear();
237 for ( int i = 0; i < handler.length; i++ )
238 {
239 monitoringHandler.add( handler[i] );
240 }
241 }
242
243 /**
244 * {@inheritDoc}
245 *
246 * @see java.util.HashMap#clone()
247 */
248 public Object clone()
249 {
250 IMonitoringContext currentMonitoringContext = new MonitoringContext();
251
252 currentMonitoringContext.setProperties( getProperties() );
253 currentMonitoringContext.setTransientProperties( getTransientProperties() );
254
255 for ( int i = 0; i < serviceTermStates.size(); i++ )
256 {
257 ServiceTermStateType state = (ServiceTermStateType) serviceTermStates.get( i );
258 currentMonitoringContext.addServiceTemState( (ServiceTermStateType) state.copy() );
259 }
260
261 for ( int i = 0; i < monitoringHandler.size(); i++ )
262 {
263 IServiceTermMonitoringHandler handler = (IServiceTermMonitoringHandler) monitoringHandler.get( i );
264 currentMonitoringContext.addMonitoringHandler( handler );
265 }
266
267 currentMonitoringContext.setAccountingSystem( getAccountingSystem() );
268
269 return currentMonitoringContext;
270 }
271
272 /**
273 * Sets the accounting system.
274 *
275 * @param accountingSystem
276 * the accountingSystem to set
277 */
278 public void setAccountingSystem( IAccountingSystem accountingSystem )
279 {
280 this.accountingSystem = accountingSystem;
281 }
282
283 /**
284 * @return the accountingSystem
285 */
286 public IAccountingSystem getAccountingSystem()
287 {
288 return accountingSystem;
289 }
290
291 }