Пример #1
0
  /**
   * Obtain the sender instance for call distant methods for an interface
   *
   * @param <TYPE> Interface type
   * @param interfaceIDL Interface class
   * @return Sender instance for call distant methods for an interface
   * @throws ClassNotFoundException If the interface is not registered
   */
  @SuppressWarnings("unchecked")
  public static <TYPE> TYPE obtainSenderInstance(final Class<TYPE> interfaceIDL)
      throws ClassNotFoundException {
    final String className = interfaceIDL.getName() + JHelpIDL.SOURCE;
    TYPE sender = (TYPE) JHelpIDL.INSTANCES.get(className);

    if (sender == null) {
      sender = (TYPE) Reflector.newInstance(className, JHelpIDL.CLASS_LOADER);
      JHelpIDL.INSTANCES.put(className, sender);
    }

    return sender;
  }
Пример #2
0
  /**
   * Link a receiver to an interface.<br>
   * It a way to say what to answer when receiver message.<br>
   * For one interface, only one receiver is allowed, have to call {@link #stopReceiver(Class)} for
   * be able change the registered object
   *
   * @param <TYPE> Interface type
   * @param interfaceIDLImplementation Given implementation
   * @param interfaceIDL Interface class
   * @throws ClassNotFoundException If interface not registered
   * @throws NoSuchMethodException If registeration had previously issue
   * @throws SecurityException If the interface targetted is protected by security form refelection
   * @throws InstantiationException If registeration had previously issue
   * @throws IllegalAccessException If the interface targetted is protected by security form
   *     refelection
   * @throws IllegalArgumentException If the implementation given is wrong type
   * @throws InvocationTargetException If the interface targetted is protected by security form
   *     refelection or registeration had previously issue
   */
  @SuppressWarnings("unchecked")
  public static <TYPE> void registerReceiver(
      final TYPE interfaceIDLImplementation, final Class<TYPE> interfaceIDL)
      throws ClassNotFoundException, NoSuchMethodException, SecurityException,
          InstantiationException, IllegalAccessException, IllegalArgumentException,
          InvocationTargetException {
    if (interfaceIDLImplementation == null) {
      throw new NullPointerException("interfaceIDLImplementation musn't be null");
    }

    final String className = interfaceIDL.getName() + JHelpIDL.TARGET;
    TYPE receiver = (TYPE) JHelpIDL.INSTANCES.get(className);

    if (receiver != null) {
      throw new IllegalStateException("A receiver is already registered !");
    }

    final Class<?> claz = JHelpIDL.CLASS_LOADER.loadClass(className);
    final Constructor<?> constructor = claz.getConstructor(interfaceIDL);
    receiver = (TYPE) constructor.newInstance(interfaceIDLImplementation);

    JHelpIDL.INSTANCES.put(className, receiver);
  }