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.api.pattern;
36
37 import java.text.MessageFormat;
38
39 import javax.xml.namespace.QName;
40
41 import org.apache.xmlbeans.XmlObject;
42 import org.ggf.schemas.jsdl.x2005.x11.jsdl.ApplicationType;
43 import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionDocument;
44 import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionType;
45 import org.ogf.graap.wsag.api.WsagConstants;
46 import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
47 import org.ogf.schemas.graap.wsAgreement.TermCompositorType;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public class ChoosableComputeApplicationPattern extends AbstractPattern
121 {
122
123 private static final QName EXACTLY_ONE_QNAME = new QName( WsagConstants.NAMESPACE_URI, "ExactlyOne" );
124
125 private ServiceDescriptionTermType selectedApplicationSDT;
126
127 private ServiceDescriptionTermType[] applicationSDTs;
128
129 private ApplicationType[] applicationDefinitions;
130
131 private String appliationDescXPath =
132 "declare namespace wsag='http://schemas.ggf.org/graap/2007/03/ws-agreement';"
133 + "$this/wsag:ServiceDescriptionTerm[@wsag:Name = 'APPLICATION_SDT' ]";
134
135 private String appliationXPath = "declare namespace jsdl='http://schemas.ggf.org/jsdl/2005/11/jsdl';"
136 + "$this/jsdl:JobDefinition/jsdl:JobDescription/jsdl:Application";
137
138
139
140
141
142
143
144 public ChoosableComputeApplicationPattern( TermCompositorType termCompositor )
145 {
146 super( termCompositor );
147
148
149
150
151 String localName = termCompositor.getDomNode().getLocalName();
152 String namespaceURI = termCompositor.getDomNode().getNamespaceURI();
153
154 QName compositorName = new QName( namespaceURI, localName );
155
156 if ( !EXACTLY_ONE_QNAME.equals( compositorName ) )
157 {
158 String msgError =
159 "Tried to apply {0} to {1} term compositor. "
160 + "This pattern can only be applied to wsag:ExactlyOne term compositor.";
161 String error =
162 MessageFormat.format( msgError, new Object[] { getClass().getName(), compositorName } );
163 throw new IllegalStateException( error );
164 }
165
166 if ( ( termCompositor.getAllArray().length > 0 ) || ( termCompositor.getExactlyOneArray().length > 0 )
167 || ( termCompositor.getOneOrMoreArray().length > 0 ) )
168 {
169
170 String msgError =
171 "A ChoosableComputeApplicationPattern must not contain any other term compositor.";
172 throw new IllegalStateException( msgError );
173 }
174
175 if ( ( termCompositor.getGuaranteeTermArray().length > 0 )
176 || ( termCompositor.getServicePropertiesArray().length > 0 )
177 || ( termCompositor.getServiceReferenceArray().length > 0 ) )
178 {
179
180 String msgError =
181 "A ChoosableComputeApplicationPattern must not contain any Service Properties, "
182 + "Service References, or Guarantee terms.";
183 throw new IllegalStateException( msgError );
184 }
185
186 initialize();
187 }
188
189 private void initialize()
190 {
191
192
193
194 XmlObject[] applicationDescriptions = getTermCompositor().selectPath( getAppliationDescXPath() );
195
196 applicationSDTs = new ServiceDescriptionTermType[applicationDescriptions.length];
197 applicationDefinitions = new ApplicationType[applicationDescriptions.length];
198
199 for ( int i = 0; i < applicationDescriptions.length; i++ )
200 {
201 applicationSDTs[i] = (ServiceDescriptionTermType) applicationDescriptions[i].copy();
202
203 XmlObject[] selectedApps = applicationDescriptions[i].selectPath( getAppliationXPath() );
204 if ( selectedApps.length != 1 )
205 {
206 String msgError = "APPLICATION_SDT must contain exactly 1 JSDL application definition.";
207 throw new IllegalStateException( msgError );
208 }
209
210 applicationDefinitions[i] = (ApplicationType) selectedApps[0].copy();
211 }
212
213
214
215
216 setSelectedApplication( applicationSDTs[0] );
217 }
218
219
220
221
222
223 public JobDefinitionType getSelectedJobDefinition()
224 {
225 final QName jobDefQName = JobDefinitionDocument.type.getDocumentElementName();
226 return (JobDefinitionType) selectedApplicationSDT.selectChildren( jobDefQName )[0];
227 }
228
229
230
231
232 public ApplicationType getSelectedApplication()
233 {
234 return (ApplicationType) selectedApplicationSDT.selectPath( getAppliationXPath() )[0];
235 }
236
237
238
239
240
241
242
243
244 public boolean selectApplication( ApplicationType application )
245 {
246 return selectApplication( application.getApplicationName(), application.getApplicationVersion() );
247 }
248
249
250
251
252
253
254
255
256
257
258 public boolean selectApplication( String name, String version )
259 {
260 name = ( name == null ) ? "" : name;
261
262 for ( int i = 0; i < applicationDefinitions.length; i++ )
263 {
264 if ( name.equals( applicationDefinitions[i].getApplicationName() ) )
265 {
266
267 boolean match = false;
268
269 if ( version != null )
270 {
271 match = version.equals( applicationDefinitions[i].getApplicationVersion() );
272 }
273 else
274 {
275 match = applicationDefinitions[i].getApplicationVersion() == null;
276 }
277
278 if ( match )
279 {
280 setSelectedApplication( applicationSDTs[i] );
281 return true;
282 }
283 }
284 }
285
286 return false;
287 }
288
289 private void setSelectedApplication( ServiceDescriptionTermType selected )
290 {
291 selectedApplicationSDT = (ServiceDescriptionTermType) selected.copy();
292 updateSelectedApplication();
293 }
294
295
296
297
298
299 public void updateSelectedApplication()
300 {
301 ServiceDescriptionTermType[] appSDT = new ServiceDescriptionTermType[] { selectedApplicationSDT };
302 getTermCompositor().setServiceDescriptionTermArray( appSDT );
303 }
304
305
306
307
308
309
310 public ApplicationType[] listApplications()
311 {
312 return applicationDefinitions;
313 }
314
315
316
317
318
319 public void setAppliationDescXPath( String appliationDescXPath )
320 {
321 this.appliationDescXPath = appliationDescXPath;
322 }
323
324
325
326
327 public String getAppliationDescXPath()
328 {
329 return appliationDescXPath;
330 }
331
332
333
334
335
336 public void setAppliationXPath( String appliationXPath )
337 {
338 this.appliationXPath = appliationXPath;
339 }
340
341
342
343
344 public String getAppliationXPath()
345 {
346 return appliationXPath;
347 }
348
349 }