您的位置:首页 > 产品设计 > 产品经理

jbpm4.4 hibernate4 的兼容整合

2014-01-08 20:30 856 查看
jbpm4.4默认用的是hibernate3,如果要使用hibernate4整合,则要覆写一些类,一共3个,如图:



1、BlobStrategyBlob.java

package org.jbpm.pvm.internal.lob;

import java.sql.SQLException;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.hibernate.LobHelper;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionImpl;
import org.jbpm.api.JbpmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BlobStrategyBlob implements BlobStrategy {

public void set(byte[] bytes, Lob lob) {
if (bytes != null) {
lob.cachedBytes = bytes;
ApplicationContext ac =  WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
Session session = sessionFactory.getCurrentSession();
lob.blob = session.getLobHelper().createBlob(bytes);
}
}

public byte[] get(Lob lob) {
if (lob.cachedBytes != null) {
return lob.cachedBytes;
}

java.sql.Blob sqlBlob = lob.blob;
if (sqlBlob != null) {
try {
return sqlBlob.getBytes(1, (int) sqlBlob.length());
} catch (SQLException e) {
throw new JbpmException("couldn't extract bytes out of blob", e);
}
}
return null;
}
}


2、SpringProcessEngine.java

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. */
package org.jbpm.pvm.internal.processengine;

import java.util.Properties;

import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

import org.jbpm.api.ProcessEngine;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.cfg.ConfigurationImpl;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.env.PvmEnvironment;
import org.jbpm.pvm.internal.env.SpringContext;
import org.jbpm.pvm.internal.wire.descriptor.ProvidedObjectDescriptor;

/**
* this environment factory will see only the singleton beans.
*
* The created {@link SpringEnvironment}s will see the prototype beans and it will cache them.
*
* @author Andries Inze
*/
public class SpringProcessEngine extends ProcessEngineImpl {

private static final Log log = Log.getLog(SpringProcessEngine.class.getName());

private static final long serialVersionUID = 1L;

private ApplicationContext applicationContext;

public static ProcessEngine create(ConfigurationImpl configuration) {
SpringProcessEngine springProcessEngine = null;

ApplicationContext applicationContext = null;
if (configuration.isInstantiatedFromSpring()) {
applicationContext = (ApplicationContext) configuration.getApplicationContext();

springProcessEngine = new SpringProcessEngine();
springProcessEngine.applicationContext = applicationContext;
springProcessEngine.initializeProcessEngine(configuration);

LocalSessionFactoryBean localSessionFactoryBean = springProcessEngine.get(LocalSessionFactoryBean.class);
Configuration hibernateConfiguration = localSessionFactoryBean.getConfiguration();
springProcessEngine.processEngineWireContext.getWireDefinition().addDescriptor(new ProvidedObjectDescriptor(hibernateConfiguration, true));

springProcessEngine.checkDb(configuration);

} else {
String springCfg = (String) configuration.getProcessEngineWireContext().get("spring.cfg");
if (springCfg == null) {
springCfg = "applicationContext.xml";
}
applicationContext = new ClassPathXmlApplicationContext(springCfg);
springProcessEngine = (SpringProcessEngine) applicationContext.getBean("processEngine");
}

return springProcessEngine;
}

public EnvironmentImpl openEnvironment() {
PvmEnvironment environment = new PvmEnvironment(this);

if (log.isTraceEnabled())
log.trace("opening jbpm-spring" + environment);

environment.setContext(new SpringContext(applicationContext));

installAuthenticatedUserId(environment);
installProcessEngineContext(environment);
installTransactionContext(environment);

return environment;
}

@SuppressWarnings("unchecked")
@Override
public <T> T get(Class<T> type) {
T candidateComponent = super.get(type);

if (candidateComponent != null) {
return candidateComponent;
}

String[] names = applicationContext.getBeanNamesForType(type);

if (names.length >= 1) {

if (names.length > 1 && log.isWarnEnabled()) {
log.warn("Multiple beans for type " + type + " found. Returning the first result.");
}

return (T) applicationContext.getBean(names[0]);
}

return null;
}

@Override
public Object get(String key) {
if (applicationContext.containsBean(key)) {
return applicationContext.getBean(key);
}

return super.get(key);
}
}


3、HibernateSessionDescriptor.java

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. */
package org.jbpm.pvm.internal.wire.descriptor;

import java.sql.Connection;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionImpl;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.tx.HibernateSessionResource;
import org.jbpm.pvm.internal.tx.StandardTransaction;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireDefinition;
import org.jbpm.pvm.internal.wire.WireException;

/**
* @author Tom Baeyens
*/
public class HibernateSessionDescriptor extends AbstractDescriptor {

private static final long serialVersionUID = 1L;
private static final Log log = Log.getLog(HibernateSessionDescriptor.class.getName());

protected String factoryName;
protected boolean useCurrent = false;
protected boolean tx = true;
protected boolean close = true;
protected String standardTransactionName;
protected String connectionName;

public Object construct(WireContext wireContext) {
EnvironmentImpl environment = EnvironmentImpl.getCurrent();
if (environment == null) {
throw new WireException("no environment");
}

// get the hibernate-session-factory
SessionFactory sessionFactory = null;
if (factoryName != null) {
sessionFactory = (SessionFactory) wireContext.get(factoryName);
} else {
sessionFactory = environment.get(SessionFactory.class);
}
if (sessionFactory == null) {
throw new WireException("couldn't find hibernate-session-factory " + (factoryName != null ? "'" + factoryName + "'" : "by type ") + "to open a hibernate-session");
}

// open the hibernate-session
Session session = null;
if (useCurrent) {
if (log.isTraceEnabled())
log.trace("getting current hibernate session");
session = sessionFactory.getCurrentSession();

} else if (connectionName != null) {
Connection connection = (Connection) wireContext.get(connectionName);
if (log.isTraceEnabled())
log.trace("creating hibernate session with connection " + connection);
session = (Session) sessionFactory.openStatelessSession(connection);

} else {
if (log.isTraceEnabled())
log.trace("creating hibernate session");
session = sessionFactory.openSession();
}

StandardTransaction standardTransaction = environment.get(StandardTransaction.class);
if (standardTransaction != null) {
HibernateSessionResource hibernateSessionResource = new HibernateSessionResource(session);
standardTransaction.enlistResource(hibernateSessionResource);
}

return session;
}

public Class<?> getType(WireDefinition wireDefinition) {
return SessionImpl.class;
}

public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}

public void setTx(boolean tx) {
this.tx = tx;
}

public void setStandardTransactionName(String standardTransactionName) {
this.standardTransactionName = standardTransactionName;
}

public void setConnectionName(String connectionName) {
this.connectionName = connectionName;
}

public void setUseCurrent(boolean useCurrent) {
this.useCurrent = useCurrent;
}

public void setClose(boolean close) {
this.close = close;
}
}


这样执行过程中就不会报错不兼容了,主要是将用到3的地方更换为4即可,其他配置请参考其他文章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: