Example usage for javax.naming Context createSubcontext

List of usage examples for javax.naming Context createSubcontext

Introduction

In this page you can find the example usage for javax.naming Context createSubcontext.

Prototype

public Context createSubcontext(String name) throws NamingException;

Source Link

Document

Creates and binds a new context.

Usage

From source file:Util.java

/**
 * Create a link//from w ww.j av a 2s  .c  o m
 * 
 * @param ctx
 *          the context
 * @param fromName
 *          the from name
 * @param toName
 *          the to name
 * @throws NamingException
 *           for any error
 */
public static void createLinkRef(Context ctx, String fromName, String toName) throws NamingException {
    LinkRef link = new LinkRef(toName);
    Context fromCtx = ctx;
    Name name = ctx.getNameParser("").parse(fromName);
    String atom = name.get(name.size() - 1);
    for (int n = 0; n < name.size() - 1; n++) {
        String comp = name.get(n);
        try {
            fromCtx = (Context) fromCtx.lookup(comp);
        } catch (NameNotFoundException e) {
            fromCtx = fromCtx.createSubcontext(comp);
        }
    }

    System.out.println("atom: " + atom);
    System.out.println("link: " + link);

    fromCtx.rebind(atom, link);

    System.out.println("Bound link " + fromName + " to " + toName);
}

From source file:com.silverpeas.jcrutil.BetterRepositoryFactoryBean.java

protected static void prepareContext(InitialContext ic, String jndiName) throws NamingException {
    Context currentContext = ic;
    StringTokenizer tokenizer = new StringTokenizer(jndiName, "/", false);
    while (tokenizer.hasMoreTokens()) {
        String name = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            try {
                currentContext = (Context) currentContext.lookup(name);
            } catch (javax.naming.NameNotFoundException nnfex) {
                currentContext = currentContext.createSubcontext(name);
            }//from  www  .  j a v a 2 s .  c o m
        }
    }
}

From source file:com.clican.pluto.common.util.JndiUtils.java

/**
 * Bind the resource manager instance to the JNDI directory.
 * <p>//from  ww w . j  ava 2 s  .  c o  m
 * This method will use the full JNDI path provided for the resource
 * manager, and will create if necessary the individual segments of that
 * path.
 * 
 * @param jndiName
 *            The full JNDI path at which the resource manager instance will
 *            be bound in the JNDI directory. JNDI clients can use that path
 *            to obtain the resource manager instance.
 * @param obj
 *            The object to be bound.
 * @return <b>true</b> if the resource manager was successfully bound to
 *         JNDI using the provided path; otherwise <b>false</b>.
 * 
 * @see #unbind(String)
 */
public static boolean bind(String jndiName, Object obj) {
    if (log.isDebugEnabled()) {
        log.debug("Binding object [" + obj + "] in JNDI at path [" + jndiName + "].");
    }
    Context ctx = null;

    try {
        // Create a binding that is local to this host only.
        Hashtable<String, String> ht = new Hashtable<String, String>();
        // If a special JNDI initial context factory was specified in the
        // constructor, then use it.
        if (jndiInitialContextFactory != null) {
            ht.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialContextFactory);
        }
        // Turn off binding replication .
        // ht.put(WLContext.REPLICATE_BINDINGS, "false");
        ctx = new InitialContext(ht);

        String[] arrJndiNames = jndiName.split("/");
        String subContext = "";

        for (int i = 0; i < arrJndiNames.length - 1; i++) {
            subContext = subContext + "/" + arrJndiNames[i];
            try {
                ctx.lookup(subContext);
            } catch (NameNotFoundException e) {
                ctx.createSubcontext(subContext);
            }

        }

        if (obj instanceof Serializable || jndiInitialContextFactory != null) {
            ctx.bind(jndiName, obj);
        } else {
            NonSerializableFactory.bind(jndiName, obj);
        }
    } catch (NamingException ex) {
        log.error("An error occured while binding [" + jndiName + "] to JNDI:", ex);
        return false;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException ne) {
                log.error("Close context error:", ne);
            }
        }
    }
    return true;
}

From source file:it.unipmn.di.dcs.sharegrid.web.model.StandardEnvironment.java

protected Context getOrCreateSubcontext(String contextName, Context context) throws NamingException {
    try {/* www .j ava 2  s . c om*/
        return (Context) context.lookup(contextName);
    } catch (Exception exp) {
        return context.createSubcontext(contextName);
    }
}

From source file:it.unipmn.di.dcs.sharegrid.web.management.ManagementEnv.java

/** Returns the given (sub-)context (eventually, creating it). */
protected Context getOrCreateSubcontext(String contextName, Context context) throws NamingException {
    try {// w  w  w . ja va2  s  .  c  o m
        return (Context) context.lookup(contextName);
    } catch (Exception exp) {
        return context.createSubcontext(contextName);
    }
}

From source file:mitm.application.djigzo.james.service.DjigzoServiceImpl.java

@Override
public void service(ServiceManager serviceManager) throws ServiceException {
    try {/*from   w ww .  j  a v  a2 s.  com*/
        Context context = new InitialContext();

        /*
         * We will bind the avalon service manager to the JNDI context so we can get a hold of the
         * service manager from spring using a JndiObjectFactoryBean.  
         */
        context = context.createSubcontext("djigzo");
        context.bind("avalonServiceManager", serviceManager);
    } catch (NamingException e) {
        throw new ServiceException("DjigzoService", "Unable to register James service manager.", e);
    }
}

From source file:com.ritchey.naming.InitialContextFactory.java

/**
 * Get Context that has access to default Namespace. This method won't be
 * called if a name URL beginning with java: is passed to an InitialContext.
 *
 * @see org.mortbay.naming.java.javaURLContextFactory
 * @param env a <code>Hashtable</code> value
 * @return a <code>Context</code> value
 *//*from w  w w  .  j av  a2  s  .  c om*/
public Context getInitialContext(Hashtable env) {
    Log.debug("InitialContext loaded");
    Context ctx = new localContextRoot(env);

    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("build.properties"));
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    Context jdbc = null;
    try {
        jdbc = ctx.createSubcontext("jdbc");
    } catch (NamingException e) {
        try {
            jdbc = (Context) ctx.lookup("jdbc");
        } catch (NamingException e1) {
            e1.printStackTrace();
        }
    }
    Context ldap = null;
    try {
        ldap = ctx.createSubcontext("ldap");
    } catch (NamingException e) {
        try {
            ldap = (Context) ctx.lookup("ldap");
        } catch (NamingException e1) {
            e1.printStackTrace();
        }
    }

    Log.debug("getInitialContext");

    String databaseNames = properties.getProperty("database.jndi.names");
    if (databaseNames == null) {
        Log.warn(new RuntimeException("database.jndi.names is not defined"
                + " in build.properties as a comma separated list in " + "build.properties"));
        return ctx;
    }

    for (String database : databaseNames.split(" *, *")) {
        Log.debug("create " + database);
        try {
            createDs(database, properties, jdbc);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    try {
        createLdapStrings(properties, ldap);
    } catch (NamingException e1) {
        e1.printStackTrace();
    }

    String url = getValue(false, "picture", null, properties);
    try {
        ctx.bind("picture", url);
    } catch (NamingException ex) {
        Logger.getLogger(InitialContextFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        Log.debug("jdbc initial context = " + ctx.listBindings("jdbc"));
        NamingEnumeration<Binding> ldapBindings = ctx.listBindings("ldap");
        Log.debug("ldap initial context = " + ctx.listBindings("ldap"));
        while (ldapBindings.hasMore()) {
            Binding binding = ldapBindings.next();
            Log.debug("binding: " + binding.getName());
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return ctx;
}

From source file:com.dattack.naming.loader.NamingLoader.java

/**
 * Scans a directory hierarchy looking for <code>*.properties</code> files. Creates a subcontext for each directory
 * in the hierarchy and binds a new resource for each <code>*.properties</code> file with a
 * <code>ResourceFactory</code> associated.
 *
 *
 * @param directory//  w  ww .  j  ava2s .  c  o  m
 *            the directory to scan
 * @param ctxt
 *            the Context to populate
 * @param extraClasspath
 *            additional paths to include to the classpath
 * @throws NamingException
 *             if a naming exception is encountered
 * @throws IOException
 *             if an I/O error occurs
 */
public void loadDirectory(final File directory, final Context ctxt, final Collection<File> extraClasspath)
        throws NamingException, IOException {

    if (!directory.isDirectory()) {
        throw new IllegalArgumentException(String.format("'%s' isn't a directory", directory));
    }

    final File[] files = directory.listFiles();
    if (files == null) {
        return;
    }

    for (final File file : files) {
        if (file.isDirectory()) {
            final Context subcontext = ctxt.createSubcontext(file.getName());
            loadDirectory(file, subcontext, extraClasspath);
        } else {

            final String fileName = file.getName();
            if (FilenameUtils.isExtension(fileName, EXTENSIONS)) {
                final String baseName = FilenameUtils.getBaseName(fileName);
                try (FileInputStream fin = new FileInputStream(file)) {
                    final Properties properties = new Properties();
                    properties.load(fin);
                    createAndBind(properties, ctxt, baseName, extraClasspath);
                }
            }
        }
    }
}

From source file:com.silverpeas.jcrutil.model.impl.AbstractJcrTestCase.java

/**
 * Workaround to be able to use Sun's JNDI file system provider on Unix
 *
 * @param ic       : the JNDI initial context
 * @param jndiName : the binding name/*from  ww w. j av a 2  s  .c o  m*/
 * @param ref      : the reference to be bound
 * @throws NamingException
 */
protected void rebind(InitialContext ic, String jndiName, Object ref) throws NamingException {
    Context currentContext = ic;
    StringTokenizer tokenizer = new StringTokenizer(jndiName, "/", false);
    while (tokenizer.hasMoreTokens()) {
        String name = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            try {
                currentContext = (Context) currentContext.lookup(name);
            } catch (javax.naming.NameNotFoundException nnfex) {
                currentContext = currentContext.createSubcontext(name);
            }
        } else {
            currentContext.rebind(name, ref);
        }
    }
}

From source file:com.stratelia.silverpeas.versioning.jcr.impl.AbstractJcrTestCase.java

/**
 * Workaround to be able to use Sun's JNDI file system provider on Unix
 * @param ic : the JNDI initial context/*from  w  w  w  .  j  a v a  2  s  .  c  o  m*/
 * @param jndiName : the binding name
 * @param ref : the reference to be bound
 * @throws NamingException
 */
protected void rebind(InitialContext ic, String jndiName, Reference ref) throws NamingException {
    Context currentContext = ic;
    StringTokenizer tokenizer = new StringTokenizer(jndiName, "/", false);
    while (tokenizer.hasMoreTokens()) {
        String name = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            try {
                currentContext = (Context) currentContext.lookup(name);
            } catch (javax.naming.NameNotFoundException nnfex) {
                currentContext = currentContext.createSubcontext(name);
            }
        } else {
            currentContext.rebind(name, ref);
        }
    }
}