summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/tuscany-policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyIntentProcessor.java
blob: 53231f704e2fb14f229302a3a379a3058fb92576 (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
/*
 * 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.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
 */

public abstract class PolicyIntentProcessor<T extends Intent> extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<T>, PolicyConstants {

    private PolicyFactory policyFactory;

    public PolicyIntentProcessor(ModelFactoryExtensionPoint modelFactories) {
        this.policyFactory = modelFactories.getFactory(PolicyFactory.class);
    }
    
    public PolicyIntentProcessor(PolicyFactory policyFactory, StAXArtifactProcessor<Object> extensionProcessor) {
        this.policyFactory = policyFactory;
    }

    public T read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
        Intent policyIntent = null;
        String policyIntentName = reader.getAttributeValue(null, NAME);
        // 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);
        }
        
        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());
            }
        }
        
        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 {
                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);
                    if (resolvedRequiredIntent != null) {
                        requiredIntents.add(resolvedRequiredIntent);
                    } else {
                        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);
    
                if (resolvedQualifiableIntent != null) {
                    policyIntent.setQualifiableIntent(resolvedQualifiableIntent);
                } else {
                    throw new ContributionResolveException("Qualifiable Intent - " + qualifiableIntent
                        + " not found for QualifiedIntent "
                        + policyIntent);
                }
    
            }
        }
    }
    
    public void resolve(T policyIntent, ModelResolver resolver) throws ContributionResolveException {
        if (policyIntent instanceof ProfileIntent) {
            resolveProfileIntent((ProfileIntent)policyIntent, resolver);
        }

        if (policyIntent instanceof QualifiedIntent) {
            resolveQualifiedIntent((QualifiedIntent)policyIntent, resolver);
        }
        
        resolveContrainedArtifacts(policyIntent, resolver);
        
        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) {
            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);
            }
        }
    }
    
}