summaryrefslogtreecommitdiffstats
path: root/sandbox/rfeng/minicore/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeLoader.java
blob: 008ee59de8193a4af434e4610f1b2907d6def5c1 (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
/*
 * 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.core.implementation.composite;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static org.osoa.sca.Constants.SCA_NS;

import java.net.URI;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.extension.LoaderExtension;
import org.apache.tuscany.spi.loader.InvalidServiceException;
import org.apache.tuscany.spi.loader.InvalidWireException;
import org.apache.tuscany.spi.loader.LoaderException;
import org.apache.tuscany.spi.loader.LoaderRegistry;
import org.apache.tuscany.spi.model.ComponentDefinition;
import org.apache.tuscany.spi.model.ComponentType;
import org.apache.tuscany.spi.model.CompositeComponentType;
import org.apache.tuscany.spi.model.Implementation;
import org.apache.tuscany.spi.model.Include;
import org.apache.tuscany.spi.model.ModelObject;
import org.apache.tuscany.spi.model.Property;
import org.apache.tuscany.spi.model.ReferenceDefinition;
import org.apache.tuscany.spi.model.ReferenceTarget;
import org.apache.tuscany.spi.model.ServiceDefinition;
import org.apache.tuscany.spi.model.WireDefinition;
import org.apache.tuscany.spi.services.artifact.ArtifactRepository;
import org.osoa.sca.annotations.Reference;

/**
 * Loads a composite component definition from an XML-based assembly file
 * 
 * @version $Rev$ $Date$
 */
public class CompositeLoader extends LoaderExtension<CompositeComponentType> {
    public static final QName COMPOSITE = new QName(SCA_NS, "composite");
    public static final String URI_DELIMITER = "/";

    private final ArtifactRepository artifactRepository;

    public CompositeLoader(@Reference
    LoaderRegistry registry, @Reference
    ArtifactRepository artifactRepository) {
        super(registry);
        this.artifactRepository = artifactRepository;
    }

    public QName getXMLType() {
        return COMPOSITE;
    }

    public CompositeComponentType load(ModelObject object, XMLStreamReader reader, DeploymentContext deploymentContext)
        throws XMLStreamException, LoaderException {

        String name = reader.getAttributeValue(null, "name");
        String targetNamespace = reader.getAttributeValue(null, "targetNamespace");
        boolean autowire = Boolean.parseBoolean(reader.getAttributeValue(null, "autowire"));

        CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> type = new CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>>(
                                                                                                                                                                           new QName(
                                                                                                                                                                                     targetNamespace,
                                                                                                                                                                                     name));
        type.setAutowire(autowire);
        boolean done = false;
        while (!done) {
            switch (reader.next()) {
                case START_ELEMENT:
                    boolean oldAutowire = deploymentContext.isAutowire();
                    deploymentContext.setAutowire(autowire);
                    ModelObject o = registry.load(type, reader, deploymentContext);
                    deploymentContext.setAutowire(oldAutowire);
                    if (o instanceof ServiceDefinition) {
                        type.add((ServiceDefinition)o);
                    } else if (o instanceof ReferenceDefinition) {
                        type.add((ReferenceDefinition)o);
                    } else if (o instanceof Property<?>) {
                        type.add((Property<?>)o);
                    } else if (o instanceof ComponentDefinition<?>) {
                        type.add((ComponentDefinition<?>)o);
                    } else if (o instanceof Include) {
                        type.add((Include)o);
                    } else if (o instanceof WireDefinition) {
                        type.add((WireDefinition)o);
                    } else {
                        // add as an unknown model extension
                        if (o != null) {
                            type.getExtensions().put(o.getClass(), o);
                        }
                    }
                    reader.next();
                    break;
                case END_ELEMENT:
                    if (COMPOSITE.equals(reader.getName())) {
                        // if there are wire defintions then link them up to the
                        // relevant components
                        resolveWires(type);
                        verifyCompositeCompleteness(type);
                        done = true;
                        break;
                    }
            }
        }
        for (ComponentDefinition<? extends Implementation<?>> c : type.getComponents().values()) {
            // PropertyHelper.processProperties(type, c, deploymentContext);
        }
        return type;
    }

    protected void resolveWires(CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> composite)
        throws InvalidWireException {
        ComponentDefinition componentDefinition;
        ServiceDefinition serviceDefinition;
        List<WireDefinition> wireDefns = composite.getDeclaredWires();
        for (WireDefinition wire : wireDefns) {
            URI targetUri = wire.getTarget();
            // validate the target before finding the source
            validateTarget(targetUri, composite);

            String sourceName = wire.getSource().getPath(); // new
                                                            // QualifiedName(wire.getSource().getPath());
            serviceDefinition = composite.getDeclaredServices().get(sourceName);
            if (serviceDefinition != null) {
                serviceDefinition.setTarget(wire.getTarget());
            } else {
                componentDefinition = composite.getDeclaredComponents().get(sourceName);
                if (componentDefinition != null) {
                    if (wire.getSource().getFragment() == null) {
                        throw new InvalidWireException("Source reference not specified", sourceName);
                    }
                    URI referenceName = URI.create(wire.getSource().getFragment());
                    ReferenceTarget referenceTarget = createReferenceTarget(referenceName,
                                                                            targetUri,
                                                                            componentDefinition);
                    componentDefinition.add(referenceTarget);
                } else {
                    throw new InvalidWireException("Source not found", sourceName);
                }
            }
        }
    }

    private ReferenceTarget createReferenceTarget(URI componentReferenceName,
                                                  URI target,
                                                  ComponentDefinition componentDefn) throws InvalidWireException {
        ComponentType componentType = componentDefn.getImplementation().getComponentType();
        if (componentReferenceName == null) {
            // if there is ambiguity in determining the source of the wire or
            // there is no reference to be wired
            if (componentType.getReferences().size() > 1 || componentType.getReferences().isEmpty()) {
                throw new InvalidWireException("Unable to determine unique source reference");
            } else {
                Map references = componentType.getReferences();
                ReferenceDefinition definition = (ReferenceDefinition)references.values().iterator().next();
                componentReferenceName = definition.getUri();
            }
        }

        ReferenceTarget referenceTarget = new ReferenceTarget();
        referenceTarget.setReferenceName(componentReferenceName);
        referenceTarget.addTarget(target);
        return referenceTarget;
    }

    protected void verifyCompositeCompleteness(CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> composite)
        throws InvalidServiceException {
        // check if all of the composite services have been wired
        for (ServiceDefinition svcDefn : composite.getDeclaredServices().values()) {
            if (svcDefn.getTarget() == null) {
                String identifier = svcDefn.getUri().toString();
                throw new InvalidServiceException("Composite service not wired to a target", identifier);
            }
        }
    }

    private void validateTarget(URI target,
                                CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> composite)
        throws InvalidWireException {
        // if target is not a reference of the composite
        String targetName = target.getPath();
        if (composite.getReferences().get(targetName) == null) {
            ComponentDefinition<?> targetDefinition = composite.getDeclaredComponents().get(targetName);
            // if a target component exists in this composite
            if (targetDefinition != null) {
                Implementation<?> implementation = targetDefinition.getImplementation();
                ComponentType<?, ?, ?> componentType = implementation.getComponentType();
                Map<String, ? extends ServiceDefinition> services = componentType.getServices();
                if (target.getFragment() == null) {
                    if (services.size() > 1 || services.isEmpty()) {
                        throw new InvalidWireException("Ambiguous target", target.toString());
                    }
                } else {
                    if (services.get(target.getFragment()) == null) {
                        throw new InvalidWireException("Invalid target service", target.toString());
                    }
                }
            } else {
                throw new InvalidWireException("Target not found", target.toString());
            }
        }
    }
}