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.persistence;
36
37 import java.text.MessageFormat;
38 import java.util.List;
39 import java.util.Vector;
40
41 import org.apache.log4j.Logger;
42 import org.ogf.graap.wsag.api.WsagConstants;
43 import org.ogf.graap.wsag.server.api.IEngineComponent;
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 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
48 import org.w3.x2005.x08.addressing.EndpointReferenceType;
49 import org.w3.x2005.x08.addressing.ReferenceParametersType;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.w3c.dom.Node;
53
54
55
56
57 public class WsDatabaseWSAG4JPersistence implements IEngineComponent, IWsAgreementFactoryHome
58 {
59
60 private final Logger log = Logger.getLogger( WsDatabaseWSAG4JPersistence.class );
61
62
63 private static int counter = 0;
64
65 private final IAgreementFactoryHome agreementFactoryHome;
66
67 private List<WsPersistentAgreementFactory> wsFactories;
68
69 private WSAG4JEngineConfigurationType wsagconfig;
70
71
72
73
74
75
76
77
78 public WsDatabaseWSAG4JPersistence( IAgreementFactoryHome agreementFactoryHome )
79 {
80 super();
81 this.agreementFactoryHome = agreementFactoryHome;
82 wsFactories = new Vector<WsPersistentAgreementFactory>();
83 }
84
85
86
87
88 public void initialize() throws Exception
89 {
90 wsFactories.clear();
91
92
93 PersistentAgreementFactory[] persistentAgreementFactories = agreementFactoryHome.list();
94
95
96 wsFactories = new Vector<WsPersistentAgreementFactory>();
97
98 for ( PersistentAgreementFactory persistentAgreementFactory : persistentAgreementFactories )
99 {
100
101 WsPersistentAgreementFactory wsPersistentAgreementFactory =
102 new WsDatabaseAgreementFactory( persistentAgreementFactory, getNextEPR() );
103 wsFactories.add( wsPersistentAgreementFactory );
104 }
105
106
107
108
109 if ( log.isDebugEnabled() )
110 {
111 log.debug( "Initialize WsDatabaseWSAG4JPersistence instance." );
112
113 WsPersistentAgreementFactory[] wsPersistentAgreementFactories = list();
114 log.debug( wsPersistentAgreementFactories.length
115 + " WsPersistentAgreementFactory instances available." );
116
117 for ( WsPersistentAgreementFactory wsPersistentAgreementFactory : wsPersistentAgreementFactories )
118 {
119 WsDatabasePersistentAgreement[] wsDatabasePersistentAgreements =
120 wsPersistentAgreementFactory.list();
121 log.debug( " " + wsDatabasePersistentAgreements.length
122 + " persisted WsDatabasePersistentAgreement instances found." );
123
124 for ( WsDatabasePersistentAgreement wsDatabasePersistentAgreement : wsDatabasePersistentAgreements )
125 {
126 log.debug( " " + wsDatabasePersistentAgreement.toString() );
127 }
128 }
129 }
130 }
131
132
133
134
135 public WsPersistentAgreementFactory find( String agreementFactoryId ) throws Exception
136 {
137
138 log.debug( "WsDatabaseWSAG4JPersistence -> find(String agreementFactoryId)" );
139
140
141 PersistentAgreementFactory persistentAgreementFactory =
142 agreementFactoryHome.find( agreementFactoryId );
143
144
145 EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
146
147
148 WsPersistentAgreementFactory wsPersistentAgreementFactory =
149 new WsDatabaseAgreementFactory( persistentAgreementFactory, epr );
150
151 return wsPersistentAgreementFactory;
152 }
153
154
155
156
157 public WsPersistentAgreementFactory[] list() throws Exception
158 {
159 log.debug( "WsDatabaseWSAG4JPersistence -> list()" );
160
161 return wsFactories.toArray( new WsPersistentAgreementFactory[wsFactories.size()] );
162 }
163
164
165
166
167 public void saveAgreementFactories( WsPersistentAgreementFactory[] wsPersistentAgreementFactories )
168 throws Exception
169 {
170 log.debug( "WsDatabaseWSAG4JPersistence -> saveAgreementFactories(WsPersistentAgreementFactory[])" );
171
172
173 for ( WsPersistentAgreementFactory wsPersistentAgreementFactory : wsPersistentAgreementFactories )
174 {
175 wsPersistentAgreementFactory.save();
176 }
177 }
178
179
180
181
182 public void remove( String agreementFactoryId ) throws Exception
183 {
184 String message = "remove() operation not supported for {0}.";
185 throw new UnsupportedOperationException( MessageFormat.format( message,
186 new Object[] { getClass().getName() } ) );
187 }
188
189
190
191
192 public EndpointReferenceType getNextEPR()
193 {
194 EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
195 epr.addNewAddress().setStringValue( WsagEngine.getGatewayURL()
196 + WsagConstants.AGREEMENT_FACTORY_SERVICE_URI );
197
198 ReferenceParametersType ref = epr.addNewReferenceParameters();
199
200 String value = "WSAG4J_ResourceId_" + counter++;
201
202 Document doc = ref.getDomNode().getOwnerDocument();
203 Element element =
204 doc.createElementNS( WsagConstants.WSAG4J_RESOURCE_ID_QNAME.getNamespaceURI(),
205 WsagConstants.WSAG4J_RESOURCE_ID_QNAME.getLocalPart() );
206
207 element.appendChild( element.getOwnerDocument().createTextNode( value ) );
208
209 Node imported = doc.importNode( element, true );
210 ref.getDomNode().appendChild( imported );
211
212 return epr;
213 }
214
215
216
217
218
219
220 public void setEngineConfiguration( WSAG4JEngineConfigurationType config )
221 {
222 wsagconfig = config;
223 }
224
225
226
227
228
229
230 public WSAG4JEngineConfigurationType getEngineConfiguration()
231 {
232 return wsagconfig;
233 }
234 }