summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyIntentProcessor.java
blob: 3d14a141e6a6aeb26899aed95e565ffe356a1afd (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * 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.policy.xml;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

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.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
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.monitor.Monitor;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.tuscany.sca.monitor.Problem.Severity;
import org.apache.tuscany.sca.monitor.impl.ProblemImpl;
import org.apache.tuscany.sca.policy.Intent;
import org.apache.tuscany.sca.policy.PolicyFactory;
import org.apache.tuscany.sca.policy.ProfileIntent;
import org.apache.tuscany.sca.policy.QualifiedIntent;

/**
 * Processor for handling XML models of PolicyIntent definitions
 *
 * @version $Rev$ $Date$
 */
abstract class PolicyIntentProcessor<T extends Intent> extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<T>, PolicyConstants {

    private PolicyFactory policyFactory;
    private Monitor monitor;

    public PolicyIntentProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) {
        this.policyFactory = modelFactories.getFactory(PolicyFactory.class);
        this.monitor = monitor;
    }
    
    public PolicyIntentProcessor(PolicyFactory policyFactory, Monitor monitor) {
        this.policyFactory = policyFactory;
        this.monitor = monitor;
    }
    
    /**
     * Report a error.
     * 
     * @param problems
     * @param message
     * @param model
     */
    private void error(String message, Object model, Object... messageParameters) {
    	 if (monitor != null) {
    		 Problem problem = new ProblemImpl(this.getClass().getName(), "policy-xml-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters);
    	     monitor.problem(problem);
    	 }        
    }

    public T read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
        Intent policyIntent = null;
        String policyIntentName = reader.getAttributeValue(null, NAME);
        if (policyIntentName == null) {
            error("IntentNameMissing", reader);
            return (T)policyIntent;
        }
        
        // Read an <sca:intent>
        if (reader.getAttributeValue(null, REQUIRES) != null) {
            policyIntent = policyFactory.createProfileIntent();
        } else if ( policyIntentName != null && policyIntentName.indexOf(QUALIFIER) != -1) {
            policyIntent = policyFactory.createQualifiedIntent();
            
            int qualifierIndex = policyIntentName.lastIndexOf(QUALIFIER);
            Intent qualifiableIntent = policyFactory.createIntent();
            qualifiableIntent.setUnresolved(true);
            qualifiableIntent.setName(new QName(policyIntentName.substring(0, qualifierIndex)));
            
            ((QualifiedIntent)policyIntent).setQualifiableIntent(qualifiableIntent);
        } else {
            policyIntent = policyFactory.createIntent();
        }
        policyIntent.setName(new QName(policyIntentName));
        
        if ( policyIntent instanceof ProfileIntent ) {
            readRequiredIntents((ProfileIntent)policyIntent, reader);
        }
        else {
            readExcludedIntents(policyIntent, reader);
        }
        
        readConstrainedArtifacts(policyIntent, reader);

        int event = reader.getEventType();
        QName name = null;
        while (reader.hasNext()) {
            event = reader.getEventType();
            switch (event) {
                case START_ELEMENT : {
                    name = reader.getName();
                    if (DESCRIPTION_QNAME.equals(name)) {
                        policyIntent.setDescription(reader.getElementText());
                    }
                    break;
                }
            }
            if (event == END_ELEMENT && POLICY_INTENT_QNAME.equals(reader.getName())) {
                break;
            }
            
            //Read the next element
            if (reader.hasNext()) {
                reader.next();
            }
        }
        return (T)policyIntent;
    }
    
    public void write(T policyIntent, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {
        // Write an <sca:intent>
        writer.writeStartElement(PolicyConstants.SCA10_NS, INTENT);
        writer.writeNamespace(policyIntent.getName().getPrefix(), policyIntent.getName().getNamespaceURI());
        writer.writeAttribute(PolicyConstants.NAME, 
                              policyIntent.getName().getPrefix() + COLON + policyIntent.getName().getLocalPart());
        if (policyIntent instanceof ProfileIntent) {
            ProfileIntent profileIntent = (ProfileIntent)policyIntent;
            if (profileIntent.getRequiredIntents() != null && 
                profileIntent.getRequiredIntents().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (Intent requiredIntents : profileIntent.getRequiredIntents()) {
                    sb.append(requiredIntents.getName());
                    sb.append(" ");
                }
                writer.writeAttribute(PolicyConstants.REQUIRES, sb.toString());
            }
        }
        else {
            if (policyIntent.getExcludedIntents() != null && 
                policyIntent.getExcludedIntents().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (Intent excludedIntents : policyIntent.getExcludedIntents()) {
                    sb.append(excludedIntents.getName());
                    sb.append(" ");
                }
                writer.writeAttribute(PolicyConstants.EXCLUDES, sb.toString());
            }
        }
        
        if (!(policyIntent instanceof QualifiedIntent) ) {
            if (policyIntent.getConstrains() != null && 
                policyIntent.getConstrains().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (QName contrainedArtifact : policyIntent.getConstrains()) {
                    sb.append(contrainedArtifact.toString());
                    sb.append(" ");
                }
                writer.writeAttribute(CONSTRAINS, sb.toString());
            } else {
            	error("ContrainsAttributeMissing", policyIntent, policyIntent.getName());
                //throw new ContributionWriteException("Contrains attribute missing from " +
                                        //"Policy Intent Definition" + policyIntent.getName());
            }
        }
        
        if ( policyIntent.getDescription() != null && policyIntent.getDescription().length() > 0) {
            writer.writeStartElement(PolicyConstants.SCA10_NS, DESCRIPTION);
            writer.writeCData(policyIntent.getDescription());
            writer.writeEndElement();
        }
        
        writer.writeEndElement();
    }

    //FIXME This method is never used
//    private Intent resolveRequiredIntents(ProfileIntent policyIntent, ModelResolver resolver) throws ContributionResolveException {
//        boolean isUnresolved = false;
//        //FIXME: Need to check for cyclic references first i.e an A requiring B and then B requiring A... 
//        if (policyIntent != null && policyIntent.isUnresolved()) {
//            
//            //resolve all required intents
//            List<Intent> requiredIntents = new ArrayList<Intent>(); 
//            for (Intent requiredIntent : policyIntent.getRequiredIntents()) {
//                if ( requiredIntent.isUnresolved() ) {
//                    //policyIntent.getRequiredIntents().remove(requiredIntent);
//                    requiredIntent = resolver.resolveModel(Intent.class, requiredIntent);
//                    requiredIntents.add(requiredIntent);
//                    if (requiredIntent.isUnresolved()) {
//                        isUnresolved = true;
//                    }
//                }
//            }
//            policyIntent.getRequiredIntents().clear();
//            policyIntent.getRequiredIntents().addAll(requiredIntents);
//        }
//        policyIntent.setUnresolved(isUnresolved);
//        
//        return policyIntent;
//    }
    
    //FIXME This method is never used
//    private Intent resolveQualifiableIntent(QualifiedIntent policyIntent, ModelResolver resolver) throws ContributionResolveException {
//        boolean isUnresolved = false;
//
//        if (policyIntent != null && policyIntent.isUnresolved()) {
//            //resolve the qualifiable intent
//            Intent qualifiableIntent = 
//                resolver.resolveModel(Intent.class, policyIntent.getQualifiableIntent());
//            policyIntent.setQualifiableIntent(qualifiableIntent);
//            isUnresolved = qualifiableIntent.isUnresolved();
//        }
//        policyIntent.setUnresolved(isUnresolved);
//        
//        return policyIntent;
//    }
    
    private void resolveContrainedArtifacts(Intent policyIntent, ModelResolver resolver) {
        //FIXME : need to figure out this resolution. 
        policyIntent.setUnresolved(false);
    }
    
    private void resolveProfileIntent(ProfileIntent policyIntent, ModelResolver resolver)
        throws ContributionResolveException {
        // FIXME: Need to check for cyclic references first i.e an A requiring B
        // and then B requiring A...
        if (policyIntent != null) {
            // resolve all required intents
            List<Intent> requiredIntents = new ArrayList<Intent>();
            for (Intent requiredIntent : policyIntent.getRequiredIntents()) {
                if (requiredIntent.isUnresolved()) {
                    Intent resolvedRequiredIntent = resolver.resolveModel(Intent.class, requiredIntent);
                    // At this point, when the required intent is not resolved, it does not mean 
                    // its undeclared, chances are that their dependency are not resolved yet. 
                    // Lets try to resolve them first.
                    if (resolvedRequiredIntent.isUnresolved()) {
                        if (resolvedRequiredIntent instanceof ProfileIntent) {
                            if ((((ProfileIntent)resolvedRequiredIntent).getRequiredIntents()).contains(policyIntent)) {
                                error("CyclicReferenceFound", resolver, requiredIntent, policyIntent);
                                return;
                            }
                            resolveDependent(resolvedRequiredIntent, resolver);
                        }
                    }
                
                    if (!resolvedRequiredIntent.isUnresolved()) {
                        requiredIntents.add(resolvedRequiredIntent);
                    } else {
                    	error("RequiredIntentNotFound", resolver, requiredIntent, policyIntent);
                    	return;
                        //throw new ContributionResolveException("Required Intent - " + requiredIntent
                                                    //+ " not found for ProfileIntent " + policyIntent);
                    }
                } else {
                    requiredIntents.add(requiredIntent);
                }
            }
            policyIntent.getRequiredIntents().clear();
            policyIntent.getRequiredIntents().addAll(requiredIntents);
        }
    }

    private void resolveQualifiedIntent(QualifiedIntent policyIntent, ModelResolver resolver)
        throws ContributionResolveException {
        if (policyIntent != null) {
            //resolve the qualifiable intent
            Intent qualifiableIntent = policyIntent.getQualifiableIntent();
            if (qualifiableIntent.isUnresolved()) {
                Intent resolvedQualifiableIntent = resolver.resolveModel(Intent.class, qualifiableIntent);
                // At this point, when the qualifiable intent is not resolved, it does not mean 
                // its undeclared, chances are that their dependency are not resolved yet. 
                // Lets try to resolve them first.
                if (resolvedQualifiableIntent.isUnresolved()) {
                    if (resolvedQualifiableIntent instanceof QualifiedIntent) {
                        resolveDependent(resolvedQualifiableIntent, resolver);
                    }
                }
                
                if (!resolvedQualifiableIntent.isUnresolved()) {
                    policyIntent.setQualifiableIntent(resolvedQualifiableIntent);
                } else {
                	error("QualifiableIntentNotFound", resolver, qualifiableIntent, policyIntent);
                    //throw new ContributionResolveException("Qualifiable Intent - " + qualifiableIntent
                                                    //+ " not found for QualifiedIntent " + policyIntent);
                }    
            }
        }
    }
    
    public void resolveDependent(Intent policyIntent, ModelResolver resolver) throws ContributionResolveException {
        if (policyIntent instanceof ProfileIntent)
            resolveProfileIntent((ProfileIntent)policyIntent, resolver);
        
        if (policyIntent instanceof QualifiedIntent)
            resolveQualifiedIntent((QualifiedIntent)policyIntent, resolver);
        
        resolveContrainedArtifacts(policyIntent, resolver);
    }
    
    public void resolve(T policyIntent, ModelResolver resolver) throws ContributionResolveException {
        if (policyIntent instanceof ProfileIntent) {
            resolveProfileIntent((ProfileIntent)policyIntent, resolver);
        }
        else {
            resolveExcludedIntents(policyIntent, resolver);
        }

        if (policyIntent instanceof QualifiedIntent) {
            resolveQualifiedIntent((QualifiedIntent)policyIntent, resolver);
        }
        
        resolveContrainedArtifacts(policyIntent, resolver);

        /* This is too late in the processing
        if ( !policyIntent.isUnresolved() ) {
            resolver.addModel(policyIntent);
        }
        */
    }
    
    public QName getArtifactType() {
        return POLICY_INTENT_QNAME;
    }
    
    private void readConstrainedArtifacts(Intent policyIntent, XMLStreamReader reader) throws ContributionReadException {
        String value = reader.getAttributeValue(null, CONSTRAINS);
        if ( policyIntent instanceof QualifiedIntent && value != null) {
        	error("ErrorInPolicyIntentDefinition", policyIntent, policyIntent.getName(), QUALIFIED_INTENT_CONSTRAINS_ERROR);
            //String errorMsg = "Error in PolicyIntent Definition - " + policyIntent.getName() + QUALIFIED_INTENT_CONSTRAINS_ERROR;
            //throw new ContributionReadException(errorMsg);
        } else {
            if (value != null) {
                List<QName> constrainedArtifacts = policyIntent.getConstrains();
                for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                    QName qname = getQNameValue(reader, tokens.nextToken());
                    constrainedArtifacts.add(qname);
                }
            }
        }
    }
    
    private void readRequiredIntents(ProfileIntent policyIntent, XMLStreamReader reader) {
        String value = reader.getAttributeValue(null, REQUIRES);
        if (value != null) {
            List<Intent> requiredIntents = policyIntent.getRequiredIntents();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(reader, tokens.nextToken());
                Intent intent = policyFactory.createIntent();
                intent.setName(qname);
                intent.setUnresolved(true);
                requiredIntents.add(intent);
            }
        }
    }

    private void readExcludedIntents(Intent policyIntent, XMLStreamReader reader) {
        String value = reader.getAttributeValue(null, "excludes");
        if (value != null) {
            List<Intent> excludedIntents = policyIntent.getExcludedIntents();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(reader, tokens.nextToken());
                Intent intent = policyFactory.createIntent();
                intent.setName(qname);
                intent.setUnresolved(true);
                excludedIntents.add(intent);
            }
        }
    }

    private void resolveExcludedIntents(Intent policyIntent, ModelResolver resolver)
        throws ContributionResolveException {
        if (policyIntent != null) {
            // resolve all excluded intents
            List<Intent> excludedIntents = new ArrayList<Intent>();
            for (Intent excludedIntent : policyIntent.getExcludedIntents()) {
                if (excludedIntent.isUnresolved()) {
                    Intent resolvedExcludedIntent = resolver.resolveModel(Intent.class, excludedIntent);                                     
                    if (!resolvedExcludedIntent.isUnresolved()) {
                        excludedIntents.add(resolvedExcludedIntent);
                    } else {
                    	error("ExcludedIntentNotFound", resolver, excludedIntent, policyIntent);
                    	return;
                        //throw new ContributionResolveException("Excluded Intent " + excludedIntent
                                                         //+ " not found for intent " + policyIntent);
                    }
                } else {
                    excludedIntents.add(excludedIntent);
                }
            }
            policyIntent.getExcludedIntents().clear();
            policyIntent.getExcludedIntents().addAll(excludedIntents);
        }
    }

}