org.craftercms.commerce.server.ServerConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.craftercms.commerce.server.ServerConfig.java

Source

/*
 * Copyright (C) 2007-2013 Crafter Software Corporation.
 *
 * This program 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.
 *
 * This program 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 org.craftercms.commerce.server;

import java.net.MalformedURLException;
import java.net.UnknownHostException;

import javax.annotation.PostConstruct;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.craftercms.commerce.api.CrafterCommerceConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * Main configuration component of the server, based on a Spring annotation driven  
 * context.  Connection details to a Solr and Mongo instance are read from a properties 
 * file (see TBD)
 * 
 * @author Michiel Verkaik (mverkaik@rivetlogic.com)
 *
 */
@Configuration
@PropertySource(CrafterCommerceConstants.SERVER_PROP_FILE_LOCATION)
public class ServerConfig {

    private static Logger LOGGER = LoggerFactory.getLogger(ServerConfig.class);
    @Autowired
    private Environment environment;
    private String solrUrl;
    private String mongoHost;
    private int mongoPort;
    private String mongoDb;

    /**
     * Initialize the server configuration by using the required properties
     * read from the environment.
     */
    @PostConstruct
    public void init() {

        solrUrl = environment.getRequiredProperty(CrafterCommerceConstants.PROP_SOLR_URL);
        mongoHost = environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_HOST);
        mongoPort = environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_PORT, Integer.class);
        mongoDb = environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_DB);

        StringBuilder initMessage = new StringBuilder();
        initMessage.append("\n\n***************************************************************\n");
        initMessage.append("Starting Crafter Commerce Server");
        initMessage.append("\n\t" + CrafterCommerceConstants.PROP_MONGO_HOST + " : "
                + environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_HOST));
        initMessage.append("\n\t" + CrafterCommerceConstants.PROP_MONGO_PORT + " : "
                + environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_PORT));
        initMessage.append("\n\t" + CrafterCommerceConstants.PROP_MONGO_DB + " : "
                + environment.getRequiredProperty(CrafterCommerceConstants.PROP_MONGO_DB));
        initMessage.append("\n\t" + CrafterCommerceConstants.PROP_SOLR_URL + " : "
                + environment.getRequiredProperty(CrafterCommerceConstants.PROP_SOLR_URL));
        initMessage.append("\n***************************************************************\n\n");

        LOGGER.info(initMessage.toString());
    }

    // -------------------- Beans ----------------------

    @Bean
    public MongoTemplate mongoTemplate() throws UnknownHostException, MongoException {
        return new MongoTemplate(mongoDbFactory());
    }

    @Bean
    public SolrServer solrServer() throws MalformedURLException {
        return new HttpSolrServer(solrUrl);
    }

    @Bean
    public Validator validator() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        return factory.getValidator();
    }

    // -------------------- Helper methods ----------------------

    private Mongo mongo() throws UnknownHostException, MongoException {
        return new Mongo(mongoHost, mongoPort);
    }

    private MongoDbFactory mongoDbFactory() throws UnknownHostException, MongoException {
        return new SimpleMongoDbFactory(mongo(), mongoDb);
    }

}