Search in sources :

Example 91 with NamingException

use of javax.naming.NamingException in project Activiti by Activiti.

the class JndiEmailTest method setUp.

@BeforeClass
public void setUp() {
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.provider.class", MockEmailTransport.class.getName());
    props.put("mail.smtp.class", MockEmailTransport.class.getName());
    props.put("mail.smtp.provider.vendor", "test");
    props.put("mail.smtp.provider.version", "0.0.0");
    Provider provider = new Provider(Type.TRANSPORT, "smtp", MockEmailTransport.class.getName(), "test", "1.0");
    Session mailSession = Session.getDefaultInstance(props);
    SimpleNamingContextBuilder builder = null;
    try {
        mailSession.setProvider(provider);
        builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        builder.bind("java:comp/env/Session", mailSession);
    } catch (NamingException e) {
        logger.error("Naming error in email setup", e);
    } catch (NoSuchProviderException e) {
        logger.error("provider error in email setup", e);
    }
}
Also used : SimpleNamingContextBuilder(org.springframework.mock.jndi.SimpleNamingContextBuilder) NamingException(javax.naming.NamingException) Properties(java.util.Properties) NoSuchProviderException(javax.mail.NoSuchProviderException) Provider(javax.mail.Provider) Session(javax.mail.Session) BeforeClass(org.junit.BeforeClass)

Example 92 with NamingException

use of javax.naming.NamingException 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");
    }
}
Also used : InitialContext(javax.naming.InitialContext) ApplicationContext(org.springframework.context.ApplicationContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) OracleDataSource(oracle.jdbc.pool.OracleDataSource) DataSource(javax.sql.DataSource)

Example 93 with NamingException

use of javax.naming.NamingException 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;
}
Also used : MalformedURLException(java.net.MalformedURLException) Hashtable(java.util.Hashtable) InvalidContextClassLoaderException(org.opennms.netmgt.jmx.impl.connection.connectors.IsolatingClassLoader.InvalidContextClassLoaderException) URL(java.net.URL) InitialContext(javax.naming.InitialContext) PrivilegedAction(java.security.PrivilegedAction) NamingException(javax.naming.NamingException) File(java.io.File)

Example 94 with NamingException

use of javax.naming.NamingException in project jdk8u_jdk by JetBrains.

the class IPv6NameserverPlatformParsingTest method main.

public static void main(String[] args) {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.dns.DnsContextFactory.class.getName());
    String[] servers;
    try {
        Context ctx = NamingManager.getInitialContext(env);
        if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) {
            throw new RuntimeException("FAIL: no platform servers available, test does not make sense");
        }
        DnsContext context = (DnsContext) ctx;
        servers = getServersFromContext(context);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
    for (String server : servers) {
        System.out.println("DEBUG: 'nameserver = " + server + "'");
        if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) {
            System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server);
            foundIPv6 = true;
        }
    }
    try {
        new com.sun.jndi.dns.DnsClient(servers, 100, 1);
    } catch (NumberFormatException e) {
        throw new RuntimeException("FAIL: Tried to parse non-[]-encapsulated IPv6 address.", e);
    } catch (Exception e) {
        throw new RuntimeException("ERROR: Something unexpected happened.");
    }
    if (!foundIPv6) {
        // platforms. See comment as to how to run this test.
        throw new RuntimeException("ERROR: No IPv6 address returned from platform.");
    }
    System.out.println("PASS: Found IPv6 address and DnsClient parsed it correctly.");
}
Also used : Context(javax.naming.Context) DnsContext(com.sun.jndi.dns.DnsContext) Hashtable(java.util.Hashtable) DnsContext(com.sun.jndi.dns.DnsContext) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Example 95 with NamingException

use of javax.naming.NamingException in project jersey by jersey.

the class EjbComponentProvider method lookupFullyQualifiedForm.

private static Object lookupFullyQualifiedForm(InitialContext ic, Class<?> c, String name, EjbComponentProvider provider) throws NamingException {
    if (provider.libNames.isEmpty()) {
        String jndiName = "java:module/" + name + "!" + c.getName();
        return ic.lookup(jndiName);
    } else {
        NamingException ne = null;
        for (String moduleName : provider.libNames) {
            String jndiName = "java:app/" + moduleName + "/" + name + "!" + c.getName();
            Object result;
            try {
                result = ic.lookup(jndiName);
                if (result != null) {
                    return result;
                }
            } catch (NamingException e) {
                ne = e;
            }
        }
        throw (ne != null) ? ne : new NamingException();
    }
}
Also used : NamingException(javax.naming.NamingException)

Aggregations

NamingException (javax.naming.NamingException)698 InitialContext (javax.naming.InitialContext)234 Context (javax.naming.Context)169 IOException (java.io.IOException)82 NameNotFoundException (javax.naming.NameNotFoundException)67 SQLException (java.sql.SQLException)58 Reference (javax.naming.Reference)53 DataSource (javax.sql.DataSource)52 Test (org.junit.Test)51 Attribute (javax.naming.directory.Attribute)49 DirContext (javax.naming.directory.DirContext)48 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)41 Name (javax.naming.Name)36 SearchResult (javax.naming.directory.SearchResult)35 Hashtable (java.util.Hashtable)34 InitialDirContext (javax.naming.directory.InitialDirContext)34 Connection (java.sql.Connection)33 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)32 Attributes (javax.naming.directory.Attributes)30