summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringBeanIntrospector.java
blob: c74d4abed9fde6dbaa99e7d53de8f954fb54a902 (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.implementation.spring.introspect;

import java.util.List;

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.ComponentType;
import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
import org.apache.tuscany.sca.implementation.java.IntrospectionException;
import org.apache.tuscany.sca.implementation.java.JavaImplementation;
import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
import org.apache.tuscany.sca.implementation.java.introspect.impl.BaseJavaClassVisitor;
import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ComponentNameProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ConstructorProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ContextProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.DestroyProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.EagerInitProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.InitProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.PropertyProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ReferenceProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ResourceProcessor;
import org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor;
import org.apache.tuscany.sca.implementation.spring.SpringConstructorArgElement;
import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
import org.apache.tuscany.sca.policy.PolicyFactory;

/**
 * Provides introspection functions for Spring beans
 * This version leans heavily on the implementation-java classes
 *
 * @version $Rev: 512919 $ $Date: 2007-02-28 19:32:56 +0000 (Wed, 28 Feb 2007) $
 */
public class SpringBeanIntrospector {

    private JavaImplementationFactory javaImplementationFactory;

    /**
     * The constructor sets up the various visitor elements that will be used to introspect
     * the Spring bean and extract SCA information.
     *
     * @param assemblyFactory The Assembly Factory to use
     * @param javaFactory The Java Interface Factory to use
     * @param policyFactory The Policy Factory to use.
     */
    public SpringBeanIntrospector(AssemblyFactory assemblyFactory,
                                  JavaInterfaceFactory javaFactory,
                                  PolicyFactory policyFactory,
                                  List<SpringConstructorArgElement> conArgs) {

        javaImplementationFactory = new DefaultJavaImplementationFactory();
        
        // Create the list of class visitors
        BaseJavaClassVisitor[] extensions =
            new BaseJavaClassVisitor[] {
                                        new ConstructorProcessor(assemblyFactory),
                                        new ComponentNameProcessor(assemblyFactory),
                                        new ContextProcessor(assemblyFactory),
                                        new DestroyProcessor(assemblyFactory),
                                        new EagerInitProcessor(assemblyFactory),
                                        new InitProcessor(assemblyFactory),
                                        new PropertyProcessor(assemblyFactory),
                                        new ReferenceProcessor(assemblyFactory, javaFactory),
                                        new ResourceProcessor(assemblyFactory),
                                        new ServiceProcessor(assemblyFactory, javaFactory),
                                        new SpringBeanPojoProcessor(assemblyFactory, javaFactory, conArgs),
                                        new PolicyProcessor(assemblyFactory, policyFactory)};
        for (JavaClassVisitor extension : extensions) {
            javaImplementationFactory.addClassVisitor(extension);
        }

    } // end constructor 

    /**
     * Introspect a Spring Bean and extract the features important to SCA
     * @param beanClass the Spring Bean class to introspect
     * @param componentType the componentType that is filled in through the introspection
     * process (assumed empty on invocation, filled on return
     * @return a Map of property names to JavaElementImpl
     * @throws ContributionResolveException - if there was a problem resolving the
     * Spring Bean or its componentType
     *
     */
    public JavaImplementation introspectBean(Class<?> beanClass, ComponentType componentType) throws ContributionResolveException 
    {
        if (componentType == null)
            throw new ContributionResolveException("Introspect Spring bean: supplied componentType is null");

        // Create a Java implementation ready for the introspection
        JavaImplementation javaImplementation = javaImplementationFactory.createJavaImplementation();

        try {
            // Introspect the bean...the results of the introspection are placed into the Java implementation
            javaImplementationFactory.createJavaImplementation(javaImplementation, beanClass);

            // Extract the services, references & properties found through introspection
            // put the services, references and properties into the component type
            componentType.getServices().addAll(javaImplementation.getServices());
            componentType.getReferences().addAll(javaImplementation.getReferences());
            componentType.getProperties().addAll(javaImplementation.getProperties());
            
        } catch (IntrospectionException e) {
            throw new ContributionResolveException(e);
        } // end try

        /* List<Service> services = javaImplementation.getServices();
        for (Service service : services) {
            String name = service.getName();
            System.out.println("Spring Bean: found service with name: " + name);
        } // end for */
        
        return javaImplementation;

    } // end method introspectBean

} // end class SpringBeanIntrospector