Exemple #1
0
  /**
   * Creates a channel from an array of protocols. Note that after a {@link
   * org.jgroups.JChannel#close()}, the protocol list <em>should not</em> be reused, ie. new
   * JChannel(protocols) would reuse the same protocol list, and this might lead to problems !
   *
   * @param protocols The list of protocols, from bottom to top, ie. the first protocol in the list
   *     is the transport, the last the top protocol
   * @throws Exception
   */
  public JChannel(Collection<Protocol> protocols) throws Exception {
    prot_stack = new ProtocolStack();
    setProtocolStack(prot_stack);
    for (Protocol prot : protocols) {
      prot_stack.addProtocol(prot);
      prot.setProtocolStack(prot_stack);
    }
    prot_stack.init();

    // Substitute vars with defined system props (if any)
    List<Protocol> prots = prot_stack.getProtocols();
    Map<String, String> map = new HashMap<>();
    for (Protocol prot : prots) Configurator.resolveAndAssignFields(prot, map);
  }
  public static void main(String[] args) throws Exception {
    String input_file = null;
    XmlConfigurator conf;
    boolean old_format = false;

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-old")) {
        old_format = true;
        continue;
      }
      if (args[i].equals("-file")) {
        input_file = args[++i];
        continue;
      }
      help();
      return;
    }

    if (input_file != null) {
      InputStream input = null;

      try {
        input = new FileInputStream(new File(input_file));
      } catch (Throwable t) {
      }
      if (input == null) {
        try {
          input = new URL(input_file).openStream();
        } catch (Throwable t) {
        }
      }

      if (input == null)
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream(input_file);

      if (old_format) {
        String cfg = inputAsString(input);
        List<ProtocolConfiguration> tmp = Configurator.parseConfigurations(cfg);
        System.out.println(dump(tmp));
      } else {
        conf = XmlConfigurator.getInstance(input);
        String tmp = conf.getProtocolStackString();
        System.out.println("\n" + tmp);
      }
    } else {
      log.error("no input file given");
    }
  }
  public ProtocolTester(String prot_spec, Protocol harness) throws Exception {
    if (prot_spec == null || harness == null)
      throw new Exception("ProtocolTester(): prot_spec or harness is null");

    props = prot_spec;
    this.harness = harness;
    props = "LOOPBACK:" + props; // add a loopback layer at the bottom of the stack

    config = new Configurator();
    JChannel mock_channel = new JChannel() {};
    ProtocolStack stack = new ProtocolStack(mock_channel);
    stack.setup(Configurator.parseConfigurations(props));
    stack.insertProtocol(harness, ProtocolStack.ABOVE, stack.getTopProtocol().getClass());

    bottom = stack.getBottomProtocol();

    // has to be set after StartProtocolStack, otherwise the up and down handler threads in the
    // harness
    // will be started as well (we don't want that) !
    // top.setUpProtocol(harness);
  }
Exemple #4
0
 /**
  * Creates a fork-stack from the configuration, initializes all protocols (setting values), sets
  * the protocol stack as top protocol, connects the protocols and calls init() on them. Returns
  * the protocols in a list, from bottom to top
  */
 protected static List<Protocol> createProtocols(
     ProtocolStack stack, List<ProtocolConfiguration> protocol_configs) throws Exception {
   return Configurator.createProtocols(protocol_configs, stack);
 }
  @Override
  public Channel createChannel(final String id) throws Exception {
    JGroupsLogger.ROOT_LOGGER.debugf(
        "Creating channel %s from stack %s", id, this.configuration.getName());

    PrivilegedExceptionAction<JChannel> action =
        new PrivilegedExceptionAction<JChannel>() {
          @Override
          public JChannel run() throws Exception {
            return new JChannel(JChannelFactory.this);
          }
        };
    final JChannel channel = WildFlySecurityManager.doChecked(action);
    ProtocolStack stack = channel.getProtocolStack();

    TransportConfiguration transportConfig = this.configuration.getTransport();
    SocketBinding binding = transportConfig.getSocketBinding();
    if (binding != null) {
      channel.setSocketFactory(new ManagedSocketFactory(binding.getSocketBindings()));
    }

    // Relay protocol is added to stack programmatically, not via ProtocolStackConfigurator
    RelayConfiguration relayConfig = this.configuration.getRelay();
    if (relayConfig != null) {
      String localSite = relayConfig.getSiteName();
      List<RemoteSiteConfiguration> remoteSites = this.configuration.getRelay().getRemoteSites();
      List<String> sites = new ArrayList<>(remoteSites.size() + 1);
      sites.add(localSite);
      // Collect bridges, eliminating duplicates
      Map<String, RelayConfig.BridgeConfig> bridges = new HashMap<>();
      for (final RemoteSiteConfiguration remoteSite : remoteSites) {
        String siteName = remoteSite.getName();
        sites.add(siteName);
        String clusterName = remoteSite.getClusterName();
        RelayConfig.BridgeConfig bridge =
            new RelayConfig.BridgeConfig(clusterName) {
              @Override
              public JChannel createChannel() throws Exception {
                JChannel channel = (JChannel) remoteSite.getChannel();
                // Don't use FORK in bridge stack
                channel.getProtocolStack().removeProtocol(FORK.class);
                return channel;
              }
            };
        bridges.put(clusterName, bridge);
      }
      RELAY2 relay = new RELAY2().site(localSite);
      for (String site : sites) {
        RelayConfig.SiteConfig siteConfig = new RelayConfig.SiteConfig(site);
        relay.addSite(site, siteConfig);
        if (site.equals(localSite)) {
          for (RelayConfig.BridgeConfig bridge : bridges.values()) {
            siteConfig.addBridge(bridge);
          }
        }
      }
      Map<String, String> relayProperties = new HashMap<>(relayConfig.getProperties());
      Configurator.resolveAndAssignFields(relay, relayProperties);
      Configurator.resolveAndInvokePropertyMethods(relay, relayProperties);
      stack.addProtocol(relay);
      relay.init();
    }

    UnknownForkHandler unknownForkHandler =
        new UnknownForkHandler() {
          private final short id = ClassConfigurator.getProtocolId(RequestCorrelator.class);

          @Override
          public Object handleUnknownForkStack(Message message, String forkStackId) {
            return this.handle(message);
          }

          @Override
          public Object handleUnknownForkChannel(Message message, String forkChannelId) {
            return this.handle(message);
          }

          private Object handle(Message message) {
            Header header = (Header) message.getHeader(this.id);
            // If this is a request expecting a response, don't leave the requester hanging - send
            // an identifiable response on which it can filter
            if ((header != null) && (header.type == Header.REQ) && header.rspExpected()) {
              Message response =
                  message
                      .makeReply()
                      .setFlag(message.getFlags())
                      .clearFlag(Message.Flag.RSVP, Message.Flag.SCOPED);

              response.putHeader(FORK.ID, message.getHeader(FORK.ID));
              response.putHeader(this.id, new Header(Header.RSP, 0, header.corrId));
              response.setBuffer(UNKNOWN_FORK_RESPONSE.array());

              channel.down(new Event(Event.MSG, response));
            }
            return null;
          }
        };

    // Add implicit FORK to the top of the stack
    FORK fork = new FORK();
    fork.setUnknownForkHandler(unknownForkHandler);
    stack.addProtocol(fork);
    fork.init();

    channel.setName(this.configuration.getNodeName());

    TransportConfiguration.Topology topology = this.configuration.getTransport().getTopology();
    if (topology != null) {
      channel.addAddressGenerator(new TopologyAddressGenerator(topology));
    }

    return channel;
  }