Search in sources :

Example 91 with Context

use of javax.naming.Context in project geode by apache.

the class QueryAndJtaJUnitTest method testFailedIndexUpdateOnJTACommitForPut.

/*
   * Enable this test when indexes are made transactional.
   */
@Ignore
@Test
public void testFailedIndexUpdateOnJTACommitForPut() throws Exception {
    Person.THROW_ON_INDEX = true;
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    Region region = cache.createRegion("sample", af.create());
    qs.createIndex("foo", IndexType.FUNCTIONAL, "index", "/sample");
    Context ctx = cache.getJNDIContext();
    UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    Integer x = new Integer(0);
    utx.begin();
    region.create(x, new Person("xyz", 45));
    try {
        utx.commit();
        fail("Commit should have thrown an exception because the index update threw");
    } catch (Exception e) {
    // this is desired
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) AttributesFactory(org.apache.geode.cache.AttributesFactory) Region(org.apache.geode.cache.Region) RollbackException(javax.transaction.RollbackException) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 92 with Context

use of javax.naming.Context in project geode by apache.

the class ContextImpl method unbind.

/**
   * Removes name and its associated object from the context.
   * 
   * @param name name to remove
   * @throws NoPermissionException if this context has been destroyed.
   * @throws InvalidNameException if name is empty or is CompositeName that spans more than one
   *         naming system
   * @throws NameNotFoundException if intermediate context can not be found
   * @throws NotContextException if name has more than one atomic name and intermediate context is
   *         not found.
   * @throws NamingException if any other naming exception occurs
   * 
   */
public void unbind(Name name) throws NamingException {
    checkIsDestroyed();
    Name parsedName = getParsedName(name);
    if (parsedName.size() == 0 || parsedName.get(0).length() == 0) {
        throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString());
    }
    String nameToRemove = parsedName.get(0);
    // remove a and its associated object
    if (parsedName.size() == 1) {
        ctxMaps.remove(nameToRemove);
    } else {
        // scenerio unbind a/b or a/b/c
        // remove b and its associated object
        Object boundObject = ctxMaps.get(nameToRemove);
        if (boundObject instanceof Context) {
            // remove b and its associated object
            ((Context) boundObject).unbind(parsedName.getSuffix(1));
        } else {
            // if the name is not found then throw exception
            if (!ctxMaps.containsKey(nameToRemove)) {
                throw new NameNotFoundException(LocalizedStrings.ContextImpl_CAN_NOT_FIND_0.toLocalizedString(name));
            }
            throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
        }
    }
}
Also used : Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 93 with Context

use of javax.naming.Context in project geode by apache.

the class ContextImpl method rebind.

/**
   * Rebinds object obj to name name . If there is existing binding it will be overwritten.
   * 
   * @param name name of the object to rebind.
   * @param obj object to add. Can be null.
   * @throws NoPermissionException if this context has been destroyed
   * @throws InvalidNameException if name is empty or is CompositeName that spans more than one
   *         naming system
   * @throws NotContextException if name has more than one atomic name and intermediate context is
   *         not found
   * @throws NamingException if any other naming error occurs
   * 
   */
public void rebind(Name name, Object obj) throws NamingException {
    checkIsDestroyed();
    Name parsedName = getParsedName(name);
    if (parsedName.size() == 0 || parsedName.get(0).length() == 0) {
        throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString());
    }
    String nameToBind = parsedName.get(0);
    if (parsedName.size() == 1) {
        ctxMaps.put(nameToBind, obj);
    } else {
        Object boundObject = ctxMaps.get(nameToBind);
        if (boundObject instanceof Context) {
            /*
         * Let the subcontext bind the object.
         */
            ((Context) boundObject).bind(parsedName.getSuffix(1), obj);
        } else {
            if (boundObject == null) {
                // Create new subcontext and let it do the binding
                Context sub = createSubcontext(nameToBind);
                sub.bind(parsedName.getSuffix(1), obj);
            } else {
                throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
            }
        }
    }
}
Also used : Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 94 with Context

use of javax.naming.Context in project geode by apache.

the class ContextImpl method createSubcontext.

/**
   * Creates subcontext with name, relative to this Context.
   * 
   * @param name subcontext name.
   * @return new subcontext named name relative to this context.
   * @throws NoPermissionException if this context has been destroyed.
   * @throws InvalidNameException if name is empty or is CompositeName that spans more than one
   *         naming system.
   * @throws NameAlreadyBoundException if name is already bound in this Context
   * @throws NotContextException if any intermediate name from name is not bound to instance of
   *         javax.naming.Context.
   * 
   */
public Context createSubcontext(Name name) throws NamingException {
    checkIsDestroyed();
    Name parsedName = getParsedName(name);
    if (parsedName.size() == 0 || parsedName.get(0).length() == 0) {
        throw new InvalidNameException(LocalizedStrings.ContextImpl_NAME_CAN_NOT_BE_EMPTY.toLocalizedString());
    }
    String subContextName = parsedName.get(0);
    Object boundObject = ctxMaps.get(parsedName.get(0));
    if (parsedName.size() == 1) {
        // Check if name is already in use
        if (boundObject == null) {
            Context subContext = new ContextImpl(this, subContextName);
            ctxMaps.put(subContextName, subContext);
            return subContext;
        } else {
            throw new NameAlreadyBoundException(LocalizedStrings.ContextImpl_NAME_0_IS_ALREADY_BOUND.toLocalizedString(subContextName));
        }
    } else {
        if (boundObject instanceof Context) {
            // an exception will be thrown in that case.
            return ((Context) boundObject).createSubcontext(parsedName.getSuffix(1));
        } else {
            throw new NotContextException(LocalizedStrings.ContextImpl_EXPECTED_CONTEXT_BUT_FOUND_0.toLocalizedString(boundObject));
        }
    }
}
Also used : Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) InvalidNameException(javax.naming.InvalidNameException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 95 with Context

use of javax.naming.Context in project geode by apache.

the class IdleTimeOutDUnitTest method runTest2.

public static void runTest2() throws Exception {
    final int MAX_CONNECTIONS = 30;
    // getLogWriter().info("runTest2 sleeping for 20 sec");
    // Thread.sleep(20 * 1000);
    DataSource ds = null;
    try {
        Context ctx = cache.getJNDIContext();
        ds = (DataSource) ctx.lookup("java:/XAPooledDataSource");
    } catch (NamingException e) {
        LogWriterUtils.getLogWriter().info("Exception caught during naming lookup: " + e);
        fail("failed in naming lookup: " + e);
        return;
    } catch (Exception e) {
        LogWriterUtils.getLogWriter().info("Exception caught during naming lookup: " + e);
        fail("failed in because of unhandled excpetion: " + e);
        return;
    }
    try {
        for (int count = 0; count < MAX_CONNECTIONS; count++) {
            Connection con = ds.getConnection();
            assertNotNull("Connection object is null", con);
            LogWriterUtils.getLogWriter().info("runTest2 :acquired connection #" + count);
        }
    } catch (SQLException sqle) {
        LogWriterUtils.getLogWriter().info("SQLException caught in runTest2: " + sqle);
        fail("failed because of SQL exception : " + sqle);
        sqle.printStackTrace();
    } catch (Exception e) {
        LogWriterUtils.getLogWriter().info("Exception caught in runTest2: " + e);
        fail("failed because of unhandled exception : " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) SQLException(java.sql.SQLException) Connection(java.sql.Connection) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataSource(javax.sql.DataSource)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23