Search in sources :

Example 11 with Datasource

use of org.apache.ofbiz.entity.config.model.Datasource in project ofbiz-framework by apache.

the class GenericHelperFactory method getHelper.

public static GenericHelper getHelper(GenericHelperInfo helperInfo) {
    GenericHelper helper = helperCache.get(helperInfo.getHelperFullName());
    if (helper == null) {
        // don't want to block here
        synchronized (GenericHelperFactory.class) {
            // must check if null again as one of the blocked threads can still enter
            helper = helperCache.get(helperInfo.getHelperFullName());
            if (helper == null) {
                try {
                    Datasource datasourceInfo = EntityConfig.getDatasource(helperInfo.getHelperBaseName());
                    if (datasourceInfo == null) {
                        throw new IllegalStateException("Could not find datasource definition with name " + helperInfo.getHelperBaseName());
                    }
                    String helperClassName = datasourceInfo.getHelperClass();
                    Class<?> helperClass = null;
                    if (UtilValidate.isNotEmpty(helperClassName)) {
                        try {
                            ClassLoader loader = Thread.currentThread().getContextClassLoader();
                            helperClass = loader.loadClass(helperClassName);
                        } catch (ClassNotFoundException e) {
                            Debug.logWarning(e, module);
                            throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
                        }
                    }
                    Class<?>[] paramTypes = new Class<?>[] { GenericHelperInfo.class };
                    Object[] params = new Object[] { helperInfo };
                    java.lang.reflect.Constructor<?> helperConstructor = null;
                    if (helperClass != null) {
                        try {
                            helperConstructor = helperClass.getConstructor(paramTypes);
                        } catch (NoSuchMethodException e) {
                            Debug.logWarning(e, module);
                            throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
                        }
                    }
                    try {
                        helper = (GenericHelper) helperConstructor.newInstance(params);
                    } catch (IllegalAccessException | InstantiationException | InvocationTargetException | ExceptionInInitializerError | IllegalArgumentException | NullPointerException e) {
                        Debug.logWarning(e, module);
                        throw new IllegalStateException("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage());
                    }
                    if (helper != null)
                        helperCache.put(helperInfo.getHelperFullName(), helper);
                } catch (SecurityException e) {
                    Debug.logError(e, module);
                    throw new IllegalStateException("Error loading GenericHelper class: " + e.toString());
                }
            }
        }
    }
    return helper;
}
Also used : Datasource(org.apache.ofbiz.entity.config.model.Datasource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Datasource (org.apache.ofbiz.entity.config.model.Datasource)11 Connection (java.sql.Connection)2 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 XAConnection (javax.sql.XAConnection)1 GenericConfigException (org.apache.ofbiz.base.config.GenericConfigException)1 MainResourceHandler (org.apache.ofbiz.base.config.MainResourceHandler)1 ResourceHandler (org.apache.ofbiz.base.config.ResourceHandler)1 UtilTimer (org.apache.ofbiz.base.util.UtilTimer)1 GenericEntityConfException (org.apache.ofbiz.entity.GenericEntityConfException)1 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)1 FieldType (org.apache.ofbiz.entity.config.model.FieldType)1 JndiJdbc (org.apache.ofbiz.entity.config.model.JndiJdbc)1 ReadData (org.apache.ofbiz.entity.config.model.ReadData)1