2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2015-08-08 19:36:14 +02:00
|
|
|
* Copyright (c) 2012-2015 The original author or authors
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server;
|
2013-09-19 20:10:18 +02:00
|
|
|
|
2016-06-28 17:27:35 +02:00
|
|
|
import com.hazelcast.config.ClasspathXmlConfig;
|
|
|
|
|
import com.hazelcast.config.Config;
|
|
|
|
|
import com.hazelcast.config.FileSystemXmlConfig;
|
|
|
|
|
import com.hazelcast.core.Hazelcast;
|
|
|
|
|
import com.hazelcast.core.HazelcastInstance;
|
|
|
|
|
import com.hazelcast.core.HazelcastInstanceNotActiveException;
|
|
|
|
|
import com.hazelcast.core.ITopic;
|
2015-12-08 14:04:35 +01:00
|
|
|
import io.moquette.BrokerConstants;
|
2016-06-28 17:27:35 +02:00
|
|
|
import io.moquette.interception.HazelcastInterceptHandler;
|
|
|
|
|
import io.moquette.interception.HazelcastMsg;
|
2015-12-08 12:06:31 +01:00
|
|
|
import io.moquette.interception.InterceptHandler;
|
2016-02-03 23:20:15 +01:00
|
|
|
import io.moquette.parser.proto.messages.PublishMessage;
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.server.config.MemoryConfig;
|
|
|
|
|
import io.moquette.spi.impl.SimpleMessaging;
|
|
|
|
|
import io.moquette.server.config.FilesystemConfig;
|
|
|
|
|
import io.moquette.server.config.IConfig;
|
2016-06-28 17:27:35 +02:00
|
|
|
import io.moquette.server.config.MemoryConfig;
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.server.netty.NettyAcceptor;
|
|
|
|
|
import io.moquette.spi.impl.ProtocolProcessor;
|
2016-06-28 17:27:35 +02:00
|
|
|
import io.moquette.spi.impl.SimpleMessaging;
|
2015-12-13 17:01:23 +01:00
|
|
|
import io.moquette.spi.security.IAuthenticator;
|
|
|
|
|
import io.moquette.spi.security.IAuthorizator;
|
2015-12-13 16:42:04 +01:00
|
|
|
import io.moquette.spi.security.ISslContextCreator;
|
2015-03-15 16:45:11 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-09-19 20:10:18 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2015-12-08 12:06:31 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
2014-03-02 18:16:50 +01:00
|
|
|
import java.util.Properties;
|
2015-03-11 10:44:00 -04:00
|
|
|
|
2013-09-19 20:10:18 +02:00
|
|
|
/**
|
|
|
|
|
* Launch a configured version of the server.
|
|
|
|
|
* @author andrea
|
|
|
|
|
*/
|
|
|
|
|
public class Server {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Server.class);
|
|
|
|
|
|
|
|
|
|
private ServerAcceptor m_acceptor;
|
2015-08-22 15:35:11 +02:00
|
|
|
|
2015-12-08 18:12:00 +01:00
|
|
|
private volatile boolean m_initialized;
|
|
|
|
|
|
|
|
|
|
private ProtocolProcessor m_processor;
|
|
|
|
|
|
2016-06-28 17:27:35 +02:00
|
|
|
private HazelcastInstance hazelcastInstance;
|
|
|
|
|
|
|
|
|
|
|
2013-09-19 20:10:18 +02:00
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
final Server server = new Server();
|
2013-11-24 11:51:15 +01:00
|
|
|
server.startServer();
|
2016-01-17 13:32:14 +01:00
|
|
|
System.out.println("Server started, version 0.9-SNAPSHOT");
|
2013-09-19 20:10:18 +02:00
|
|
|
//Bind a shutdown hook
|
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
server.stopServer();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 12:56:38 +02:00
|
|
|
/**
|
|
|
|
|
* Starts Moquette bringing the configuration from the file
|
2015-08-22 15:35:11 +02:00
|
|
|
* located at m_config/moquette.conf
|
2014-07-12 12:56:38 +02:00
|
|
|
*/
|
2013-09-19 20:10:18 +02:00
|
|
|
public void startServer() throws IOException {
|
2015-08-22 15:35:11 +02:00
|
|
|
final IConfig config = new FilesystemConfig();
|
|
|
|
|
startServer(config);
|
2014-07-12 12:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts Moquette bringing the configuration from the given file
|
|
|
|
|
*/
|
|
|
|
|
public void startServer(File configFile) throws IOException {
|
2015-08-22 15:35:11 +02:00
|
|
|
LOG.info("Using m_config file: " + configFile.getAbsolutePath());
|
|
|
|
|
final IConfig config = new FilesystemConfig(configFile);
|
|
|
|
|
startServer(config);
|
2014-07-12 12:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts the server with the given properties.
|
|
|
|
|
*
|
2015-03-11 10:44:00 -04:00
|
|
|
* Its suggested to at least have the following properties:
|
2014-07-12 12:56:38 +02:00
|
|
|
* <ul>
|
|
|
|
|
* <li>port</li>
|
|
|
|
|
* <li>password_file</li>
|
|
|
|
|
* </ul>
|
|
|
|
|
*/
|
|
|
|
|
public void startServer(Properties configProps) throws IOException {
|
2015-08-22 15:35:11 +02:00
|
|
|
final IConfig config = new MemoryConfig(configProps);
|
|
|
|
|
startServer(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts Moquette bringing the configuration files from the given Config implementation.
|
|
|
|
|
*/
|
|
|
|
|
public void startServer(IConfig config) throws IOException {
|
2015-12-08 12:06:31 +01:00
|
|
|
startServer(config, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-13 16:26:12 +01:00
|
|
|
* Starts Moquette with config provided by an implementation of IConfig class and with the
|
|
|
|
|
* set of InterceptHandler.
|
2015-12-08 12:06:31 +01:00
|
|
|
* */
|
|
|
|
|
public void startServer(IConfig config, List<? extends InterceptHandler> handlers) throws IOException {
|
2015-12-13 17:01:23 +01:00
|
|
|
startServer(config, handlers, null, null, null);
|
2015-12-13 16:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-13 17:01:23 +01:00
|
|
|
public void startServer(IConfig config, List<? extends InterceptHandler> handlers,
|
|
|
|
|
ISslContextCreator sslCtxCreator, IAuthenticator authenticator,
|
|
|
|
|
IAuthorizator authorizator) throws IOException {
|
2015-12-08 12:06:31 +01:00
|
|
|
if (handlers == null) {
|
|
|
|
|
handlers = Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-24 01:15:09 -03:00
|
|
|
final String handlerProp = System.getProperty("intercept.handler");
|
|
|
|
|
if (handlerProp != null) {
|
2015-08-22 15:35:11 +02:00
|
|
|
config.setProperty("intercept.handler", handlerProp);
|
2015-07-24 01:15:09 -03:00
|
|
|
}
|
2015-12-08 14:04:35 +01:00
|
|
|
LOG.info("Persistent store file: " + config.getProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME));
|
2016-06-28 17:27:35 +02:00
|
|
|
if (config.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME) !=null && config.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME).equals(HazelcastInterceptHandler.class.getCanonicalName())) {
|
|
|
|
|
if (config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION) !=null){
|
|
|
|
|
Config hzconfig = null;
|
|
|
|
|
if (this.getClass().getClassLoader().getResource(config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION)) != null) {
|
|
|
|
|
hzconfig = new ClasspathXmlConfig(config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION));
|
|
|
|
|
} else {
|
|
|
|
|
hzconfig = new FileSystemXmlConfig(config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION));
|
|
|
|
|
}
|
|
|
|
|
LOG.info(String.format("starting server with hazelcast configuration file : %s",hzconfig));
|
|
|
|
|
hazelcastInstance = Hazelcast.newHazelcastInstance(hzconfig);
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info("starting server with hazelcast default file");
|
|
|
|
|
hazelcastInstance = Hazelcast.newHazelcastInstance();
|
|
|
|
|
}
|
|
|
|
|
listenOnHazelCastMsg();
|
|
|
|
|
}
|
|
|
|
|
final ProtocolProcessor processor = SimpleMessaging.getInstance().init(config, handlers, authenticator, authorizator, this);
|
2015-08-22 15:35:11 +02:00
|
|
|
|
2015-12-13 16:26:12 +01:00
|
|
|
if (sslCtxCreator == null) {
|
|
|
|
|
sslCtxCreator = new DefaultMoquetteSslContextCreator(config);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 11:51:15 +01:00
|
|
|
m_acceptor = new NettyAcceptor();
|
2015-12-13 16:26:12 +01:00
|
|
|
m_acceptor.initialize(processor, config, sslCtxCreator);
|
2015-12-08 18:12:00 +01:00
|
|
|
m_processor = processor;
|
|
|
|
|
m_initialized = true;
|
2016-06-28 17:27:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void listenOnHazelCastMsg() {
|
|
|
|
|
HazelcastInstance hz = getHazelcastInstance();
|
|
|
|
|
ITopic<HazelcastMsg> topic = hz.getTopic("moquette");
|
|
|
|
|
topic.addMessageListener(new HazelcastListener(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HazelcastInstance getHazelcastInstance(){
|
|
|
|
|
return hazelcastInstance;
|
2015-12-08 18:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use the broker to publish a message. It's intended for embedding applications.
|
|
|
|
|
* It can be used only after the server is correctly started with startServer.
|
|
|
|
|
*
|
|
|
|
|
* @param msg the message to forward.
|
|
|
|
|
* @throws IllegalStateException if the server is not yet started
|
|
|
|
|
* */
|
|
|
|
|
public void internalPublish(PublishMessage msg) {
|
|
|
|
|
if (!m_initialized) {
|
|
|
|
|
throw new IllegalStateException("Can't publish on a server is not yet started");
|
|
|
|
|
}
|
|
|
|
|
m_processor.internalPublish(msg);
|
2013-09-19 20:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stopServer() {
|
2015-03-11 11:04:56 -04:00
|
|
|
LOG.info("Server stopping...");
|
2013-09-19 20:10:18 +02:00
|
|
|
m_acceptor.close();
|
2015-09-30 21:41:55 +02:00
|
|
|
SimpleMessaging.getInstance().shutdown();
|
2015-12-08 18:12:00 +01:00
|
|
|
m_initialized = false;
|
2016-06-28 17:27:35 +02:00
|
|
|
if (hazelcastInstance != null ){
|
|
|
|
|
try {
|
|
|
|
|
hazelcastInstance.shutdown();
|
|
|
|
|
} catch (HazelcastInstanceNotActiveException e){
|
|
|
|
|
LOG.info("hazelcast allready shutdown");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2015-03-11 11:04:56 -04:00
|
|
|
LOG.info("Server stopped");
|
2013-09-19 20:10:18 +02:00
|
|
|
}
|
|
|
|
|
}
|