Search in sources :

Example 96 with InitialContext

use of javax.naming.InitialContext in project jackrabbit by apache.

the class JNDIRepositoryStub method getRepository.

/**
     * Returns a reference to the <code>Repository</code> provided by this
     * <code>RepositoryStub</code>.
     * @return
     */
public synchronized Repository getRepository() throws RepositoryStubException {
    if (repository == null) {
        try {
            String lookupName = environment.getProperty(REPOSITORY_LOOKUP_PROP);
            if (lookupName == null) {
                throw new RepositoryStubException("Property " + REPOSITORY_LOOKUP_PROP + " not defined!");
            }
            InitialContext initial = new InitialContext(environment);
            Object obj = initial.lookup(lookupName);
            repository = (Repository) PortableRemoteObject.narrow(obj, Repository.class);
        } catch (ClassCastException e) {
            // ClassCastException may be thrown by ProtableRemoteObject.narrow()
            throw new RepositoryStubException("Object cannot be narrowed to javax.jcr.Repository", e);
        } catch (NamingException e) {
            throw new RepositoryStubException(e);
        }
    }
    return repository;
}
Also used : PortableRemoteObject(javax.rmi.PortableRemoteObject) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 97 with InitialContext

use of javax.naming.InitialContext in project jackrabbit by apache.

the class JNDIBindingServlet method init.

/**
     * Binds a repository from the servlet context in the configured
     * JNDI location.
     *
     * @throws ServletException if the repository could not be bound in JNDI
     */
public void init() throws ServletException {
    try {
        Hashtable environment = new Hashtable();
        Enumeration names = getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            if (name.equals("location")) {
                location = getInitParameter(name);
            } else if (!name.equals(Repository.class.getName())) {
                environment.put(name, getInitParameter(name));
            }
        }
        context = new InitialContext(environment);
        context.bind(location, new ServletRepository(this));
    } catch (NamingException e) {
        throw new ServletException("Failed to bind repository to JNDI: " + location, e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Repository(javax.jcr.Repository) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 98 with InitialContext

use of javax.naming.InitialContext in project jackrabbit-oak by apache.

the class RepositoryAccessServlet method getRepositoryByJNDI.

/**
     * Checks if the repository is available via JNDI and returns it.
     * @return the repository or <code>null</code>
     * @throws ServletException if this servlet is not properly configured.
     */
private Repository getRepositoryByJNDI() throws ServletException {
    BootstrapConfig config = getConfig();
    if (!config.getJndiConfig().isValid() || !config.getJndiConfig().enabled()) {
        return null;
    }
    // acquire via JNDI
    String repositoryName = config.getRepositoryName();
    InitialContext ctx = getInitialContext();
    if (ctx == null) {
        return null;
    }
    try {
        Repository r = (Repository) ctx.lookup(repositoryName);
        log.info("Acquired repository via JNDI.");
        return r;
    } catch (NamingException e) {
        log.error("Error while retrieving repository using JNDI (name={})", repositoryName, e);
        return null;
    }
}
Also used : Repository(javax.jcr.Repository) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 99 with InitialContext

use of javax.naming.InitialContext in project jackrabbit-oak by apache.

the class RepositoryStartupServlet method registerJNDI.

/**
     * Binds the repository to the JNDI context
     * @throws ServletException if an error occurs.
     */
private void registerJNDI() throws ServletException {
    JNDIConfig jc = config.getJndiConfig();
    if (jc.isValid() && jc.enabled()) {
        try {
            jndiContext = new InitialContext(jc.getJndiEnv());
            jndiContext.bind(jc.getJndiName(), repository);
            log.info("Repository bound to JNDI with name: " + jc.getJndiName());
        } catch (NamingException e) {
            throw new ServletExceptionWithCause("Unable to bind repository using JNDI: " + jc.getJndiName(), e);
        }
    }
}
Also used : NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 100 with InitialContext

use of javax.naming.InitialContext in project jmeter by apache.

the class JMSSampler method getInitialContext.

private Context getInitialContext() throws NamingException {
    Hashtable<String, String> table = new Hashtable<>();
    if (getInitialContextFactory() != null && getInitialContextFactory().trim().length() > 0) {
        LOGGER.debug("Using InitialContext [{}]", getInitialContextFactory());
        table.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
    }
    if (getContextProvider() != null && getContextProvider().trim().length() > 0) {
        LOGGER.debug("Using Provider [{}]", getContextProvider());
        table.put(Context.PROVIDER_URL, getContextProvider());
    }
    Map<String, String> map = getArguments(JMSSampler.JNDI_PROPERTIES).getArgumentsAsMap();
    if (LOGGER.isDebugEnabled()) {
        if (map.isEmpty()) {
            LOGGER.debug("Empty JNDI properties");
        } else {
            LOGGER.debug("Number of JNDI properties: {}", map.size());
        }
    }
    for (Map.Entry<String, String> me : map.entrySet()) {
        table.put(me.getKey(), me.getValue());
    }
    Context context = new InitialContext(table);
    if (LOGGER.isDebugEnabled()) {
        printEnvironment(context);
    }
    return context;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) Hashtable(java.util.Hashtable) Map(java.util.Map) InitialContext(javax.naming.InitialContext)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30