001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.component;
018:
019: import java.util.Arrays;
020: import java.util.Properties;
021: import java.util.concurrent.Callable;
022: import java.util.concurrent.atomic.AtomicReference;
023:
024: import javax.naming.NamingException;
025: import javax.sql.DataSource;
026: import javax.xml.namespace.QName;
027:
028: import org.apache.activemq.ActiveMQConnectionFactory;
029: import org.apache.commons.dbcp.BasicDataSource;
030: import org.bpmscript.IProcessInstanceManager;
031: import org.bpmscript.IProcessManager;
032: import org.bpmscript.LockingProcessInstanceManager;
033: import org.bpmscript.ProcessExecutor;
034: import org.bpmscript.XStreamMarshaler;
035: import org.bpmscript.db.HibernateProcessInstanceManager;
036: import org.bpmscript.db.HibernateProcessManager;
037: import org.bpmscript.db.ProcessStateCreator;
038: import org.bpmscript.jbi.PojoServiceEndpoint;
039: import org.bpmscript.jbi.component.db.HibernateExchangeStore;
040: import org.bpmscript.jms.JmsTemplate;
041: import org.bpmscript.remote.RemoteProcessManager;
042: import org.hibernate.SessionFactory;
043: import org.springframework.aop.framework.ProxyFactoryBean;
044: import org.springframework.jdbc.support.lob.DefaultLobHandler;
045: import org.springframework.jndi.JndiTemplate;
046: import org.springframework.orm.hibernate3.HibernateInterceptor;
047: import org.springframework.orm.hibernate3.HibernateTransactionManager;
048: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
049: import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
050: import org.springframework.transaction.interceptor.TransactionInterceptor;
051:
052: public class ClusteredBpmScriptServicesFactory implements
053: IBpmScriptServicesFactory {
054:
055: private final transient org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
056: .getLog(getClass());
057:
058: public static final String PROCESS_MANAGER_JNDI_NAME = "java:comp/env/jdbc/bpmscript/processmanager";
059: public static final String PROCESS_MANAGER_DIALECT_JNDI_NAME = "java:comp/env/jdbc/bpmscript/processmanager_dialect";
060: public static final String JMS_URL_JNDI_NAME = "java:comp/env/jms/bpmscript/jms_url";
061:
062: private ClusteredBpmScriptConfiguration configuration = new ClusteredBpmScriptConfiguration();
063:
064: {
065: configuration.load();
066: }
067:
068: public Object getConfiguration() {
069: return configuration;
070: }
071:
072: public IBpmScriptServices getServices() throws Exception {
073:
074: Thread.currentThread().setContextClassLoader(
075: getClass().getClassLoader());
076:
077: DataSource jndiDataSource = null;
078: try {
079: JndiTemplate jndiTemplate = new JndiTemplate();
080: jndiDataSource = (DataSource) jndiTemplate
081: .lookup(PROCESS_MANAGER_JNDI_NAME);
082: } catch (NamingException n) {
083: log.debug(n.getMessage());
084: }
085:
086: String jndiDialect = null;
087: try {
088: JndiTemplate jndiTemplate = new JndiTemplate();
089: jndiDialect = (String) jndiTemplate
090: .lookup(PROCESS_MANAGER_DIALECT_JNDI_NAME);
091: } catch (NamingException n) {
092: log.debug(n.getMessage());
093: }
094:
095: String jndiJmsUrl = null;
096: try {
097: JndiTemplate jndiTemplate = new JndiTemplate();
098: jndiJmsUrl = (String) jndiTemplate
099: .lookup(JMS_URL_JNDI_NAME);
100: } catch (NamingException n) {
101: log.debug(n.getMessage());
102: }
103:
104: final AtomicReference<BasicDataSource> basicDataSourceReference = new AtomicReference<BasicDataSource>();
105: if (jndiDataSource == null) {
106: basicDataSourceReference.set(new BasicDataSource());
107: basicDataSourceReference.get().setDriverClassName(
108: configuration.getDataSourceDriverClassName());
109: basicDataSourceReference.get().setUrl(
110: configuration.getDataSourceUrl());
111: basicDataSourceReference.get().setUsername(
112: configuration.getDataSourceUsername());
113: basicDataSourceReference.get().setPassword(
114: configuration.getDataSourcePassword());
115: }
116:
117: final DataSource dataSource = jndiDataSource != null ? jndiDataSource
118: : basicDataSourceReference.get();
119:
120: Properties properties = new Properties();
121: properties.setProperty("hibernate.hbm2ddl.auto", "update");
122: properties.setProperty("hibernate.dialect",
123: jndiDialect != null ? jndiDialect : configuration
124: .getHibernateDialect());
125: properties.setProperty("hibernate.query.substitutions",
126: "true 1, false 0");
127: properties.setProperty("hibernate.show_sql", "false");
128:
129: final LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
130: sessionFactoryBean.setLobHandler(new DefaultLobHandler());
131: sessionFactoryBean.setHibernateProperties(properties);
132: sessionFactoryBean.setMappingResources(new String[] {
133: "/org/bpmscript/db/DbProcess.hbm.xml",
134: "/org/bpmscript/db/DbProcessSource.hbm.xml",
135: "/org/bpmscript/db/DbProcessInstance.hbm.xml",
136: "/org/bpmscript/db/DbProcessInstanceState.hbm.xml",
137: "/org/bpmscript/jbi/component/db/DbExchange.hbm.xml" });
138: sessionFactoryBean.setDataSource(dataSource);
139: sessionFactoryBean.afterPropertiesSet();
140:
141: final SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean
142: .getObject();
143: HibernateTransactionManager transactionManager = new HibernateTransactionManager();
144: transactionManager.setSessionFactory(sessionFactory);
145: transactionManager.afterPropertiesSet();
146:
147: HibernateInterceptor hibernateInterceptor = new HibernateInterceptor();
148: hibernateInterceptor.setSessionFactory(sessionFactory);
149: hibernateInterceptor.afterPropertiesSet();
150:
151: TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
152: transactionInterceptor
153: .setTransactionManager(transactionManager);
154: transactionInterceptor
155: .setTransactionAttributeSource(new MatchAlwaysTransactionAttributeSource());
156: transactionInterceptor.afterPropertiesSet();
157:
158: HibernateProcessManager hibernateProcessManager = new HibernateProcessManager();
159: hibernateProcessManager.setSessionFactory(sessionFactory);
160: hibernateProcessManager.afterPropertiesSet();
161:
162: ProxyFactoryBean processManagerProxyFactoryBean = new ProxyFactoryBean();
163: processManagerProxyFactoryBean
164: .setProxyInterfaces(new Class[] { IProcessManager.class });
165: processManagerProxyFactoryBean.addAdvice(hibernateInterceptor);
166: processManagerProxyFactoryBean
167: .addAdvice(transactionInterceptor);
168: processManagerProxyFactoryBean
169: .setTarget(hibernateProcessManager);
170: IProcessManager processManager = (IProcessManager) processManagerProxyFactoryBean
171: .getObject();
172:
173: HibernateProcessInstanceManager hibernateProcessInstanceManager = new HibernateProcessInstanceManager();
174: hibernateProcessInstanceManager
175: .setSessionFactory(sessionFactory);
176: hibernateProcessInstanceManager.afterPropertiesSet();
177:
178: ProxyFactoryBean processInstanceManagerProxyFactoryBean = new ProxyFactoryBean();
179: processInstanceManagerProxyFactoryBean
180: .setProxyInterfaces(new Class[] { IProcessInstanceManager.class });
181: processInstanceManagerProxyFactoryBean
182: .addAdvice(hibernateInterceptor);
183: processInstanceManagerProxyFactoryBean
184: .addAdvice(transactionInterceptor);
185: processInstanceManagerProxyFactoryBean
186: .setTarget(hibernateProcessInstanceManager);
187: IProcessInstanceManager processInstanceManagerAdvised = (IProcessInstanceManager) processInstanceManagerProxyFactoryBean
188: .getObject();
189: LockingProcessInstanceManager processInstanceManager = new LockingProcessInstanceManager(
190: processInstanceManagerAdvised);
191:
192: ProcessExecutor processExecutor = new ProcessExecutor(
193: processManager);
194: processExecutor.setCompress(true);
195: processExecutor.setLibraries(Arrays.asList(configuration
196: .getProcessExecutorLibraries().split(",")));
197:
198: final JmsTemplate template = new JmsTemplate();
199: template.setConnectionFactory(new ActiveMQConnectionFactory(
200: jndiJmsUrl != null ? jndiJmsUrl : configuration
201: .getJmsUrl()));
202: template.afterPropertiesSet();
203:
204: JmsAlarmEndpoint alarmEndpoint = new JmsAlarmEndpoint();
205: alarmEndpoint.setEndpoint("alarm");
206: HibernateExchangeStore exchangeStoreTarget = new HibernateExchangeStore();
207: exchangeStoreTarget.setSessionFactory(sessionFactory);
208: exchangeStoreTarget.afterPropertiesSet();
209:
210: ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
211: proxyFactoryBean
212: .setProxyInterfaces(new Class[] { IExchangeStore.class });
213: proxyFactoryBean.addAdvice(hibernateInterceptor);
214: proxyFactoryBean.addAdvice(transactionInterceptor);
215: proxyFactoryBean.setTarget(exchangeStoreTarget);
216:
217: IExchangeStore exchangeStore = (IExchangeStore) proxyFactoryBean
218: .getObject();
219:
220: String defaultNamespace = "http://bpmscript.org/jbi";
221:
222: alarmEndpoint.setExchangeStore(exchangeStore);
223: alarmEndpoint.setTemplate(template);
224: alarmEndpoint.setService(new QName(defaultNamespace, "alarm"));
225: alarmEndpoint.setTopicName("org.bpmscript.alarm.topic");
226:
227: CorellationEndpoint corellationEndpoint = new CorellationEndpoint();
228: corellationEndpoint.setEndpoint("corellation");
229: corellationEndpoint.setService(new QName(defaultNamespace,
230: "corellation"));
231: corellationEndpoint
232: .setLibrary("classpath:/org/bpmscript/jbi/component/corellation.js");
233:
234: RemoteProcessManager remoteProcessManager = new RemoteProcessManager();
235: remoteProcessManager.setProcessManager(processManager);
236: remoteProcessManager
237: .setProcessInstanceManager(processInstanceManager);
238:
239: PojoServiceEndpoint remoteProcessManagerEndpoint = new PojoServiceEndpoint();
240: remoteProcessManagerEndpoint.setPojo(remoteProcessManager);
241: remoteProcessManagerEndpoint
242: .setEndpoint("remoteProcessManager");
243: remoteProcessManagerEndpoint.setService(new QName(
244: defaultNamespace, "remoteProcessManager"));
245: remoteProcessManagerEndpoint
246: .setMarshaler(new XStreamMarshaler());
247:
248: ProcessStateCreator creator = new ProcessStateCreator();
249: creator.setSessionFactory(sessionFactory);
250: creator.afterPropertiesSet();
251:
252: BpmScriptServices services = new BpmScriptServices(
253: processManager, processInstanceManager,
254: processExecutor, alarmEndpoint, corellationEndpoint,
255: remoteProcessManagerEndpoint, defaultNamespace,
256: new Callable<Void>() {
257:
258: public Void call() throws Exception {
259: sessionFactoryBean.destroy();
260: sessionFactory.close();
261: BasicDataSource basicDataSource = basicDataSourceReference
262: .get();
263: if (basicDataSource != null) {
264: basicDataSource.close();
265: }
266: template.destroy();
267: return null;
268: }
269:
270: });
271:
272: return services;
273: }
274:
275: }
|