Package org.jbpm.wire.descriptor

Source Code of org.jbpm.wire.descriptor.HibernateConfigurationDescriptor

/*
* 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.wire.descriptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.jbpm.env.session.hibernate.PvmNamingStrategy;
import org.jbpm.util.JbpmReflectException;
import org.jbpm.util.ReflectUtil;
import org.jbpm.wire.WireContext;
import org.jbpm.wire.WireDefinition;
import org.jbpm.wire.WireException;
import org.jbpm.wire.operation.Operation;


/**
* @author Tom Baeyens
*/
public class HibernateConfigurationDescriptor extends AbstractDescriptor {
 
  private static final long serialVersionUID = 1L;
 
  private static final Logger log = Logger.getLogger(HibernateConfigurationDescriptor.class.getName());

  String className;
  String tablePrefix = "JBPM_";
  String namingStrategyClassName;
  List<Operation> mappingOperations;
  List<Operation> cacheOperations;
  PropertiesDescriptor propertiesDescriptor;

  public Object construct(WireContext wireContext) {
    // instantiation of the configuration
    Configuration configuration = null;
    if (className!=null) {
      ClassLoader classLoader = wireContext.getClassLoader();
      Class<?> configurationClass = ReflectUtil.loadClass(classLoader, className);
      configuration = (Configuration) ReflectUtil.newInstance(configurationClass);
    } else {
      configuration = new Configuration();
    }
   
    // the naming strategy
    if (tablePrefix!=null) {
      configuration.setNamingStrategy(new PvmNamingStrategy(tablePrefix));

    } else if (namingStrategyClassName!=null) {
      ClassLoader classLoader = wireContext.getClassLoader();
      Class<?> namingStrategyClass = ReflectUtil.loadClass(classLoader, namingStrategyClassName);
      NamingStrategy namingStrategy = (NamingStrategy) ReflectUtil.newInstance(namingStrategyClass);
      configuration.setNamingStrategy(namingStrategy);
    }
   
    Properties properties = (Properties) propertiesDescriptor.construct(wireContext);
    configuration.addProperties(properties);
   
    return configuration;
  }
 
  public void initialize(Object object, WireContext wireContext) {
    Configuration configuration = (Configuration) object;
    apply(mappingOperations, configuration, wireContext);
    apply(cacheOperations, configuration, wireContext);
  }

  private void apply(List<Operation> operations, Configuration configuration, WireContext wireContext) {
    if (operations!=null) {
      for (Operation operation: operations) {
        log.fine(operation.toString());
        operation.apply(configuration, wireContext);
      }
    }
  }

  public Class<?> getType(WireDefinition wireDefinition) {
    if (className!=null) {
      try {
        return ReflectUtil.loadClass(wireDefinition.getClassLoader(), className);
      } catch (JbpmReflectException e) {
        throw new WireException("couldn't create hibernate configuration '"+className+"': "+e.getMessage(), e.getCause());
      }
    }
    return Configuration.class;
  }

  public void addMappingOperation(Operation operation) {
    if (mappingOperations==null) {
      mappingOperations = new ArrayList<Operation>();
    }
    mappingOperations.add(operation);
  }
  public void addCacheOperation(Operation operation) {
    if (cacheOperations==null) {
      cacheOperations = new ArrayList<Operation>();
    }
    cacheOperations.add(operation);
  }

  public String getClassName() {
    return className;
  }
  public void setClassName(String className) {
    this.className = className;
  }
  public PropertiesDescriptor getPropertiesDescriptor() {
    return propertiesDescriptor;
  }
  public void setPropertiesDescriptor(PropertiesDescriptor propertiesDescriptor) {
    this.propertiesDescriptor = propertiesDescriptor;
  }
  public String getTablePrefix() {
    return tablePrefix;
  }
  public void setTablePrefix(String tablePrefix) {
    this.tablePrefix = tablePrefix;
  }
  public String getNamingStrategyClassName() {
    return namingStrategyClassName;
  }
  public void setNamingStrategyClassName(String namingStrategyClassName) {
    this.namingStrategyClassName = namingStrategyClassName;
  }
}
TOP

Related Classes of org.jbpm.wire.descriptor.HibernateConfigurationDescriptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.