summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/policy-xml/src/main/java/org/apache/tuscany/sca/policy/xml/IntentAttachPointTypeProcessor.java
blob: 37e547c62007f6cd3f9036133d0f6a5a64eb4f64 (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
/*
 * 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 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.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.IntentAttachPointType;
import org.apache.tuscany.sca.policy.IntentAttachPointTypeFactory;
import org.apache.tuscany.sca.policy.PolicyFactory;
import org.apache.tuscany.sca.policy.impl.BindingTypeImpl;
import org.apache.tuscany.sca.policy.impl.ImplementationTypeImpl;


/**
 * Processor for handling XML models of ExtensionType meta data definitions
 *
 * @version $Rev$ $Date$
 */
abstract class IntentAttachPointTypeProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor<IntentAttachPointType>, PolicyConstants {

    private IntentAttachPointTypeFactory attachPointTypeFactory;
    private PolicyFactory policyFactory;
    private Monitor monitor;
    
    protected abstract IntentAttachPointType resolveExtensionType(IntentAttachPointType extnType, ModelResolver resolver) throws ContributionResolveException;

    public IntentAttachPointTypeProcessor(PolicyFactory policyFactory, 
    		                              IntentAttachPointTypeFactory attachPointTypeFactory, 
    		                              StAXArtifactProcessor<Object> extensionProcessor,
    		                              Monitor monitor) {
        this.policyFactory = policyFactory;
        this.attachPointTypeFactory = attachPointTypeFactory;
        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 IntentAttachPointType read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
        QName type = getQName(reader, TYPE);
        
        if ( type != null ) {
            if ( type.getLocalPart().startsWith(BINDING) ) {
                IntentAttachPointType bindingType = attachPointTypeFactory.createBindingType();
                bindingType.setName(type);
                bindingType.setUnresolved(true);
                
                readAlwaysProvidedIntents(bindingType, reader);
                readMayProvideIntents(bindingType, reader);
                return bindingType; 
            } else if ( type.getLocalPart().startsWith(IMPLEMENTATION) ) {
                IntentAttachPointType implType = attachPointTypeFactory.createImplementationType();
                implType.setName(type);
                implType.setUnresolved(true);
                
                readAlwaysProvidedIntents(implType, reader);
                readMayProvideIntents(implType, reader);
                return implType;
            } else {
            	error("UnrecognizedIntentAttachPointType", reader, type);
                //throw new ContributionReadException("Unrecognized IntentAttachPointType - " + type);
            }
        } else {
        	error("RequiredAttributeMissing", reader, TYPE);
            //throw new ContributionReadException("Required attribute '" + TYPE + 
                                                //"' missing from BindingType Definition");
        }        
        return null;
    }

    private void readAlwaysProvidedIntents(IntentAttachPointType extnType, XMLStreamReader reader) {
        String value = reader.getAttributeValue(null, ALWAYS_PROVIDES);
        if (value != null) {
            List<Intent> alwaysProvided = extnType.getAlwaysProvidedIntents();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(reader, tokens.nextToken());
                Intent intent = policyFactory.createIntent();
                intent.setName(qname);
                alwaysProvided.add(intent);
            }
        }
    }
    
    private void readMayProvideIntents(IntentAttachPointType extnType, XMLStreamReader reader) {
        String value = reader.getAttributeValue(null, MAY_PROVIDE);
        if (value != null) {
            List<Intent> mayProvide = extnType.getMayProvideIntents();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(reader, tokens.nextToken());
                Intent intent = policyFactory.createIntent();
                intent.setName(qname);
                mayProvide.add(intent);
            }
        }
    }
  
    public void write(IntentAttachPointType extnType, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException {

        // Write an <sca:bindingType or sca:implementationType>
        if ( extnType instanceof BindingTypeImpl ) {
            writer.writeStartElement(SCA10_NS, BINDING_TYPE);
        } else if ( extnType instanceof ImplementationTypeImpl ) {
            writer.writeStartElement(SCA10_NS, IMPLEMENTATION_TYPE);
        }
        
        writeAlwaysProvidesIntentsAttribute(extnType, writer);
        writeMayProvideIntentsAttribute(extnType, writer);
        
        writer.writeEndElement();
    }
    
    private void writeMayProvideIntentsAttribute(IntentAttachPointType extnType, XMLStreamWriter writer) throws XMLStreamException {
        StringBuffer sb  = new StringBuffer();
        for ( Intent intent : extnType.getMayProvideIntents() ) {
            writer.writeNamespace(intent.getName().getPrefix(), intent.getName().getNamespaceURI());
            sb.append(intent.getName().getPrefix() + COLON + intent.getName().getLocalPart());
            sb.append(WHITE_SPACE);
        }
        
        if ( sb.length() > 0 ) {
            writer.writeAttribute(MAY_PROVIDE, sb.toString());
        }
    }
    
    private void writeAlwaysProvidesIntentsAttribute(IntentAttachPointType extnType, XMLStreamWriter writer) throws XMLStreamException {
        StringBuffer sb  = new StringBuffer();
        for ( Intent intent : extnType.getAlwaysProvidedIntents() ) {
            writer.writeNamespace(intent.getName().getPrefix(), intent.getName().getNamespaceURI());
            sb.append(intent.getName().getPrefix() + COLON + intent.getName().getLocalPart());
            sb.append(WHITE_SPACE);
        }
        
        if ( sb.length() > 0 ) {
            writer.writeAttribute(ALWAYS_PROVIDES, sb.toString());
            
        }
    }
    
    public void resolve(IntentAttachPointType extnType, ModelResolver resolver) throws ContributionResolveException {
        
    	if (extnType != null && extnType.isUnresolved()) {
    	    resolveAlwaysProvidedIntents(extnType, resolver);
            resolveMayProvideIntents(extnType, resolver);
            extnType.setUnresolved(false);
            //resolveExtensionType(extnType, resolver);
        }
    }

    private void resolveAlwaysProvidedIntents(IntentAttachPointType extensionType,
                                              ModelResolver resolver) throws ContributionResolveException {
        if (extensionType != null) {
            // resolve all provided intents
            List<Intent> alwaysProvided = new ArrayList<Intent>();
            for (Intent providedIntent : extensionType.getAlwaysProvidedIntents()) {
                if (providedIntent.isUnresolved()) {
                    providedIntent = resolver.resolveModel(Intent.class, providedIntent);
                    if (!providedIntent.isUnresolved()) {
                        alwaysProvided.add(providedIntent);
                    } else {
                    	error("AlwaysProvidedIntentNotFound", resolver, providedIntent, extensionType);
                        //throw new ContributionResolveException("Always Provided Intent - " + providedIntent
                                                                     //+ " not found for ExtensionType "
                                                                     //+ extensionType);
                    }
                } else {
                    alwaysProvided.add(providedIntent);
                }
            }
            extensionType.getAlwaysProvidedIntents().clear();
            extensionType.getAlwaysProvidedIntents().addAll(alwaysProvided);
        }
    }
    
    private void resolveMayProvideIntents(IntentAttachPointType extensionType,
                                            ModelResolver resolver) throws ContributionResolveException {
        if (extensionType != null) {
            // resolve all provided intents
            List<Intent> mayProvide = new ArrayList<Intent>();
            for (Intent providedIntent : extensionType.getMayProvideIntents()) {
                if (providedIntent.isUnresolved()) {
                    providedIntent = resolver.resolveModel(Intent.class, providedIntent);
                    if (!providedIntent.isUnresolved()) {
                        mayProvide.add(providedIntent);
                    } else {
                    	error("MayProvideIntentNotFound", resolver, providedIntent, extensionType);
                        //throw new ContributionResolveException("May Provide Intent - " + providedIntent
                                                                     //+ " not found for ExtensionType "
                                                                     //+ extensionType);
                    }
                } else {
                    mayProvide.add(providedIntent);
                }
            }
            extensionType.getMayProvideIntents().clear();
            extensionType.getMayProvideIntents().addAll(mayProvide);
        }
    }
    
    public Class<IntentAttachPointType> getModelType() {
        return IntentAttachPointType.class;
    }
}