Search in sources :

Example 16 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class MultiTenantConnectionProviderInitiator method initiateService.

@Override
@SuppressWarnings({ "unchecked" })
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(configurationValues);
    if (strategy == MultiTenancyStrategy.NONE || strategy == MultiTenancyStrategy.DISCRIMINATOR) {
        // nothing to do, but given the separate hierarchies have to handle this here.
        return null;
    }
    final Object configValue = configurationValues.get(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER);
    if (configValue == null) {
        // if they also specified the data source *name*, then lets assume they want
        // DataSourceBasedMultiTenantConnectionProviderImpl
        final Object dataSourceConfigValue = configurationValues.get(AvailableSettings.DATASOURCE);
        if (dataSourceConfigValue != null && String.class.isInstance(dataSourceConfigValue)) {
            return new DataSourceBasedMultiTenantConnectionProviderImpl();
        }
        return null;
    }
    if (MultiTenantConnectionProvider.class.isInstance(configValue)) {
        return (MultiTenantConnectionProvider) configValue;
    } else {
        final Class<MultiTenantConnectionProvider> implClass;
        if (Class.class.isInstance(configValue)) {
            implClass = (Class) configValue;
        } else {
            final String className = configValue.toString();
            final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);
            try {
                implClass = classLoaderService.classForName(className);
            } catch (ClassLoadingException cle) {
                log.warn("Unable to locate specified class [" + className + "]", cle);
                throw new ServiceException("Unable to locate specified multi-tenant connection provider [" + className + "]");
            }
        }
        try {
            return implClass.newInstance();
        } catch (Exception e) {
            log.warn("Unable to instantiate specified class [" + implClass.getName() + "]", e);
            throw new ServiceException("Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]");
        }
    }
}
Also used : MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) DataSourceBasedMultiTenantConnectionProviderImpl(org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ServiceException(org.hibernate.service.spi.ServiceException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 17 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class DefaultIdentifierGeneratorFactory method getIdentifierGeneratorClass.

@Override
public Class getIdentifierGeneratorClass(String strategy) {
    if ("hilo".equals(strategy)) {
        throw new UnsupportedOperationException("Support for 'hilo' generator has been removed");
    }
    String resolvedStrategy = "native".equals(strategy) ? getDialect().getNativeIdentifierGeneratorStrategy() : strategy;
    Class generatorClass = generatorStrategyToClassNameMap.get(resolvedStrategy);
    try {
        if (generatorClass == null) {
            final ClassLoaderService cls = serviceRegistry.getService(ClassLoaderService.class);
            generatorClass = cls.classForName(resolvedStrategy);
        }
    } catch (ClassLoadingException e) {
        throw new MappingException(String.format("Could not interpret id generator strategy [%s]", strategy));
    }
    return generatorClass;
}
Also used : ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) MappingException(org.hibernate.MappingException)

Example 18 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method bindNamedSubgraph.

private static void bindNamedSubgraph(XMLContext.Default defaults, AnnotationDescriptor ann, List<Element> subgraphNodes, ClassLoaderAccess classLoaderAccess) {
    List<NamedSubgraph> annSubgraphNodes = new ArrayList<NamedSubgraph>();
    for (Element subgraphNode : subgraphNodes) {
        AnnotationDescriptor annSubgraphNode = new AnnotationDescriptor(NamedSubgraph.class);
        copyStringAttribute(annSubgraphNode, subgraphNode, "name", true);
        String clazzName = subgraphNode.attributeValue("class");
        Class clazz;
        try {
            clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
        } catch (ClassLoadingException e) {
            throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
        }
        annSubgraphNode.setValue("type", clazz);
        bindNamedAttributeNodes(subgraphNode, annSubgraphNode);
        annSubgraphNodes.add((NamedSubgraph) AnnotationFactory.create(annSubgraphNode));
    }
    ann.setValue("subgraphs", annSubgraphNodes.toArray(new NamedSubgraph[annSubgraphNodes.size()]));
}
Also used : NamedSubgraph(javax.persistence.NamedSubgraph) AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 19 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildNamedStoreProcedureQueries.

public static List<NamedStoredProcedureQuery> buildNamedStoreProcedureQueries(Element element, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    if (element == null) {
        return new ArrayList<NamedStoredProcedureQuery>();
    }
    List namedStoredProcedureElements = element.elements("named-stored-procedure-query");
    List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
    for (Object obj : namedStoredProcedureElements) {
        Element subElement = (Element) obj;
        AnnotationDescriptor ann = new AnnotationDescriptor(NamedStoredProcedureQuery.class);
        copyStringAttribute(ann, subElement, "name", true);
        copyStringAttribute(ann, subElement, "procedure-name", true);
        List<Element> elements = subElement.elements("parameter");
        List<StoredProcedureParameter> storedProcedureParameters = new ArrayList<StoredProcedureParameter>();
        for (Element parameterElement : elements) {
            AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor(StoredProcedureParameter.class);
            copyStringAttribute(parameterDescriptor, parameterElement, "name", false);
            String modeValue = parameterElement.attributeValue("mode");
            if (modeValue == null) {
                parameterDescriptor.setValue("mode", ParameterMode.IN);
            } else {
                parameterDescriptor.setValue("mode", ParameterMode.valueOf(modeValue.toUpperCase(Locale.ROOT)));
            }
            String clazzName = parameterElement.attributeValue("class");
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            parameterDescriptor.setValue("type", clazz);
            storedProcedureParameters.add((StoredProcedureParameter) AnnotationFactory.create(parameterDescriptor));
        }
        ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
        elements = subElement.elements("result-class");
        List<Class> returnClasses = new ArrayList<Class>();
        for (Element classElement : elements) {
            String clazzName = classElement.getTextTrim();
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            returnClasses.add(clazz);
        }
        ann.setValue("resultClasses", returnClasses.toArray(new Class[returnClasses.size()]));
        elements = subElement.elements("result-set-mapping");
        List<String> resultSetMappings = new ArrayList<String>();
        for (Element resultSetMappingElement : elements) {
            resultSetMappings.add(resultSetMappingElement.getTextTrim());
        }
        ann.setValue("resultSetMappings", resultSetMappings.toArray(new String[resultSetMappings.size()]));
        elements = subElement.elements("hint");
        buildQueryHints(elements, ann);
        namedStoredProcedureQueries.add((NamedStoredProcedureQuery) AnnotationFactory.create(ann));
    }
    return namedStoredProcedureQueries;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StoredProcedureParameter(javax.persistence.StoredProcedureParameter) AnnotationException(org.hibernate.AnnotationException) ArrayList(java.util.ArrayList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 20 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class SelectParser method token.

public void token(String token, QueryTranslatorImpl q) throws QueryException {
    String lctoken = token.toLowerCase(Locale.ROOT);
    if (first) {
        first = false;
        if ("distinct".equals(lctoken)) {
            q.setDistinct(true);
            return;
        } else if ("all".equals(lctoken)) {
            q.setDistinct(false);
            return;
        }
    }
    if (afterNew) {
        afterNew = false;
        try {
            holderClass = q.getFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(QuerySplitter.getImportedClass(token, q.getFactory()));
        } catch (ClassLoadingException e) {
            throw new QueryException(e);
        }
        if (holderClass == null) {
            throw new QueryException("class not found: " + token);
        }
        q.setHolderClass(holderClass);
        insideNew = true;
    } else if (token.equals(",")) {
        if (!aggregate && ready) {
            throw new QueryException("alias or expression expected in SELECT");
        }
        q.appendScalarSelectToken(", ");
        ready = true;
    } else if ("new".equals(lctoken)) {
        afterNew = true;
        ready = false;
    } else if ("(".equals(token)) {
        if (insideNew && !aggregate && !ready) {
            //opening paren in new Foo ( ... )
            ready = true;
        } else if (aggregate) {
            q.appendScalarSelectToken(token);
        } else {
            throw new QueryException("aggregate function expected beforeQuery ( in SELECT");
        }
        ready = true;
    } else if (")".equals(token)) {
        if (insideNew && !aggregate && !ready) {
            //if we are inside a new Result(), but not inside a nested function
            insideNew = false;
        } else if (aggregate && ready) {
            q.appendScalarSelectToken(token);
            aggregateFuncTokenList.removeLast();
            if (aggregateFuncTokenList.size() < 1) {
                aggregate = false;
                ready = false;
            }
        } else {
            throw new QueryException("( expected beforeQuery ) in select");
        }
    } else if (COUNT_MODIFIERS.contains(lctoken)) {
        if (!ready || !aggregate) {
            throw new QueryException(token + " only allowed inside aggregate function in SELECT");
        }
        q.appendScalarSelectToken(token);
        if ("*".equals(token)) {
            // special case
            q.addSelectScalar(getFunction("count", q).getReturnType(StandardBasicTypes.LONG, q.getFactory()));
        }
    } else if (getFunction(lctoken, q) != null && token.equals(q.unalias(token))) {
        // the name of an SQL function
        if (!ready) {
            throw new QueryException(", expected beforeQuery aggregate function in SELECT: " + token);
        }
        aggregate = true;
        aggregateAddSelectScalar = true;
        aggregateFuncTokenList.add(lctoken);
        ready = false;
        q.appendScalarSelectToken(token);
        if (!aggregateHasArgs(lctoken, q)) {
            q.addSelectScalar(aggregateType(aggregateFuncTokenList, null, q));
            if (!aggregateFuncNoArgsHasParenthesis(lctoken, q)) {
                aggregateFuncTokenList.removeLast();
                if (aggregateFuncTokenList.size() < 1) {
                    aggregate = false;
                    ready = false;
                } else {
                    ready = true;
                }
            }
        }
    } else if (aggregate) {
        boolean constantToken = false;
        if (!ready) {
            throw new QueryException("( expected afterQuery aggregate function in SELECT");
        }
        try {
            ParserHelper.parse(aggregatePathExpressionParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q);
        } catch (QueryException qex) {
            constantToken = true;
        }
        if (constantToken) {
            q.appendScalarSelectToken(token);
        } else {
            if (aggregatePathExpressionParser.isCollectionValued()) {
                q.addCollection(aggregatePathExpressionParser.getCollectionName(), aggregatePathExpressionParser.getCollectionRole());
            }
            q.appendScalarSelectToken(aggregatePathExpressionParser.getWhereColumn());
            if (aggregateAddSelectScalar) {
                q.addSelectScalar(aggregateType(aggregateFuncTokenList, aggregatePathExpressionParser.getWhereColumnType(), q));
                aggregateAddSelectScalar = false;
            }
            aggregatePathExpressionParser.addAssociation(q);
        }
    } else {
        if (!ready) {
            throw new QueryException(", expected in SELECT");
        }
        ParserHelper.parse(pathExpressionParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q);
        if (pathExpressionParser.isCollectionValued()) {
            q.addCollection(pathExpressionParser.getCollectionName(), pathExpressionParser.getCollectionRole());
        } else if (pathExpressionParser.getWhereColumnType().isEntityType()) {
            q.addSelectClass(pathExpressionParser.getSelectName());
        }
        q.appendScalarSelectTokens(pathExpressionParser.getWhereColumns());
        q.addSelectScalar(pathExpressionParser.getWhereColumnType());
        pathExpressionParser.addAssociation(q);
        ready = false;
    }
}
Also used : QueryException(org.hibernate.QueryException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException)

Aggregations

ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)26 Element (org.dom4j.Element)10 AnnotationException (org.hibernate.AnnotationException)10 AnnotatedElement (java.lang.reflect.AnnotatedElement)9 IdClass (javax.persistence.IdClass)9 MapKeyClass (javax.persistence.MapKeyClass)9 ArrayList (java.util.ArrayList)8 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)7 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)7 List (java.util.List)5 MappingException (org.hibernate.MappingException)5 HibernateException (org.hibernate.HibernateException)3 AccessibleObject (java.lang.reflect.AccessibleObject)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 NamedNativeQuery (javax.persistence.NamedNativeQuery)2 NamedQuery (javax.persistence.NamedQuery)2 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)2 Attribute (org.dom4j.Attribute)2