Java tutorial
/******************************************************************************* * Copyright (c) 2010, 2011 Ed Anuff and Usergrid, all rights reserved. * http://www.usergrid.com * * This file is part of Usergrid Stack. * * Usergrid Stack is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your option) * any later version. * * Usergrid Stack 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 Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License along * with Usergrid Stack. If not, see <http://www.gnu.org/licenses/>. * * Additional permission under GNU AGPL version 3 section 7 * * Linking Usergrid Stack statically or dynamically with other modules is making * a combined work based on Usergrid Stack. Thus, the terms and conditions of the * GNU General Public License cover the whole combination. * * In addition, as a special exception, the copyright holders of Usergrid Stack * give you permission to combine Usergrid Stack with free software programs or * libraries that are released under the GNU LGPL and with independent modules * that communicate with Usergrid Stack solely through: * * - Classes implementing the org.usergrid.services.Service interface * - Apache Shiro Realms and Filters * - Servlet Filters and JAX-RS/Jersey Filters * * You may copy and distribute such a system following the terms of the GNU AGPL * for Usergrid Stack and the licenses of the other code concerned, provided that ******************************************************************************/ 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.concurrent.Executors; import org.apache.log4j.Logger; 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.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 = Logger.getLogger(MongoServer.class); EntityManagerFactory emf; ServiceManagerFactory smf; ManagementService management; Realm realm; SessionsSecurityManager securityManager; Channel channel; 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 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() { 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)); 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..."); } }