summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-final/java/sca/core/src/main/java/org/apache/tuscany/core/config/processor/ServiceProcessor.java
blob: 2c7ba823f204381da3e6c16d3465a05947e9ec17 (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
/**
 *
 * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 * Licensed 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.config.processor;

import java.util.List;

import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.InvalidMetaDataException;
import org.apache.tuscany.core.config.JavaIntrospectionHelper;
import org.apache.tuscany.model.assembly.AssemblyFactory;
import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.assembly.Service;
import org.apache.tuscany.model.types.java.JavaServiceContract;
import org.osoa.sca.annotations.Callback;

/**
 * Processes the {@link org.osoa.sca.annotations.Service} annotation
 *
 * @version $$Rev$$ $$Date$$
 */
public class ServiceProcessor extends ImplementationProcessorSupport {

    public ServiceProcessor() {
    }

    public ServiceProcessor(AssemblyFactory factory) {
        super(factory);
    }

    @Override
    public void visitClass(Class<?> clazz, ComponentType type) throws ConfigurationLoadException {
        if (!clazz.isInterface()) {
            processImplementation(clazz,type);
        } else {
            processInterface(clazz, type);
        }
    }

    private void processImplementation(Class<?> clazz, ComponentType type) throws ConfigurationLoadException {
        // visiting the base implementation class
        List<org.apache.tuscany.model.assembly.Service> services = type.getServices();
        Class[] interfaces = clazz.getInterfaces();
        org.osoa.sca.annotations.Service serviceAnnotation = clazz.getAnnotation(org.osoa.sca.annotations.Service.class);
        if (interfaces.length == 0) {
            // no interfaces so the class is the service
            addService(services, clazz);
        } else if (serviceAnnotation == null && interfaces.length == 1) {
            // the impl has one interface, assign it to be the service
            addService(services, interfaces[0]);
        } else {
            // visiting the implementation class
            if (serviceAnnotation == null) {
                return;
            }
            Class<?>[] serviceInterfaces = serviceAnnotation.interfaces();
            Class<?> value = serviceAnnotation.value();
            if (serviceInterfaces.length > 0) {
                if (!Void.class.equals(value)) {
                    InvalidMetaDataException e = new InvalidMetaDataException("Both interfaces and value specified in @Service on ");
                    e.setIdentifier(clazz.getName());
                    throw e;
                }
                for (Class<?> intf : interfaces) {
                    addService(services, intf);
                }
            } else if (!Void.class.equals(value)) {
                addService(services, value);
            }
        }
    }


    @Override
    public void visitEnd(Class<?> clazz, ComponentType type) throws ConfigurationLoadException {
        List<Service> services = type.getServices();
        if (services.size() == 0) {
            // no services processed so the class is the service
            addService(services, clazz);
        }
    }

    private void processInterface(Class<?> clazz, ComponentType type) {
        List<org.apache.tuscany.model.assembly.Service> services = type.getServices();
        // the interface is a remotable service, add it
        org.osoa.sca.annotations.Remotable remotableAnnotation = clazz.getAnnotation(org.osoa.sca.annotations.Remotable.class);
        if (remotableAnnotation != null) {
            // check to see if service added previously b/c it was specified on @Service
            if (ProcessorHelper.getService(clazz, services) == null) {
                addService(services, clazz);
            }
        }
    }


    private void addService(List<Service> services, Class<?> serviceClass) {
        //FIXME how do we support specifying remotable?
        JavaServiceContract javaInterface = factory.createJavaServiceContract();
        javaInterface.setInterface(serviceClass);
        org.osoa.sca.annotations.Scope scopeAnnotation = serviceClass.getAnnotation(org.osoa.sca.annotations.Scope.class);
        Scope scope;
        if (scopeAnnotation == null) {
            scope = Scope.INSTANCE;
        } else {
            scope = ProcessorHelper.getScope(scopeAnnotation);
        }
        javaInterface.setScope(scope);
        Callback callback = serviceClass.getAnnotation(Callback.class);
        if (callback != null && !Void.class.equals(callback.value())) {
            javaInterface.setCallbackInterface(callback.value());
        }
        String name = JavaIntrospectionHelper.getBaseName(serviceClass);
        Service service = factory.createService();
        service.setName(name);
        service.setServiceContract(javaInterface);
        services.add(service);
    }
}