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.tasklist;
018:
019: import java.util.Properties;
020:
021: import javax.jbi.JBIException;
022: import javax.naming.NamingException;
023: import javax.sql.DataSource;
024: import javax.xml.namespace.QName;
025:
026: import org.apache.commons.dbcp.BasicDataSource;
027: import org.apache.servicemix.common.BaseComponent;
028: import org.apache.servicemix.common.BaseLifeCycle;
029: import org.apache.servicemix.common.xbean.XBeanServiceUnit;
030: import org.bpmscript.XStreamMarshaler;
031: import org.bpmscript.jbi.PojoServiceEndpoint;
032: import org.bpmscript.jbi.tasklist.db.HibernateTaskListStore;
033: import org.hibernate.SessionFactory;
034: import org.springframework.aop.framework.ProxyFactoryBean;
035: import org.springframework.jdbc.support.lob.DefaultLobHandler;
036: import org.springframework.jndi.JndiTemplate;
037: import org.springframework.orm.hibernate3.HibernateInterceptor;
038: import org.springframework.orm.hibernate3.HibernateTransactionManager;
039: import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
040: import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
041: import org.springframework.transaction.interceptor.TransactionInterceptor;
042:
043: public class TaskListLifeCycle extends BaseLifeCycle {
044:
045: private final transient org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
046: .getLog(getClass());
047:
048: public static final String TASK_LIST_JNDI_NAME = "java:comp/env/jdbc/bpmscript/tasklist";
049: public static final String TASK_LIST_DIALECT_JNDI_NAME = "java:comp/env/jdbc/bpmscript/tasklist_dialect";
050:
051: protected TaskListConfiguration configuration;
052: protected ITaskListStore taskListStore;
053: protected String defaultNamespace = "http://bpmscript.org/jbi";
054: protected PojoServiceEndpoint remoteTaskListEndpoint = null;
055: protected TaskListEndpoint taskListEndpoint = null;
056: protected XBeanServiceUnit defaultServiceUnit = null;
057:
058: public TaskListLifeCycle(BaseComponent component) {
059: super (component);
060: }
061:
062: @Override
063: protected void doInit() throws Exception {
064: super .doInit();
065: if (configuration == null) {
066: configuration = new TaskListConfiguration();
067: configuration.setRootDir(context.getWorkspaceRoot());
068: configuration.load();
069: }
070:
071: if (taskListStore == null) {
072:
073: DataSource jndiDataSource = null;
074: try {
075: JndiTemplate jndiTemplate = new JndiTemplate();
076: jndiDataSource = (DataSource) jndiTemplate
077: .lookup(TASK_LIST_JNDI_NAME);
078: } catch (NamingException n) {
079: log.debug(n.getMessage());
080: }
081:
082: BasicDataSource basicDataSource = null;
083: if (jndiDataSource == null) {
084: basicDataSource = new BasicDataSource();
085: basicDataSource.setDriverClassName(configuration
086: .getDataSourceDriverClassName());
087: basicDataSource
088: .setUrl(configuration.getDataSourceUrl());
089: basicDataSource.setUsername(configuration
090: .getDataSourceUsername());
091: basicDataSource.setPassword(configuration
092: .getDataSourcePassword());
093: }
094:
095: DataSource dataSource = jndiDataSource != null ? jndiDataSource
096: : basicDataSource;
097:
098: String jndiDialect = null;
099: try {
100: JndiTemplate jndiTemplate = new JndiTemplate();
101: jndiDialect = (String) jndiTemplate
102: .lookup(TASK_LIST_DIALECT_JNDI_NAME);
103: } catch (NamingException n) {
104: log.debug(n.getMessage());
105: }
106:
107: Properties properties = new Properties();
108: properties.setProperty("hibernate.hbm2ddl.auto", "update");
109: properties.setProperty("hibernate.dialect",
110: jndiDialect != null ? jndiDialect : configuration
111: .getHibernateDialect());
112: properties.setProperty("hibernate.query.substitutions",
113: "true 1, false 0");
114: properties.setProperty("hibernate.show_sql", "false");
115:
116: LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
117: sessionFactoryBean.setLobHandler(new DefaultLobHandler());
118: sessionFactoryBean.setHibernateProperties(properties);
119: sessionFactoryBean
120: .setMappingResources(new String[] { "/org/bpmscript/jbi/tasklist/db/DbTask.hbm.xml" });
121: sessionFactoryBean.setDataSource(dataSource);
122: sessionFactoryBean.afterPropertiesSet();
123:
124: SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean
125: .getObject();
126: HibernateTransactionManager transactionManager = new HibernateTransactionManager();
127: transactionManager.setSessionFactory(sessionFactory);
128:
129: HibernateInterceptor hibernateInterceptor = new HibernateInterceptor();
130: hibernateInterceptor.setSessionFactory(sessionFactory);
131: hibernateInterceptor.afterPropertiesSet();
132:
133: TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
134: transactionInterceptor
135: .setTransactionManager(transactionManager);
136: transactionInterceptor
137: .setTransactionAttributeSource(new MatchAlwaysTransactionAttributeSource());
138:
139: HibernateTaskListStore hibernateTaskListStore = new HibernateTaskListStore();
140: hibernateTaskListStore.setSessionFactory(sessionFactory);
141: hibernateTaskListStore.afterPropertiesSet();
142:
143: ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
144: proxyFactoryBean
145: .setProxyInterfaces(new Class[] { ITaskListStore.class });
146: proxyFactoryBean.addAdvice(hibernateInterceptor);
147: proxyFactoryBean.addAdvice(transactionInterceptor);
148: proxyFactoryBean.setTarget(hibernateTaskListStore);
149: taskListStore = (ITaskListStore) proxyFactoryBean
150: .getObject();
151:
152: }
153: if (remoteTaskListEndpoint == null) {
154: remoteTaskListEndpoint = new PojoServiceEndpoint();
155: remoteTaskListEndpoint.setService(new QName(
156: defaultNamespace, "remoteTaskListManager"));
157: remoteTaskListEndpoint.setEndpoint("taskList");
158: remoteTaskListEndpoint.setMarshaler(new XStreamMarshaler());
159: RemoteTaskListManager remoteTaskListManager = new RemoteTaskListManager();
160: remoteTaskListManager.setMarshaler(new XStreamMarshaler());
161: remoteTaskListManager.setTaskListStore(taskListStore);
162: remoteTaskListEndpoint.setPojo(remoteTaskListManager);
163: }
164: if (taskListEndpoint == null) {
165: taskListEndpoint = new TaskListEndpoint();
166: taskListEndpoint.setService(new QName(defaultNamespace,
167: "taskList"));
168: taskListEndpoint.setEndpoint("taskList");
169: taskListEndpoint.setMarshaler(new XStreamMarshaler());
170: taskListEndpoint.setTaskListStore(taskListStore);
171: }
172: defaultServiceUnit = new XBeanServiceUnit();
173: defaultServiceUnit.setComponent(component);
174:
175: remoteTaskListEndpoint.setServiceUnit(defaultServiceUnit);
176: taskListEndpoint.setServiceUnit(defaultServiceUnit);
177:
178: defaultServiceUnit.addEndpoint(remoteTaskListEndpoint);
179: defaultServiceUnit.addEndpoint(taskListEndpoint);
180:
181: defaultServiceUnit.setName("default");
182:
183: component.getRegistry().registerServiceUnit(defaultServiceUnit);
184: }
185:
186: @Override
187: public void start() throws JBIException {
188: super .start();
189: try {
190: defaultServiceUnit.start();
191: } catch (Exception e) {
192: throw new JBIException(e);
193: }
194: }
195:
196: @Override
197: public void stop() throws JBIException {
198: super .stop();
199: try {
200: defaultServiceUnit.stop();
201: } catch (Exception e) {
202: throw new JBIException(e);
203: }
204: }
205:
206: @Override
207: public void shutDown() throws JBIException {
208: super .shutDown();
209: try {
210: defaultServiceUnit.shutDown();
211: } catch (Exception e) {
212: throw new JBIException(e);
213: }
214: }
215:
216: public ITaskListStore getTaskListStore() {
217: return taskListStore;
218: }
219:
220: public void setTaskListStore(ITaskListStore taskListStore) {
221: this .taskListStore = taskListStore;
222: }
223:
224: public String getDefaultNamespace() {
225: return defaultNamespace;
226: }
227:
228: public void setDefaultNamespace(String defaultNamespace) {
229: this .defaultNamespace = defaultNamespace;
230: }
231:
232: @Override
233: protected Object getExtensionMBean() throws Exception {
234: return configuration;
235: }
236:
237: public void setConfiguration(TaskListConfiguration configuration) {
238: this .configuration = configuration;
239: }
240:
241: public TaskListConfiguration getConfiguration() {
242: return configuration;
243: }
244:
245: }
|