de.thm.arsnova.config.ExtraConfig.java Source code

Java tutorial

Introduction

Here is the source code for de.thm.arsnova.config.ExtraConfig.java

Source

/*
 * This file is part of ARSnova Backend.
 * Copyright (C) 2012-2015 The ARSnova Team
 *
 * ARSnova Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ARSnova Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.thm.arsnova.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import de.thm.arsnova.connector.client.ConnectorClient;
import de.thm.arsnova.connector.client.ConnectorClientImpl;
import de.thm.arsnova.socket.ARSnovaSocket;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import de.thm.arsnova.ImageUtils;

@Configuration
@EnableCaching
public class ExtraConfig {

    @Autowired
    private Environment env;

    @Value(value = "${connector.enable}")
    private boolean connectorEnable;
    @Value(value = "${connector.uri}")
    private String connectorUri;
    @Value(value = "${connector.username}")
    private String connectorUsername;
    @Value(value = "${connector.password}")
    private String connectorPassword;

    @Value(value = "${socketio.ip}")
    private String socketIp;
    @Value(value = "${socketio.port}")
    private int socketPort;
    @Value(value = "${security.ssl}")
    private boolean socketUseSll;
    @Value(value = "${security.keystore}")
    private String socketKeystore;
    @Value(value = "${security.storepass}")
    private String socketStorepass;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocations(new Resource[] { new ClassPathResource("arsnova.properties.example"),
                new FileSystemResource("/etc/arsnova/arsnova.properties"), });
        configurer.setIgnoreResourceNotFound(true);
        configurer.setIgnoreUnresolvablePlaceholders(false);
        return configurer;
    }

    @Bean(name = "connectorClient")
    public ConnectorClient connectorClient() {
        if (!connectorEnable) {
            return null;
        }

        final ConnectorClientImpl connectorClient = new ConnectorClientImpl();
        connectorClient.setServiceLocation(connectorUri);
        connectorClient.setUsername(connectorUsername);
        connectorClient.setPassword(connectorPassword);
        return connectorClient;
    }

    @Profile("!test")
    @Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer")
    public ARSnovaSocket socketServer() {
        final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer();
        socketServer.setHostIp(socketIp);
        socketServer.setPortNumber(socketPort);
        socketServer.setUseSSL(socketUseSll);
        socketServer.setKeystore(socketKeystore);
        socketServer.setStorepass(socketStorepass);
        return socketServer;
    }

    @Profile("test")
    @Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer")
    public ARSnovaSocket socketTestServer() {
        final int testSocketPort = 1234;
        final ARSnovaSocketIOServer socketServer = new ARSnovaSocketIOServer();
        socketServer.setHostIp(socketIp);
        socketServer.setPortNumber(socketPort + testSocketPort);
        socketServer.setUseSSL(socketUseSll);
        socketServer.setKeystore(socketKeystore);
        socketServer.setStorepass(socketStorepass);
        return socketServer;
    }

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }

    @Bean
    public ImageUtils imageUtils() {
        return new ImageUtils();
    }
}