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
36
37
38
39
40
41
42
43
44
45
46
47
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.assembly.xml;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Callback;
import org.apache.tuscany.sca.assembly.ComponentType;
import org.apache.tuscany.sca.assembly.Contract;
import org.apache.tuscany.sca.assembly.Extensible;
import org.apache.tuscany.sca.assembly.ExtensionFactory;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor;
import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.interfacedef.impl.OperationImpl;
import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.policy.IntentAttachPoint;
import org.apache.tuscany.sca.policy.PolicyFactory;
import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
import org.w3c.dom.Document;
/**
* A componentType processor.
*
* @version $Rev$ $Date$
*/
public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor<ComponentType> {
/**
* Constructs a new componentType processor.
* @param factory
* @param policyFactory
* @param registry
*/
public ComponentTypeProcessor(AssemblyFactory factory,
ExtensionFactory extensionFactory,
PolicyFactory policyFactory,
StAXArtifactProcessor extensionProcessor, StAXAttributeProcessor extensionAttributeProcessor, Monitor monitor) {
super(factory, extensionFactory, policyFactory, extensionProcessor, monitor);
}
/**
* Constructs a new componentType processor.
*
* @param modelFactories
* @param extensionProcessor
*/
public ComponentTypeProcessor(ModelFactoryExtensionPoint modelFactories,
StAXArtifactProcessor extensionProcessor,
StAXAttributeProcessor extensionAttributeProcessor,
Monitor monitor) {
super(modelFactories.getFactory(AssemblyFactory.class),
modelFactories.getFactory(ExtensionFactory.class),
modelFactories.getFactory(PolicyFactory.class),
extensionProcessor,
monitor);
}
public ComponentType read(XMLStreamReader reader) throws ContributionReadException {
ComponentType componentType = null;
Service service = null;
Reference reference = null;
Contract contract = null;
Property property = null;
Callback callback = null;
QName name = null;
try {
// Read the componentType document
while (reader.hasNext()) {
int event = reader.getEventType();
switch (event) {
case START_ELEMENT:
name = reader.getName();
if (Constants.COMPONENT_TYPE_QNAME.equals(name)) {
// Read a <componentType>
componentType = assemblyFactory.createComponentType();
componentType.setConstrainingType(readConstrainingType(reader));
} else if (Constants.SERVICE_QNAME.equals(name)) {
// Read a <service>
service = assemblyFactory.createService();
contract = service;
service.setName(getString(reader, Constants.NAME));
componentType.getServices().add(service);
policyProcessor.readPolicies(service, reader);
} else if (Constants.REFERENCE_QNAME.equals(name)) {
// Read a <reference>
reference = assemblyFactory.createReference();
contract = reference;
reference.setName(getString(reader, Constants.NAME));
reference.setWiredByImpl(getBoolean(reader, Constants.WIRED_BY_IMPL));
readMultiplicity(reference, reader);
readTargets(reference, reader);
componentType.getReferences().add(reference);
policyProcessor.readPolicies(reference, reader);
} else if (Constants.PROPERTY_QNAME.equals(name)) {
// Read a <property>
property = assemblyFactory.createProperty();
readAbstractProperty(property, reader);
policyProcessor.readPolicies(property, reader);
// Read the property value
Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader);
property.setValue(value);
componentType.getProperties().add(property);
} else if (Constants.IMPLEMENTATION_QNAME.equals(name)) {
// Read an <implementation> element
policyProcessor.readPolicies(componentType, reader);
} else if (Constants.CALLBACK_QNAME.equals(name)) {
// Read a <callback>
callback = assemblyFactory.createCallback();
contract.setCallback(callback);
policyProcessor.readPolicies(callback, reader);
} else if (OPERATION_QNAME.equals(name)) {
// Read an <operation>
Operation operation = new OperationImpl();
operation.setName(getString(reader, NAME));
operation.setUnresolved(true);
if (callback != null) {
policyProcessor.readPolicies(callback, operation, reader);
} else {
policyProcessor.readPolicies(contract, operation, reader);
}
} else {
// Read an extension element
Object extension = extensionProcessor.read(reader);
if (extension != null) {
if (extension instanceof InterfaceContract) {
// <service><interface> and <reference><interface>
contract.setInterfaceContract((InterfaceContract)extension);
} else if (extension instanceof Binding) {
// <service><binding> and <reference><binding>
if (callback != null) {
callback.getBindings().add((Binding)extension);
} else {
contract.getBindings().add((Binding)extension);
}
} else {
// Add the extension element to the current element
if (callback != null) {
callback.getExtensions().add(extension);
} else if (contract != null) {
contract.getExtensions().add(extension);
} else if (property != null) {
property.getExtensions().add(extension);
} else {
if (componentType instanceof Extensible) {
((Extensible)componentType).getExtensions().add(extension);
}
}
}
}
}
break;
case END_ELEMENT:
name = reader.getName();
// Clear current state when reading reaching end element
if (SERVICE_QNAME.equals(name)) {
service = null;
contract = null;
} else if (REFERENCE_QNAME.equals(name)) {
reference = null;
contract = null;
} else if (PROPERTY_QNAME.equals(name)) {
property = null;
} else if (CALLBACK_QNAME.equals(name)) {
callback = null;
}
break;
}
// Read the next element
if (reader.hasNext()) {
reader.next();
}
}
}
catch (XMLStreamException e) {
ContributionReadException ex = new ContributionReadException(e);
error("XMLStreamException", reader, ex);
}
return componentType;
}
public void write(ComponentType componentType, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
// Write <componentType> element
writeStartDocument(writer, COMPONENT_TYPE,
writeConstrainingType(componentType));
// Write <service> elements
for (Service service : componentType.getServices()) {
writeStart(writer, SERVICE, new XAttr(NAME, service.getName()),
policyProcessor.writePolicies(service));
if (service.getInterfaceContract() != null) {
extensionProcessor.write(service.getInterfaceContract(), writer);
}
for (Binding binding: service.getBindings()) {
extensionProcessor.write(binding, writer);
}
if (service.getCallback() != null) {
Callback callback = service.getCallback();
writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback));
for (Binding binding: callback.getBindings()) {
extensionProcessor.write(binding, writer);
}
for (Object extension: callback.getExtensions()) {
extensionProcessor.write(extension, writer);
}
writeEnd(writer);
}
for (Object extension: service.getExtensions()) {
extensionProcessor.write(extension, writer);
}
writeEnd(writer);
}
// Write <reference> elements
for (Reference reference : componentType.getReferences()) {
writeStart(writer, REFERENCE,
new XAttr(NAME, reference.getName()),
writeMultiplicity(reference),
writeTargets(reference),
policyProcessor.writePolicies(reference));
extensionProcessor.write(reference.getInterfaceContract(), writer);
for (Binding binding: reference.getBindings()) {
extensionProcessor.write(binding, writer);
}
if (reference.getCallback() != null) {
Callback callback = reference.getCallback();
writeStart(writer, CALLBACK,
policyProcessor.writePolicies(callback));
for (Binding binding: callback.getBindings()) {
extensionProcessor.write(binding, writer);
}
for (Object extension: callback.getExtensions()) {
extensionProcessor.write(extension, writer);
}
writeEnd(writer);
}
for (Object extension: reference.getExtensions()) {
extensionProcessor.write(extension, writer);
}
writeEnd(writer);
}
// Write <property> elements
for (Property property : componentType.getProperties()) {
writeStart(writer,
PROPERTY,
new XAttr(NAME, property.getName()),
new XAttr(MUST_SUPPLY, property.isMustSupply()),
new XAttr(MANY, property.isMany()),
new XAttr(TYPE, property.getXSDType()),
new XAttr(ELEMENT, property.getXSDElement()),
policyProcessor.writePolicies(property));
// Write property value
writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer);
// Write extensions
for (Object extension : property.getExtensions()) {
extensionProcessor.write(extension, writer);
}
writeEnd(writer);
}
// Write extension elements
if (componentType instanceof Extensible) {
for (Object extension: ((Extensible)componentType).getExtensions()) {
extensionProcessor.write(extension, writer);
}
}
// Write <implementation> elements if the componentType has
// any intents or policySets
boolean writeImplementation = false;
if (componentType instanceof IntentAttachPoint) {
if (!((IntentAttachPoint)componentType).getRequiredIntents().isEmpty()) {
writeImplementation = true;
}
}
if (componentType instanceof PolicySetAttachPoint) {
if (!((PolicySetAttachPoint)componentType).getPolicySets().isEmpty()) {
writeImplementation = true;
}
}
if (writeImplementation) {
writeStart(writer, IMPLEMENTATION,
policyProcessor.writePolicies(componentType));
}
writeEndDocument(writer);
}
public void resolve(ComponentType componentType, ModelResolver resolver) throws ContributionResolveException {
// Resolve component type services and references
resolveContracts(componentType.getServices(), resolver);
resolveContracts(componentType.getReferences(), resolver);
if ( componentType instanceof PolicySetAttachPoint ) {
resolveIntents(((PolicySetAttachPoint)componentType).getRequiredIntents(), resolver);
resolvePolicySets(((PolicySetAttachPoint)componentType).getPolicySets(), resolver);
}
}
public QName getArtifactType() {
return COMPONENT_TYPE_QNAME;
}
public Class<ComponentType> getModelType() {
return ComponentType.class;
}
}
|