1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.server.persistence.impl;
36
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Vector;
41
42 import org.apache.log4j.Logger;
43 import org.apache.xmlbeans.XmlObject;
44 import org.ogf.graap.wsag.api.Agreement;
45 import org.ogf.graap.wsag.api.AgreementFactory;
46 import org.ogf.graap.wsag.api.AgreementOffer;
47 import org.ogf.graap.wsag.api.Negotiation;
48 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
49 import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
50 import org.ogf.graap.wsag.api.logging.LogMessage;
51 import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
52 import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
53 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
54 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
55 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
56 import org.w3.x2005.x08.addressing.EndpointReferenceType;
57
58
59
60
61
62
63
64
65 public abstract class AbstractPersistentAgreementFactory
66 implements PersistentAgreementFactory
67 {
68
69 private static final Logger LOG = Logger.getLogger( SimplePersistentAgreementFactory.class );
70
71
72
73
74 protected AgreementFactory factory;
75
76
77
78
79 protected String resourceId;
80
81 private final List<PersistentAgreement> activeAgreements = new Vector<PersistentAgreement>();
82
83
84
85
86
87
88 public AbstractPersistentAgreementFactory( AgreementFactory factory )
89 {
90 this.factory = factory;
91 }
92
93
94
95
96 public AgreementFactory getAgreementFactory()
97 {
98 return factory;
99 }
100
101
102
103
104
105
106
107
108
109
110
111 protected abstract PersistentAgreement persistAgreement( Agreement agreement )
112 throws PersistedResourceException;
113
114
115
116
117
118
119
120
121
122 protected abstract PersistentAgreement[] doLoad() throws PersistedResourceException;
123
124
125
126
127
128
129
130
131
132
133 protected abstract void doRemove( PersistentAgreement toRemove ) throws PersistedResourceException;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
150 {
151 Agreement agreement = factory.createAgreement( offer );
152
153
154 PersistentAgreement persistentAgreement;
155
156 try
157 {
158 persistentAgreement = persistAgreement( agreement );
159 activeAgreements.add( persistentAgreement );
160 }
161 catch ( PersistedResourceException e )
162 {
163 throw new AgreementFactoryException( "Failed to persist agreement.", e );
164 }
165
166
167 String agreementId = agreement.getAgreementId();
168
169 try
170 {
171 persistentAgreement.save();
172 }
173 catch ( Exception ex )
174 {
175 LOG.error( LogMessage.getMessage( "Could not save the new agreement ''{0}''.", agreementId ), ex );
176 }
177
178 return agreement;
179 }
180
181
182
183
184
185
186
187 public void addAgreement( Agreement agreement, EndpointReferenceType agreementEpr )
188 {
189
190
191 try
192 {
193 PersistentAgreement persisted = persistAgreement( agreement );
194 activeAgreements.add( persisted );
195 }
196 catch ( Exception ex )
197 {
198 LOG.error(
199 LogMessage.getMessage( "Could not persist agreement ''{0}''.", agreement.getAgreementId() ),
200 ex );
201 }
202 }
203
204
205
206
207
208
209 public void load() throws Exception
210 {
211 activeAgreements.clear();
212
213 PersistentAgreement[] agreements = doLoad();
214
215
216 for ( int i = 0; i < agreements.length; i++ )
217 {
218 activeAgreements.add( agreements[i] );
219 }
220 }
221
222
223
224
225
226
227 public void save() throws Exception
228 {
229 Iterator<PersistentAgreement> it = activeAgreements.iterator();
230 while ( it.hasNext() )
231 {
232 PersistentAgreement persistentAgreement = it.next();
233 persistentAgreement.save();
234 }
235 }
236
237
238
239
240
241
242 public PersistentAgreement[] list()
243 {
244
245 return activeAgreements.toArray( new PersistentAgreement[activeAgreements.size()] );
246 }
247
248
249
250
251
252
253 public PersistentAgreement[] list( String agreementFactoryId ) throws Exception
254 {
255 if ( resourceId.equals( agreementFactoryId ) )
256 {
257 return list();
258 }
259 else
260 {
261 return new PersistentAgreement[0];
262 }
263 }
264
265
266
267
268
269
270 public PersistentAgreement find( String agreementId ) throws PersistedResourceException
271 {
272 LOG.debug( LogMessage.getMessage( "Try to find a agreement with id ''{0}''.", agreementId ) );
273
274 synchronized ( activeAgreements )
275 {
276 for ( Iterator<PersistentAgreement> iterator = activeAgreements.iterator(); iterator.hasNext(); )
277 {
278 PersistentAgreement actual = iterator.next();
279
280 if ( actual.getAgreement().getAgreementId().equals( agreementId ) )
281 {
282 return actual;
283 }
284 }
285 }
286
287 String msgText = "agreement with id ''{0}'' was not found at factory ''{1}''.";
288 String error = LogMessage.format( msgText, agreementId, getResourceId() );
289 throw new PersistedResourceException( error );
290 }
291
292
293
294
295
296
297 public void remove( String agreementId ) throws PersistedResourceException
298 {
299 LOG.debug( LogMessage.getMessage( "Remove agreement with id''{0}''.", agreementId ) );
300
301
302
303
304 PersistentAgreement toRemove = null;
305 Iterator<PersistentAgreement> it = activeAgreements.iterator();
306 while ( it.hasNext() )
307 {
308 PersistentAgreement persistentAgreement = it.next();
309 if ( persistentAgreement.getAgreement().getAgreementId().equals( agreementId ) )
310 {
311 toRemove = persistentAgreement;
312 break;
313 }
314 }
315
316 if ( toRemove == null )
317 {
318
319
320
321 String msgText = "The agreement with id ''{0}'' does not exist at factory ''{1}''";
322 LOG.debug( LogMessage.getMessage( msgText, agreementId, getResourceId() ) );
323
324 String removeTxt = "Agreement ''{0}'' was not removed. Agreement does not exist in factory.";
325 throw new PersistedResourceException( LogMessage.format( removeTxt, agreementId ) );
326 }
327
328 try
329 {
330 synchronized ( activeAgreements )
331 {
332 doRemove( toRemove );
333 activeAgreements.remove( toRemove );
334 }
335 }
336 catch ( Exception e )
337 {
338 LOG.error( LogMessage.getMessage( "Agreement ''{0}'' could not be removed.", agreementId ), e );
339 }
340
341 LOG.debug( LogMessage.getMessage( "Agreement ''{0}'' removed.", agreementId ) );
342 }
343
344
345
346
347 public String getResourceId()
348 {
349 return resourceId;
350 }
351
352
353
354
355
356 public void setResourceId( String resourceId )
357 {
358 this.resourceId = resourceId;
359
360 String message = "Replaced generated unique resource id with set resource id ''{0}''.";
361 LOG.debug( LogMessage.getMessage( message, resourceId ) );
362 }
363
364
365
366
367
368
369 public AgreementTemplateType[] getTemplates()
370 {
371 return factory.getTemplates();
372 }
373
374
375
376
377 public Negotiation
378 initiateNegotiation( NegotiationContextType context, XmlObject[] criticalExtensions,
379 XmlObject[] nonCriticalExtensions, Map<String, Object> environment )
380 throws NegotiationFactoryException
381 {
382
383 Negotiation negotiation =
384 factory.initiateNegotiation( context, criticalExtensions, nonCriticalExtensions, environment );
385
386 return negotiation;
387 }
388
389 }