alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (DBAllocationHelper.groovy)

This example Hibernate source code file (DBAllocationHelper.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

db, db, dballocating, dballocationhelper, driver_prop, map, map, password_prop, properties, properties, string, string, url_prop, username_prop

The Hibernate DBAllocationHelper.groovy source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2011, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program 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 distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

package hudson.util

import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.hibernate.gradle.testing.matrix.MatrixNode
import org.hibernate.gradle.util.FileUtil

/**
 *
 * @author Strong Liu
 */
public class DBAllocationHelper {
    private static final Logger log = Logging.getLogger(DBAllocationHelper.class);

    private static final String DRIVER_PROP = "hibernate.connection.driver_class";
    private static final String URL_PROP = "hibernate.connection.url";
    private static final String USERNAME_PROP = "hibernate.connection.username";
    private static final String PASSWORD_PROP = "hibernate.connection.password";
    //DBAllocator supports the following DBs
    private static def supportedDB = ["oracle9i", "oracle10g", "oracle11gR1", "oracle11gR2",
            "postgresql82", "postgresql83", "postgresql84", "mysql50", "mysql51",
            "db2-91", "db2-97", "mssql2005", "mssql2008R1", "mssql2008R2", "sybase155"];

    public static Map<String, String> getProperties(MatrixNode node) {
        Map<String, String> map = new HashMap();
        if ( FileUtil.isFile(node.hibernatePropertyFile) ) {
            Properties hibernateProperties = new Properties();
            hibernateProperties.load(new FileInputStream(node.hibernatePropertyFile));
            map.putAll(hibernateProperties);
        }
        if ( isDBAllocationEnabled(node.name) ) {
            log.lifecycle("using DBAllocator to get DB[${node.name}] connection info");
            try {
                DBAllocation db = node.DBAllocation
                db.allocate(node.name, 300);
                Properties prop = db.properties
                log.lifecycle("DBAllocating finished for DB[${node.name}], uuid is [${prop['uuid']}]")
                map[DRIVER_PROP] = prop["db.jdbc_class"]
                map[URL_PROP] = prop["db.jdbc_url"]
                map[USERNAME_PROP] = prop["db.username"]
                map[PASSWORD_PROP] = prop["db.password"]
                map["uuid"] = prop["uuid"];
                db.clean();
            }
            catch (RuntimeException e) {
                log.debug("DBAllocating error, ignore", e);
            }
        }
        return map;
    }
    /**
     * use -Dhibernate-matrix-dballocation=all to enable DBAllocation for all matrix node
     * or
     * add systemProp.hibernate-matrix-dballocation=all to ${user.home}/.gradle/gradle.properties
     */
    public static boolean isDBAllocationEnabled(String name) {
        if ( !supportedDB.contains(name) ) return false;
        String value = System.properties["hibernate-matrix-dballocation"]
        return value != null && (value.contains(name) || value.equals("all"));
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate DBAllocationHelper.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.