Esempio n. 1
0
  /**
   * This checks the configuration file, and attempts to register to the IndexService if
   * shouldPerformRegistration==true. It will first read the current container URL, and compare it
   * against the saved value. If the value exists, it will only try to reregister if the values are
   * different. This exists to handle fixing the registration URL which may be incorrect during
   * initialization, then later corrected during invocation. The existence of baseURL does not imply
   * successful registration (a non-null registrationTimer does). We will only attempt to reregister
   * when the URL changes (to prevent attempting registration with each invocation if there is a
   * configuration problem).
   */
  public void refreshRegistration(boolean forceRefresh) {
    if (getConfiguration().shouldPerformRegistration()) {

      URL currentContainerURL = null;
      try {
        currentContainerURL = ServiceHost.getBaseURL();
      } catch (IOException e) {
        logger.error("Unable to determine container's URL!  Skipping registration.");
        return;
      }

      if (this.baseURL != null) {
        // we've tried to register before (or we are being forced to
        // retry)
        // do a string comparison as we don't want to do DNS lookups
        // for comparison
        // if (forceRefresh ||
        // !this.baseURL.toExternalForm().equals(currentContainerURL.toExternalForm())) {
        if (forceRefresh || !this.baseURL.equals(currentContainerURL)) {
          // we've tried to register before, and we have a different
          // URL now.. so cancel the old registration (if it exists),
          // and try to redo it.
          if (registrationTimer != null) {
            registrationTimer.cancel();
          }

          // save the new value
          this.baseURL = currentContainerURL;
          logger.info("Refreshing existing registration [container URL=" + this.baseURL + "].");
        } else {
          // URLs are the same (and we weren't forced), so don't try
          // to reregister
          return;
        }

      } else {
        // we've never saved the baseURL (and therefore haven't tried to
        // register)
        this.baseURL = currentContainerURL;
        logger.info(
            "Attempting registration for the first time[container URL=" + this.baseURL + "].");
      }

      // register with the index service
      ResourceContext ctx;
      try {
        MessageContext msgContext = MessageContext.getCurrentContext();
        if (msgContext == null) {
          logger.error("Unable to determine message context!");
          return;
        }

        ctx = ResourceContext.getResourceContext(msgContext);
      } catch (ResourceContextException e) {
        logger.error("Could not get ResourceContext: " + e);
        return;
      }

      EndpointReferenceType epr;
      try {
        // since this is a singleton, pretty sure we dont't want to
        // register the key (allows multiple instances of same service
        // on successive restarts)
        // epr = AddressingUtils.createEndpointReference(ctx, key);
        epr = AddressingUtils.createEndpointReference(ctx, null);
      } catch (Exception e) {
        logger.error("Could not form EPR: " + e);
        return;
      }
      try {
        // This is how registration parameters are set (read from
        // template)
        File registrationFile =
            new File(
                ContainerConfig.getBaseDirectory()
                    + File.separator
                    + getConfiguration().getRegistrationTemplateFile());

        if (registrationFile.exists() && registrationFile.canRead()) {
          logger.debug("Loading registration argumentsrmation from:" + registrationFile);

          ServiceGroupRegistrationParameters params =
              ServiceGroupRegistrationClient.readParams(registrationFile.getAbsolutePath());
          // set our service's EPR as the registrant
          params.setRegistrantEPR(epr);

          ServiceGroupRegistrationClient client = new ServiceGroupRegistrationClient();
          // apply the registration params to the client
          registrationTimer = client.register(params);
        } else {
          logger.error("Unable to read registration file:" + registrationFile);
        }
      } catch (Exception e) {
        logger.error("Exception when trying to register service (" + epr + "): " + e);
      }
    } else {
      logger.info("Skipping registration.");
    }
  }
  /**
   * This checks the configuration file, and attempts to register to the IndexService if
   * shouldPerformRegistration==true. It will first read the current container URL, and compare it
   * against the saved value. If the value exists, it will only try to reregister if the values are
   * different. This exists to handle fixing the registration URL which may be incorrect during
   * initialization, then later corrected during invocation. The existence of baseURL does not imply
   * successful registration (a non-null registrationClient does). We will only attempt to
   * reregister when the URL changes (to prevent attempting registration with each invocation if
   * there is a configuration problem).
   */
  public void refreshRegistration(boolean forceRefresh) {
    if (getConfiguration().shouldPerformRegistration()) {

      // first check to see if there are any resource properties that
      // require registration
      ResourceContext ctx;
      try {
        MessageContext msgContext = MessageContext.getCurrentContext();
        if (msgContext == null) {
          logger.error("Unable to determine message context!");
          return;
        }

        ctx = ResourceContext.getResourceContext(msgContext);
      } catch (ResourceContextException e) {
        logger.error("Could not get ResourceContext: " + e, e);
        return;
      }
      EndpointReferenceType epr;
      try {
        // since this is a singleton, pretty sure we dont't want to
        // register the key (allows multiple instances of same service
        // on successive restarts)
        epr = AddressingUtils.createEndpointReference(ctx, null);
      } catch (Exception e) {
        logger.error("Could not form EPR: " + e, e);
        return;
      }

      ServiceGroupRegistrationParameters params = null;

      File registrationFile =
          new File(
              ContainerConfig.getBaseDirectory()
                  + File.separator
                  + getConfiguration().getRegistrationTemplateFile());

      if (registrationFile.exists() && registrationFile.canRead()) {
        logger.debug("Loading registration argumentsrmation from:" + registrationFile);

        try {
          params = ServiceGroupRegistrationClient.readParams(registrationFile.getAbsolutePath());
        } catch (Exception e) {
          logger.error("Unable to read registration file:" + registrationFile, e);
        }

        // set our service's EPR as the registrant, or use the specified
        // value
        EndpointReferenceType registrantEpr = params.getRegistrantEPR();
        if (registrantEpr == null) {
          params.setRegistrantEPR(epr);
        }

      } else {
        logger.error("Unable to read registration file:" + registrationFile);
      }

      if (params != null) {

        AggregatorContent content = (AggregatorContent) params.getContent();
        AggregatorConfig config = content.getAggregatorConfig();
        MessageElement[] elements = config.get_any();
        GetMultipleResourcePropertiesPollType pollType = null;
        try {
          pollType =
              (GetMultipleResourcePropertiesPollType)
                  ObjectDeserializer.toObject(
                      elements[0], GetMultipleResourcePropertiesPollType.class);
        } catch (DeserializationException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }

        if (pollType != null) {

          // if there are properties names that need to be registered then
          // register them to the index service
          if (pollType.getResourcePropertyNames() != null
              && pollType.getResourcePropertyNames().length != 0) {

            URL currentContainerURL = null;
            try {
              currentContainerURL = ServiceHost.getBaseURL();
            } catch (IOException e) {
              logger.error("Unable to determine container's URL!  Skipping registration.", e);
              return;
            }

            if (this.baseURL != null) {
              // we've tried to register before (or we are being
              // forced to
              // retry)
              // do a string comparison as we don't want to do DNS
              // lookups
              // for comparison
              if (forceRefresh || !this.baseURL.equals(currentContainerURL)) {
                // we've tried to register before, and we have a
                // different
                // URL now.. so cancel the old registration (if
                // it
                // exists),
                // and try to redo it.
                if (registrationClient != null) {
                  try {
                    this.registrationClient.unregister();
                  } catch (UnregistrationException e) {
                    logger.error(
                        "Problem unregistering existing registration:" + e.getMessage(), e);
                  }
                }

                // save the new value
                this.baseURL = currentContainerURL;
                logger.info(
                    "Refreshing existing registration [container URL=" + this.baseURL + "].");
              } else {
                // URLs are the same (and we weren't forced), so
                // don't
                // try
                // to reregister
                return;
              }

            } else {
              // we've never saved the baseURL (and therefore
              // haven't
              // tried to
              // register)
              this.baseURL = currentContainerURL;
              logger.info(
                  "Attempting registration for the first time[container URL="
                      + this.baseURL
                      + "].");
            }

            try {
              // perform the registration for this service
              this.registrationClient = new AdvertisementClient(params);
              this.registrationClient.register();

            } catch (Exception e) {
              logger.error("Exception when trying to register service (" + epr + "): " + e, e);
            }
          } else {
            logger.info("No resource properties to register for service (" + epr + ")");
          }
        } else {
          logger.warn("Registration file deserialized with no poll type (" + epr + ")");
        }
      } else {
        logger.warn("Registration file deserialized with returned null SeviceGroupParams");
      }
    } else {
      logger.info("Skipping registration.");
    }
  }