/* (non-Javadoc)
   * @see com.googlecode.psiprobe.beans.ResourceResolver#resetResource(org.apache.catalina.Context, java.lang.String, com.googlecode.psiprobe.beans.ContainerWrapperBean)
   */
  public synchronized boolean resetResource(
      final Context context, String resourceName, ContainerWrapperBean containerWrapper)
      throws NamingException {

    if (context != null) {
      ((AbstractTomcatContainer) containerWrapper.getTomcatContainer()).bindToContext(context);
    }
    try {
      javax.naming.Context ctx =
          (context != null) ? new InitialContext() : getGlobalNamingContext();
      String jndiName = resolveJndiName(resourceName, (context == null));
      Object obj = ctx.lookup(jndiName);
      try {
        for (DatasourceAccessor accessor : datasourceMappers) {
          if (accessor.reset(obj)) {
            return true;
          }
        }
        return false;
      } catch (Throwable e) {
        //
        // make sure we always re-throw ThreadDeath
        //
        if (e instanceof ThreadDeath) {
          throw (ThreadDeath) e;
        }
        return false;
      }
    } finally {
      if (context != null) {
        ((AbstractTomcatContainer) containerWrapper.getTomcatContainer())
            .unbindFromContext(context);
      }
    }
  }
  /**
   * Lookup resource.
   *
   * @param resource the resource
   * @param contextBound the context bound
   * @param global the global
   */
  public void lookupResource(ApplicationResource resource, boolean contextBound, boolean global) {
    DataSourceInfo dataSourceInfo = null;
    if (contextBound) {
      try {
        javax.naming.Context ctx = !global ? new InitialContext() : getGlobalNamingContext();
        String jndiName = resolveJndiName(resource.getName(), global);
        Object obj = ctx.lookup(jndiName);
        resource.setLookedUp(true);
        for (DatasourceAccessor accessor : datasourceMappers) {
          dataSourceInfo = accessor.getInfo(obj);
          if (dataSourceInfo != null) {
            break;
          }
        }

      } catch (Throwable e) {
        resource.setLookedUp(false);
        dataSourceInfo = null;
        logger.error("Failed to lookup: " + resource.getName(), e);
        //
        // make sure we always re-throw ThreadDeath
        //
        if (e instanceof ThreadDeath) {
          throw (ThreadDeath) e;
        }
      }
    } else {
      resource.setLookedUp(false);
    }

    /*
     * Tomcat 5.0.x DBCP datasources would have URL set to null if they incorrectly configured so we
     * need to deal with this little feature
     */
    if (dataSourceInfo != null && dataSourceInfo.getJdbcUrl() == null) {
      resource.setLookedUp(false);
    }

    if (resource.isLookedUp() && dataSourceInfo != null) {
      resource.setDataSourceInfo(dataSourceInfo);
    }
  }