Internal refactoring using Guice injection

master
David BRASSELY 2014-03-04 15:09:54 +01:00
parent 32726e5802
commit a4f79a0f0c
26 changed files with 449 additions and 746 deletions

View File

@ -91,6 +91,13 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<!-- Injection support -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -1,27 +1,15 @@
package net.openesb.standalone.framework; package net.openesb.standalone.framework;
import java.io.File; import com.sun.jbi.platform.PlatformContext;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.ObjectName; import javax.management.ObjectName;
import net.openesb.standalone.inject.Injector;
import net.openesb.standalone.jmx.MBServerConnectorFactory; 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 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. * JBI framework wrapper for OpenESB Standalone platform.
@ -40,24 +28,12 @@ public class StandaloneContainer
extends com.sun.jbi.framework.JBIFramework extends com.sun.jbi.framework.JBIFramework
implements StandaloneContainerMBean { 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 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()); Logger.getLogger(StandaloneContainer.class.getPackage().getName());
// private Node instanceNode;
private Settings settings;
/** /**
* Creates a new instance of the JBI framework. * Creates a new instance of the JBI framework.
@ -67,46 +43,6 @@ public class StandaloneContainer
systemEnv = environment; 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<String,Map<String,String>>) configurations.get("realm")));
}
/** /**
* Load the JBI framework with the specified environment. When this method * Load the JBI framework with the specified environment. When this method
* retuns, all public interfaces and system services have completely * retuns, all public interfaces and system services have completely
@ -119,10 +55,10 @@ public class StandaloneContainer
public synchronized void load() public synchronized void load()
throws Exception { throws Exception {
if (mLoaded) { 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 // Register a management MBean for this framework instance
ObjectName fwMBeanName = new ObjectName("net.open-esb.standalone", ObjectName fwMBeanName = new ObjectName("net.open-esb.standalone",
@ -140,19 +76,9 @@ public class StandaloneContainer
} }
mbs.registerMBean(this, fwMBeanName); mbs.registerMBean(this, fwMBeanName);
// Setup the remote JMX connector server MBServerConnectorFactory connectorFactory =
Integer port = null; Injector.getInstance().getInjector().getInstance(MBServerConnectorFactory.class);
connectorFactory.createConnector();
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);
}
// For stand-alone JBI, JBI_HOME = platform install root // For stand-alone JBI, JBI_HOME = platform install root
systemEnv.setProperty("com.sun.jbi.home", systemEnv.setProperty("com.sun.jbi.home",
@ -160,6 +86,8 @@ public class StandaloneContainer
// -------------------------------------------- // --------------------------------------------
// TODO: removing this part asap // TODO: removing this part asap
Settings settings = Injector.getInstance().getInjector().getInstance(Settings.class);
System.setProperty("http.port", System.setProperty("http.port",
settings.get("http.port", "4848")); settings.get("http.port", "4848"));
System.setProperty("http.enabled", System.setProperty("http.enabled",
@ -171,16 +99,14 @@ public class StandaloneContainer
prepare(); prepare();
ready(true); ready(true);
// instanceNode = NodeBuilder.nodeBuilder(settings).build();
// instanceNode.start();
// JBI framework has been loaded // JBI framework has been loaded
mLoaded = true; mLoaded = true;
} }
public String getServiceUrl() { 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; return;
} }
// instanceNode.stop();
shutdown(); shutdown();
terminate(); terminate();
try { try {
MBServerConnectorFactory.getInstance().destroy(); MBServerConnectorFactory connectorFactory =
Injector.getInstance().getInjector().getInstance(MBServerConnectorFactory.class);
connectorFactory.destroy();
} catch (Exception ex) { } 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; mLoaded = false;

View File

@ -1,7 +1,7 @@
package net.openesb.standalone.framework; 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 David BRASSELY (brasseld at gmail.com)
* @author OpenESB Community * @author OpenESB Community

View File

@ -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();
}
}

View File

@ -5,19 +5,20 @@ import com.sun.jbi.platform.PlatformEventListener;
import com.sun.jbi.security.KeyStoreUtil; import com.sun.jbi.security.KeyStoreUtil;
import java.io.File; import java.io.File;
import java.net.MalformedURLException;
import java.util.Set; import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
import java.util.Hashtable;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.logging.Level; import java.util.logging.Level;
import javax.inject.Inject;
import javax.inject.Named;
import javax.management.MBeanServerConnection; import javax.management.MBeanServerConnection;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.transaction.TransactionManager;
import net.openesb.security.SecurityProvider; 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. * 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 { public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformContext {
private String mInstanceName; private static final String DEFAULT_INSTANCE_NAME = "server";
private String mInstanceRoot; private static final String INSTANCE_NAME = "instance.name";
private String mInstallRoot;
private int mConnectorPort; private final String mInstanceName;
private InitialContext mNamingContext;
private Logger mLog = Logger.getLogger(getClass().getPackage().getName());
public StandalonePlatformContext(String installRoot, String instanceName, int connectorPort) { @Inject private MBServerConnectorFactory jmxConnector;
mInstallRoot = installRoot; @Inject private SecurityProvider securityProvider;
mInstanceName = instanceName; @Inject private TransactionManager transactionManager;
mInstanceRoot = installRoot + File.separator + instanceName; @Inject private InitialContext namingContext;
mConnectorPort = connectorPort; @Inject @Named("install.root") private String mInstallRoot;
try { @Inject
Hashtable env = new Hashtable(); public StandalonePlatformContext(Settings settings) {
env.put(Context.INITIAL_CONTEXT_FACTORY, mInstanceName = settings.get(INSTANCE_NAME, DEFAULT_INSTANCE_NAME);
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());
}
} }
/** /**
@ -69,7 +57,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC
@Override @Override
public javax.transaction.TransactionManager getTransactionManager() public javax.transaction.TransactionManager getTransactionManager()
throws Exception { 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 @Override
public String getJmxRmiPort() { 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 @Override
public MBeanServer getMBeanServer() { 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 @Override
public String getInstanceRoot() { public String getInstanceRoot() {
return mInstanceRoot; return mInstallRoot + File.separator + mInstanceName;
} }
/** /**
@ -328,7 +316,7 @@ public class StandalonePlatformContext implements com.sun.jbi.platform.PlatformC
*/ */
@Override @Override
public InitialContext getNamingContext() { 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) { public void setJbiLogLevel(String target, java.util.logging.Level level) {
Logger.getLogger(JBI_LOGGER_NAME).setLevel(level); Logger.getLogger(JBI_LOGGER_NAME).setLevel(level);
} }
private SecurityProvider securityProvider;
public void setSecurityProvider(SecurityProvider securityProvider) {
this.securityProvider = securityProvider;
}
@Override @Override
public SecurityProvider getSecurityProvider() { public SecurityProvider getSecurityProvider() {

View File

@ -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;
}
}

View File

@ -5,11 +5,13 @@ import java.rmi.registry.LocateRegistry;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.inject.Inject;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MBeanServerFactory; import javax.management.remote.JMXAuthenticator;
import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL; 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 String DEFAULT_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:%s/jmxrmi";
private static final Logger LOG = Logger.getLogger(MBServerConnectorFactory.class.getName()); private static final Logger LOG = Logger.getLogger(MBServerConnectorFactory.class.getName());
private static MBeanServer server; private static final int DEFAULT_CONNECTOR_PORT = 8699;
private static boolean threaded; private static final String CONNECTOR_PORT = "instance.port";
private static boolean daemon;
private static int port; @Inject
private static String serviceUrl; private Settings settings;
private static JMXConnectorServer connectorServer;
@Inject
private static class MBServerConnectorFactoryHolder { private JMXAuthenticator authenticator;
private static final MBServerConnectorFactory INSTANCE = private MBeanServer server;
new MBServerConnectorFactory(); private boolean threaded;
} private boolean daemon;
private int port;
private String serviceUrl;
private JMXConnectorServer connectorServer;
private static class MBeanServerHolder { private static class MBeanServerHolder {
private static final MBeanServer INSTANCE = private static final MBeanServer INSTANCE =
MBeanServerFactory.createMBeanServer(); java.lang.management.ManagementFactory.getPlatformMBeanServer();
} }
private MBServerConnectorFactory() { private int getURLLocalHostPort(String url) {
}
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) {
int portStart = url.indexOf("localhost") + 10; int portStart = url.indexOf("localhost") + 10;
int portEnd; int portEnd;
int port = 0; int port = 0;
@ -70,16 +52,16 @@ public class MBServerConnectorFactory {
portEnd = indexNotOfNumber(url, portStart); portEnd = indexNotOfNumber(url, portStart);
if (portEnd > portStart) { if (portEnd > portStart) {
final String portString = url.substring(portStart, portEnd); final String portString = url.substring(portStart, portEnd);
port = Integer.parseInt(portString); port = Integer.parseInt(portString);
} }
} }
return port; return port;
} }
public String getServiceUrl() { public String getServiceUrl() {
return serviceUrl; return serviceUrl;
} }
private static int indexNotOfNumber(String str, int index) { private static int indexNotOfNumber(String str, int index) {
int i = 0; int i = 0;
for (i = index; i < str.length(); i++) { for (i = index; i < str.length(); i++) {
@ -95,68 +77,80 @@ public class MBServerConnectorFactory {
server = MBeanServerHolder.INSTANCE; server = MBeanServerHolder.INSTANCE;
} }
serviceUrl = String.format(DEFAULT_SERVICE_URL, port); try {
port = settings.getAsInt(CONNECTOR_PORT, DEFAULT_CONNECTOR_PORT);
// Create the JMX service URL. serviceUrl = String.format(DEFAULT_SERVICE_URL, port);
JMXServiceURL url = new JMXServiceURL(serviceUrl);
// Create the JMX service URL.
// if the URL is localhost, start up an Registry JMXServiceURL url = new JMXServiceURL(serviceUrl);
if (serviceUrl.indexOf("localhost") > -1
&& url.getProtocol().compareToIgnoreCase("rmi") == 0) { // if the URL is localhost, start up an Registry
try { if (serviceUrl.indexOf("localhost") > -1
int registryPort = getURLLocalHostPort(serviceUrl); && url.getProtocol().compareToIgnoreCase("rmi") == 0) {
try { try {
LocateRegistry.createRegistry(registryPort); int registryPort = getURLLocalHostPort(serviceUrl);
} 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<String, Object> environment = new HashMap<String, Object>();
// 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() {
try { try {
connectorServer.start(); LocateRegistry.createRegistry(registryPort);
} catch (IOException ex) { } catch (Exception ex) {
LOG.log(Level.SEVERE, "Start connector failure: {0}", 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() + "]"); HashMap<String, Object> environment = new HashMap<String, Object>();
connectorThread.setDaemon(daemon); environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
connectorThread.start(); environment.put("com.sun.management.jmxremote.authenticate", Boolean.TRUE.toString());
} else {
// Start the connector server in the same thread.
connectorServer.start();
}
if (LOG.isLoggable(Level.INFO)) { // Create the connector server now.
LOG.log(Level.INFO, "JMX connector server started: {0}", connectorServer.getAddress()); 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 { public void destroy() throws IOException {
connectorServer.stop(); if (connectorServer != null) {
if (LOG.isLoggable(Level.INFO)) { connectorServer.stop();
LOG.log(Level.INFO, "JMX connector server stopped: {0}", connectorServer); 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;
}
} }

View File

@ -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 javax.security.auth.Subject;
import net.openesb.security.AuthenticationException; import net.openesb.security.AuthenticationException;
import net.openesb.security.AuthenticationToken; import net.openesb.security.AuthenticationToken;
@ -11,13 +11,10 @@ import net.openesb.security.SecurityProvider;
* @author David BRASSELY (brasseld at gmail.com) * @author David BRASSELY (brasseld at gmail.com)
* @author OpenESB Community * @author OpenESB Community
*/ */
public class JMXauthenticator implements JMXAuthenticator { public class JMXAuthenticator implements javax.management.remote.JMXAuthenticator {
private final SecurityProvider securityProvider; @Inject
private SecurityProvider securityProvider;
public JMXauthenticator(final SecurityProvider securityProvider) {
this.securityProvider = securityProvider;
}
@Override @Override
public Subject authenticate(Object credentialsObj) { public Subject authenticate(Object credentialsObj) {

View File

@ -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<TransactionManager> {
@Override
public TransactionManager get() {
return new UserTransactionManager();
}
}

View File

@ -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);
}
}

View File

@ -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<InitialContext> {
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;
}
}

View File

@ -25,7 +25,7 @@ import net.openesb.standalone.naming.jaxb.DataSourcePoolPropertiesComplexType;
import net.openesb.standalone.naming.jaxb.JdbcResourceComplexType; import net.openesb.standalone.naming.jaxb.JdbcResourceComplexType;
import net.openesb.standalone.naming.jaxb.OeContextComplexType; import net.openesb.standalone.naming.jaxb.OeContextComplexType;
import net.openesb.standalone.naming.jndi.DataSourcePoolFactory; import net.openesb.standalone.naming.jndi.DataSourcePoolFactory;
import net.openesb.standalone.naming.utils.I18NBundle; import net.openesb.standalone.utils.I18NBundle;
/** /**
* *

View File

@ -21,7 +21,7 @@ import net.openesb.standalone.naming.jaxb.DataSourcePropertiesComplexType;
import net.openesb.standalone.naming.jaxb.PoolPropertiesComplexType; import net.openesb.standalone.naming.jaxb.PoolPropertiesComplexType;
import net.openesb.standalone.naming.jaxb.PropertyComplexType; import net.openesb.standalone.naming.jaxb.PropertyComplexType;
import net.openesb.standalone.naming.jndi.DataSourcePoolFactory; 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; import org.apache.tomcat.jdbc.pool.PoolProperties;
/** /**

View File

@ -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);
}
}

View File

@ -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

View File

@ -1,79 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : OEContext.xml
Created on : 24 January 2014, 10:22
Author : polperez
Description:
Purpose of the document follows.
-->
<ns0:context xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ns0='http://org.open-esb/standalone/OEContext'
xsi:schemaLocation='http://org.open-esb/standalone/OEContext OEContext.xsd'>
<ns0:dataSource-pool-properties>
<ns0:dbConnector-name>Derby Connector</ns0:dbConnector-name>
<ns0:datasource-classname>org.apache.derby.jdbc.ClientDataSource40</ns0:datasource-classname>
<ns0:resource-type>Datasource</ns0:resource-type>
<ns0:database-name>DERBY</ns0:database-name>
<ns0:database-vendor>OpenESB Community</ns0:database-vendor>
<ns0:database-version>10.7.1.1</ns0:database-version>
<ns0:dbconnector-description>DBConnector for Derby</ns0:dbconnector-description>
<ns0:dataSource-properties>
<ns0:property>
<ns0:name>user</ns0:name>
<ns0:value>app</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>password</ns0:name>
<ns0:value>app</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>serverName</ns0:name>
<ns0:value>localhost</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>portNumber</ns0:name>
<ns0:value>1527</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>databaseName</ns0:name>
<ns0:value>sample</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:dataSource-properties>
<ns0:pool-properties>
<ns0:property>
<ns0:name>initialSize</ns0:name>
<ns0:value>11</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxActive</ns0:name>
<ns0:value>20</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>minIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:pool-properties>
</ns0:dataSource-pool-properties>
<ns0:jdbc-resources>
<ns0:dbConnector-name>Derby Connector</ns0:dbConnector-name>
<ns0:jndi-name>Derby01</ns0:jndi-name>
<ns0:description>Datasource connection to Derby</ns0:description>
</ns0:jdbc-resources>
</ns0:context>

View File

@ -1,66 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://org.open-esb/standalone/OEContext"
xmlns:tns="http://org.open-esb/standalone/OEContext"
elementFormDefault="qualified">
<xsd:complexType name="pool-propertiesComplexType">
<xsd:sequence>
<xsd:element name="property" type="tns:propertyComplexType" minOccurs="0" maxOccurs="unbounded"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="propertyComplexType">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="name" type="xsd:string"></xsd:element>
<xsd:element name="value" type="xsd:string"></xsd:element>
<xsd:element name="description" type="xsd:string" minOccurs="0"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="dataSource-pool-propertiesComplexType">
<xsd:sequence>
<xsd:element name="dbConnector-name" type="xsd:string"></xsd:element>
<xsd:element name="datasource-classname" type="xsd:string"></xsd:element>
<xsd:element name="resource-type">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Datasource">
<xsd:annotation>
<xsd:documentation>Datasource</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="XADatasource">
<xsd:annotation>
<xsd:documentation>XADatasource</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="database-name" type="xsd:string"></xsd:element>
<xsd:element name="database-vendor" type="xsd:string"></xsd:element>
<xsd:element name="database-version" type="xsd:string"></xsd:element>
<xsd:element name="dbconnector-description" type="xsd:string" minOccurs="0"></xsd:element>
<xsd:element name="dataSource-properties" type="tns:dataSource-propertiesComplexType" minOccurs="1" maxOccurs="1"></xsd:element>
<xsd:element name="pool-properties" type="tns:pool-propertiesComplexType"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="dataSource-propertiesComplexType">
<xsd:sequence>
<xsd:element name="property" type="tns:propertyComplexType" maxOccurs="unbounded" minOccurs="0"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="oeContextComplexType">
<xsd:sequence>
<xsd:element name="dataSource-pool-properties" type="tns:dataSource-pool-propertiesComplexType" minOccurs="0" maxOccurs="unbounded"></xsd:element>
<xsd:element name="jdbc-resources" type="tns:jdbc-resourceComplexType" maxOccurs="unbounded" minOccurs="0"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="context" type="tns:oeContextComplexType"></xsd:element>
<xsd:complexType name="jdbc-resourceComplexType">
<xsd:sequence>
<xsd:element name="dbConnector-name" type="xsd:string"></xsd:element>
<xsd:element name="jndi-name" type="xsd:string"></xsd:element>
<xsd:element name="description" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

View File

@ -1,208 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : OEContext.xml
Created on : 25 December 2013, 10:05
Author : polperez
Description:
Purpose of the document follows.
-->
<ns0:context xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ns0='http://org.open-esb/standalone/OEContext'
xsi:schemaLocation='http://org.open-esb/standalone/OEContext OEContext.xsd'>
<ns0:dataSource-pool-properties>
<ns0:dbConnector-name>MYSQL Connector</ns0:dbConnector-name>
<ns0:datasource-classname>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</ns0:datasource-classname>
<ns0:resource-type>Datasource</ns0:resource-type>
<ns0:database-name>MYSQL</ns0:database-name>
<ns0:database-vendor>Oracle</ns0:database-vendor>
<ns0:database-version>5.6</ns0:database-version>
<ns0:dbconnector-description>DBConnector for MySQL</ns0:dbconnector-description>
<ns0:dataSource-properties>
<ns0:property>
<ns0:name>user</ns0:name>
<ns0:value>root</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>password</ns0:name>
<ns0:value>password</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>hostName</ns0:name>
<ns0:value>localhost</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>port</ns0:name>
<ns0:value>3306</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>databaseName</ns0:name>
<ns0:value>test</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:dataSource-properties>
<ns0:pool-properties>
<ns0:property>
<ns0:name>initialSize</ns0:name>
<ns0:value>11</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxActive</ns0:name>
<ns0:value>20</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxIdle</ns0:name>
<ns0:value>11</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>minIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:pool-properties>
</ns0:dataSource-pool-properties>
<ns0:dataSource-pool-properties>
<ns0:dbConnector-name>Derby Connector</ns0:dbConnector-name>
<ns0:datasource-classname>org.apache.derby.jdbc.ClientDataSource40</ns0:datasource-classname>
<ns0:resource-type>Datasource</ns0:resource-type>
<ns0:database-name>DERBY</ns0:database-name>
<ns0:database-vendor>OpenESB Community</ns0:database-vendor>
<ns0:database-version>10.7.1.1</ns0:database-version>
<ns0:dbconnector-description>DBConnector for Derby</ns0:dbconnector-description>
<ns0:dataSource-properties>
<ns0:property>
<ns0:name>user</ns0:name>
<ns0:value>app</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>password</ns0:name>
<ns0:value>app</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>serverName</ns0:name>
<ns0:value>localhost</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>portNumber</ns0:name>
<ns0:value>1527</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>databaseName</ns0:name>
<ns0:value>sample</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:dataSource-properties>
<ns0:pool-properties>
<ns0:property>
<ns0:name>initialSize</ns0:name>
<ns0:value>11</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxActive</ns0:name>
<ns0:value>20</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>minIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:pool-properties>
</ns0:dataSource-pool-properties>
<ns0:dataSource-pool-properties>
<ns0:dbConnector-name>Postgres Connector</ns0:dbConnector-name>
<ns0:datasource-classname>org.postgresql.ds.PGSimpleDataSource</ns0:datasource-classname>
<ns0:resource-type>Datasource</ns0:resource-type>
<ns0:database-name>POSTGRESQL</ns0:database-name>
<ns0:database-vendor>Postgres</ns0:database-vendor>
<ns0:database-version>9.1-90</ns0:database-version>
<ns0:dbconnector-description>DBConnector for postgres</ns0:dbconnector-description>
<ns0:dataSource-properties>
<ns0:property>
<ns0:name>user</ns0:name>
<ns0:value>postgres</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>password</ns0:name>
<ns0:value>password</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>serverName</ns0:name>
<ns0:value>localhost</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>portNumber</ns0:name>
<ns0:value>5432</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>databaseName</ns0:name>
<ns0:value>postgres</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:dataSource-properties>
<ns0:pool-properties>
<ns0:property>
<ns0:name>initialSize</ns0:name>
<ns0:value>11</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxActive</ns0:name>
<ns0:value>20</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>maxIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
<ns0:property>
<ns0:name>minIdle</ns0:name>
<ns0:value>10</ns0:value>
<ns0:description></ns0:description>
</ns0:property>
</ns0:pool-properties>
</ns0:dataSource-pool-properties>
<ns0:jdbc-resources>
<ns0:dbConnector-name>MYSQL Connector</ns0:dbConnector-name>
<ns0:jndi-name>MySQLServer01</ns0:jndi-name>
<ns0:description>Datasource connection to MySQ</ns0:description>
</ns0:jdbc-resources>
<ns0:jdbc-resources>
<ns0:dbConnector-name>Derby Connector</ns0:dbConnector-name>
<ns0:jndi-name>Derby01</ns0:jndi-name>
<ns0:description>Datasource connection to Derby</ns0:description>
</ns0:jdbc-resources>
<ns0:jdbc-resources>
<ns0:dbConnector-name>Postgres Connector</ns0:dbConnector-name>
<ns0:jndi-name>Postgres01</ns0:jndi-name>
<ns0:description>Datasource connection to Postgres</ns0:description>
</ns0:jdbc-resources>
</ns0:context>

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.inject.Inject;
import javax.security.auth.Subject; import javax.security.auth.Subject;
import net.openesb.security.AuthenticationException; import net.openesb.security.AuthenticationException;
import net.openesb.security.AuthenticationToken; 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.Realm;
import net.openesb.standalone.security.realm.RealmBuilder; import net.openesb.standalone.security.realm.RealmBuilder;
import net.openesb.standalone.security.realm.shiro.ShiroAuthenticator; 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 { public class SecurityProviderImpl implements SecurityProvider {
private final Logger mLog = private static final Logger LOG =
Logger.getLogger(this.getClass().getPackage().getName()); Logger.getLogger(SecurityProviderImpl.class.getPackage().getName());
private final static String SETTINGS_KEY = "realm";
private final static String MANAGEMENT_REALM = "admin"; private final static String MANAGEMENT_REALM = "admin";
private final Map<String, Realm> realms = new HashMap<String, Realm>(); private final Map<String, Realm> realms = new HashMap<String, Realm>();
private final ShiroAuthenticator authenticator = new ShiroAuthenticator(); private final ShiroAuthenticator authenticator = new ShiroAuthenticator();
public SecurityProviderImpl(Map<String, Map<String, String>> realmsConfiguration) { @Inject
this.init(realmsConfiguration); public SecurityProviderImpl(final Settings settings) {
init(settings);
} }
private void init(Map<String, Map<String, String>> realmsConfiguration) { private void init(final Settings settings) {
if (realmsConfiguration != null) { try {
mLog.log(Level.INFO, "Loading security realms from configuration."); Map<String, Map<String, String>> realmsConfiguration =
(Map<String, Map<String, String>>) settings.getAsObject(SETTINGS_KEY);
for(Map.Entry<String, Map<String, String>> realmConfig : realmsConfiguration.entrySet()) {
if (! realms.containsKey(realmConfig.getKey())) { LOG.log(Level.INFO, "Loading security realms from configuration.");
for (Map.Entry<String, Map<String, String>> realmConfig : realmsConfiguration.entrySet()) {
if (!realms.containsKey(realmConfig.getKey())) {
Realm realm = RealmBuilder. Realm realm = RealmBuilder.
realmBuilder(). realmBuilder().
build(realmConfig.getKey(), realmConfig.getValue()); build(realmConfig.getKey(), realmConfig.getValue());
authenticator.loadRealm(realm); authenticator.loadRealm(realm);
realms.put(realmConfig.getKey(), realm); realms.put(realmConfig.getKey(), realm);
if (realm.getName().equals(MANAGEMENT_REALM)) { if (realm.getName().equals(MANAGEMENT_REALM)) {
mLog.log(Level.INFO, "Management Realm ({0}) has been correctly configured.", LOG.log(Level.INFO, "Management Realm ({0}) has been correctly configured.",
realmConfig.getKey()); realmConfig.getKey());
} else { } else {
mLog.log(Level.INFO, "Realm {0} has been correctly configured.", LOG.log(Level.INFO, "Realm {0} has been correctly configured.",
realmConfig.getKey()); realmConfig.getKey());
} }
} else { } else {
mLog.log(Level.INFO, "Realm {0} is already defined, skipping...", LOG.log(Level.INFO, "Realm {0} is already defined, skipping...",
realmConfig.getKey()); realmConfig.getKey());
} }
} }
} else { } catch (NullPointerException npe) {
mLog.log(Level.WARNING, "No realm defined. Please have a look to " LOG.log(Level.WARNING, "No realm defined. Please have a look to "
+ " the configuration !"); + "the configuration !");
} }
} }
@Override @Override
public Collection<String> getRealms() { public Collection<String> getRealms() {
return Collections.unmodifiableSet( return Collections.unmodifiableSet(
@ -74,7 +80,7 @@ public class SecurityProviderImpl implements SecurityProvider {
public Subject login(String realmName, AuthenticationToken authenticationToken) throws AuthenticationException { public Subject login(String realmName, AuthenticationToken authenticationToken) throws AuthenticationException {
return authenticator.authenticate(realmName, authenticationToken); return authenticator.authenticate(realmName, authenticationToken);
} }
@Override @Override
public Subject login(AuthenticationToken authenticationToken) throws AuthenticationException { public Subject login(AuthenticationToken authenticationToken) throws AuthenticationException {
return login(MANAGEMENT_REALM, authenticationToken); return login(MANAGEMENT_REALM, authenticationToken);

View File

@ -69,4 +69,9 @@ public class ImmutableSettings implements Settings {
} }
return !(value.equals("false") || value.equals("0") || value.equals("off") || value.equals("no")); 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);
}
} }

View File

@ -32,4 +32,6 @@ public interface Settings {
* returns the default value provided. * returns the default value provided.
*/ */
Boolean getAsBoolean(String setting, Boolean defaultValue) throws SettingsException; Boolean getAsBoolean(String setting, Boolean defaultValue) throws SettingsException;
Object getAsObject(String setting) throws SettingsException;
} }

View File

@ -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<Settings> {
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;
}
}

View File

@ -1,4 +1,4 @@
package net.openesb.standalone.naming.utils; package net.openesb.standalone.utils;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Locale; import java.util.Locale;
@ -17,7 +17,7 @@ import java.util.logging.Logger;
*/ */
public class I18NBundle { 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 * package name
*/ */
@ -27,8 +27,6 @@ public class I18NBundle {
*/ */
private ResourceBundle mBundle = null; private ResourceBundle mBundle = null;
/** /**
* constructor * constructor
* *
@ -166,19 +164,4 @@ public class I18NBundle {
Object[] args = {arg1, arg2, arg3}; Object[] args = {arg1, arg2, arg3};
return getMessage(aI18NKey, args); 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"}));
}
} }

View File

@ -115,6 +115,18 @@
<version>${slf4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<!-- Injection support -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- OpenESB REST API & Web Console --> <!-- OpenESB REST API & Web Console -->
<dependency> <dependency>
<groupId>net.open-esb</groupId> <groupId>net.open-esb</groupId>

View File

@ -99,6 +99,8 @@
<include>org.apache.shiro:shiro-core</include> <include>org.apache.shiro:shiro-core</include>
<include>org.slf4j:slf4j-api</include> <include>org.slf4j:slf4j-api</include>
<include>org.slf4j:slf4j-jdk14</include> <include>org.slf4j:slf4j-jdk14</include>
<include>com.google.inject:guice</include>
<include>aopalliance:aopalliance</include>
</includes> </includes>
<outputDirectory>lib/ext</outputDirectory> <outputDirectory>lib/ext</outputDirectory>
<useProjectArtifact>false</useProjectArtifact> <useProjectArtifact>false</useProjectArtifact>

View File

@ -37,6 +37,7 @@
<shiro.version>1.2.2</shiro.version> <shiro.version>1.2.2</shiro.version>
<junit.version>4.11</junit.version> <junit.version>4.11</junit.version>
<slf4j.version>1.7.6</slf4j.version> <slf4j.version>1.7.6</slf4j.version>
<guice.version>3.0</guice.version>
</properties> </properties>
<build> <build>