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.List;
40 import java.util.Map;
41 import java.util.Vector;
42
43 import org.apache.log4j.Logger;
44 import org.ogf.graap.wsag.api.logging.LogMessage;
45 import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
46 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
47 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
48
49
50
51
52
53
54
55
56
57 public class WSAG4JPersistenceFacade implements IAgreementFactoryHome
58 {
59
60 private static final Logger LOG = Logger.getLogger( WSAG4JPersistenceFacade.class );
61
62 private WSAG4JEngineConfigurationType[] config = new WSAG4JEngineConfigurationType[] { null };;
63
64
65
66
67
68 protected Map<String, PersistentAgreementFactory> persistentFactories =
69 new HashMap<String, PersistentAgreementFactory>();
70
71
72
73
74
75 protected List<PersistentAgreementFactory> factoriesOL = new Vector<PersistentAgreementFactory>();
76
77
78
79
80
81
82
83 public WSAG4JPersistenceFacade( WSAG4JEngineConfigurationType[] config )
84 {
85 this.config = config;
86 }
87
88
89
90
91 public void initialize()
92 {
93 persistentFactories.clear();
94 factoriesOL.clear();
95
96 for ( int i = 0; i < config.length; i++ )
97 {
98 try
99 {
100 IAgreementFactoryHome home = loadPersistenceLayer( config[i] );
101 home.setEngineConfiguration( config[i] );
102 home.initialize();
103
104 PersistentAgreementFactory[] factories = home.list();
105 for ( int j = 0; j < factories.length; j++ )
106 {
107 if ( persistentFactories.containsKey( factories[j].getResourceId() ) )
108 {
109 String message1 =
110 "[duplicated resource id] "
111 + "the agreement factory resource id must be unique in a WSAG4J engine.";
112 LOG.error( message1 );
113
114 String message2 =
115 "[duplicated resource id] the factory with resource id ''{0}'' was not loaded.";
116 LOG.error( MessageFormat.format( message2,
117 new Object[] { factories[j].getResourceId() } ) );
118 }
119 else
120 {
121 persistentFactories.put( factories[j].getResourceId(), factories[j] );
122 factoriesOL.add( factories[j] );
123 }
124 }
125 }
126 catch ( Exception e )
127 {
128 LOG.error( "error loading persistence layer", e );
129 }
130 }
131 }
132
133 private IAgreementFactoryHome loadPersistenceLayer( WSAG4JEngineConfigurationType configuration )
134 throws Exception
135 {
136 String implementationClass =
137 configuration.getFactory().getPersistenceImplementation().getImplementationClass();
138
139 if ( LOG.isDebugEnabled() )
140 {
141 LOG.debug( LogMessage.getMessage( "Create ''{0}'' instance as persistence layer.",
142 implementationClass ) );
143 }
144
145 try
146 {
147 Object instance = Class.forName( implementationClass ).newInstance();
148
149 if ( instance instanceof IAgreementFactoryHome )
150 {
151 if ( LOG.isDebugEnabled() )
152 {
153 LOG.debug( "Persistence layer instance created and loaded." );
154 }
155
156 return (IAgreementFactoryHome) instance;
157 }
158 else
159 {
160 throw new Exception(
161 MessageFormat.format(
162 "Error loading WSAG4J persistence layer. Class [{0}] does not implement interface [{1}].",
163 implementationClass, IAgreementFactoryHome.class.getName() ) );
164 }
165 }
166 catch ( ClassNotFoundException e )
167 {
168 throw new Exception( MessageFormat.format(
169 "Error loading WSAG4J persistence layer. Class [{0}] not found. Error: {1}",
170 implementationClass, e.getMessage() ) );
171 }
172 catch ( InstantiationException e )
173 {
174 throw new Exception( MessageFormat.format(
175 "Error loading WSAG4J persistence layer. Class [{0}] could not be instantiated. Error: {1}",
176 implementationClass, e.getMessage() ) );
177 }
178 catch ( IllegalAccessException e )
179 {
180 throw new Exception( MessageFormat.format(
181 "Error loading WSAG4J persistence layer. Class [{0}] could not be accessed. Error: {1}",
182 implementationClass, e.getMessage() ) );
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public PersistentAgreementFactory find( String factoryId ) throws Exception
200 {
201 if ( persistentFactories.containsKey( factoryId ) )
202 {
203 return persistentFactories.get( factoryId );
204 }
205
206 return null;
207 }
208
209
210
211
212
213
214
215
216
217 public PersistentAgreementFactory[] list() throws Exception
218 {
219 return factoriesOL.toArray( new PersistentAgreementFactory[persistentFactories.size()] );
220 }
221
222
223
224
225
226
227
228
229
230
231 public void remove( String factoryId ) throws Exception
232 {
233 if ( persistentFactories.containsKey( factoryId ) )
234 {
235 factoriesOL.remove( persistentFactories.get( factoryId ) );
236 persistentFactories.remove( factoryId );
237 }
238 }
239
240
241
242
243
244
245
246 public void save() throws Exception
247 {
248 PersistentAgreementFactory[] factories = list();
249 for ( int i = 0; i < factories.length; i++ )
250 {
251 factories[i].save();
252 }
253 }
254
255
256
257
258
259
260
261 public void setEngineConfiguration( WSAG4JEngineConfigurationType configs )
262 {
263 this.config = new WSAG4JEngineConfigurationType[] { configs };
264 }
265
266
267
268
269
270 public WSAG4JEngineConfigurationType getEngineConfiguration()
271 {
272 return config[0];
273 }
274
275
276
277
278
279
280
281
282
283
284 public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
285 {
286 for ( int i = 0; i < factories.length; i++ )
287 {
288 factories[i].save();
289 }
290 }
291
292 }