net.kamhon.ieagle.orm.hibernate.HAnnotationSessionFactoryBean.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.orm.hibernate.HAnnotationSessionFactoryBean.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kamhon.ieagle.orm.hibernate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;

import net.kamhon.ieagle.util.ReflectionUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

/**
 * This is hibernate 3 utility class
 * 
 */
@Deprecated
public class HAnnotationSessionFactoryBean extends AnnotationSessionFactoryBean {
    private Log log = LogFactory.getLog(HAnnotationSessionFactoryBean.class);

    public void setAnnotatationOrHbmXmlPackagePattern(String[] patterns) {

        List<Class<?>> annotationClasses = new ArrayList<Class<?>>();
        List<String> hbmXmlFiles = new ArrayList<String>();

        try {
            for (String pattern : patterns) {
                Set<String> filePaths = ReflectionUtil.findFileNames(pattern.substring(0, pattern.indexOf("\\")),
                        true, pattern, ".class", ".hbm.xml");

                for (String filePath : filePaths) {

                    if (filePath.endsWith(".class")) {
                        try {

                            Class<?> clazz = Class.forName(filePath.substring(0, filePath.indexOf(".class")));
                            if (clazz.isAnnotationPresent(Entity.class)) {

                                annotationClasses.add(clazz);
                                log.info("<mapping class=\"" + clazz.toString() + "\" />");

                            }

                        } catch (Exception e) {
                            log.debug("Class can't be loaded for hibernate mapping = " + filePath);
                        }

                    } else if (filePath.endsWith(".hbm.xml")) {

                        filePath = filePath.replaceAll("\\.", "\\\\");
                        filePath = filePath.replaceAll("\\\\hbm\\\\xml", ".hbm.xml");
                        hbmXmlFiles.add(filePath);
                        log.info("Hibernate hbm.xml mapping file: " + filePath);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info((annotationClasses.size() + hbmXmlFiles.size()) + " hibernate mapping file.");
        setAnnotatedClasses(annotationClasses.toArray(new Class[] {}));
        setMappingResources(hbmXmlFiles.toArray(new String[0]));
    }
}