summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/implementation-bpel-runtime/src/main/java/org/apache/tuscany/sca/implementation/bpel/ode/EmbeddedODEServer.java
blob: 1069015963275666d0e59c3efd32ed90c5e462ff (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * 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.bpel.ode;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import javax.transaction.TransactionManager;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import org.apache.ode.axis2.BindingContextImpl;
//import org.apache.ode.axis2.EndpointReferenceContextImpl;
//import org.apache.ode.axis2.MessageExchangeContextImpl;
//import org.apache.ode.axis2.EndpointReferenceContextImpl;
import org.apache.ode.bpel.dao.BpelDAOConnectionFactoryJDBC;
import org.apache.ode.bpel.engine.BpelServerImpl;
import org.apache.ode.bpel.engine.CountLRUDehydrationPolicy;
import org.apache.ode.bpel.iapi.Scheduler;
import org.apache.ode.bpel.memdao.BpelDAOConnectionFactoryImpl;
import org.apache.ode.il.config.OdeConfigProperties;
import org.apache.ode.il.dbutil.Database;
import org.apache.ode.scheduler.simple.JdbcDelegate;
import org.apache.ode.scheduler.simple.SimpleScheduler;
import org.apache.ode.utils.GUID;
import org.apache.tuscany.sca.implementation.bpel.BPELImplementation;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.eclipse.core.runtime.FileLocator;



/**
 * Embedded ODE process server
 * 
 * @version $Rev$ $Date$
 */
public class EmbeddedODEServer {
    protected final Log __log = LogFactory.getLog(getClass());

    private boolean _initialized;

    private OdeConfigProperties _config;

    private TransactionManager _txMgr;

    private Database _db;

    private File _workRoot;

    private BpelDAOConnectionFactoryJDBC _daoCF;

    private BpelServerImpl _bpelServer;

    private Scheduler _scheduler;
    
    protected ExecutorService _executorService;

    private Map<QName, RuntimeComponent> tuscanyRuntimeComponents = new ConcurrentHashMap<QName, RuntimeComponent>();
    
    public EmbeddedODEServer(TransactionManager txMgr) {
        _txMgr = txMgr;
    }
    
    public void init() throws ODEInitializationException {
        Properties p = System.getProperties();
        p.put("derby.system.home", "target");

        Properties confProps = new Properties();
        confProps.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=false)");
        // MJE 30/06/2009 - testing - limit the thread pools to max = 1 thread
        confProps.put("ode-scathreads.pool.size", "1" );
        
        _config = new OdeConfigProperties(confProps, "ode-sca");

        // Setting work root as the directory containing our database
        try {
        	_workRoot = getDatabaseLocationAsFile();
            //_workRoot = new File(dbLocation.toURI()).getParentFile();
        } catch (URISyntaxException e) {
            throw new ODEInitializationException(e);
        }

        initTxMgr();
        initPersistence();
        initBpelServer();

        try {
            _bpelServer.start();
        } catch (Exception ex) {
            String errmsg = "An error occured during the ODE BPEL server startup.";
            __log.error(errmsg, ex);
            throw new ODEInitializationException(errmsg, ex);
        }
        
        // Added MJE, 24/06/2009 - for 1.3.2 version of ODE
        _scheduler.start();
        // end of addition

        __log.info("ODE BPEL server started.");
        _initialized = true;
    }
    
    /**
     * Gets the location of the database used for the ODE BPEL engine as a File object for
     * the directory containing the database
     * @return
     * @throws ODEInitializationException
     * @throws URISyntaxException
     */
    private File getDatabaseLocationAsFile() throws ODEInitializationException,
                                                    URISyntaxException {
    	File locationFile = null;
    	// TODO - provide a system property / environment variable to set the path to the DB
    	
        URL dbLocation = getClass().getClassLoader().getResource("jpadb");
        if (dbLocation == null) {
            throw new ODEInitializationException("Couldn't find database in the classpath");
        }
        // Handle OSGI bundle case
        if( dbLocation.getProtocol() == "bundleresource" ) {
        	try {
	        	dbLocation = FileLocator.toFileURL( dbLocation );
        	} catch (Exception ce ) {
        		throw new ODEInitializationException("Couldn't find database in the OSGi bundle");
        	} // end try
        } // end if 

        locationFile = new File(dbLocation.toURI()).getParentFile();
    	return locationFile;
    } // end method getDatabaseLocationAsFile

    private void initTxMgr() {
        if(_txMgr == null) {
            try {
                GeronimoTxFactory txFactory = new GeronimoTxFactory();
                _txMgr = txFactory.getTransactionManager();
            } catch (Exception e) {
                __log.fatal("Couldn't initialize a transaction manager using Geronimo's transaction factory.", e);
                throw new ODEInitializationException("Couldn't initialize a transaction manager using " + "Geronimo's transaction factory.", e);
            }            
        }
    }

    private void initPersistence() {
        _db = new Database(_config);
        _db.setTransactionManager(_txMgr);
        _db.setWorkRoot(_workRoot);

        try {
            _db.start();
            _daoCF = _db.createDaoCF();
        } catch (Exception ex) {
            String errmsg = "Error while configuring ODE persistence.";
            __log.error(errmsg, ex);
            throw new ODEInitializationException(errmsg, ex);
        }
    }
  
    private void initBpelServer() {
        if (__log.isDebugEnabled()) {
            __log.debug("ODE initializing");
        }
        ThreadFactory threadFactory = new ThreadFactory() {
            int threadNumber = 0;
            public Thread newThread(Runnable r) {
                threadNumber += 1;
                Thread t = new Thread(r, "ODEServer-"+threadNumber);
                t.setDaemon(true);
                return t;
            }
        };

        _executorService = Executors.newCachedThreadPool(threadFactory);
        
        // executor service for long running bulk transactions
        ExecutorService _polledRunnableExecutorService = Executors.newCachedThreadPool(new ThreadFactory() {
            int threadNumber = 0;
            public Thread newThread(Runnable r) {
                threadNumber += 1;
                Thread t = new Thread(r, "PolledRunnable-"+threadNumber);
                t.setDaemon(true);
                return t;
            }
        });

        _bpelServer = new BpelServerImpl();
        _scheduler = createScheduler();
        _scheduler.setJobProcessor(_bpelServer);
        
        BpelServerImpl.PolledRunnableProcessor polledRunnableProcessor = new BpelServerImpl.PolledRunnableProcessor();
        polledRunnableProcessor.setPolledRunnableExecutorService(_polledRunnableExecutorService);
        polledRunnableProcessor.setContexts(_bpelServer.getContexts());
        //_scheduler.setPolledRunnableProcesser(polledRunnableProcessor);

        _bpelServer.setDaoConnectionFactory(_daoCF);
        _bpelServer.setInMemDaoConnectionFactory(new BpelDAOConnectionFactoryImpl(_scheduler));
        
        //_bpelServer.setEndpointReferenceContext(eprContext);
        _bpelServer.setMessageExchangeContext(new ODEMessageExchangeContext(this));
        _bpelServer.setBindingContext(new ODEBindingContext());
        _bpelServer.setScheduler(_scheduler);
        if (_config.isDehydrationEnabled()) {
            CountLRUDehydrationPolicy dehy = new CountLRUDehydrationPolicy();
            dehy.setProcessMaxAge(_config.getDehydrationMaximumAge());
            dehy.setProcessMaxCount(_config.getDehydrationMaximumCount());
            _bpelServer.setDehydrationPolicy(dehy);
        }
        
        System.out.println("TuscanyODEServer: threads.pool.size = " +
        		           _config.getThreadPoolMaxSize() );
        
        
        _bpelServer.setConfigProperties(_config.getProperties());
        _bpelServer.init();
        _bpelServer.setInstanceThrottledMaximumCount(_config.getInstanceThrottledMaximumCount());
        _bpelServer.setProcessThrottledMaximumCount(_config.getProcessThrottledMaximumCount());
        _bpelServer.setProcessThrottledMaximumSize(_config.getProcessThrottledMaximumSize());
        _bpelServer.setHydrationLazy(_config.isHydrationLazy());
        _bpelServer.setHydrationLazyMinimumSize(_config.getHydrationLazyMinimumSize());
    }

    public void stop() throws ODEShutdownException {
        if(_bpelServer != null) {
            try {
                __log.debug("Stopping BPEL Embedded server");
                _bpelServer.shutdown();
                _bpelServer = null;
            } catch (Exception ex) {
                __log.debug("Error stopping BPEL server");
            }
        }

        if(_scheduler != null) {
            try {
                __log.debug("Stopping scheduler");
                _scheduler.shutdown();
                _scheduler = null;
            } catch (Exception ex) {
                __log.debug("Error stopping scheduler");
            }
        }
        
        if(_daoCF != null) {
            try {
                __log.debug("Stopping DAO");
                _daoCF.shutdown();
                _daoCF = null;
            } catch (Exception ex) {
                __log.debug("Error stopping DAO");
            }
        }
        
        if(_db != null) {
            try {
                __log.debug("Stopping DB");
                _db.shutdown();
                _db = null;
            } catch (Exception ex) {
                __log.debug("Error stopping DB");
            }
        }
        
        if(_txMgr != null) {
            try {
                __log.debug("Stopping Transaction Manager");
                _txMgr = null;
            } catch (Exception ex) {
                __log.debug("Error stopping Transaction Manager");
            }
        }
    }

    protected Scheduler createScheduler() {
    	Properties odeProperties = new Properties();
    	// TODO Find correct values for these properties - MJE 22/06/2009
    	odeProperties.put("ode.scheduler.queueLength", "100" );
    	odeProperties.put("ode.scheduler.immediateInterval", "30000" );
    	odeProperties.put("ode.scheduler.nearFutureInterval", "600000" );
    	odeProperties.put("ode.scheduler.staleInterval", "100000" );
    	
        SimpleScheduler scheduler = new SimpleScheduler(new GUID().toString(),
        		                                        new JdbcDelegate(_db.getDataSource()),
                                                        odeProperties );
        scheduler.setTransactionManager(_txMgr);

        return scheduler;
    }

    public boolean isInitialized() {
        return _initialized;
    }

    public BpelServerImpl getBpelServer() {
        return _bpelServer;
    }
    
    public Scheduler getScheduler() {
        return _scheduler;
    }
    
    public ExecutorService getExecutor() {
    	return _executorService;
    }

    // Updated by Mike Edwards, 23/05/2008
    public void deploy(ODEDeployment d, BPELImplementation implementation) {
        try {
        	TuscanyProcessConfImpl processConf = new TuscanyProcessConfImpl( implementation );
        	_bpelServer.register(processConf);
        	__log.debug("Completed calling new Process deployment code...");
        } catch (Exception ex) {
            String errMsg = ">>> DEPLOY: Unexpected exception: " + ex.getMessage();
            __log.debug(errMsg, ex);
            throw new ODEDeploymentException(errMsg,ex);
        }
    }

    public void undeploy(ODEDeployment d) {
        //TODO
    }
    
    public void registerTuscanyRuntimeComponent(QName processName,RuntimeComponent componentContext) {
        tuscanyRuntimeComponents.put(processName, componentContext);
    }
        
    public RuntimeComponent getTuscanyRuntimeComponent(QName processName) {
        return tuscanyRuntimeComponents.get(processName);
    }
}