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.wsrf;
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.server.engine.WsagEngine;
45 import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
46 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
47
48
49
50
51
52 public class WSAG4JPersistenceFacade
53 implements IAgreementFactoryHome
54 {
55 private static final Logger LOG = Logger.getLogger( WSAG4JPersistenceFacade.class );
56
57 private WsagEngine[] engines = new WsagEngine[0];
58
59
60
61
62
63 protected Map<String, PersistentAgreementFactory> persistentFactories =
64 new HashMap<String, PersistentAgreementFactory>();
65
66
67
68
69
70 protected List<PersistentAgreementFactory> factoriesOL = new Vector<PersistentAgreementFactory>();
71
72
73
74
75
76
77
78 public WSAG4JPersistenceFacade( WsagEngine[] engines )
79 {
80 this.engines = engines;
81 }
82
83
84
85
86 public void initialize()
87 {
88 persistentFactories.clear();
89 factoriesOL.clear();
90
91 for ( int i = 0; i < getEngines().length; i++ )
92 {
93 try
94 {
95 IAgreementFactoryHome home = getEngines()[i].getPersistenceLayer();
96 PersistentAgreementFactory[] factories = home.list();
97
98 for ( int j = 0; j < factories.length; j++ )
99 {
100 if ( persistentFactories.containsKey( factories[j].getResourceId() ) )
101 {
102 String message1 =
103 "[duplicated resource id] "
104 + "the agreement factory resource id must be unique in a WSAG4J engine.";
105 LOG.error( message1 );
106
107 String message2 =
108 "[duplicated resource id] the factory with resource id ''{0}'' was not loaded.";
109 LOG.error( MessageFormat.format( message2,
110 new Object[] { factories[j].getResourceId() } ) );
111 }
112 else
113 {
114 persistentFactories.put( factories[j].getResourceId(), factories[j] );
115 factoriesOL.add( factories[j] );
116 }
117 }
118 }
119 catch ( Exception e )
120 {
121 LOG.error( "error loading persistence layer", e );
122 }
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public PersistentAgreementFactory find( String factoryId ) throws Exception
140 {
141 if ( persistentFactories.containsKey( factoryId ) )
142 {
143 return persistentFactories.get( factoryId );
144 }
145
146 return null;
147 }
148
149
150
151
152
153
154
155
156
157 public PersistentAgreementFactory[] list() throws Exception
158 {
159 return factoriesOL.toArray( new PersistentAgreementFactory[persistentFactories.size()] );
160 }
161
162
163
164
165
166
167
168
169
170
171 public void remove( String factoryId ) throws Exception
172 {
173 if ( persistentFactories.containsKey( factoryId ) )
174 {
175 factoriesOL.remove( persistentFactories.get( factoryId ) );
176 persistentFactories.remove( factoryId );
177 }
178 }
179
180
181
182
183
184
185
186 public void save() throws Exception
187 {
188 PersistentAgreementFactory[] factories = list();
189 for ( int i = 0; i < factories.length; i++ )
190 {
191 factories[i].save();
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204 public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
205 {
206 for ( int i = 0; i < factories.length; i++ )
207 {
208 factories[i].save();
209 }
210 }
211
212
213
214
215
216
217 public WsagEngine[] getEngines()
218 {
219 return engines;
220 }
221 }