summaryrefslogtreecommitdiffstats
path: root/sandbox/event/modules/contribution-jee/src/main/java/org/apache/tuscany/sca/contribution/jee/WebModuleProcessor.java
blob: deb9d9e755ac31bf13ec8b041a160d21772e69a5 (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
/*
 * 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.contribution.jee;

import java.util.Map;

import org.apache.openejb.config.WebModule;
import org.apache.openejb.jee.EjbRef;
import org.apache.openejb.jee.EjbRefType;
import org.apache.openejb.jee.EjbReference;
import org.apache.openejb.jee.EnvEntry;
import org.apache.openejb.jee.WebApp;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentProperty;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentType;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.CompositeReference;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.contribution.DefaultModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
import org.apache.tuscany.sca.contribution.service.ContributionException;
import org.apache.tuscany.sca.implementation.web.WebImplementation;
import org.apache.tuscany.sca.implementation.web.WebImplementationFactory;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;

public class WebModuleProcessor {
    private WebModule webModule;
    private ComponentType componentType;
    private AssemblyHelper helper;

    public WebModuleProcessor(WebModule webModule, AssemblyHelper helper) {
        super();
        this.webModule = webModule;
        this.helper = helper;
    }

    public WebModuleProcessor(WebModule module) {
        webModule = module;
        helper = new AssemblyHelper();
    }

    public ComponentType getWebAppComponentType() throws ContributionException {
        if (componentType != null) {
            return componentType;
        }
        componentType = helper.createComponentType();

        WebApp webApp = webModule.getWebApp();
        ClassLoader classLoader = webModule.getClassLoader();

        // Process Remote EJB References
        for (Map.Entry<String, EjbRef> entry : webApp.getEjbRefMap().entrySet()) {
            EjbRef ejbRef = entry.getValue();
            if(ejbRef.getHome() != null) {
                // References to only EJB3 beans need to be considered.
                // Skip the current on as it is not a reference to an EJB3 bean.
                continue;
            }
            if (ejbRef.getRefType().compareTo(EjbReference.Type.REMOTE) != 0) {
                // Only Remote EJB references need to be considered.
                // Skip the current one as it is not a remote reference.
                continue;
            }
            //FIXME: ejbRef.getEjbRefType() is null sometimes.  Need a different way to figure the type.
            if(ejbRef.getEjbRefType() != null && ejbRef.getEjbRefType().compareTo(EjbRefType.SESSION) != 0) {
                // Only references to Session beans need to be considered.
                // Skip the current one as it is not a Session bean.
                continue;
            }
            String referenceName = entry.getKey();
            referenceName = referenceName.replace("/", "_");
            Reference reference = helper.createComponentReference();
            reference.setName(referenceName);
            InterfaceContract ic = null;
            try {
                Class<?> clazz = classLoader.loadClass(ejbRef.getInterface());
                ic = helper.createInterfaceContract(clazz);
            } catch (Exception e) {
                componentType = null;
                throw new ContributionException(e);
            }
            reference.setInterfaceContract(ic);
            reference.getRequiredIntents().add(AssemblyHelper.EJB_INTENT);
            componentType.getReferences().add(reference);
        }

        // Process env-entries to compute properties
        for (Map.Entry<String, EnvEntry> entry : webApp.getEnvEntryMap().entrySet()) {
            EnvEntry envEntry = entry.getValue();
            String type = envEntry.getEnvEntryType();
            if (!AssemblyHelper.ALLOWED_ENV_ENTRY_TYPES.containsKey(type)) {
                continue;
            }
            String propertyName = entry.getKey();
            propertyName = propertyName.replace("/", "_");
            String value = envEntry.getEnvEntryValue();
            Property property = helper.createComponentProperty();
            property.setName(propertyName);
            property.setXSDType(AssemblyHelper.ALLOWED_ENV_ENTRY_TYPES.get(type));
            property.setValue(value);
            componentType.getProperties().add(property);
        }

        return componentType;
    }

    public Composite getWebAppComposite() throws ContributionException {
        getWebAppComponentType();

        Composite composite = helper.createComposite();

        ModelFactoryExtensionPoint mfep = new DefaultModelFactoryExtensionPoint();
        WebImplementationFactory wif = mfep.getFactory(WebImplementationFactory.class);
        WebImplementation impl = wif.createWebImplementation();
        impl.setWebURI(webModule.getModuleId());

        // Create component
        Component component = helper.createComponent();
        String componentName = webModule.getModuleId();
        component.setName(componentName);
        component.setImplementation(impl);

        // Add references
        for (Reference reference : componentType.getReferences()) {
            ComponentReference componentReference = helper.createComponentReference();
            componentReference.setReference(reference);
            componentReference.setInterfaceContract(reference.getInterfaceContract());
            componentReference.getRequiredIntents().addAll(reference.getRequiredIntents());
            component.getReferences().add(componentReference);
        }

        // Add properties
        for (Property property : componentType.getProperties()) {
            ComponentProperty componentProperty = helper.createComponentProperty();
            componentProperty.setProperty(property);
            component.getProperties().add(componentProperty);
        }

        // Add component to composite
        composite.getComponents().add(component);

        // Add composite references
        for (ComponentReference reference : component.getReferences()) {
            CompositeReference compositeReference = helper.createCompositeReference();
            compositeReference.setInterfaceContract(reference.getInterfaceContract());
            compositeReference.getRequiredIntents().addAll(reference.getRequiredIntents());
            compositeReference.getPromotedReferences().add(reference);
            composite.getReferences().add(compositeReference);
        }

        return composite;
    }
}