net.kamhon.ieagle.function.config.BasicAppConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.function.config.BasicAppConfig.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.function.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.sql.DataSource;

import net.kamhon.ieagle.aop.AppDetailsBaseAdvice;
import net.kamhon.ieagle.aop.DefaultAppDetailsBaseAdvice;
import net.kamhon.ieagle.aop.ExceptionLoggingAroundAdvice;
import net.kamhon.ieagle.application.ServerConfiguration;
import net.kamhon.ieagle.dao.DynamicDataSource;
import net.kamhon.ieagle.dao.JdbcDao;
import net.kamhon.ieagle.exception.ExceptionLoggingBundle;
import net.kamhon.ieagle.function.email.EmailServerSetting;
import net.kamhon.ieagle.function.email.SendMail;
import net.kamhon.ieagle.function.user.dao.UserDetailsSqlDao;
import net.kamhon.ieagle.function.user.dao.mysql.UserDetailsSqlDaoImpl;
import net.kamhon.ieagle.util.FreeMarkerTemplate;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.dao.SystemWideSaltSource;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;

@Configuration
public class BasicAppConfig {
    @Autowired
    protected Environment env;

    // **************************
    // application context start
    // **************************
    @Bean
    public AppDetailsBaseAdvice appDetailsBaseAdvice() {
        return new DefaultAppDetailsBaseAdvice();
    }

    @Bean
    public ExceptionLoggingBundle exceptionLoggingBundle() {
        ExceptionLoggingBundle exceptionLoggingBundle = new ExceptionLoggingBundle();
        exceptionLoggingBundle.setServerConfiguration(serverConfiguration());
        exceptionLoggingBundle.setExceptionsNotToLog(exceptionNotToLog());

        return exceptionLoggingBundle;
    }

    public List<String> exceptionNotToLog() {
        List<String> exceptions = new ArrayList<String>();

        String property = env.getProperty("exceptionNotToLog");
        if (StringUtils.isNotBlank(property)) {
            String[] ss = StringUtils.split(property, ',');
            for (String s : ss) {
                exceptions.add(s);
            }
        } else {
            exceptions.add("net.kamhon.ieagle.exception.ValidatorException");
            exceptions.add("net.kamhon.ieagle.exception.InvalidCredentialsException");
        }
        return exceptions;
    }

    @Bean
    public ExceptionLoggingAroundAdvice exceptionLoggingAroundAdvice() {
        ExceptionLoggingAroundAdvice aroundAdvice = new ExceptionLoggingAroundAdvice();
        aroundAdvice.setExceptionLoggingBundle(exceptionLoggingBundle());
        return aroundAdvice;
    }

    // **************************
    // application context end
    // **************************

    @Bean
    public ServerConfiguration serverConfiguration() {
        ServerConfiguration serverConfiguration = new ServerConfiguration();
        serverConfiguration.setProductionMode(env.getProperty("production.mode", Boolean.class));
        return serverConfiguration;
    }

    @Bean
    public Object resourceBaseNames() {
        List<String> resources = new ArrayList<String>();
        String resource = env.getProperty("resourceBaseNames");
        if (StringUtils.isNotBlank(resource)) {
            String[] ss = StringUtils.split(resource, ',');
            for (String s : ss)
                resources.add(StringUtils.trim(s));
        }

        return resources;
    }

    private JdbcDao jdbcDao() {
        return null; // To change body of created methods use File | Settings | File Templates.
    }

    // *********************
    // DataSource start
    // *********************
    @Bean
    public DataSource dataSource() {
        DynamicDataSource ds = new DynamicDataSource();
        ds.setJndiName(env.getProperty("db.jndiName"));
        ds.setDriverClassName(env.getProperty("db.connection.driver_class"));
        ds.setUrl(env.getProperty("db.connection.url"));
        ds.setUsername(env.getProperty("db.connection.username"));
        ds.setPassword(env.getProperty("db.connection.password"));
        return ds;
    }

    // *********************
    // DataSource end
    // *********************

    // *********************
    // Core library start
    // *********************

    @Bean
    public Object exclusiveKeyPrefix() {
        List<String> prefixList = new ArrayList<String>();
        prefixList.add("");
        prefixList.add("format.");
        return prefixList;
    }

    @Bean
    public Locale systemLocale() {
        return new Locale(env.getProperty("default.locale.language"), env.getProperty("default.locale.country"));
    }

    /*@Bean
    public I18n i18n() {
    I18nImpl i18n = new I18nImpl();
    i18n.setSystemLocale(systemLocale());
    i18n.setLanguageService(languageService());
    return i18n;
    }
        
    @Bean
    public EmailService emailService() {
    EmailServiceImpl service = new EmailServiceImpl();
    service.setEmailqDao(emailqDao());
    return service;
    }
        
    @Bean
    public LanguageFrameworkService languageService() {
    LanguageFrameworkServiceImpl service = new LanguageFrameworkServiceImpl();
    service.setLanguageDao(languageDao());
    service.setLanguageResDao(languageResDao());
    service.setSystemLocale(systemLocale());
    service.setServerConfiguration(serverConfiguration());
    service.setExclusiveKeyPrefix(exclusiveKeyPrefix());
        
    return service;
    }
        
    @Bean
    public MenuFrameworkService menuFrameworkService() {
    MenuFrameworkServiceImpl service = new MenuFrameworkServiceImpl();
        
    UserMenuFrameworkDaoImpl userMenuFrameworkDao = new UserMenuFrameworkDaoImpl();
    userMenuFrameworkDao.setGenericDao((GenericDao) genericDao);
    service.setUserMenuDao(userMenuFrameworkDao);
    return service;
    }
        
    @Bean
    public SessionLogService sessionLogService() {
    SessionLogServiceImpl service = new SessionLogServiceImpl();
    service.setSessionLogDao(sessionLogDao());
    return service;
    }
        
    @Bean
    public SystemFrameworkService systemFrameworkService() {
    SystemFrameworkServiceImpl service = new SystemFrameworkServiceImpl();
    service.setSysParamDao(sysParamDao());
    return service;
    }
        
    @Bean
    public UserDetailsService userDetailsService() {
    UserDetailsServiceImpl service = new UserDetailsServiceImpl();
    service.setUserDetailsAccessCatDao(userDetailsAccessCatDao());
    service.setUserDetailsAccessDao(userDetailsAccessDao());
    service.setUserDetailsAccessExDao(userDetailsAccessExDao());
    service.setUserDetailsDao(userDetailsDao());
    service.setUserDetailsRoleAccessDao(userDetailsRoleAccessDao());
    service.setUserDetailsRoleDao(userDetailsRoleDao());
    service.setUserDetailsSqlDao(userDetailsSqlDao());
    service.setPasswordEncoder(encoder());
    service.setSaltSource(saltSource());
    return service;
    }*/

    @Bean
    public UserDetailsSqlDao userDetailsSqlDao() {
        UserDetailsSqlDaoImpl dao = new UserDetailsSqlDaoImpl();
        dao.setJdbcDao(jdbcDao());
        return dao;
    }

    @Bean
    public PasswordEncoder encoder() {
        return new Md5PasswordEncoder();
    }

    @Bean
    public SaltSource saltSource() {
        SystemWideSaltSource source = new SystemWideSaltSource();
        source.setSystemWideSalt("iLikeAutobot");
        return source;
    }

    @Bean
    public FreeMarkerTemplate freeMarkerEmailTemplate() {
        FreeMarkerTemplate template = new FreeMarkerTemplate();
        template.setClasspathTemplateDir(env.getProperty("mail.template.dir"));
        return template;
    }

    @Bean
    public EmailServerSetting emailServerSetting() {
        EmailServerSetting setting = new EmailServerSetting();
        setting.setSmtpHost(env.getProperty("mail.smtpHost.name"));
        setting.setSmtpPort(env.getProperty("mail.smtpPort"));
        setting.setDebug(env.getProperty("mail.debug.mode", Boolean.class));
        setting.setSendRetry(env.getProperty("mail.max.retry", Integer.class));
        setting.setDefaultSender(env.getProperty("mail.default.sender"));
        setting.setDefaultSender(env.getProperty("mail.default.receiver"));
        setting.setUsername(env.getProperty("mail.username"));
        setting.setPassword(env.getProperty("mail.password"));
        return setting;
    }

    /*@Bean
    public MailSender mailSender() {
    MailSenderImpl sender = new MailSenderImpl();
    sender.setEmailService(emailService());
    sender.setSendMail(sendMail());
    return sender;
    }*/

    @Bean
    public SendMail sendMail() {
        SendMail sendMail = new SendMail();
        sendMail.setEmailServerSetting(emailServerSetting());
        return sendMail;
    }

    // *********************
    // DAO
    // *********************
    /*@Bean
    public EmailqDao emailqDao() {
    EmailqDaoImpl dao = new EmailqDaoImpl();
    dao.setGenericDao((GenericDao<Emailq>) genericDao);
    return dao;
    }
        
    @Bean
    public LanguageDao languageDao() {
    LanguageDaoImpl dao = new LanguageDaoImpl();
    dao.setGenericDao((GenericDao<Language>) genericDao);
    return dao;
    }
        
    @Bean
    public LanguageResDao languageResDao() {
    LanguageResDaoImpl dao = new LanguageResDaoImpl();
    dao.setGenericDao((GenericDao<LanguageRes>) genericDao);
    return dao;
    }
        
    @Bean
    public SessionLogDao sessionLogDao() {
    SessionLogDaoImpl dao = new SessionLogDaoImpl();
    dao.setGenericDao((GenericDao<SessionLog>) genericDao);
    return dao;
    }
        
    @Bean
    public SysParamDao sysParamDao() {
    SysParamDaoImpl dao = new SysParamDaoImpl();
    dao.setGenericDao((GenericDao<SysParam>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsAccessCatDao userDetailsAccessCatDao() {
    UserDetailsAccessCatDaoImpl dao = new UserDetailsAccessCatDaoImpl();
    dao.setGenericDao((GenericDao<UserAccessCat>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsAccessDao userDetailsAccessDao() {
    UserDetailsAccessDaoImpl dao = new UserDetailsAccessDaoImpl();
    dao.setGenericDao((GenericDao<UserAccess>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsAccessExDao userDetailsAccessExDao() {
    UserDetailsAccessExDaoImpl dao = new UserDetailsAccessExDaoImpl();
    dao.setGenericDao((GenericDao<UserDetailsAccessEx>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsDao userDetailsDao() {
    UserDetailsDaoImpl dao = new UserDetailsDaoImpl();
    dao.setGenericDao((GenericDao<User>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsRoleAccessDao userDetailsRoleAccessDao() {
    UserDetailsRoleAccessDaoImpl dao = new UserDetailsRoleAccessDaoImpl();
    dao.setGenericDao((GenericDao<UserRoleAccess>) genericDao);
    return dao;
    }
        
    @Bean
    public UserDetailsRoleDao userDetailsRoleDao() {
    UserDetailsRoleDaoImpl dao = new UserDetailsRoleDaoImpl();
    dao.setGenericDao((GenericDao<UserRole>) genericDao);
    return dao;
    }*/
    // *********************
    // DAO - end
    // *********************

    // *********************
    // Core library end
    // *********************
}