From a4f79a0f0c3b33af3966327ff46762e9e12fcd45 Mon Sep 17 00:00:00 2001 From: David BRASSELY Date: Tue, 4 Mar 2014 15:09:54 +0100 Subject: [PATCH] Internal refactoring using Guice injection --- openesb-standalone-container/pom.xml | 7 + .../framework/StandaloneContainer.java | 114 ++-------- .../framework/StandaloneContainerMBean.java | 2 +- .../framework/StandaloneInstance.java | 33 +++ .../framework/StandalonePlatformContext.java | 64 ++---- .../openesb/standalone/inject/Injector.java | 31 +++ .../jmx/MBServerConnectorFactory.java | 180 ++++++++------- .../auth/login/JMXAuthenticator.java} | 13 +- .../jta/TransactionManagerProvider.java | 18 ++ .../standalone/module/StandaloneModule.java | 42 ++++ .../standalone/naming/ContextProvider.java | 51 +++++ .../jndi/impl/InitialContexFactoryImpl.java | 2 +- .../tomcat/TomcatDataSourcePoolFactory.java | 2 +- .../naming/test/TestConnection01.java | 79 ------- .../standalone/naming/utils/Bundle.properties | 28 --- .../standalone/naming/utils/OEContext.xml | 79 ------- .../standalone/naming/utils/OEContext.xsd | 66 ------ .../naming/utils/OEContextSample.xml | 208 ------------------ .../security/SecurityProviderImpl.java | 62 +++--- .../settings/ImmutableSettings.java | 5 + .../openesb/standalone/settings/Settings.java | 2 + .../settings/yaml/YamlSettingsProvider.java | 71 ++++++ .../{naming => }/utils/I18NBundle.java | 21 +- openesb-standalone-packaging/pom.xml | 12 + .../src/main/assembly/distribution.xml | 2 + pom.xml | 1 + 26 files changed, 449 insertions(+), 746 deletions(-) create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneInstance.java create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/inject/Injector.java rename openesb-standalone-container/src/main/java/net/openesb/standalone/{security/auth/login/JMXauthenticator.java => jmx/auth/login/JMXAuthenticator.java} (72%) create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/jta/TransactionManagerProvider.java create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/module/StandaloneModule.java create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/ContextProvider.java delete mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/test/TestConnection01.java delete mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/Bundle.properties delete mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xml delete mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xsd delete mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContextSample.xml create mode 100644 openesb-standalone-container/src/main/java/net/openesb/standalone/settings/yaml/YamlSettingsProvider.java rename openesb-standalone-container/src/main/java/net/openesb/standalone/{naming => }/utils/I18NBundle.java (88%) diff --git a/openesb-standalone-container/pom.xml b/openesb-standalone-container/pom.xml index 34091a7..b41f3a7 100644 --- a/openesb-standalone-container/pom.xml +++ b/openesb-standalone-container/pom.xml @@ -91,6 +91,13 @@ runtime + + + com.google.inject + guice + ${guice.version} + + junit junit diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainer.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainer.java index 2ca5166..b8fa9c0 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainer.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainer.java @@ -1,27 +1,15 @@ package net.openesb.standalone.framework; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.Map; +import com.sun.jbi.platform.PlatformContext; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.management.MBeanServer; import javax.management.ObjectName; +import net.openesb.standalone.inject.Injector; import net.openesb.standalone.jmx.MBServerConnectorFactory; -import net.openesb.standalone.security.SecurityProviderImpl; -//import net.openesb.standalone.node.Node; -//import net.openesb.standalone.node.NodeBuilder; -import net.openesb.standalone.settings.ImmutableSettings; import net.openesb.standalone.settings.Settings; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; -import org.yaml.snakeyaml.representer.Representer; -import org.yaml.snakeyaml.resolver.Resolver; /** * JBI framework wrapper for OpenESB Standalone platform. @@ -40,24 +28,12 @@ public class StandaloneContainer extends com.sun.jbi.framework.JBIFramework implements StandaloneContainerMBean { - public static final String CONFIG_FILE = "openesb.config"; - public static final String INSTALL_ROOT = "install.root"; - public static final String INSTANCE_NAME = "instance.name"; - public static final String CONNECTOR_PORT = "instance.port"; - /** - * Configuration defaults. - */ - private static final String DEFAULT_INSTALL_ROOT = System.getProperty("user.dir"); - private static final String DEFAULT_INSTANCE_NAME = "server"; - private static final int DEFAULT_CONNECTOR_PORT = 8699; - private StandalonePlatformContext mPlatformContext; private boolean mLoaded; - private final Properties systemEnv; + public static Properties systemEnv; - private static final Logger mLog = + private static final Logger LOG = Logger.getLogger(StandaloneContainer.class.getPackage().getName()); -// private Node instanceNode; - private Settings settings; + /** * Creates a new instance of the JBI framework. @@ -67,46 +43,6 @@ public class StandaloneContainer systemEnv = environment; } - private void init() throws Exception { - String installRoot = systemEnv.getProperty(INSTALL_ROOT, DEFAULT_INSTALL_ROOT); - - String configFile = systemEnv.getProperty(CONFIG_FILE); - - if (configFile == null) { - configFile = installRoot + File.separatorChar + "config/openesb.yaml"; - } - - mLog.log(Level.FINE, "Trying to load configuration from {0}", configFile); - - Map configurations = null; - - try { - Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(), - new Resolver() { - @Override - protected void addImplicitResolvers() { - } - }); - InputStream input = new FileInputStream(new File(configFile)); - configurations = (Map) yaml.load(input); - - settings = new ImmutableSettings(configurations); - mLog.log(Level.FINE, "Configuration loaded from {0}", configFile); - } catch (FileNotFoundException fnfe) { - mLog.log(Level.WARNING, "Unable to load configuration file {0}. Default configuration will be used.", configFile); - settings = new ImmutableSettings(null); - } - - mPlatformContext = new StandalonePlatformContext( - installRoot, - settings.get(INSTANCE_NAME, DEFAULT_INSTANCE_NAME), - settings.getAsInt(CONNECTOR_PORT, DEFAULT_CONNECTOR_PORT)); - - mPlatformContext.setSecurityProvider( - new SecurityProviderImpl( - (Map>) configurations.get("realm"))); - } - /** * Load the JBI framework with the specified environment. When this method * retuns, all public interfaces and system services have completely @@ -119,10 +55,10 @@ public class StandaloneContainer public synchronized void load() throws Exception { if (mLoaded) { - throw new IllegalStateException("JBI framework already loaded!"); + throw new IllegalStateException("OpenESB runtime already loaded!"); } - this.init(); + PlatformContext mPlatformContext = Injector.getInstance().getInjector().getInstance(PlatformContext.class); // Register a management MBean for this framework instance ObjectName fwMBeanName = new ObjectName("net.open-esb.standalone", @@ -140,19 +76,9 @@ public class StandaloneContainer } mbs.registerMBean(this, fwMBeanName); - // Setup the remote JMX connector server - Integer port = null; - - try { - port = settings.getAsInt(CONNECTOR_PORT, DEFAULT_CONNECTOR_PORT); - MBServerConnectorFactory.getInstance().setPort(port); - MBServerConnectorFactory.getInstance().setMBeanServer( - mPlatformContext.getMBeanServer()); - - MBServerConnectorFactory.getInstance().createConnector(); - } catch (NumberFormatException nfEx) { - mLog.log(Level.WARNING, "Invalid connector server port: {0}. Remote JMX connector will not be created.", port); - } + MBServerConnectorFactory connectorFactory = + Injector.getInstance().getInjector().getInstance(MBServerConnectorFactory.class); + connectorFactory.createConnector(); // For stand-alone JBI, JBI_HOME = platform install root systemEnv.setProperty("com.sun.jbi.home", @@ -160,6 +86,8 @@ public class StandaloneContainer // -------------------------------------------- // TODO: removing this part asap + Settings settings = Injector.getInstance().getInjector().getInstance(Settings.class); + System.setProperty("http.port", settings.get("http.port", "4848")); System.setProperty("http.enabled", @@ -171,16 +99,14 @@ public class StandaloneContainer prepare(); ready(true); - // instanceNode = NodeBuilder.nodeBuilder(settings).build(); - - // instanceNode.start(); - // JBI framework has been loaded mLoaded = true; } - + public String getServiceUrl() { - return MBServerConnectorFactory.getInstance().getServiceUrl(); + MBServerConnectorFactory connectorFactory = + Injector.getInstance().getInjector().getInstance(MBServerConnectorFactory.class); + return connectorFactory.getServiceUrl(); } /** @@ -207,15 +133,15 @@ public class StandaloneContainer return; } - // instanceNode.stop(); - shutdown(); terminate(); try { - MBServerConnectorFactory.getInstance().destroy(); + MBServerConnectorFactory connectorFactory = + Injector.getInstance().getInjector().getInstance(MBServerConnectorFactory.class); + connectorFactory.destroy(); } catch (Exception ex) { - mLog.log(Level.SEVERE, "Error during framework shutdown: {0}", ex.toString()); + LOG.log(Level.SEVERE, "Error during framework shutdown: {0}", ex.toString()); } mLoaded = false; diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainerMBean.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainerMBean.java index 5707136..9cd6f57 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainerMBean.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneContainerMBean.java @@ -1,7 +1,7 @@ package net.openesb.standalone.framework; /** - * Management interface for OpenESB Standalone Container. + * Management interface for OpenESB Standalone Container. * * @author David BRASSELY (brasseld at gmail.com) * @author OpenESB Community diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneInstance.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneInstance.java new file mode 100644 index 0000000..507efb3 --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandaloneInstance.java @@ -0,0 +1,33 @@ +package net.openesb.standalone.framework; + +import javax.inject.Inject; +import net.openesb.standalone.jmx.MBServerConnectorFactory; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class StandaloneInstance implements StandaloneContainerMBean { + + private boolean mLoaded; + + @Inject + private MBServerConnectorFactory connectorFactory; + + @Override + public boolean isLoaded() { + return mLoaded; + } + + @Override + public void load() throws Exception { + connectorFactory.createConnector(); + } + + @Override + public void unload() throws Exception { + connectorFactory.destroy(); + } + +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandalonePlatformContext.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandalonePlatformContext.java index 048db85..4e013d7 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandalonePlatformContext.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/framework/StandalonePlatformContext.java @@ -5,19 +5,20 @@ import com.sun.jbi.platform.PlatformEventListener; import com.sun.jbi.security.KeyStoreUtil; import java.io.File; -import java.net.MalformedURLException; import java.util.Set; import java.util.HashSet; -import java.util.Hashtable; import java.util.logging.Logger; import java.util.logging.Level; +import javax.inject.Inject; +import javax.inject.Named; import javax.management.MBeanServerConnection; import javax.management.MBeanServer; -import javax.naming.Context; import javax.naming.InitialContext; +import javax.transaction.TransactionManager; import net.openesb.security.SecurityProvider; -import net.openesb.standalone.naming.jndi.impl.InitialContexFactoryImpl; +import net.openesb.standalone.jmx.MBServerConnectorFactory; +import net.openesb.standalone.settings.Settings; /** * Implementation of PlatformContext for OpenESB Standalone. @@ -27,33 +28,20 @@ import net.openesb.standalone.naming.jndi.impl.InitialContexFactoryImpl; */ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformContext { - private String mInstanceName; - private String mInstanceRoot; - private String mInstallRoot; - private int mConnectorPort; - private InitialContext mNamingContext; - private Logger mLog = Logger.getLogger(getClass().getPackage().getName()); + private static final String DEFAULT_INSTANCE_NAME = "server"; + private static final String INSTANCE_NAME = "instance.name"; + + private final String mInstanceName; - public StandalonePlatformContext(String installRoot, String instanceName, int connectorPort) { - mInstallRoot = installRoot; - mInstanceName = instanceName; - mInstanceRoot = installRoot + File.separator + instanceName; - mConnectorPort = connectorPort; - - try { - Hashtable env = new Hashtable(); - env.put(Context.INITIAL_CONTEXT_FACTORY, - InitialContexFactoryImpl.class.getName()); - - File context = new File(mInstallRoot + "/config/context.xml"); - env.put(Context.PROVIDER_URL, context.toURI().toURL().toString()); - - mNamingContext = new InitialContext(env); - } catch (javax.naming.NamingException nmEx) { - mLog.warning(nmEx.toString()); - } catch (MalformedURLException nmEx) { - mLog.warning(nmEx.toString()); - } + @Inject private MBServerConnectorFactory jmxConnector; + @Inject private SecurityProvider securityProvider; + @Inject private TransactionManager transactionManager; + @Inject private InitialContext namingContext; + @Inject @Named("install.root") private String mInstallRoot; + + @Inject + public StandalonePlatformContext(Settings settings) { + mInstanceName = settings.get(INSTANCE_NAME, DEFAULT_INSTANCE_NAME); } /** @@ -69,7 +57,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC @Override public javax.transaction.TransactionManager getTransactionManager() throws Exception { - return new com.atomikos.icatch.jta.UserTransactionManager(); + return transactionManager; } /** @@ -266,7 +254,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC */ @Override public String getJmxRmiPort() { - return Integer.toString(mConnectorPort); + return Integer.toString(jmxConnector.getPort()); } /** @@ -276,7 +264,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC */ @Override public MBeanServer getMBeanServer() { - return java.lang.management.ManagementFactory.getPlatformMBeanServer(); + return jmxConnector.getMBeanServer(); } /** @@ -286,7 +274,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC */ @Override public String getInstanceRoot() { - return mInstanceRoot; + return mInstallRoot + File.separator + mInstanceName; } /** @@ -328,7 +316,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC */ @Override public InitialContext getNamingContext() { - return mNamingContext; + return namingContext; } /** @@ -394,12 +382,6 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC public void setJbiLogLevel(String target, java.util.logging.Level level) { Logger.getLogger(JBI_LOGGER_NAME).setLevel(level); } - - private SecurityProvider securityProvider; - - public void setSecurityProvider(SecurityProvider securityProvider) { - this.securityProvider = securityProvider; - } @Override public SecurityProvider getSecurityProvider() { diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/inject/Injector.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/inject/Injector.java new file mode 100644 index 0000000..5dac86c --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/inject/Injector.java @@ -0,0 +1,31 @@ +package net.openesb.standalone.inject; + +import com.google.inject.Guice; +import net.openesb.standalone.module.StandaloneModule; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class Injector { + + private static Injector instance; + private com.google.inject.Injector injector; + + private Injector() { + injector = Guice.createInjector(new StandaloneModule()); + } + + public static Injector getInstance() { + if (instance == null) { + instance = new Injector(); + } + + return instance; + } + + public com.google.inject.Injector getInjector() { + return this.injector; + } +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/MBServerConnectorFactory.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/MBServerConnectorFactory.java index 056a182..1f605c0 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/MBServerConnectorFactory.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/MBServerConnectorFactory.java @@ -5,11 +5,13 @@ import java.rmi.registry.LocateRegistry; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; +import javax.inject.Inject; import javax.management.MBeanServer; -import javax.management.MBeanServerFactory; +import javax.management.remote.JMXAuthenticator; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL; +import net.openesb.standalone.settings.Settings; /** * @@ -20,49 +22,29 @@ public class MBServerConnectorFactory { private static final String DEFAULT_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:%s/jmxrmi"; private static final Logger LOG = Logger.getLogger(MBServerConnectorFactory.class.getName()); - private static MBeanServer server; - private static boolean threaded; - private static boolean daemon; - private static int port; - private static String serviceUrl; - private static JMXConnectorServer connectorServer; - - private static class MBServerConnectorFactoryHolder { - - private static final MBServerConnectorFactory INSTANCE = - new MBServerConnectorFactory(); - } + private static final int DEFAULT_CONNECTOR_PORT = 8699; + private static final String CONNECTOR_PORT = "instance.port"; + + @Inject + private Settings settings; + + @Inject + private JMXAuthenticator authenticator; + + private MBeanServer server; + private boolean threaded; + private boolean daemon; + private int port; + private String serviceUrl; + private JMXConnectorServer connectorServer; private static class MBeanServerHolder { private static final MBeanServer INSTANCE = - MBeanServerFactory.createMBeanServer(); + java.lang.management.ManagementFactory.getPlatformMBeanServer(); } - private MBServerConnectorFactory() { - } - - public static MBServerConnectorFactory getInstance() { - return MBServerConnectorFactoryHolder.INSTANCE; - } - - public void setPort(int newPort) { - port = newPort; - } - - public void setMBeanServer(MBeanServer ms) { - server = ms; - } - - public void setThreaded(boolean fthread) { - threaded = fthread; - } - - public void setDaemon(boolean fdaemon) { - daemon = fdaemon; - } - - private int getURLLocalHostPort(String url) { + private int getURLLocalHostPort(String url) { int portStart = url.indexOf("localhost") + 10; int portEnd; int port = 0; @@ -70,16 +52,16 @@ public class MBServerConnectorFactory { portEnd = indexNotOfNumber(url, portStart); if (portEnd > portStart) { final String portString = url.substring(portStart, portEnd); - port = Integer.parseInt(portString); + port = Integer.parseInt(portString); } } return port; } - + public String getServiceUrl() { return serviceUrl; } - + private static int indexNotOfNumber(String str, int index) { int i = 0; for (i = index; i < str.length(); i++) { @@ -95,68 +77,80 @@ public class MBServerConnectorFactory { server = MBeanServerHolder.INSTANCE; } - serviceUrl = String.format(DEFAULT_SERVICE_URL, port); - - // Create the JMX service URL. - JMXServiceURL url = new JMXServiceURL(serviceUrl); - - // if the URL is localhost, start up an Registry - if (serviceUrl.indexOf("localhost") > -1 - && url.getProtocol().compareToIgnoreCase("rmi") == 0) { - try { - int registryPort = getURLLocalHostPort(serviceUrl); + try { + port = settings.getAsInt(CONNECTOR_PORT, DEFAULT_CONNECTOR_PORT); + serviceUrl = String.format(DEFAULT_SERVICE_URL, port); + + // Create the JMX service URL. + JMXServiceURL url = new JMXServiceURL(serviceUrl); + + // if the URL is localhost, start up an Registry + if (serviceUrl.indexOf("localhost") > -1 + && url.getProtocol().compareToIgnoreCase("rmi") == 0) { try { - LocateRegistry.createRegistry(registryPort); - } catch (Exception ex) { - // the registry may had been created - LocateRegistry.getRegistry(registryPort); - } - - } catch (Exception ex) { - LOG.log(Level.SEVERE, "Create RMI registry failure: {0}", ex); - } - } - - HashMap environment = new HashMap(); - // environment.put(JMXConnectorServer.AUTHENTICATOR, new JMXauthenticator( - // mPlatformContext.getSecurityProvider())); - // environment.put(Context.INITIAL_CONTEXT_FACTORY, RegistryContextFactory.class.getName()); - environment.put("com.sun.management.jmxremote.authenticate", Boolean.TRUE.toString()); - - // Create the connector server now. - connectorServer = - JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server); - - if (threaded) { - // Start the connector server asynchronously (in a separate thread). - Thread connectorThread = new Thread() { - @Override - public void run() { + int registryPort = getURLLocalHostPort(serviceUrl); try { - connectorServer.start(); - } catch (IOException ex) { - LOG.log(Level.SEVERE, "Start connector failure: {0}", ex); + LocateRegistry.createRegistry(registryPort); + } catch (Exception ex) { + // the registry may had been created + LocateRegistry.getRegistry(registryPort); } + } catch (Exception ex) { + LOG.log(Level.SEVERE, "Create RMI registry failure: {0}", ex); } - }; + } - connectorThread.setName("JMX Connector Thread [" + connectorServer.getAddress() + "]"); - connectorThread.setDaemon(daemon); - connectorThread.start(); - } else { - // Start the connector server in the same thread. - connectorServer.start(); - } + HashMap environment = new HashMap(); + environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator); + environment.put("com.sun.management.jmxremote.authenticate", Boolean.TRUE.toString()); - if (LOG.isLoggable(Level.INFO)) { - LOG.log(Level.INFO, "JMX connector server started: {0}", connectorServer.getAddress()); + // Create the connector server now. + connectorServer = + JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server); + + if (threaded) { + // Start the connector server asynchronously (in a separate thread). + Thread connectorThread = new Thread() { + @Override + public void run() { + try { + connectorServer.start(); + } catch (IOException ex) { + LOG.log(Level.SEVERE, "Start connector failure: {0}", ex); + } + } + }; + + connectorThread.setName("JMX Connector Thread [" + connectorServer.getAddress() + "]"); + connectorThread.setDaemon(daemon); + connectorThread.start(); + } else { + // Start the connector server in the same thread. + connectorServer.start(); + } + + if (LOG.isLoggable(Level.INFO)) { + LOG.log(Level.INFO, "JMX connector server started: {0}", connectorServer.getAddress()); + } + } catch (NumberFormatException nfEx) { + LOG.log(Level.WARNING, "Invalid connector server port. JMX connector will not be created."); } } public void destroy() throws IOException { - connectorServer.stop(); - if (LOG.isLoggable(Level.INFO)) { - LOG.log(Level.INFO, "JMX connector server stopped: {0}", connectorServer); + if (connectorServer != null) { + connectorServer.stop(); + if (LOG.isLoggable(Level.INFO)) { + LOG.log(Level.INFO, "JMX connector server stopped: {0}", connectorServer); + } } } + + public MBeanServer getMBeanServer() { + return MBeanServerHolder.INSTANCE; + } + + public int getPort() { + return port; + } } diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/security/auth/login/JMXauthenticator.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/auth/login/JMXAuthenticator.java similarity index 72% rename from openesb-standalone-container/src/main/java/net/openesb/standalone/security/auth/login/JMXauthenticator.java rename to openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/auth/login/JMXAuthenticator.java index 40da503..bcad929 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/security/auth/login/JMXauthenticator.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/jmx/auth/login/JMXAuthenticator.java @@ -1,6 +1,6 @@ -package net.openesb.standalone.security.auth.login; +package net.openesb.standalone.jmx.auth.login; -import javax.management.remote.JMXAuthenticator; +import javax.inject.Inject; import javax.security.auth.Subject; import net.openesb.security.AuthenticationException; import net.openesb.security.AuthenticationToken; @@ -11,13 +11,10 @@ import net.openesb.security.SecurityProvider; * @author David BRASSELY (brasseld at gmail.com) * @author OpenESB Community */ -public class JMXauthenticator implements JMXAuthenticator { +public class JMXAuthenticator implements javax.management.remote.JMXAuthenticator { - private final SecurityProvider securityProvider; - - public JMXauthenticator(final SecurityProvider securityProvider) { - this.securityProvider = securityProvider; - } + @Inject + private SecurityProvider securityProvider; @Override public Subject authenticate(Object credentialsObj) { diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/jta/TransactionManagerProvider.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/jta/TransactionManagerProvider.java new file mode 100644 index 0000000..da61836 --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/jta/TransactionManagerProvider.java @@ -0,0 +1,18 @@ +package net.openesb.standalone.jta; + +import com.atomikos.icatch.jta.UserTransactionManager; +import com.google.inject.Provider; +import javax.transaction.TransactionManager; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class TransactionManagerProvider implements Provider { + + @Override + public TransactionManager get() { + return new UserTransactionManager(); + } +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/module/StandaloneModule.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/module/StandaloneModule.java new file mode 100644 index 0000000..46c6af9 --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/module/StandaloneModule.java @@ -0,0 +1,42 @@ +package net.openesb.standalone.module; + +import com.google.inject.AbstractModule; +import com.google.inject.Scopes; +import com.google.inject.name.Names; +import com.sun.jbi.platform.PlatformContext; +import java.util.Properties; +import javax.transaction.TransactionManager; +import net.openesb.security.SecurityProvider; +import net.openesb.standalone.framework.StandaloneContainer; +import net.openesb.standalone.framework.StandalonePlatformContext; +import net.openesb.standalone.jmx.MBServerConnectorFactory; +import net.openesb.standalone.jmx.auth.login.JMXAuthenticator; +import net.openesb.standalone.jta.TransactionManagerProvider; +import net.openesb.standalone.security.SecurityProviderImpl; +import net.openesb.standalone.settings.Settings; +import net.openesb.standalone.settings.yaml.YamlSettingsProvider; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class StandaloneModule extends AbstractModule { + + private static final String DEFAULT_INSTALL_ROOT = System.getProperty("user.dir"); + private static final String INSTALL_ROOT = "install.root"; + + @Override + protected void configure() { + bind(Properties.class).toInstance(StandaloneContainer.systemEnv); + String installRoot = StandaloneContainer.systemEnv.getProperty(INSTALL_ROOT, DEFAULT_INSTALL_ROOT); + bindConstant().annotatedWith(Names.named(INSTALL_ROOT)).to(installRoot); + + bind(PlatformContext.class).to(StandalonePlatformContext.class).asEagerSingleton(); + bind(Settings.class).toProvider(YamlSettingsProvider.class).asEagerSingleton(); + bind(TransactionManager.class).toProvider(TransactionManagerProvider.class).in(Scopes.SINGLETON); + bind(SecurityProvider.class).to(SecurityProviderImpl.class).asEagerSingleton(); + bind(javax.management.remote.JMXAuthenticator.class).to(JMXAuthenticator.class).in(Scopes.SINGLETON); + } + +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/ContextProvider.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/ContextProvider.java new file mode 100644 index 0000000..e398a22 --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/ContextProvider.java @@ -0,0 +1,51 @@ +package net.openesb.standalone.naming; + +import com.google.inject.Provider; +import java.io.File; +import java.net.MalformedURLException; +import java.util.Hashtable; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.inject.Inject; +import javax.inject.Named; +import javax.naming.Context; +import javax.naming.InitialContext; +import net.openesb.standalone.naming.jndi.impl.InitialContexFactoryImpl; +import net.openesb.standalone.settings.yaml.YamlSettingsProvider; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class ContextProvider implements Provider { + + private static final Logger LOG = + Logger.getLogger(ContextProvider.class.getPackage().getName()); + + @Inject @Named("install.root") + private String installRoot; + + @Override + public InitialContext get() { + InitialContext mNamingContext = null; + + try { + Hashtable env = new Hashtable(); + env.put(Context.INITIAL_CONTEXT_FACTORY, + InitialContexFactoryImpl.class.getName()); + + File context = new File(installRoot + "/config/context.xml"); + env.put(Context.PROVIDER_URL, context.toURI().toURL().toString()); + + mNamingContext = new InitialContext(env); + } catch (javax.naming.NamingException nmEx) { + LOG.log(Level.SEVERE, "", nmEx); + } catch (MalformedURLException nmEx) { + LOG.log(Level.SEVERE, "", nmEx); + } + + return mNamingContext; + } + +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/impl/InitialContexFactoryImpl.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/impl/InitialContexFactoryImpl.java index 80ef67d..f6fdcf6 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/impl/InitialContexFactoryImpl.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/impl/InitialContexFactoryImpl.java @@ -25,7 +25,7 @@ import net.openesb.standalone.naming.jaxb.DataSourcePoolPropertiesComplexType; import net.openesb.standalone.naming.jaxb.JdbcResourceComplexType; import net.openesb.standalone.naming.jaxb.OeContextComplexType; import net.openesb.standalone.naming.jndi.DataSourcePoolFactory; -import net.openesb.standalone.naming.utils.I18NBundle; +import net.openesb.standalone.utils.I18NBundle; /** * diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/tomcat/TomcatDataSourcePoolFactory.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/tomcat/TomcatDataSourcePoolFactory.java index 3bc3024..1900ab4 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/tomcat/TomcatDataSourcePoolFactory.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/jndi/tomcat/TomcatDataSourcePoolFactory.java @@ -21,7 +21,7 @@ import net.openesb.standalone.naming.jaxb.DataSourcePropertiesComplexType; import net.openesb.standalone.naming.jaxb.PoolPropertiesComplexType; import net.openesb.standalone.naming.jaxb.PropertyComplexType; import net.openesb.standalone.naming.jndi.DataSourcePoolFactory; -import net.openesb.standalone.naming.utils.I18NBundle; +import net.openesb.standalone.utils.I18NBundle; import org.apache.tomcat.jdbc.pool.PoolProperties; /** diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/test/TestConnection01.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/test/TestConnection01.java deleted file mode 100644 index 1e70de7..0000000 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/test/TestConnection01.java +++ /dev/null @@ -1,79 +0,0 @@ -package net.openesb.standalone.naming.test; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Hashtable; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.naming.Context; -import javax.naming.NamingException; -import net.openesb.standalone.naming.jndi.impl.InitialContexFactoryImpl; -import javax.naming.spi.InitialContextFactory; -import javax.sql.DataSource; - -/** - * - * @author Paul PEREZ (paul.perez at pymma.com) - * @author OpenESB Community - */ -public class TestConnection01 { - - public static void main(String[] args) throws Exception{ - InitialContextFactory factory = new InitialContexFactoryImpl(); - Hashtable environment = new Hashtable(); - environment.put(Context.PROVIDER_URL, "file:///G:/projects/jndi-standalone/jndi-standalone/src/main/resources/net/openesb/standalone/naming/utils/OEContextSample.xml"); - - Context context = null; - try { - context = factory.getInitialContext(environment); - } catch (NamingException ex) { - Logger.getLogger(TestConnection01.class.getName()).log(Level.SEVERE, null, ex); - System.out.println("Get initial context Error"); - System.exit(1); - } - - DataSource dsMySQL = null; - try { - dsMySQL = (DataSource) context.lookup("MySQLServer01"); - } catch (NamingException ex) { - Logger.getLogger(TestConnection01.class.getName()).log(Level.SEVERE, null, ex); - System.out.println("Look up Error"); - System.exit(1); - } - Connection connectionMySQL = null; - try { - connectionMySQL = dsMySQL.getConnection(); - PreparedStatement sta2 = connectionMySQL.prepareStatement("SELECT * FROM test.test"); - ResultSet rs = sta2.executeQuery(); - rs.next(); - String name = rs.getString("NAME"); - System.out.println(name); - } catch (SQLException ex) { - Logger.getLogger(TestConnection01.class.getName()).log(Level.SEVERE, null, ex); - System.out.println("Look up Error"); - System.exit(1); - } - - - // Derby test - DataSource dsDerby = (DataSource)context.lookup("Derby01"); - Connection connectionDerby = dsDerby.getConnection(); - PreparedStatement staDerby = connectionDerby.prepareStatement("select * from APP.CUSTOMER where CUSTOMER_ID=1"); - ResultSet rsDerby = staDerby.executeQuery(); - rsDerby.next(); - String customerID = rsDerby.getString("NAME"); - System.out.println(customerID); - - //PostGresql test - DataSource dsPostgres = (DataSource)context.lookup("Postgres01"); - Connection connectionPG = dsPostgres.getConnection(); - PreparedStatement staPG = connectionPG.prepareStatement("SELECT * FROM public.distributors"); - ResultSet rsPG = staPG.executeQuery(); - rsPG.next(); - String customerName = rsPG.getString(2); - System.out.println(customerName); - - } -} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/Bundle.properties b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/Bundle.properties deleted file mode 100644 index a6df13a..0000000 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/Bundle.properties +++ /dev/null @@ -1,28 +0,0 @@ -context.created=Empty JNDI Context has been created -context.url.not.provided.ID=NAMING-0001 -context.url.not.provided=The URL context context is not provided. -context.url.read=The URL for the context has been retrieved and its value is {0} -url.context.name.malformed.ID=NAMING-0002 -url.context.name.malformed=The URL {0} provided for the context is malformed -jaxb.unmarshalling.failed=UNMarshalling failed with the provided URL {0} -context.binding.ok=context binding is successful and the data have been unmarshaled -number.dataSourcePoolProperties.found={0} DataSourcePool properties have been found in the context -datasourcepoolproperties.found.in.context=DatasourcePoolProperties {0} has been found in the context -number.jdbcResource.declaration.found={0} JDBC Resources have defined in the context -in.process=in process -datasource.created.succesfully=A datasource has been created successfully for the JNDI Name {0} -jndi.value.already.defined=The JNDI value {0} has been declare more than once. Just the first declaration is taken into account -datasource.in.process=Creation Datasource for {0} in process -datasource.processed.bind.success=Datasource for {0} has been processed and bind with success -xadatasource.in.process=Creation XADatasource for {0} in process -xadatasource.processed.bind.success=XADatasource for {0} has been processed and bind with success -bad.resource.type=The resource type {0} defined in the datasourcePoolProperties {1} is not supported -start.instanciate.datasource=Start datasource {0} instanciation -datasource.class.not.found=Datasource class {0} is not found, please check OpenESB classpath -impossible.instanciate.datasource=Impossible to instanciate the datasource {0} -catch.exception=The following exception has been catch -invalid.field.name=Field name "{0}" does not exist in the class {1} please check class and ancestor content and context properties. -field.type.not.process= The field {0} in the class {1} is a not supported type {2}. Just set filed with primitive and String type. -field.not.set=The field {0} has not been set. -native.datasource.set.succesfully=Native datasource {0} has been set successfully -start.pool.configuration= Start pool configuration \ No newline at end of file diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xml b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xml deleted file mode 100644 index a1486a0..0000000 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - - Derby Connector - org.apache.derby.jdbc.ClientDataSource40 - Datasource - DERBY - OpenESB Community - 10.7.1.1 - DBConnector for Derby - - - user - app - - - - password - app - - - - serverName - localhost - - - - portNumber - 1527 - - - - databaseName - sample - - - - - - initialSize - 11 - - - - maxActive - 20 - - - - maxIdle - 10 - - - - minIdle - 10 - - - - - - - Derby Connector - Derby01 - Datasource connection to Derby - - - diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xsd b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xsd deleted file mode 100644 index 3bcd09b..0000000 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContext.xsd +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - Datasource - - - - - XADatasource - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContextSample.xml b/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContextSample.xml deleted file mode 100644 index b2a29c1..0000000 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/OEContextSample.xml +++ /dev/null @@ -1,208 +0,0 @@ - - - - - - - MYSQL Connector - com.mysql.jdbc.jdbc2.optional.MysqlDataSource - Datasource - MYSQL - Oracle - 5.6 - DBConnector for MySQL - - - user - root - - - - password - password - - - - hostName - localhost - - - - port - 3306 - - - - databaseName - test - - - - - - initialSize - 11 - - - - maxActive - 20 - - - - maxIdle - 11 - - - - minIdle - 10 - - - - - - - Derby Connector - org.apache.derby.jdbc.ClientDataSource40 - Datasource - DERBY - OpenESB Community - 10.7.1.1 - DBConnector for Derby - - - user - app - - - - password - app - - - - serverName - localhost - - - - portNumber - 1527 - - - - databaseName - sample - - - - - - initialSize - 11 - - - - maxActive - 20 - - - - maxIdle - 10 - - - - minIdle - 10 - - - - - - - Postgres Connector - org.postgresql.ds.PGSimpleDataSource - Datasource - POSTGRESQL - Postgres - 9.1-90 - DBConnector for postgres - - - user - postgres - - - - password - password - - - - serverName - localhost - - - - portNumber - 5432 - - - - databaseName - postgres - - - - - - initialSize - 11 - - - - maxActive - 20 - - - - maxIdle - 10 - - - - minIdle - 10 - - - - - - - MYSQL Connector - MySQLServer01 - Datasource connection to MySQ - - - - Derby Connector - Derby01 - Datasource connection to Derby - - - - Postgres Connector - Postgres01 - Datasource connection to Postgres - - diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/security/SecurityProviderImpl.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/security/SecurityProviderImpl.java index 1e9dd04..10c6c35 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/security/SecurityProviderImpl.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/security/SecurityProviderImpl.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import javax.inject.Inject; import javax.security.auth.Subject; import net.openesb.security.AuthenticationException; import net.openesb.security.AuthenticationToken; @@ -13,6 +14,7 @@ import net.openesb.security.SecurityProvider; import net.openesb.standalone.security.realm.Realm; import net.openesb.standalone.security.realm.RealmBuilder; import net.openesb.standalone.security.realm.shiro.ShiroAuthenticator; +import net.openesb.standalone.settings.Settings; /** * @@ -21,49 +23,53 @@ import net.openesb.standalone.security.realm.shiro.ShiroAuthenticator; */ public class SecurityProviderImpl implements SecurityProvider { - private final Logger mLog = - Logger.getLogger(this.getClass().getPackage().getName()); - + private static final Logger LOG = + Logger.getLogger(SecurityProviderImpl.class.getPackage().getName()); + + private final static String SETTINGS_KEY = "realm"; private final static String MANAGEMENT_REALM = "admin"; - private final Map realms = new HashMap(); private final ShiroAuthenticator authenticator = new ShiroAuthenticator(); - - public SecurityProviderImpl(Map> realmsConfiguration) { - this.init(realmsConfiguration); + + @Inject + public SecurityProviderImpl(final Settings settings) { + init(settings); } - - private void init(Map> realmsConfiguration) { - if (realmsConfiguration != null) { - mLog.log(Level.INFO, "Loading security realms from configuration."); - - for(Map.Entry> realmConfig : realmsConfiguration.entrySet()) { - if (! realms.containsKey(realmConfig.getKey())) { + + private void init(final Settings settings) { + try { + Map> realmsConfiguration = + (Map>) settings.getAsObject(SETTINGS_KEY); + + LOG.log(Level.INFO, "Loading security realms from configuration."); + + for (Map.Entry> realmConfig : realmsConfiguration.entrySet()) { + if (!realms.containsKey(realmConfig.getKey())) { Realm realm = RealmBuilder. - realmBuilder(). - build(realmConfig.getKey(), realmConfig.getValue()); - + realmBuilder(). + build(realmConfig.getKey(), realmConfig.getValue()); + authenticator.loadRealm(realm); realms.put(realmConfig.getKey(), realm); - + if (realm.getName().equals(MANAGEMENT_REALM)) { - mLog.log(Level.INFO, "Management Realm ({0}) has been correctly configured.", - realmConfig.getKey()); + LOG.log(Level.INFO, "Management Realm ({0}) has been correctly configured.", + realmConfig.getKey()); } else { - mLog.log(Level.INFO, "Realm {0} has been correctly configured.", - realmConfig.getKey()); + LOG.log(Level.INFO, "Realm {0} has been correctly configured.", + realmConfig.getKey()); } } else { - mLog.log(Level.INFO, "Realm {0} is already defined, skipping...", + LOG.log(Level.INFO, "Realm {0} is already defined, skipping...", realmConfig.getKey()); } } - } else { - mLog.log(Level.WARNING, "No realm defined. Please have a look to " - + " the configuration !"); + } catch (NullPointerException npe) { + LOG.log(Level.WARNING, "No realm defined. Please have a look to " + + "the configuration !"); } } - + @Override public Collection getRealms() { return Collections.unmodifiableSet( @@ -74,7 +80,7 @@ public class SecurityProviderImpl implements SecurityProvider { public Subject login(String realmName, AuthenticationToken authenticationToken) throws AuthenticationException { return authenticator.authenticate(realmName, authenticationToken); } - + @Override public Subject login(AuthenticationToken authenticationToken) throws AuthenticationException { return login(MANAGEMENT_REALM, authenticationToken); diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/ImmutableSettings.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/ImmutableSettings.java index 4ab7bc7..ea46786 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/ImmutableSettings.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/ImmutableSettings.java @@ -69,4 +69,9 @@ public class ImmutableSettings implements Settings { } return !(value.equals("false") || value.equals("0") || value.equals("off") || value.equals("no")); } + + @Override + public Object getAsObject(String setting) throws SettingsException { + return settings.get(setting); + } } \ No newline at end of file diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/Settings.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/Settings.java index 259727b..ed7cae6 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/Settings.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/Settings.java @@ -32,4 +32,6 @@ public interface Settings { * returns the default value provided. */ Boolean getAsBoolean(String setting, Boolean defaultValue) throws SettingsException; + + Object getAsObject(String setting) throws SettingsException; } diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/yaml/YamlSettingsProvider.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/yaml/YamlSettingsProvider.java new file mode 100644 index 0000000..dd8bfdc --- /dev/null +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/settings/yaml/YamlSettingsProvider.java @@ -0,0 +1,71 @@ +package net.openesb.standalone.settings.yaml; + +import com.google.inject.Provider; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.inject.Inject; +import javax.inject.Named; +import net.openesb.standalone.settings.ImmutableSettings; +import net.openesb.standalone.settings.Settings; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.representer.Representer; +import org.yaml.snakeyaml.resolver.Resolver; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class YamlSettingsProvider implements Provider { + + private static final Logger mLog = + Logger.getLogger(YamlSettingsProvider.class.getPackage().getName()); + + private static final String CONFIG_FILE = "openesb.config"; + + @Inject @Named("install.root") + private String mInstallRoot; + + @Inject + private Properties environment; + + @Override + public Settings get() { + String configFile = environment.getProperty(CONFIG_FILE); + + if (configFile == null) { + configFile = mInstallRoot + File.separatorChar + "config/openesb.yaml"; + } + + mLog.log(Level.FINE, "Trying to load configuration from {0}", configFile); + + Settings settings; + + try { + Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(), + new Resolver() { + @Override + protected void addImplicitResolvers() { + } + }); + InputStream input = new FileInputStream(new File(configFile)); + Map configurations = (Map) yaml.load(input); + + settings = new ImmutableSettings(configurations); + mLog.log(Level.INFO, "Configuration loaded from {0}", configFile); + } catch (FileNotFoundException fnfe) { + mLog.log(Level.WARNING, "Unable to load configuration file {0}. Default configuration will be used.", configFile); + settings = new ImmutableSettings(null); + } + + return settings; + } +} diff --git a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/I18NBundle.java b/openesb-standalone-container/src/main/java/net/openesb/standalone/utils/I18NBundle.java similarity index 88% rename from openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/I18NBundle.java rename to openesb-standalone-container/src/main/java/net/openesb/standalone/utils/I18NBundle.java index 4c5ffdd..1b5acf7 100644 --- a/openesb-standalone-container/src/main/java/net/openesb/standalone/naming/utils/I18NBundle.java +++ b/openesb-standalone-container/src/main/java/net/openesb/standalone/utils/I18NBundle.java @@ -1,4 +1,4 @@ -package net.openesb.standalone.naming.utils; +package net.openesb.standalone.utils; import java.text.MessageFormat; import java.util.Locale; @@ -17,7 +17,7 @@ import java.util.logging.Logger; */ public class I18NBundle { - private final Logger sLogger = Logger.getLogger("net.openesb.standalone.naming"); + private final Logger sLogger = Logger.getLogger("net.openesb.standalone.utils"); /** * package name */ @@ -27,8 +27,6 @@ public class I18NBundle { */ private ResourceBundle mBundle = null; - - /** * constructor * @@ -166,19 +164,4 @@ public class I18NBundle { Object[] args = {arg1, arg2, arg3}; return getMessage(aI18NKey, args); } - - /** - * main - * - * @param args string array. - */ - public static void main(String[] args) { - Locale l = new Locale(""); - System.out.println("Locale : " + l); - System.out.println("Default Locale : " + Locale.getDefault()); - I18NBundle bundle = new I18NBundle("net.openesb.standalone.naming.utils"); - bundle.loadBundle("net.openesb.standalone.naming.utils","toto"); - System.out.println( - bundle.getMessage("jbi.ui.ant.jmx.msg.jmxmp.connected", new String[]{"xyz"})); - } } \ No newline at end of file diff --git a/openesb-standalone-packaging/pom.xml b/openesb-standalone-packaging/pom.xml index 34c1c42..f5ee012 100644 --- a/openesb-standalone-packaging/pom.xml +++ b/openesb-standalone-packaging/pom.xml @@ -115,6 +115,18 @@ ${slf4j.version} + + + com.google.inject + guice + ${guice.version} + + + aopalliance + aopalliance + 1.0 + + net.open-esb diff --git a/openesb-standalone-packaging/src/main/assembly/distribution.xml b/openesb-standalone-packaging/src/main/assembly/distribution.xml index 60cd9df..aae0324 100644 --- a/openesb-standalone-packaging/src/main/assembly/distribution.xml +++ b/openesb-standalone-packaging/src/main/assembly/distribution.xml @@ -99,6 +99,8 @@ org.apache.shiro:shiro-core org.slf4j:slf4j-api org.slf4j:slf4j-jdk14 + com.google.inject:guice + aopalliance:aopalliance lib/ext false diff --git a/pom.xml b/pom.xml index e19167d..6162a5a 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ 1.2.2 4.11 1.7.6 + 3.0