summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/1.6-TUSCANY-3909/host-openejb/src/main/java/org/apache/tuscany/sca/host/openejb/OpenEJBServer.java
blob: 82e619ddbec4cb15efa588cf49f8e722df153b69 (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
/*
 * 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.host.openejb;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;

import org.apache.openejb.OpenEJB;
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.assembler.classic.Assembler;
import org.apache.openejb.assembler.classic.StatelessSessionContainerInfo;
import org.apache.openejb.config.ConfigurationFactory;
import org.apache.openejb.core.ServerFederation;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.StatelessBean;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.server.ServiceDaemon;
import org.apache.openejb.server.ServiceException;
import org.apache.openejb.server.ejbd.EjbServer;
import org.apache.tuscany.sca.host.ejb.EJBHost;
import org.apache.tuscany.sca.host.ejb.EJBRegistrationException;
import org.apache.tuscany.sca.host.ejb.EJBSessionBean;

/**
 * OpenEJB-based EJB host implementation. 
 *
 * @version $Rev: $ $Date: $
 */
public class OpenEJBServer implements EJBHost {

    private boolean started;
    private EjbServer ejbServer;
    private ServiceDaemon serviceDaemon;
    private ConfigurationFactory config;
    private Assembler assembler;
    
    public void addSessionBean(String ejbName, EJBSessionBean sessionBean) throws EJBRegistrationException {
        if (!started) {
            start();
        }
        
        try {
            StatelessBean bean = new StatelessBean(ejbName, sessionBean.getImplementationClass());
            bean.addBusinessRemote(sessionBean.getRemoteInterface().getName());
            bean.addPostConstruct("init");
            bean.addPreDestroy("destroy");

            EjbJar ejbJar = new EjbJar();
            ejbJar.addEnterpriseBean(bean);

            assembler.createApplication(config.configureApplication(ejbJar));
            
        } catch (NamingException e) {
            throw new EJBRegistrationException(e);
        } catch (IOException e) {
            throw new EJBRegistrationException(e);
        } catch (OpenEJBException e) {
            throw new EJBRegistrationException(e);
        }
    }

    public EJBSessionBean getSessionBean(String ejbName) throws EJBRegistrationException {
        // TODO Auto-generated method stub
        return null;
    }

    public EJBSessionBean removeSessionBean(String ejbName) throws EJBRegistrationException {
        // TODO Auto-generated method stub
        return null;
    }
    
    /**
     * Start the OpenEJB server.
     * 
     * @throws EJBRegistrationException
     */
    private void start() throws EJBRegistrationException {
        try {
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
            properties.put(Context.PROVIDER_URL, "ejbd://localhost:2888");
            SystemInstance.init(properties);
            
            ejbServer = new EjbServer();
            SystemInstance.get().setComponent(EjbServer.class, ejbServer);
            OpenEJB.init(properties, new ServerFederation());
            ejbServer.init(properties);
            
            serviceDaemon = new ServiceDaemon(ejbServer, 2888, "localhost");
            serviceDaemon.start();
            
            config = new ConfigurationFactory();
            assembler = (Assembler)SystemInstance.get().getComponent(org.apache.openejb.spi.Assembler.class);

            // containers
            StatelessSessionContainerInfo statelessContainerInfo = config.configureService(StatelessSessionContainerInfo.class);
            statelessContainerInfo.properties.setProperty("TimeOut", "10");
            statelessContainerInfo.properties.setProperty("PoolSize", "0");
            statelessContainerInfo.properties.setProperty("StrictPooling", "false");
            assembler.createContainer(statelessContainerInfo);
            
        } catch (OpenEJBException e) {
            throw new EJBRegistrationException(e);
        } catch (Exception e) {
            throw new EJBRegistrationException(e);
        }
        
        started = true;
    }
    
    /**
     * Stop the OpenEJB server.
     */
    void stop() {
        if (started) {
            try {
                serviceDaemon.stop();
            } catch (ServiceException e) {
                throw new EJBRegistrationException(e);
            }
        }
        SystemInstance.get().removeComponent(EjbServer.class);
        OpenEJB.destroy();
    }

}