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.text.MessageFormat;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Vector;
41
42 import org.apache.log4j.Logger;
43 import org.ogf.graap.wsag.api.AgreementFactory;
44 import org.ogf.graap.wsag.api.WsagConstants;
45 import org.ogf.graap.wsag.server.api.IAgreementFactory;
46 import org.ogf.graap.wsag.server.api.impl.AgreementFactoryContext;
47 import org.ogf.graap.wsag.server.api.impl.AgreementFactoryFacade;
48 import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
49 import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
50 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
51 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public abstract class AbstractWSAG4JPersistence
69 implements IAgreementFactoryHome
70 {
71
72
73
74
75
76 private static final Logger LOG = Logger.getLogger( AbstractWSAG4JPersistence.class );
77
78
79
80
81 private boolean changed = false;
82
83
84
85
86 protected WSAG4JEngineConfigurationType wsag4jConfiguration;
87
88
89
90
91 private final Vector<PersistentAgreementFactory> persistentFactories =
92 new Vector<PersistentAgreementFactory>();
93
94
95
96
97 private final Map<String, PersistentAgreementFactory> factoriesById =
98 new HashMap<String, PersistentAgreementFactory>();
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 protected abstract PersistentAgreementFactory[] doLoad() throws PersistedResourceException;
116
117
118
119
120
121
122
123
124
125
126
127
128 protected abstract boolean doRemove( PersistentAgreementFactory factory )
129 throws PersistedResourceException;
130
131
132
133
134
135
136
137
138 public WSAG4JEngineConfigurationType getEngineConfiguration()
139 {
140 return wsag4jConfiguration;
141 }
142
143
144
145
146
147 public void setEngineConfiguration( WSAG4JEngineConfigurationType configuration )
148 {
149 this.wsag4jConfiguration = configuration;
150 }
151
152
153
154
155
156
157
158 public boolean hasChanged()
159 {
160 return changed;
161 }
162
163
164
165
166
167
168
169
170
171 public void setChanged( boolean changed )
172 {
173 this.changed = changed;
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 protected IAgreementFactory getAgreementFactoryPrototype( WSAG4JEngineConfigurationType configuration )
190 throws Exception
191 {
192 String implementationClass = null;
193
194 try
195 {
196 implementationClass =
197 configuration.getFactory().getFactoryImplementation().getImplementationClass();
198
199 if ( implementationClass == null )
200 {
201 throw new Exception();
202 }
203 }
204 catch ( Exception ex )
205 {
206 String message =
207 "Error in WSAG4J configuration: "
208 + "Could not load agreement factory implementation from configuration file.";
209 throw new Exception( message, ex );
210 }
211
212 try
213 {
214
215 Object instance = Class.forName( implementationClass ).newInstance();
216
217 if ( instance instanceof AgreementFactory )
218 {
219 AgreementFactory impl = (AgreementFactory) instance;
220
221 IAgreementFactory factory = null;
222
223
224
225
226
227
228 if ( impl instanceof IAgreementFactory )
229 {
230 factory = (IAgreementFactory) impl;
231 }
232 else
233 {
234 factory = new AgreementFactoryFacade( impl );
235 }
236
237
238
239
240
241 factory.setFactoryContext( new AgreementFactoryContext( factory ) );
242 factory.setEngineConfiguration( configuration );
243
244
245
246
247
248
249 factory.getFactoryContext().put( WsagConstants.WSAG4J_FACTORY_CONFIGURATION, configuration );
250
251 return factory;
252
253 }
254 else
255 {
256 Object[] filler = new Object[] { implementationClass, IAgreementFactoryHome.class.getName() };
257 String msgLoadPersistenceLayerError =
258 "Error loading WSAG4J persistence layer. Class [{0}] does not implement interface [{1}].";
259 String message = MessageFormat.format( msgLoadPersistenceLayerError, filler );
260 throw new Exception( message );
261 }
262
263 }
264 catch ( ClassNotFoundException e )
265 {
266 String message =
267 MessageFormat.format(
268 "Error loading WSAG4J persistence layer. Class [{0}] not found. Error: {1}",
269 implementationClass, e.getMessage() );
270 throw new Exception( message );
271 }
272 catch ( InstantiationException e )
273 {
274 String message =
275 MessageFormat.format( "Error loading WSAG4J persistence layer. "
276 + "Class [{0}] could not be instantiated. Error: {1}", implementationClass,
277 e.getMessage() );
278 throw new Exception( message );
279 }
280 catch ( IllegalAccessException e )
281 {
282 String message =
283 MessageFormat.format( "Error loading WSAG4J persistence layer. "
284 + "Class [{0}] could not be accessed. Error: {1}", implementationClass, e.getMessage() );
285 throw new Exception( message );
286 }
287
288 }
289
290
291
292
293 public void initialize() throws Exception
294 {
295 synchronized ( this )
296 {
297
298 try
299 {
300 persistentFactories.clear();
301 factoriesById.clear();
302
303 PersistentAgreementFactory[] factories = doLoad();
304
305 for ( int i = 0; i < factories.length; i++ )
306 {
307 persistentFactories.add( factories[i] );
308 factoriesById.put( factories[i].getResourceId(), factories[i] );
309 }
310
311 setChanged( false );
312 }
313 catch ( Exception e )
314 {
315 Object[] filler = new Object[] { e.getMessage() };
316 String message =
317 MessageFormat.format(
318 "Could not load WSAG4J factory instance. Ignoring this instance. Error: {0}", filler );
319 LOG.error( message );
320 }
321
322 }
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336 public PersistentAgreementFactory find( String agreementFactoryId ) throws Exception
337 {
338 if ( hasChanged() )
339 {
340 initialize();
341 }
342
343 PersistentAgreementFactory persistentAgreementFactory = null;
344
345
346 if ( factoriesById.containsKey( agreementFactoryId ) )
347 {
348 persistentAgreementFactory = factoriesById.get( agreementFactoryId );
349 }
350 else
351 {
352 throw new Exception( MessageFormat.format( "No agreement factory with id ''{0}'' found.",
353 agreementFactoryId ) );
354 }
355
356 return persistentAgreementFactory;
357 }
358
359
360
361
362 public PersistentAgreementFactory[] list() throws Exception
363 {
364
365 if ( hasChanged() )
366 {
367 initialize();
368 }
369
370 return persistentFactories.toArray( new PersistentAgreementFactory[persistentFactories.size()] );
371 }
372
373
374
375
376 public void remove( String factoryId ) throws Exception
377 {
378
379 if ( hasChanged() )
380 {
381 initialize();
382 }
383
384 if ( factoriesById.containsKey( factoryId ) )
385 {
386 PersistentAgreementFactory factory = factoriesById.get( factoryId );
387
388 if ( doRemove( factory ) )
389 {
390 persistentFactories.remove( factory );
391 factoriesById.remove( factoryId );
392 }
393 }
394 }
395 }