All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.usergrid.mongo.MongoServer Maven / Gradle / Ivy

There is a newer version: 0.0.27.1
Show newest version
/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * 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 org.usergrid.mongo;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.InetSocketAddress;
import java.nio.ByteOrder;
import java.util.Properties;
import java.util.concurrent.Executors;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.realm.Realm;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.HeapChannelBufferFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.usergrid.management.ManagementService;
import org.usergrid.persistence.EntityManagerFactory;
import org.usergrid.persistence.cassandra.EntityManagerFactoryImpl;
import org.usergrid.services.ServiceManagerFactory;

public class MongoServer {

	private static final Logger logger = LoggerFactory
			.getLogger(MongoServer.class);

	EntityManagerFactory emf;
	ServiceManagerFactory smf;
	ManagementService management;
	Realm realm;
	SessionsSecurityManager securityManager;
	Channel channel;
	Properties properties;

	public static void main(String[] args) throws Exception {
		MongoServer server = new MongoServer();
		server.startSpring();
		server.startServer();
	}

	public MongoServer() {
	}

	@Autowired
	public void setEntityManagerFactory(EntityManagerFactory emf) {
		this.emf = emf;
	}

	@Autowired
	public void setServiceManagerFactory(ServiceManagerFactory smf) {
		this.smf = smf;
	}

	@Autowired
	public void setManagementService(ManagementService management) {
		this.management = management;
	}

	@Autowired
	public void setRealm(Realm realm) {
		this.realm = realm;
	}

	public Properties getProperties() {
		return properties;
	}

	@Autowired
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String[] getApplicationContextLocations() {
		String[] locations = { "applicationContext.xml" };
		return locations;
	}

	public void startSpring() {

		String[] locations = getApplicationContextLocations();
		ApplicationContext ac = new ClassPathXmlApplicationContext(locations);

		AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
		acbf.autowireBeanProperties(this,
				AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
		acbf.initializeBean(this, "mongoServer");

		assertNotNull(emf);
		assertTrue(
				"EntityManagerFactory is instance of EntityManagerFactoryImpl",
				emf instanceof EntityManagerFactoryImpl);

	}

	public void startServer() {

		if ((properties != null)
				&& (Boolean.parseBoolean(properties.getProperty(
						"usergrid.mongo.disable", "false")))) {
			logger.info("Usergrid Mongo Emulation Server Disabled");
			return;
		}

		logger.info("Starting Usergrid Mongo Emulation Server");

		if (realm != null) {
			securityManager = new DefaultSecurityManager(realm);
		}

		// Configure the server.
		ServerBootstrap bootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));

		bootstrap.setOption("child.bufferFactory",
				HeapChannelBufferFactory.getInstance(ByteOrder.LITTLE_ENDIAN));

		// Set up the pipeline factory.
		ExecutionHandler executionHandler = new ExecutionHandler(
				new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));
    // TODO if config'ed for SSL, start the SslMSPF instead, change port as well?
		bootstrap.setPipelineFactory(new MongoServerPipelineFactory(emf, smf,
				management, securityManager, executionHandler));

		// Bind and start to accept incoming connections.
		channel = bootstrap.bind(new InetSocketAddress(27017));

		logger.info("Usergrid Mongo API Emulation Server accepting connections...");
	}

	public void stopServer() {
		logger.info("Stopping Usergrid Mongo Emulation Server");
		if (channel != null) {
			channel.close();
			channel = null;
		}
		logger.info("Usergrid Mongo API Emulation Server stopped...");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy