broker/ProtocolProcessorBootstrapper: code formatter used

This commit is contained in:
Andreas Mager
2017-02-01 17:13:39 +01:00
parent db31f77f9b
commit e23ac97e8e
@@ -13,6 +13,7 @@
* *
* You may elect to redistribute this code under either of these licenses. * You may elect to redistribute this code under either of these licenses.
*/ */
package io.moquette.spi.impl; package io.moquette.spi.impl;
import io.moquette.BrokerConstants; import io.moquette.BrokerConstants;
@@ -35,7 +36,6 @@ import io.moquette.spi.security.IAuthenticator;
import io.moquette.spi.security.IAuthorizator; import io.moquette.spi.security.IAuthorizator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.text.ParseException; import java.text.ParseException;
@@ -68,19 +68,25 @@ public class ProtocolProcessorBootstrapper {
/** /**
* Initialize the processing part of the broker. * Initialize the processing part of the broker.
* @param props the properties carrier where some props like port end host could be loaded. *
* For the full list check of configurable properties check moquette.conf file. * @param props
* @param embeddedObservers a list of callbacks to be notified of certain events inside the broker. * the properties carrier where some props like port end host could be loaded. For
* Could be empty list of null. * the full list check of configurable properties check moquette.conf file.
* @param authenticator an implementation of the authenticator to be used, if null load that specified in config * @param embeddedObservers
* and fallback on the default one (permit all). * a list of callbacks to be notified of certain events inside the broker. Could be
* @param authorizator an implementation of the authorizator to be used, if null load that specified in config * empty list of null.
* and fallback on the default one (permit all). * @param authenticator
* @param server the serber to init. * an implementation of the authenticator to be used, if null load that specified in
* config and fallback on the default one (permit all).
* @param authorizator
* an implementation of the authorizator to be used, if null load that specified in
* config and fallback on the default one (permit all).
* @param server
* the serber to init.
* @return the processor created for the broker. * @return the processor created for the broker.
* */ */
public ProtocolProcessor init(IConfig props, List<? extends InterceptHandler> embeddedObservers, public ProtocolProcessor init(IConfig props, List<? extends InterceptHandler> embeddedObservers,
IAuthenticator authenticator, IAuthorizator authorizator, Server server) { IAuthenticator authenticator, IAuthorizator authorizator, Server server) {
subscriptions = new SubscriptionsStore(); subscriptions = new SubscriptionsStore();
LOG.info("Initializing messages and sessions stores..."); LOG.info("Initializing messages and sessions stores...");
@@ -94,10 +100,10 @@ public class ProtocolProcessorBootstrapper {
List<InterceptHandler> observers = new ArrayList<>(embeddedObservers); List<InterceptHandler> observers = new ArrayList<>(embeddedObservers);
String interceptorClassName = props.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME); String interceptorClassName = props.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME);
if (interceptorClassName != null && !interceptorClassName.isEmpty()) { if (interceptorClassName != null && !interceptorClassName.isEmpty()) {
InterceptHandler handler = loadClass(interceptorClassName, InterceptHandler.class, Server.class, server); InterceptHandler handler = loadClass(interceptorClassName, InterceptHandler.class, Server.class, server);
if (handler != null) { if (handler != null) {
observers.add(handler); observers.add(handler);
} }
} }
m_interceptor = new BrokerInterceptor(props, observers); m_interceptor = new BrokerInterceptor(props, observers);
@@ -136,8 +142,11 @@ public class ProtocolProcessorBootstrapper {
LOG.info("Parsing ACL file. Path = {}.", aclFilePath); LOG.info("Parsing ACL file. Path = {}.", aclFilePath);
authorizator = ACLFileParser.parse(resourceLoader.loadResource(aclFilePath)); authorizator = ACLFileParser.parse(resourceLoader.loadResource(aclFilePath));
} catch (ParseException pex) { } catch (ParseException pex) {
LOG.error("Unable to parse ACL file. Path = {}, cause = {}, errorMessage = {}.", aclFilePath, LOG.error(
pex.getCause(), pex.getMessage()); "Unable to parse ACL file. Path = {}, cause = {}, errorMessage = {}.",
aclFilePath,
pex.getCause(),
pex.getMessage());
} }
} else { } else {
authorizator = new PermitAllAuthorizator(); authorizator = new PermitAllAuthorizator();
@@ -149,69 +158,105 @@ public class ProtocolProcessorBootstrapper {
connectionDescriptors = new ConnectionDescriptorStore(m_sessionsStore); connectionDescriptors = new ConnectionDescriptorStore(m_sessionsStore);
LOG.info("Initializing MQTT protocol processor..."); LOG.info("Initializing MQTT protocol processor...");
boolean allowAnonymous = Boolean.parseBoolean(props.getProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, "true")); boolean allowAnonymous = Boolean
boolean allowZeroByteClientId = Boolean.parseBoolean(props.getProperty(BrokerConstants.ALLOW_ZERO_BYTE_CLIENT_ID_PROPERTY_NAME, "false")); .parseBoolean(props.getProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, "true"));
m_processor.init(connectionDescriptors, subscriptions, messagesStore, m_sessionsStore, authenticator, allowAnonymous, allowZeroByteClientId, authorizator, m_interceptor, props.getProperty(BrokerConstants.PORT_PROPERTY_NAME)); boolean allowZeroByteClientId = Boolean
.parseBoolean(props.getProperty(BrokerConstants.ALLOW_ZERO_BYTE_CLIENT_ID_PROPERTY_NAME, "false"));
m_processor.init(
connectionDescriptors,
subscriptions,
messagesStore,
m_sessionsStore,
authenticator,
allowAnonymous,
allowZeroByteClientId,
authorizator,
m_interceptor,
props.getProperty(BrokerConstants.PORT_PROPERTY_NAME));
return m_processor; return m_processor;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T, U> T loadClass(String className, Class<T> iface, Class<U> constructorArgClass, U props) { private <T, U> T loadClass(String className, Class<T> iface, Class<U> constructorArgClass, U props) {
T instance = null; T instance = null;
try { try {
LOG.info("Loading class. ClassName = {}, interfaceName = {}.", className, iface.getName()); LOG.info("Loading class. ClassName = {}, interfaceName = {}.", className, iface.getName());
Class<?> clazz = Class.forName(className); Class<?> clazz = Class.forName(className);
// check if method getInstance exists // check if method getInstance exists
Method method = clazz.getMethod("getInstance", new Class[] {}); Method method = clazz.getMethod("getInstance", new Class[]{});
try { try {
LOG.info("Invoking getInstance() method. ClassName = {}, interfaceName = {}.", className, LOG.info(
iface.getName()); "Invoking getInstance() method. ClassName = {}, interfaceName = {}.",
instance = (T) method.invoke(null, new Object[] {}); className,
} catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException ex) { iface.getName());
LOG.error( instance = (T) method.invoke(null, new Object[]{});
"Unable to invoke getInstance() method. ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.", } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException ex) {
className, iface.getName(), ex.getCause(), ex.getMessage()); LOG.error(
return null; "Unable to invoke getInstance() method. "
} + "ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.",
} catch (NoSuchMethodException nsmex) { className,
try { iface.getName(),
// check if constructor with constructor arg class parameter ex.getCause(),
// exists ex.getMessage());
LOG.info("Invoking constructor with {} argument. ClassName = {}, interfaceName = {}.", return null;
constructorArgClass.getName(), className, iface.getName()); }
instance = this.getClass().getClassLoader().loadClass(className).asSubclass(iface) } catch (NoSuchMethodException nsmex) {
.getConstructor(constructorArgClass).newInstance(props); try {
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { // check if constructor with constructor arg class parameter
LOG.warn( // exists
"Unable to invoke constructor with {} argument. ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.", LOG.info(
constructorArgClass.getName(), className, iface.getName(), ex.getCause(), ex.getMessage()); "Invoking constructor with {} argument. ClassName = {}, interfaceName = {}.",
return null; constructorArgClass.getName(),
} catch (NoSuchMethodException | InvocationTargetException e) { className,
try { iface.getName());
LOG.info("Invoking default constructor. ClassName = {}, interfaceName = {}.", className, instance = this.getClass().getClassLoader().loadClass(className).asSubclass(iface)
iface.getName()); .getConstructor(constructorArgClass).newInstance(props);
// fallback to default constructor } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
instance = this.getClass().getClassLoader().loadClass(className).asSubclass(iface).newInstance(); LOG.warn(
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { "Unable to invoke constructor with {} argument. "
LOG.error( + "ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.",
"Unable to invoke default constructor. ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.", constructorArgClass.getName(),
className, iface.getName(), ex.getCause(), ex.getMessage()); className,
return null; iface.getName(),
} ex.getCause(),
} ex.getMessage());
} catch (ClassNotFoundException ex) { return null;
LOG.error("The class does not exist. ClassName = {}, interfaceName = {}.", className, iface.getName()); } catch (NoSuchMethodException | InvocationTargetException e) {
return null; try {
} catch (SecurityException ex) { LOG.info(
LOG.error( "Invoking default constructor. ClassName = {}, interfaceName = {}.",
"Unable to load class due to a security violation. ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.", className,
className, iface.getName(), ex.getCause(), ex.getMessage()); iface.getName());
return null; // fallback to default constructor
} instance = this.getClass().getClassLoader().loadClass(className).asSubclass(iface).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
LOG.error(
"Unable to invoke default constructor. "
+ "ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.",
className,
iface.getName(),
ex.getCause(),
ex.getMessage());
return null;
}
}
} catch (ClassNotFoundException ex) {
LOG.error("The class does not exist. ClassName = {}, interfaceName = {}.", className, iface.getName());
return null;
} catch (SecurityException ex) {
LOG.error(
"Unable to load class due to a security violation. "
+ "ClassName = {}, interfaceName = {}, cause = {}, errorMessage = {}.",
className,
iface.getName(),
ex.getCause(),
ex.getMessage());
return null;
}
return instance; return instance;
} }
public List<Subscription> getSubscriptions() { public List<Subscription> getSubscriptions() {
return m_sessionsStore.getSubscriptions(); return m_sessionsStore.getSubscriptions();