コード例 #1
0
  /**
   * Create the channel instance and sets its name.
   *
   * <p>If the channel class matches to constructor parameters of the default channel (ChannelImpl)
   * that constructor is used, else the empty constructor is invoked.
   *
   * @param channelClass Channel implementation class to instanciate.
   * @param name Name of the channel.
   * @return a new Channel instance.
   * @throws IllegalStateException if a channel with name already created or channelClass could not
   *     be instancieated.
   */
  protected Channel getNewChannel(Class<? extends Channel> channelClass, String name)
      throws IllegalStateException {
    try {
      channelRegister.getChannel(name);
      throw new IllegalStateException("Channel with name " + name + " has already been created.");
    } catch (NoSuchChannelException nsce) {
      try {
        if (ChannelImpl.class.isAssignableFrom(channelClass)) {

          Constructor<? extends Channel> cons =
              channelClass.getConstructor(
                  String.class,
                  ChannelRegister.class,
                  EventRegister.class,
                  EventMethodInvoker.class,
                  BeanResolver.class);
          return cons.newInstance(
              name, channelRegister, eventRegister, eventMethodInvoker, beanResolver);
        } else {
          Channel channel = channelClass.newInstance();
          channel.setName(name);
          return channel;
        }
      } catch (Exception e) {
        throw new IllegalStateException(
            "Could not find or instanciate channel class " + channelClass, e);
      }
    }
  }