Example #1
0
  /**
   * Locates and loads the bean proxy which is specified in the class from the default context.
   *
   * @param <T> The generic class
   * @param local The class of the interface
   * @return The bean proxy
   * @throws NamingException Any error occurred
   */
  public static <T> T locate(final Class<T> local) throws NamingException {
    PerfTracer tracer = new PerfTracer(TRACER);
    PerfLogger logger = new PerfLogger(LOGGER);
    tracer.entry();

    try {
      String className = local.getName();
      logger.debug("Before " + className + CONSTANT_LOOKUP);
      T instance = (T) new InitialContext().lookup(className);
      logger.debug("After " + className + CONSTANT_LOOKUP);

      return instance;
    } finally {
      tracer.exit();
    }
  }
Example #2
0
  /**
   * Locates and loads the bean proxy which is specified in the parameters, the local or the remote
   * proxy will be load, it depends the configuration of this ServiceLocator.
   *
   * @param <T> The generic class
   * @param local The class of the interface
   * @param remote The class of the interface
   * @return The bean proxy
   * @throws NamingException Any error occurred
   */
  public static <T> T locate(final Class<T> local, final Class remote) throws NamingException {
    PerfTracer tracer = new PerfTracer(TRACER);
    PerfLogger logger = new PerfLogger(LOGGER);
    tracer.entry();

    try {
      try {
        return locate(local);
      } catch (Exception except) {
        logger.debug("Local interface not bound: %1$s", except.toString());
      }

      String className = remote.getName();
      logger.debug("Before " + className + CONSTANT_LOOKUP);

      String providerUrl = PROVIDER_URL;
      logger.debug("Default provider URL is %1$s", providerUrl);
      if (System.getProperty("providerUrl." + className) != null) {
        providerUrl = System.getProperty("providerUrl." + className);
        logger.debug("Got %1$s provider URL from property", providerUrl);
      }

      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
      properties.put(Context.URL_PKG_PREFIXES, URL_PKG_PREFIXES);
      properties.put(Context.PROVIDER_URL, providerUrl);
      InitialContext ic = new InitialContext(properties);

      T instance = (T) ic.lookup(className);
      logger.debug("After " + className + CONSTANT_LOOKUP);

      return instance;
    } finally {
      tracer.exit();
    }
  }