Search in sources :

Example 1 with NoInitialContextException

use of javax.naming.NoInitialContextException in project SearchServices by Alfresco.

the class ConfigUtil method locateProperty.

/**
 * Finds the property based on looking up the value in one of three places (in order of preference):
 * <ol>
 *  <li>JNDI: via java:comp/env/{propertyName/converted/to/slash}</li>
 *  <li>A Java system property or a Java system property prefixed with solr.</li>
 *  <li>OS environment variable</li>
 * </ol>
 *
 * @return A property
 */
public static String locateProperty(String propertyName, String defaultValue) {
    String propertyValue = null;
    String propertyKey = propertyName.toLowerCase();
    String jndiKey = convertPropertyNameToJNDIPath(propertyKey);
    String envVar = convertPropertyNameToEnvironmentParam(propertyKey);
    // Try JNDI
    try {
        Context c = new InitialContext();
        propertyValue = (String) c.lookup(jndiKey);
        log.debug("Using JNDI key: " + jndiKey + ": " + propertyValue);
        return propertyValue;
    } catch (NoInitialContextException e) {
        log.debug("JNDI not configured (NoInitialContextEx)");
    } catch (NamingException e) {
        log.debug("No " + jndiKey + " in JNDI");
    } catch (RuntimeException ex) {
        log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage());
    }
    // Now try system property
    propertyValue = System.getProperty(propertyKey);
    if (propertyValue != null) {
        log.debug("Using system property " + propertyKey + ": " + propertyValue);
        return propertyValue;
    }
    // try system property again with a solr. prefix
    propertyValue = System.getProperty("solr." + propertyKey);
    if (propertyValue != null) {
        log.debug("Using system property " + "solr." + propertyKey + ": " + propertyValue);
        return propertyValue;
    }
    // Now try an environment variable
    propertyValue = System.getenv(envVar);
    if (propertyValue != null) {
        log.debug("Using environment variable " + envVar + ": " + propertyValue);
        return propertyValue;
    }
    // if all else fails then return the default
    log.debug("Using default value for variable " + propertyName + ": " + defaultValue);
    return defaultValue;
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NoInitialContextException(javax.naming.NoInitialContextException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 2 with NoInitialContextException

use of javax.naming.NoInitialContextException in project Payara by payara.

the class JmsNotifierService method bootstrap.

@Override
public void bootstrap() {
    super.bootstrap();
    try {
        final String contextFactoryClass = configuration.getContextFactoryClass();
        final String connectionFactoryName = configuration.getConnectionFactoryName();
        final String url = configuration.getUrl();
        final String username = configuration.getUsername();
        final String password = configuration.getPassword();
        final Properties env = new Properties();
        if (StringUtils.ok(contextFactoryClass)) {
            env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactoryClass);
        }
        if (StringUtils.ok(url)) {
            env.put(Context.PROVIDER_URL, url);
        }
        if (StringUtils.ok(username)) {
            env.put(Context.SECURITY_PRINCIPAL, username);
        }
        if (StringUtils.ok(password)) {
            env.put(Context.SECURITY_CREDENTIALS, password);
        }
        if (StringUtils.ok(connectionFactoryName)) {
            try {
                InitialContext ctx = new InitialContext(env);
                ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(connectionFactoryName);
                this.connection = connectionFactory.createConnection();
            } catch (NoInitialContextException e) {
                if (e.getRootCause() instanceof ClassNotFoundException) {
                    LOGGER.log(Level.SEVERE, "Context factory class cannot be found on classpath: " + configuration.getContextFactoryClass());
                }
            }
        }
    } catch (NamingException e) {
        LOGGER.log(Level.SEVERE, "Cannot lookup JMS resources", e);
    } catch (JMSException e) {
        LOGGER.log(Level.SEVERE, "Cannot create JMS connection", e);
    }
}
Also used : NoInitialContextException(javax.naming.NoInitialContextException) ConnectionFactory(javax.jms.ConnectionFactory) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Example 3 with NoInitialContextException

use of javax.naming.NoInitialContextException in project lucene-solr by apache.

the class SolrResourceLoader method locateSolrHome.

/**
   * Determines the solrhome from the environment.
   * Tries JNDI (java:comp/env/solr/home) then system property (solr.solr.home);
   * if both fail, defaults to solr/
   * @return the instance directory name
   */
/**
   * Finds the solrhome based on looking up the value in one of three places:
   * <ol>
   *  <li>JNDI: via java:comp/env/solr/home</li>
   *  <li>The system property solr.solr.home</li>
   *  <li>Look in the current working directory for a solr/ directory</li> 
   * </ol>
   *
   * The return value is normalized.  Normalization essentially means it ends in a trailing slash.
   * @return A normalized solrhome
   * @see #normalizeDir(String)
   */
public static Path locateSolrHome() {
    String home = null;
    // Try JNDI
    try {
        Context c = new InitialContext();
        home = (String) c.lookup("java:comp/env/" + project + "/home");
        logOnceInfo("home_using_jndi", "Using JNDI solr.home: " + home);
    } catch (NoInitialContextException e) {
        log.debug("JNDI not configured for " + project + " (NoInitialContextEx)");
    } catch (NamingException e) {
        log.debug("No /" + project + "/home in JNDI");
    } catch (RuntimeException ex) {
        log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage());
    }
    // Now try system property
    if (home == null) {
        String prop = project + ".solr.home";
        home = System.getProperty(prop);
        if (home != null) {
            logOnceInfo("home_using_sysprop", "Using system property " + prop + ": " + home);
        }
    }
    // if all else fails, try 
    if (home == null) {
        home = project + '/';
        logOnceInfo("home_default", project + " home defaulted to '" + home + "' (could not find system property or JNDI)");
    }
    return Paths.get(home);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NoInitialContextException(javax.naming.NoInitialContextException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 4 with NoInitialContextException

use of javax.naming.NoInitialContextException in project geronimo-xbean by apache.

the class GlobalContextManagerTest method testNoGlobalContextSet.

public void testNoGlobalContextSet() throws Exception {
    // force reset since in java 7 order is not guaranteed
    GlobalContextManager.setGlobalContext(null);
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
    try {
        InitialContext initialContext = new InitialContext(env);
        initialContext.lookup("");
        fail("expected a NoInitialContextException");
    } catch (NoInitialContextException expected) {
    }
}
Also used : NoInitialContextException(javax.naming.NoInitialContextException) Hashtable(java.util.Hashtable) InitialContext(javax.naming.InitialContext)

Example 5 with NoInitialContextException

use of javax.naming.NoInitialContextException in project tomee by apache.

the class AbstractObjectFactory method getObjectInstance.

@Override
public Object getObjectInstance(final Object object, final Name name, final Context context, final Hashtable environment) throws Exception {
    final Reference ref = (Reference) object;
    // the jndi context to use for the lookup (usually null which is the default context)
    final String jndiProviderId = NamingUtil.getProperty(ref, NamingUtil.JNDI_PROVIDER_ID);
    // the jndi name
    String jndiName = NamingUtil.getProperty(ref, NamingUtil.JNDI_NAME);
    if (jndiName == null) {
        jndiName = buildJndiName(ref);
    }
    // look up the reference
    try {
        return lookup(jndiProviderId, jndiName);
    } catch (final NameNotFoundException nnfe) {
        // 2nd try
        if (jndiName.startsWith("java:")) {
            try {
                return new InitialContext().lookup(jndiName);
            } catch (final NameNotFoundException ignored) {
            // no-op
            } catch (final NoInitialContextException nice) {
                final Properties props = new Properties();
                props.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
                try {
                    return new InitialContext(props).lookup(jndiName);
                } catch (final NameNotFoundException ignored) {
                // no-op
                }
            }
        }
        // 3rd try
        if (ref.getClassName() != null) {
            final String moduleName = "java:module/" + Strings.lastPart(ref.getClassName(), '.');
            try {
                return new InitialContext().lookup(moduleName);
            } catch (final NameNotFoundException ignored) {
            // no-op
            } catch (final NoInitialContextException nice) {
                final Properties props = new Properties();
                props.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
                try {
                    return new InitialContext(props).lookup(moduleName);
                } catch (final NameNotFoundException ignored) {
                // no-op
                }
            }
        }
        throw nnfe;
    }
}
Also used : NoInitialContextException(javax.naming.NoInitialContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) Properties(java.util.Properties) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) InitialContext(javax.naming.InitialContext)

Aggregations

NoInitialContextException (javax.naming.NoInitialContextException)7 InitialContext (javax.naming.InitialContext)5 NamingException (javax.naming.NamingException)3 Properties (java.util.Properties)2 Context (javax.naming.Context)2 BundleContext (org.osgi.framework.BundleContext)2 Hashtable (java.util.Hashtable)1 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSException (javax.jms.JMSException)1 NameNotFoundException (javax.naming.NameNotFoundException)1 Reference (javax.naming.Reference)1 LocalInitialContextFactory (org.apache.openejb.core.LocalInitialContextFactory)1 Bundle (org.osgi.framework.Bundle)1