summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/1.6-TUSCANY-3909/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/impl/ImplementationsActivator.java
blob: a861a8ddf0ce42ecb6bfc3408989c14b5b211435 (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
/*
 * 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.extension.helper.impl;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.xml.Constants;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.ModuleActivator;
import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.databinding.Mediator;
import org.apache.tuscany.sca.extension.helper.ImplementationActivator;
import org.apache.tuscany.sca.extension.helper.utils.DefaultPropertyValueObjectFactory;
import org.apache.tuscany.sca.extension.helper.utils.PropertyValueObjectFactory;
import org.apache.tuscany.sca.provider.ImplementationProvider;
import org.apache.tuscany.sca.provider.ImplementationProviderFactory;
import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint;
import org.apache.tuscany.sca.runtime.RuntimeComponent;

/**
 * A Tuscany ModuleActivator which activates all the ImplementationActivators
 *
 * @version $Rev$ $Date$
 */
public class ImplementationsActivator implements ModuleActivator {

    protected List<ImplementationActivator> implementationActivators;

    public void start(ExtensionPointRegistry registry) {

        ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class);
        AssemblyFactory assemblyFactory = factories.getFactory(AssemblyFactory.class);

        Mediator mediator = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Mediator.class);

        //FIXME Pass this factory differently as it's not an extension point
        PropertyValueObjectFactory propertyFactory = new DefaultPropertyValueObjectFactory(mediator);
        registry.addExtensionPoint(propertyFactory);

        this.implementationActivators = DiscoveryUtils.discoverActivators(ImplementationActivator.class, registry);

        StAXArtifactProcessorExtensionPoint staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
        ProviderFactoryExtensionPoint providerFactories = registry.getExtensionPoint(ProviderFactoryExtensionPoint.class);

        for (final ImplementationActivator implementationActivator : implementationActivators) {

            Class<Implementation> implClass = implementationActivator.getImplementationClass();
            QName scdlQName = getSCDLQName(implClass);
            staxProcessors.addArtifactProcessor(new SCDLProcessor(assemblyFactory, scdlQName, implClass, registry, factories));

            if (implementationActivator.getImplementationClass() != null && providerFactories != null) {
                addImplementationProvider(implementationActivator, providerFactories);
            }
        }
    }

    public void stop(ExtensionPointRegistry registry) {
        StAXArtifactProcessorExtensionPoint staxProcessors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);

        for (final ImplementationActivator implementationActivator : implementationActivators) {
            if (staxProcessors != null) {
                StAXArtifactProcessor processor = staxProcessors.getProcessor(getSCDLQName(implementationActivator.getImplementationClass()));
                if (processor != null) {
                    staxProcessors.removeArtifactProcessor(processor);
                }
            }
        }
    }

    private void addImplementationProvider(final ImplementationActivator implementationActivator, ProviderFactoryExtensionPoint providerFactories) {

        providerFactories.addProviderFactory(new ImplementationProviderFactory() {
            public ImplementationProvider createImplementationProvider(final RuntimeComponent rc, final Implementation impl) {
                if (impl instanceof PojoImplementation) {
                    return new ImplementationImplementationProvider(implementationActivator, rc, impl, ((PojoImplementation)impl).getUserImpl());
                } else {
                    return new ImplementationImplementationProvider(implementationActivator, rc, impl, impl);
                }
            }
            public Class getModelType() {
                Class c = implementationActivator.getImplementationClass();

                if (Implementation.class.isAssignableFrom(c)) {
                    return c;
                } else {
                    return PojoImplementation.class;
                }
            }
        });
    }

    protected QName getSCDLQName(Class implementationClass) {
        String localName = implementationClass.getName();
        if (localName.lastIndexOf('.') > -1) {
            localName = localName.substring(localName.lastIndexOf('.') + 1);
        }
        if (localName.endsWith("Implementation")) {
            localName = localName.substring(0, localName.length() - 14);
        }
        StringBuilder sb = new StringBuilder(localName);
        for (int i=0; i<sb.length(); i++) {
            if (Character.isUpperCase(sb.charAt(i))) {
                sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
            } else {
                break;
            }
        }
        return new QName(Constants.SCA10_TUSCANY_NS, "implementation." + sb.toString());
    }

}