summaryrefslogtreecommitdiffstats
path: root/sandbox/SPI/implementation-spi-impl/src/main/java/org/apache/tuscany/spi/implementation/impl/ImplAtomicComponent.java
blob: a6bd04baadf137e3ccad38e4d6812a770fe4d1ec (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
/*
 * 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.spi.implementation.impl;

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

import org.apache.tuscany.assembly.ComponentProperty;
import org.apache.tuscany.assembly.Implementation;
import org.apache.tuscany.core.component.ComponentContextImpl;
import org.apache.tuscany.core.component.ComponentContextProvider;
import org.apache.tuscany.core.component.ServiceReferenceImpl;
import org.apache.tuscany.core.component.scope.InstanceWrapperBase;
import org.apache.tuscany.databinding.DataBindingExtensionPoint;
import org.apache.tuscany.interfacedef.Operation;
import org.apache.tuscany.spi.ObjectCreationException;
import org.apache.tuscany.spi.ObjectFactory;
import org.apache.tuscany.spi.SingletonObjectFactory;
import org.apache.tuscany.spi.component.InstanceWrapper;
import org.apache.tuscany.spi.component.TargetInvokerCreationException;
import org.apache.tuscany.spi.component.TargetResolutionException;
import org.apache.tuscany.spi.extension.AtomicComponentExtension;
import org.apache.tuscany.spi.implementation.ImplementationActivator;
import org.apache.tuscany.spi.implementation.Invoker;
import org.apache.tuscany.spi.wire.TargetInvoker;
import org.apache.tuscany.spi.wire.Wire;
import org.osoa.sca.CallableReference;
import org.osoa.sca.ComponentContext;
import org.osoa.sca.ServiceReference;

public class ImplAtomicComponent extends AtomicComponentExtension implements ComponentContextProvider {

    private ComponentContext componentContext;
    private ImplementationActivator ia;
    private Implementation impl;

    private Map<String, Object> references;
    private Map<String, ObjectFactory<?>> propertyValueFactories;
    
    private PropertyValueObjectFactory propertyValueObjectFactory = null;
    private DataBindingExtensionPoint dataBindingRegistry;

    public ImplAtomicComponent(URI uri, URI groupId, ImplementationActivator ia, Implementation impl) {
        super(uri, null, null, groupId, 50);
        this.ia = ia;
        this.componentContext = new ComponentContextImpl(this);
        this.impl = impl;
        references = new HashMap<String, Object>();
        propertyValueFactories = new HashMap<String, ObjectFactory<?>>();
    }

    public Object createInstance() throws ObjectCreationException {
        Map<String, Object> properties = new HashMap<String, Object>();
        for (String propertyName : propertyValueFactories.keySet()) {
            ObjectFactory<?> propertyValueFactory = propertyValueFactories.get(propertyName);
            if ( propertyValueFactory != null) {
                properties.put(propertyName, propertyValueFactory.getInstance());
            }
        }
        return ia.getInvokerFactory(impl).getInstance(references, properties);
    }

    public InstanceWrapper createInstanceWrapper() throws ObjectCreationException {
        return new InstanceWrapperBase(createInstance());
    }

    public void attachCallbackWire(Wire arg0) {
    }

    public void attachWire(Wire wire) {
        references.put(wire.getSourceUri().getFragment(), createWireProxy(wire));
    }

    protected Object createWireProxy(Wire wire) {
        // TODO: this is completly wrong :) Need to create a proxy wraping the wire
        Object ref;
        try {
            ref = wire.getTargetInstance();
        } catch (TargetResolutionException e) {
            throw new RuntimeException(e);
        }
        return ref;
    }

    public void attachWires(List<Wire> arg0) {
    }

    public List<Wire> getWires(String arg0) {
        return null;
    }

    public TargetInvoker createTargetInvoker(String targetName, final Operation operation, boolean callback)
        throws TargetInvokerCreationException {
        Invoker invoker = ia.getInvokerFactory(impl).createInvoker(operation);
        return new ImplTargetInvoker(invoker, this, scopeContainer, workContext);
    }

    @Override
    public ComponentContext getComponentContext() {
        return componentContext;
    }

    public <B, R extends CallableReference<B>> R cast(B target) {
        return null;
    }

    public <B> B getProperty(Class<B> type, String propertyName) {
        return null;
    }

    public <B> B getService(Class<B> businessInterface, String referenceName) {
        return null;
    }

    public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, String referenceName) {
        return new ServiceReferenceImpl<B>(businessInterface, new SingletonObjectFactory<B>((B)createInstance()));
    }
    public void setDataBindingRegistry(DataBindingExtensionPoint dataBindingRegistry) {
        this.dataBindingRegistry = dataBindingRegistry;
    }

    public void setPropertyValueObjectFactory(PropertyValueObjectFactory propertyValueObjectFactory) {
        this.propertyValueObjectFactory = propertyValueObjectFactory;
    }
    
    public void initializePropertyValueFactories(List<ComponentProperty> properties) {
        ObjectFactory<?> propertyObjectFactory = null;
        
        for (ComponentProperty aProperty : properties) {
            if (aProperty.getValue() != null) {
                propertyObjectFactory = propertyValueObjectFactory.createValueFactory(aProperty);
                propertyValueFactories.put(aProperty.getName(), propertyObjectFactory);
            }
        }
    }
}