Beispiel #1
0
  private static Properties adjustProperties(Properties pp) {
    // A BackEndContainer is never a Main
    pp.setProperty(Profile.MAIN, "false");

    // Set default additional services if not already set
    String services = pp.getProperty(Profile.SERVICES);
    if (services == null) {
      services = "jade.core.event.NotificationService";
    }
    String additionalServices = pp.getProperty(BEManagementService.ADDITIONAL_SERVICES);
    if (additionalServices != null) {
      services += ";" + additionalServices;
    }
    pp.setProperty(Profile.SERVICES, services);

    // Merge back-end and front-end services
    String feServices = pp.getProperty(MicroRuntime.BE_REQUIRED_SERVICES_KEY);
    if (feServices != null) {
      Vector ss = Specifier.parseList(pp.getProperty(Profile.SERVICES), ';');
      Vector fess = Specifier.parseList(feServices, ';');
      for (int i = 0; i < fess.size(); ++i) {
        String s = (String) fess.get(i);
        if (!ss.contains(s)) {
          ss.add(s);
        }
      }
      pp.setProperty(Profile.SERVICES, Specifier.encodeList(ss, Specifier.SPECIFIER_SEPARATOR));
    }
    return pp;
  }
  /** Initialize this JICPMediator */
  public void init(JICPMediatorManager mgr, String id, Properties props) throws ICPException {
    myMediatorManager = mgr;
    myID = id;
    myProperties = props;

    // Max disconnection time
    long maxDisconnectionTime = JICPProtocol.DEFAULT_MAX_DISCONNECTION_TIME;
    try {
      maxDisconnectionTime =
          Long.parseLong(props.getProperty(JICPProtocol.MAX_DISCONNECTION_TIME_KEY));
    } catch (Exception e) {
      // Keep default
    }

    // Keep-alive time
    keepAliveTime = JICPProtocol.DEFAULT_KEEP_ALIVE_TIME;
    try {
      keepAliveTime = Long.parseLong(props.getProperty(JICPProtocol.KEEP_ALIVE_TIME_KEY));
    } catch (Exception e) {
      // Keep default
    }

    // Server-keep-alive time
    enableServerKeepAlive = false;
    try {
      enableServerKeepAlive =
          Boolean.valueOf(props.getProperty("enable-server-keep-alive")).booleanValue();
    } catch (Exception e) {
      // Keep default
    }

    // inpCnt
    int inpCnt = 0;
    try {
      inpCnt = (Integer.parseInt(props.getProperty("lastsid")) + 1) & 0x0f;
    } catch (Exception e) {
      // Keep default
    }
    System.out.println("Next command for FE will have sessionID " + inpCnt);

    // lastSid
    int lastSid = 0x0f;
    try {
      lastSid = (byte) (Integer.parseInt(props.getProperty("outcnt")) - 1);
      if (lastSid < 0) {
        lastSid = 0x0f;
      }
    } catch (Exception e) {
      // Keep default
    }

    FrontEndStub st = new FrontEndStub(this);
    inpManager = new InputManager(inpCnt, st);

    BackEndSkel sk = startBackEndContainer(props);
    outManager = new OutputManager(lastSid, sk, maxDisconnectionTime);
  }
  /** Create a BackEnd in the fixed network and return a stub to communicate with it */
  public BackEnd getBackEnd(FrontEnd fe, Properties p) throws IMTPException {
    props = p;

    beAddrsText = props.getProperty(FrontEnd.REMOTE_BACK_END_ADDRESSES);
    backEndAddresses = parseBackEndAddresses(beAddrsText);

    // Host
    String host = props.getProperty("host");
    if (host == null) {
      host = "localhost";
    }
    // Port
    int port = JICPProtocol.DEFAULT_PORT;
    try {
      port = Integer.parseInt(props.getProperty("port"));
    } catch (NumberFormatException nfe) {
      // Use default
    }
    // Compose URL. Note that we build a JICPAddress just to avoid loading the HTTPAddress class.
    mediatorTA = JICPProtocol.getInstance().buildAddress(host, String.valueOf(port), null, null);

    // Mediator class
    String tmp = props.getProperty(JICPProtocol.MEDIATOR_CLASS_KEY);
    if (tmp != null) {
      myMediatorClass = tmp;
    } else {
      // Set the default mediator class since this must be propagated to the mediator.
      props.setProperty(JICPProtocol.MEDIATOR_CLASS_KEY, myMediatorClass);
    }

    // Read re-connection retry time
    long retryTime = JICPProtocol.DEFAULT_RETRY_TIME;
    try {
      retryTime = Long.parseLong(props.getProperty(JICPProtocol.RECONNECTION_RETRY_TIME_KEY));
    } catch (Exception e) {
      // Use default
    }

    // Read Max disconnection time
    maxDisconnectionTime = JICPProtocol.DEFAULT_MAX_DISCONNECTION_TIME;
    try {
      maxDisconnectionTime =
          Long.parseLong(props.getProperty(JICPProtocol.MAX_DISCONNECTION_TIME_KEY));
    } catch (Exception e) {
      // Use default
      props.setProperty(
          JICPProtocol.MAX_DISCONNECTION_TIME_KEY, String.valueOf(maxDisconnectionTime));
    }

    // Read Keep-alive time
    keepAliveTime = JICPProtocol.DEFAULT_KEEP_ALIVE_TIME;
    try {
      keepAliveTime = Long.parseLong(props.getProperty(JICPProtocol.KEEP_ALIVE_TIME_KEY));
    } catch (Exception e) {
      // Use default
      props.setProperty(JICPProtocol.KEEP_ALIVE_TIME_KEY, String.valueOf(keepAliveTime));
    }

    if (myLogger.isLoggable(Logger.CONFIG)) {
      myLogger.log(Logger.CONFIG, "Remote URL = http://" + host + ":" + port);
      myLogger.log(Logger.CONFIG, "Mediator class = " + myMediatorClass);
      myLogger.log(Logger.CONFIG, "Reconnection retry time = " + retryTime);
      myLogger.log(Logger.CONFIG, "Max disconnection time = " + maxDisconnectionTime);
      myLogger.log(Logger.CONFIG, "Keep-alive time = " + keepAliveTime);
    }

    myDisconnectionManager = new DisconnectionManager(retryTime, maxDisconnectionTime);
    myKeepAliveManager = new KeepAliveManager(keepAliveTime);
    myInputManager = new InputManager();

    // Read the owner if any
    owner = props.getProperty("owner");

    // Create the BackEnd stub and the FrontEnd skeleton
    myStub = new BackEndStub(this);
    mySkel = new FrontEndSkel(fe);

    // Start the InputManager
    myInputManager.start();

    // Create the remote BackEnd
    createBackEnd();

    return myStub;
  }
  /**
   * Send the CREATE_MEDIATOR command with the necessary parameter in order to create the BackEnd in
   * the fixed network. Executed - at bootstrap time by the thread that creates the
   * FrontEndContainer. - To re-attach to the platform after a fault of the BackEnd
   */
  private synchronized void createBackEnd() throws IMTPException {
    StringBuffer sb = BackEndStub.encodeCreateMediatorRequest(props);
    if (myMediatorID != null) {
      // This is a request to re-create my expired back-end
      BackEndStub.appendProp(sb, JICPProtocol.MEDIATOR_ID_KEY, myMediatorID);
      BackEndStub.appendProp(sb, "outcnt", String.valueOf(outCnt));
      BackEndStub.appendProp(sb, "lastsid", String.valueOf(lastSid));
    }
    JICPPacket pkt =
        new JICPPacket(
            JICPProtocol.CREATE_MEDIATOR_TYPE,
            JICPProtocol.DEFAULT_INFO,
            null,
            sb.toString().getBytes());

    // Try first with the current transport address, then with the various backup addresses
    for (int i = -1; i < backEndAddresses.length; i++) {

      if (i >= 0) {
        // Set the mediator address to a new address..
        String addr = backEndAddresses[i];
        int colonPos = addr.indexOf(':');
        String host = addr.substring(0, colonPos);
        String port = addr.substring(colonPos + 1, addr.length());
        mediatorTA = new JICPAddress(host, port, myMediatorID, "");
      }

      try {
        HTTPClientConnection hc = (HTTPClientConnection) getConnection(mediatorTA);
        myLogger.log(
            Logger.INFO,
            "Creating BackEnd on "
                + hc.getProtocol()
                + mediatorTA.getHost()
                + ":"
                + mediatorTA.getPort());
        pkt = deliver(pkt);

        String replyMsg = new String(pkt.getData());
        if (pkt.getType() != JICPProtocol.ERROR_TYPE) {
          // BackEnd creation successful
          BackEndStub.parseCreateMediatorResponse(replyMsg, props);
          myMediatorID = props.getProperty(JICPProtocol.MEDIATOR_ID_KEY);
          // Complete the mediator address with the mediator ID
          mediatorTA =
              new JICPAddress(mediatorTA.getHost(), mediatorTA.getPort(), myMediatorID, null);
          myDisconnectionManager.setReachable();
          myKeepAliveManager.update();
          myLogger.log(Logger.INFO, "BackEnd OK. Mediator ID is " + myMediatorID);
          return;
        } else {
          myLogger.log(Logger.WARNING, "Mediator error: " + replyMsg);
        }
      } catch (IOException ioe) {
        // Ignore it, and try the next address...
        myLogger.log(Logger.WARNING, "Connection error", ioe);
      }
    }

    // No address succeeded!
    throw new IMTPException("Error creating the BackEnd.");
  }
Beispiel #5
0
 /**
  * Searches for the user defined parameter with the specified key. The method returns <code>null
  * </code> if the parameter is not found.
  *
  * @param key the parameter key.
  * @return the value in this ACLMessage with the specified key value.
  */
 public String getUserDefinedParameter(String key) {
   if (userDefProps == null) return null;
   else return userDefProps.getProperty(key);
 }