Java 8 Context - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Context class.

This code example shows how to use the following methods:close, lookup
   *           if a JNDI error occurs 
   */ 
  public static DataSource lookupDataSource(String dataSourceName) throws TasteException {
    Context context = null;
    try { 
      context = new InitialContext();
      return (DataSource) context.lookup("java:comp/env/" + dataSourceName);
    } catch (NamingException ne) {
      throw new TasteException(ne);
    } finally { 
      if (context != null) {
        try { 
          context.close();
        } catch (NamingException ne) {
          log.warn("Error while closing Context; continuing...", ne);
        } 
      } 
    } 
  } 
   
7
CodeRank
This code example shows how to use the following methods:lookup
  } 
 
  private DataSource lookupDataSource() throws NamingException, SQLException {
    addInfo("Looking up ["+jndiLocation+"] in JNDI");
    DataSource ds;
    Context initialContext = new InitialContext();
    Object obj = initialContext.lookup(jndiLocation);
 
    // PortableRemoteObject was introduced in JDK 1.3. We won't use it. 
    // ds = (DataSource)PortableRemoteObject.narrow(obj, DataSource.class); 
    ds = (DataSource) obj;
 
    if (ds == null) {
      throw new SQLException("Failed to obtain data source from JNDI location "
          + jndiLocation);
    } else { 
      return ds;
    } 
  } 
} 
7
CodeRank
This code example shows how to use the following methods:createSubcontext, getNameParser, lookup, lookupLink, rebind
    else { 
      Context context = (Context) _javaCompEnvMap.getLevel();
 
      if (context == null) {
        context = (Context) new InitialContext().lookup("java:comp/env");
        _javaCompEnvMap.set(context);
      } 
 
      return context;
    } 
  } 
   
  public static void bindDeepShort(String name, Object obj)
    throws NamingExceptionString fullName = getFullName(name);
    Context context = getShortContext(name);
       
    bindImpl(context, name, obj, fullName);
  } 

6
CodeRank
This code example shows how to use the following methods:bind, createSubcontext
    public static void setUp() throws ExceptionSystem.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
 
        Context ctx = new InitialContext();
        ctx.createSubcontext("java:comp");
        // This is what we need to bind to get handle of JTA's 
        // UserTransaction. 
 
        ctx.bind("java:comp/UserTransaction", new KunderaJTAUserTransaction());
 
    } 
 
    /** 
     * Context lookup on before running each method. 
     *  
     * @throws Exception 
     */ 
    @Before 
6
CodeRank
This code example shows how to use the following methods:lookup
class EJBUtil { 
 
    static IntermediateEJBRemote lookupIntermediateEJB() throws Exception {
        final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);
 
        return (IntermediateEJBRemote) context.lookup("ejb:/jboss-ejb-security-interceptors/IntermediateEJB!"
            + IntermediateEJBRemote.class.getName());
    } 
 
    static SecuredEJBRemote lookupSecuredEJB() throws Exception {
        final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);
 
        return (SecuredEJBRemote) context.lookup("ejb:/jboss-ejb-security-interceptors/SecuredEJB!"
            + SecuredEJBRemote.class.getName());
    } 
 
6
CodeRank
This code example shows how to use the following methods:close
 
    try { 
      Context jndi = buildJNDIContext();
 
      // addInfo("Looking up [" + qcfBindingName + "]"); 
      queueConnectionFactory = (QueueConnectionFactory) lookup(jndi,
          qcfBindingName);
      // addInfo("About to create QueueConnection."); 
      if (userName != null) { 
        this.queueConnection = queueConnectionFactory.createQueueConnection(
            userName, password); 
      } else { 
        this.queueConnection = queueConnectionFactory.createQueueConnection();
      } 
 
      // addInfo( 
      // "Creating QueueSession, non-transactional, " 
      // + "in AUTO_ACKNOWLEDGE mode."); 
      this.queueSession = queueConnection.createQueueSession(false,
          Session.AUTO_ACKNOWLEDGE);

6
CodeRank
This code example shows how to use the following methods:lookup, unbind
		} 
 
		// Check the same call will work again, but the context is empty 
		builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
		try { 
			ctx = new InitialContext();
			ctx.lookup(name);
			fail("Should have thrown NamingException"); 
		} 
		catch (NamingException ex) {
			// expected 
		} 
		Object o2 = new Object(); 
		builder.bind(name, o2);
		assertEquals(ctx.lookup(name), o2);
	} 
 
 
	static class StubDataSource implements DataSource {
 
7
CodeRank
This code example shows how to use the following methods:close, lookup
	} 
 
	private EmbeddedCacheManager locateCacheManager(String jndiNamespace, Properties jndiProperties) {
		Context ctx = null;
		try { 
			ctx = new InitialContext( jndiProperties );
			return (EmbeddedCacheManager) ctx.lookup( jndiNamespace );
		} 
		catch (NamingException ne) {
			String msg = "Unable to retrieve CacheManager from JNDI [" + jndiNamespace + "]";
			log.unableToRetrieveCacheManagerFromJndi( jndiNamespace, ne );
			throw new SearchException( msg );
		} 
		finally { 
			if ( ctx != null ) {
				try { 
					ctx.close();
				} 
				catch (NamingException ne) {
					log.unableToReleaseInitialContext( ne );

6
CodeRank
This code example shows how to use the following methods:lookup
    private CustomerService getCustomerService() { 
        if (customerService != null) {
            return customerService;
        } 
        try { 
            Context context = new InitialContext();
            beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
            Bean<?> bean = beanManager.getBeans(CustomerService.class).iterator().next();
            CreationalContext cc = beanManager.createCreationalContext(bean);
            customerService = (CustomerService) beanManager.getReference(bean, CustomerService.class, cc);
 
        } catch (NamingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } 
 
        return customerService;
 
    } 
 
7
CodeRank
This code example shows how to use the following methods:close, getAttributes
    // 
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
      + parts[0] + ".in-addr.arpa";
 
    DirContext ictx = new InitialDirContext();
    Attributes attribute;
    try { 
      attribute = ictx.getAttributes("dns://"               // Use "dns:///" if the default
                         + ((ns == null) ? "" : ns) +
                         // nameserver is to be used 
                         "/" + reverseIP, new String[] { "PTR" });
    } finally { 
      ictx.close();
    } 
 
    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
      hostname = hostname.substring(0, hostnameLength - 1);
6
CodeRank
See Code Examples for Java 8 Context Methods: