<commit_before>/*
 * 
 * 
 * Copyright (C) 2005 SIPfoundry Inc.
 * Licensed by SIPfoundry under the LGPL license.
 * 
 * Copyright (C) 2005 Pingtel Corp.
 * Licensed to SIPfoundry under a Contributor Agreement.
 * 
 * $
 */
package org.sipfoundry.sipxconfig.common;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.lang.enums.Enum;
import org.apache.commons.lang.enums.EnumUtils;
import org.hibernate.Hibernate;
import org.hibernate.usertype.UserType;

/**
 * EnumUserType Maps a commons-lang <code>Enum</code> to a Hibernate type. From the example on
 * http://www.hibernate.org/172.html
 */
public class EnumUserType implements UserType {
    private Class m_enumClass;

    public EnumUserType(Class enumClass) {
        m_enumClass = enumClass;
    }

    /**
     * @see org.hibernate.usertype.UserType#sqlTypes()
     */
    public int[] sqlTypes() {
        return new int[] {
            Types.VARCHAR
        };
    }

    /**
     * @see org.hibernate.usertype.UserType#returnedClass()
     */
    public Class returnedClass() {
        return m_enumClass;
    }

    /**
     * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
     */
    public boolean equals(Object x, Object y)  {
        if (x == y) {
            return true;
        }

        if (x == null || y == null) {
            return false;
        }

        return Hibernate.STRING.isEqual(x, y);
    }

    /**
     * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[],
     *      java.lang.Object)
     */
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner_) throws SQLException {
        String enumCode = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);

        return EnumUtils.getEnum(m_enumClass, enumCode);
    }

    /**
     * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object,
     *      int)
     */
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws SQLException {
        // make sure the received value is of the right type
        if ((value != null) && !returnedClass().isAssignableFrom(value.getClass())) {
            throw new IllegalArgumentException("Received value is not a ["
                    + returnedClass().getName() + "] but [" + value.getClass() + "]");
        }

        Enum enumeration = (Enum) value;
        String enumCode = enumeration.getName();
        st.setString(index, enumCode);
    }
<commit_msg>XCF-1044 workaround for http://issues.apache.org/jira/browse/LANG-76 that prevents EnumUserType from correctly loading Enum types in some situations. It's only triggered if: - code is compiled with 1.5 as a target JVM - Enum class has not been used before we try to load it from DB<commit_after>/*
 * 
 * 
 * Copyright (C) 2005 SIPfoundry Inc.
 * Licensed by SIPfoundry under the LGPL license.
 * 
 * Copyright (C) 2005 Pingtel Corp.
 * Licensed to SIPfoundry under a Contributor Agreement.
 * 
 * $
 */
package org.sipfoundry.sipxconfig.common;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.lang.enums.Enum;
import org.apache.commons.lang.enums.EnumUtils;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.usertype.UserType;

/**
 * EnumUserType Maps a commons-lang <code>Enum</code> to a Hibernate type. From the example on
 * http://www.hibernate.org/172.html
 */
public class EnumUserType implements UserType {
    private Class m_enumClass;

    public EnumUserType(Class enumClass) {
        m_enumClass = enumClass;
        initStaticFields();
    }

    /**
     * Initializes static fields for this enumeration
     * 
     * Workaround for: http://issues.apache.org/jira/browse/LANG-76
     * 
     */
    public void initStaticFields() {
        Field[] fields = m_enumClass.getFields();
        if (fields.length > 0) {
            try {
                fields[0].get(null);
            } catch (Exception e) {
                // we are going to get NullPointerException here
                // ignore it - we are just loading class to trigger static field init
                LogFactory.getLog(getClass()).debug("Initializing static fields for: " + m_enumClass);
            }
        }
    }

    /**
     * @see org.hibernate.usertype.UserType#sqlTypes()
     */
    public int[] sqlTypes() {
        return new int[] {
            Types.VARCHAR
        };
    }

    /**
     * @see org.hibernate.usertype.UserType#returnedClass()
     */
    public Class returnedClass() {
        return m_enumClass;
    }

    /**
     * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
     */
    public boolean equals(Object x, Object y) {
        if (x == y) {
            return true;
        }

        if (x == null || y == null) {
            return false;
        }

        return Hibernate.STRING.isEqual(x, y);
    }

    /**
     * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[],
     *      java.lang.Object)
     */
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner_) throws SQLException {
        String enumCode = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);

        return EnumUtils.getEnum(m_enumClass, enumCode);
    }

    /**
     * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
     *      java.lang.Object, int)
     */
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws SQLException {
        // make sure the received value is of the right type
        if ((value != null) && !returnedClass().isAssignableFrom(value.getClass())) {
            throw new IllegalArgumentException("Received value is not a ["
                    + returnedClass().getName() + "] but [" + value.getClass() + "]");
        }

        Enum enumeration = (Enum) value;
        String enumCode = enumeration.getName();
        st.setString(index, enumCode);
    }

{'fraction_non_alphanumeric': 0.11599297012302284, 'fraction_numerical': 0.005751717526761463, 'mean_word_length': 2.508968609865471, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126194', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1821, 'n_words': 455}