blob: 11849efca495b031f889b499d72131ae5735d33b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.syncope.core.persistence.jpa;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.ValidationMode;
import javax.validation.Validator;
import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
import org.apache.syncope.core.persistence.api.dao.AuditConfDAO;
import org.apache.syncope.core.persistence.api.dao.GroupDAO;
import org.apache.syncope.core.persistence.api.dao.PlainAttrDAO;
import org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO;
import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
import org.apache.syncope.core.persistence.api.dao.UserDAO;
import org.apache.syncope.core.persistence.api.entity.EntityFactory;
import org.apache.syncope.core.persistence.api.search.SearchCondVisitor;
import org.apache.syncope.core.persistence.jpa.spring.CommonEntityManagerFactoryConf;
import org.apache.syncope.core.persistence.jpa.spring.DomainTransactionInterceptorInjector;
import org.apache.syncope.core.persistence.jpa.spring.MultiJarAwarePersistenceUnitPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@ComponentScan("org.apache.syncope.core.persistence.jpa")
@EnableConfigurationProperties(PersistenceProperties.class)
@Configuration
public class PersistenceContext {
private static final Logger OPENJPA_LOG = LoggerFactory.getLogger("org.apache.openjpa");
@Bean
public static BeanFactoryPostProcessor domainTransactionInterceptorInjector() {
return new DomainTransactionInterceptorInjector();
}
@Autowired
private PersistenceProperties props;
@Autowired
private ResourceLoader resourceLoader;
@ConditionalOnMissingBean
@Bean
public CommonEntityManagerFactoryConf commonEMFConf() {
CommonEntityManagerFactoryConf commonEMFConf = new CommonEntityManagerFactoryConf();
commonEMFConf.setPackagesToScan("org.apache.syncope.core.persistence.jpa.entity");
commonEMFConf.setValidationMode(ValidationMode.NONE);
commonEMFConf.setPersistenceUnitPostProcessors(new MultiJarAwarePersistenceUnitPostProcessor());
Map<String, Object> jpaPropertyMap = new HashMap<>();
jpaPropertyMap.put("openjpa.Log", "slf4j");
if (OPENJPA_LOG.isDebugEnabled()) {
jpaPropertyMap.put("openjpa.Log", "SQL=TRACE");
jpaPropertyMap.put("openjpa.ConnectionFactoryProperties",
"PrintParameters=true, PrettyPrint=true, PrettyPrintLineLength=120");
}
jpaPropertyMap.put("openjpa.NontransactionalWrite", false);
jpaPropertyMap.put("openjpa.jdbc.MappingDefaults",
"ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict,"
+ "FieldStrategies='"
+ "java.util.Locale=org.apache.syncope.core.persistence.jpa.openjpa.LocaleValueHandler,"
+ "java.lang.Boolean=org.apache.syncope.core.persistence.jpa.openjpa.BooleanValueHandler'");
jpaPropertyMap.put("openjpa.DataCache", "true");
jpaPropertyMap.put("openjpa.QueryCache", "true");
jpaPropertyMap.put("openjpa.RemoteCommitProvider", props.getRemoteCommitProvider());
commonEMFConf.setJpaPropertyMap(jpaPropertyMap);
return commonEMFConf;
}
@Bean
public EntityFactory entityFactory() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getEntityFactory().getDeclaredConstructor().newInstance();
}
@Bean
public PlainSchemaDAO plainSchemaDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getPlainSchemaDAO().getDeclaredConstructor().newInstance();
}
@Bean
public PlainAttrDAO plainAttrDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getPlainAttrDAO().getDeclaredConstructor().newInstance();
}
@Bean
public PlainAttrValueDAO plainAttrValueDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getPlainAttrValueDAO().getDeclaredConstructor().newInstance();
}
@Bean
public AnySearchDAO anySearchDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getAnySearchDAO().getDeclaredConstructor().newInstance();
}
@Bean
public SearchCondVisitor searchCondVisitor() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getSearchCondVisitor().getDeclaredConstructor().newInstance();
}
@Bean
public UserDAO userDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getUserDAO().getDeclaredConstructor().newInstance();
}
@Bean
public GroupDAO groupDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getGroupDAO().getDeclaredConstructor().newInstance();
}
@Bean
public AnyObjectDAO anyObjectDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getAnyObjectDAO().getDeclaredConstructor().newInstance();
}
@Bean
public AuditConfDAO auditConfDAO() throws NoSuchMethodException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
return props.getAuditConfDAO().getDeclaredConstructor().newInstance();
}
@Bean
public Validator localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public Resource viewsXML() {
return resourceLoader.getResource(props.getViewsXML());
}
@Bean
public Resource indexesXML() {
return resourceLoader.getResource(props.getIndexesXML());
}
}