use of javax.naming.InitialContext in project nhin-d by DirectProject.
the class LDAPResearchTest method createContext.
private DirContext createContext(String partition) throws Exception {
int port = configuration.getLdapPort();
String url = "ldap://localhost:" + port + "/" + partition;
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
InitialContext initialContext = new InitialContext(env);
assertNotNull(initialContext);
return (DirContext) initialContext.lookup("");
}
use of javax.naming.InitialContext in project jdk8u_jdk by JetBrains.
the class HelloClient method executeRmiClientCall.
public static void executeRmiClientCall() throws Exception {
Context ic;
Object objref;
HelloInterface helloSvc;
String response;
int retryCount = 0;
Test test = new Test();
System.out.println("HelloClient.main: enter ...");
while (retryCount < MAX_RETRY) {
try {
ic = new InitialContext();
System.out.println("HelloClient.main: HelloService lookup ...");
// STEP 1: Get the Object reference from the Name Service
// using JNDI call.
objref = ic.lookup("HelloService");
System.out.println("HelloClient: Obtained a ref. to Hello server.");
// STEP 2: Narrow the object reference to the concrete type and
// invoke the method.
helloSvc = (HelloInterface) PortableRemoteObject.narrow(objref, HelloInterface.class);
System.out.println("HelloClient: Invoking on remote server with ConcurrentHashMap parameter");
ConcurrentHashMap<String, String> testConcurrentHashMap = new ConcurrentHashMap<String, String>();
response = helloSvc.sayHelloWithHashMap(testConcurrentHashMap);
System.out.println("HelloClient: Server says: " + response);
if (!response.contains("Hello with hashMapSize ==")) {
System.out.println("HelloClient: expected response not received");
throw new RuntimeException("Expected Response Hello with hashMapSize == 0 not received");
}
responseReceived = true;
break;
} catch (NameNotFoundException nnfEx) {
System.err.println("NameNotFoundException Caught .... try again");
retryCount++;
try {
Thread.sleep(ONE_SECOND);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
} catch (Exception e) {
System.err.println("Exception " + e + "Caught");
e.printStackTrace();
throw new RuntimeException(e);
}
}
System.err.println("HelloClient terminating ");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of javax.naming.InitialContext in project OpenClinica by OpenClinica.
the class SessionManager method setupDataSource.
public void setupDataSource() {
// end remove later
try {
Context ctx = new InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
dbName = CoreResources.getField("dataBase");
if ("oracle".equals(dbName)) {
logger.debug("looking up oracle...");
ds = (DataSource) env.lookup("SQLOracle");
} else if ("postgres".equals(dbName)) {
// logger.info("looking up postgres...");
ds = (DataSource) env.lookup("SQLPostgres");
}
} catch (NamingException ne) {
ne.printStackTrace();
logger.warn("This is :" + ne.getMessage() + " when we tried to get the connection");
}
}
use of javax.naming.InitialContext in project opennms by OpenNMS.
the class JBossMBeanServerConnector method createConnection.
@Override
public JmxServerConnectionWrapper createConnection(final InetAddress ipAddress, final Map<String, String> propertiesMap) throws JmxServerConnectionException {
JBossConnectionWrapper wrapper = null;
ClassLoader icl = null;
final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
String connectionType = ParameterMap.getKeyedString(propertiesMap, "factory", "RMI");
String timeout = ParameterMap.getKeyedString(propertiesMap, "timeout", "3000");
String jbossVersion = ParameterMap.getKeyedString(propertiesMap, "version", "4");
String port = ParameterMap.getKeyedString(propertiesMap, "port", "1099");
if (jbossVersion == null || jbossVersion.startsWith("4")) {
try {
icl = new IsolatingClassLoader("jboss", new URL[] { new File(System.getProperty("opennms.home") + "/lib/jboss/jbossall-client.jar").toURI().toURL() }, originalLoader, PACKAGES, true);
} catch (MalformedURLException e) {
LOG.error("JBossConnectionWrapper MalformedURLException", e);
} catch (InvalidContextClassLoaderException e) {
LOG.error("JBossConnectionWrapper InvalidContextClassLoaderException", e);
}
} else if (jbossVersion.startsWith("3")) {
PrivilegedAction<IsolatingClassLoader> action = new PrivilegedAction<IsolatingClassLoader>() {
@Override
public IsolatingClassLoader run() {
try {
return new IsolatingClassLoader("jboss", new URL[] { new File(System.getProperty("opennms.home") + "/lib/jboss/jbossall-client32.jar").toURI().toURL() }, originalLoader, PACKAGES, true);
} catch (MalformedURLException e) {
LOG.error("JBossConnectionWrapper MalformedURLException", e);
} catch (InvalidContextClassLoaderException e) {
LOG.error("JBossConnectionWrapper InvalidContextClassLoaderException", e);
}
return null;
}
};
AccessController.doPrivileged(action);
}
if (icl == null) {
return null;
}
Thread.currentThread().setContextClassLoader(icl);
if (connectionType.equals("RMI")) {
InitialContext ctx = null;
final String hostAddress = InetAddressUtils.toUrlIpAddress(ipAddress);
try {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
props.put(Context.PROVIDER_URL, "jnp://" + hostAddress + ":" + port);
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
props.put("jnp.sotimeout", timeout);
ctx = new InitialContext(props);
Object rmiAdaptor = ctx.lookup("jmx/rmi/RMIAdaptor");
wrapper = new JBossConnectionWrapper(MBeanServerProxy.buildServerProxy(rmiAdaptor));
} catch (Throwable e) {
LOG.debug("JBossConnectionFactory - unable to get MBeanServer using RMI on {}:{}", hostAddress, port);
} finally {
try {
if (ctx != null) {
ctx.close();
}
} catch (Throwable e1) {
LOG.debug("JBossConnectionFactory error closing initial context");
}
}
} else if (connectionType.equals("HTTP")) {
InitialContext ctx = null;
String invokerSuffix = null;
final String hostAddress = InetAddressUtils.toUrlIpAddress(ipAddress);
try {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
props.put(Context.PROVIDER_URL, "http://" + hostAddress + ":" + port + "/invoker/JNDIFactory");
props.put("jnp.sotimeout", timeout);
ctx = new InitialContext(props);
Object rmiAdaptor = ctx.lookup("jmx/rmi/RMIAdaptor");
wrapper = new JBossConnectionWrapper(MBeanServerProxy.buildServerProxy(rmiAdaptor));
} catch (Throwable e) {
LOG.debug("JBossConnectionFactory - unable to get MBeanServer using HTTP on {}{}", hostAddress, invokerSuffix);
} finally {
try {
if (ctx != null)
ctx.close();
} catch (NamingException e1) {
LOG.debug("JBossConnectionFactory error closing initial context");
}
}
}
Thread.currentThread().setContextClassLoader(originalLoader);
return wrapper;
}
use of javax.naming.InitialContext in project opennms by OpenNMS.
the class AlarmListJ2eeConnectionManagerThread method lookupBean.
/**
* Method to find and connect to the remote bean.
*/
private void lookupBean() throws NamingException, RemoteException {
/* Create a new InitialContext with the properties paramters -
* The starting point of naming resolution
*/
LOG.info("AlarmListJ2eeConnectionManagerThread.lookupBean() Looking up QoS bean");
InitialContext ic = new InitialContext(env);
LOG.info("AlarmListJ2eeConnectionManagerThread.lookupBean() InitialContext created");
try {
ref = ic.lookup(props.getProperty("org.openoss.opennms.spring.qosd.jvthome"));
LOG.info("AlarmListJ2eeConnectionManagerThread.lookupBean() QoS Bean found");
JVTAlarmMonitorHome home = (JVTAlarmMonitorHome) PortableRemoteObject.narrow(ref, JVTAlarmMonitorHome.class);
LOG.debug("AlarmListJ2eeConnectionManagerThread.lookupBean() home initialised");
session = home.create();
LOG.debug("AlarmListJ2eeConnectionManagerThread.lookupBean() Session created");
alarmInternals = (AlarmMonitor) PortableRemoteObject.narrow(session.getHandle().getEJBObject(), AlarmMonitor.class);
if (alarmInternals == null)
LOG.error("AlarmListJ2eeConnectionManagerThread.lookupBean() AlarmMonitor alarmInternals is null line 244");
} catch (IllegalArgumentException iae_ex) {
LOG.error("AlarmListJ2eeConnectionManagerThread.lookupBean() jvthome property does not exist", iae_ex);
}/*catch(RemoteException remote_ex)
{
log.error("Cannot connect to bean", remote_ex);
status = DISCONNECTED;
}*/
catch (CreateException create_ex) {
LOG.error("AlarmListJ2eeConnectionManagerThread.lookupBean() Cannot create new session", create_ex);
} catch (NullPointerException np_ex) {
LOG.error("AlarmListJ2eeConnectionManagerThread.lookupBean() NullPointerException caught", np_ex);
} finally {
ic.close();
}
LOG.info("AlarmListJ2eeConnectionManagerThread.lookupBean() New bean session started");
}
Aggregations