Пример #1
0
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Please specify the path to a properties file with your amazon keys.");
      return;
    }

    CloudWatchMetricObserver observer =
        new CloudWatchMetricObserver(
            "SampleObserver", "SampleDomain", new PropertiesCredentials(new File(args[0])));

    List<Tag> tags = new ArrayList<Tag>(2);
    tags.add(InjectableTag.HOSTNAME);
    tags.add(InjectableTag.IP);

    BasicExample example = new BasicExample(tags);

    DefaultMonitorRegistry.getInstance().register(Monitors.newObjectMonitor(example));

    PollCallable poller =
        new PollCallable(new MonitorRegistryMetricPoller(), BasicMetricFilter.MATCH_ALL);

    while (true) {
      example.counter.incrementAndGet();
      example.setSampleGauge(Math.round(Math.random() * 1000));
      observer.update(poller.call());
      Thread.sleep(60000);
    }
  }
  /** Set necessary parameters from client configuration and register with Servo monitors. */
  @Override
  public void initWithNiwsConfig(IClientConfig clientConfig) {
    if (clientConfig == null) {
      return;
    }
    vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses();
    clientName = clientConfig.getClientName();
    if (clientName == null) {
      clientName = "default";
    }
    try {
      maxAutoRetries =
          Integer.parseInt(
              clientConfig
                  .getProperty(CommonClientConfigKey.MaxAutoRetries, maxAutoRetries)
                  .toString());
    } catch (Exception e) {
      logger.warn("Invalid maxRetries set for client:" + clientName);
    }
    try {
      maxAutoRetriesNextServer =
          Integer.parseInt(
              clientConfig
                  .getProperty(
                      CommonClientConfigKey.MaxAutoRetriesNextServer, maxAutoRetriesNextServer)
                  .toString());
    } catch (Exception e) {
      logger.warn("Invalid maxRetriesNextServer set for client:" + clientName);
    }

    try {
      Boolean bOkToRetryOnAllOperations =
          Boolean.valueOf(
              clientConfig
                  .getProperty(
                      CommonClientConfigKey.OkToRetryOnAllOperations, okToRetryOnAllOperations)
                  .toString());
      okToRetryOnAllOperations = bOkToRetryOnAllOperations.booleanValue();
    } catch (Exception e) {
      logger.warn("Invalid OkToRetryOnAllOperations set for client:" + clientName);
    }
    tracer = Monitors.newTimer(clientName + "_OperationTimer", TimeUnit.MILLISECONDS);
    Monitors.registerObject("Client_" + clientName, this);
  }
 /**
  * Create a Netflix {@link RestClient} integrated with Ribbon if none already exists in the
  * application context. It is not required for Ribbon to work properly and is therefore created
  * lazily if ever another component requires it.
  *
  * @param config the configuration to use by the underlying Ribbon instance
  * @param loadBalancer the load balancer to use by the underlying Ribbon instance
  * @return a {@link RestClient} instances backed by Ribbon
  */
 @Bean
 @Lazy
 @ConditionalOnMissingBean
 public RestClient ribbonRestClient(
     IClientConfig config, ILoadBalancer loadBalancer, ServerIntrospector serverIntrospector) {
   RestClient client = new OverrideRestClient(config, serverIntrospector);
   client.setLoadBalancer(loadBalancer);
   Monitors.registerObject("Client_" + this.name, client);
   return client;
 }
 private ConnectionCleanerTask(int connectionIdleTimeout) {
   this.connectionIdleTimeout = connectionIdleTimeout;
   MonitorConfig.Builder monitorConfigBuilder =
       MonitorConfig.builder("Eureka-Connection-Cleaner-Time");
   executionTimeStats = new BasicTimer(monitorConfigBuilder.build());
   cleanupFailed =
       new BasicCounter(MonitorConfig.builder("Eureka-Connection-Cleaner-Failure").build());
   try {
     Monitors.registerObject(this);
   } catch (Exception e) {
     s_logger.error("Unable to register with servo.", e);
   }
 }
Пример #5
0
 @Inject
 public EIPManager(
     EurekaServerConfig serverConfig,
     EurekaClientConfig clientConfig,
     PeerAwareInstanceRegistry registry,
     ApplicationInfoManager applicationInfoManager) {
   this.serverConfig = serverConfig;
   this.clientConfig = clientConfig;
   this.registry = registry;
   this.applicationInfoManager = applicationInfoManager;
   try {
     Monitors.registerObject(this);
   } catch (Throwable e) {
     logger.warn("Cannot register the JMX monitor for the InstanceRegistry", e);
   }
 }
Пример #6
0
  /**
   * Utility method to create client and load balancer (if enabled in client config) given the name
   * and client config. Instances are created using reflection (see {@link
   * #instantiateInstanceWithClientConfig(String, IClientConfig)}
   *
   * @param restClientName
   * @param clientConfig
   * @throws ClientException if any errors occurs in the process, or if the client with the same
   *     name already exists
   */
  public static synchronized IClient<?, ?> registerClientFromProperties(
      String restClientName, IClientConfig clientConfig) throws ClientException {
    IClient<?, ?> client = null;
    ILoadBalancer loadBalancer = null;
    if (simpleClientMap.get(restClientName) != null) {
      throw new ClientException(
          ClientException.ErrorType.GENERAL,
          "A Rest Client with this name is already registered. Please use a different name");
    }
    try {
      String clientClassName =
          (String) clientConfig.getProperty(CommonClientConfigKey.ClientClassName);
      client = (IClient<?, ?>) instantiateInstanceWithClientConfig(clientClassName, clientConfig);
      boolean initializeNFLoadBalancer =
          Boolean.parseBoolean(
              clientConfig
                  .getProperty(
                      CommonClientConfigKey.InitializeNFLoadBalancer,
                      DefaultClientConfigImpl.DEFAULT_ENABLE_LOADBALANCER)
                  .toString());
      if (initializeNFLoadBalancer) {
        loadBalancer = registerNamedLoadBalancerFromclientConfig(restClientName, clientConfig);
      }
      if (client instanceof AbstractLoadBalancerAwareClient) {
        ((AbstractLoadBalancerAwareClient) client).setLoadBalancer(loadBalancer);
      }
    } catch (Throwable e) {
      String message =
          "Unable to InitializeAndAssociateNFLoadBalancer set for RestClient:" + restClientName;
      logger.warn(message, e);
      throw new ClientException(ClientException.ErrorType.CONFIGURATION, message, e);
    }
    simpleClientMap.put(restClientName, client);

    Monitors.registerObject("Client_" + restClientName, client);

    logger.info("Client Registered:" + client.toString());
    return client;
  }
Пример #7
0
 @Inject
 public ThreadPoolStatsMonitor(IConfiguration config) {
   super(config);
   tpStatsReporter = new Elasticsearch_ThreadPoolStatsReporter();
   Monitors.registerObject(tpStatsReporter);
 }
  /**
   * Execute the request on single server after the final URI is calculated. This method takes care
   * of retries and update server stats.
   */
  protected T executeOnSingleServer(S request) throws ClientException {
    boolean done = false;
    int retries = 0;

    boolean retryOkayOnOperation = okToRetryOnAllOperations;
    if (request.isRetriable()) {
      retryOkayOnOperation = true;
    }
    int numRetries = maxAutoRetries;
    URI uri = request.getUri();
    Server server = new Server(uri.getHost(), uri.getPort());
    ServerStats serverStats = null;
    ILoadBalancer lb = this.getLoadBalancer();
    if (lb instanceof AbstractLoadBalancer) {
      LoadBalancerStats lbStats = ((AbstractLoadBalancer) lb).getLoadBalancerStats();
      serverStats = lbStats.getSingleServerStat(server);
    }
    IClientConfig overriddenClientConfig = request.getOverrideConfig();
    if (overriddenClientConfig != null) {
      try {
        numRetries =
            Integer.parseInt(
                ""
                    + overriddenClientConfig.getProperty(
                        CommonClientConfigKey.MaxAutoRetries, maxAutoRetries));
      } catch (Exception e) {
        logger.warn("Invalid maxRetries requested for RestClient:" + this.clientName);
      }
    }

    T response = null;
    Exception lastException = null;
    if (tracer == null) {
      tracer =
          Monitors.newTimer(this.getClass().getName() + "_ExecutionTimer", TimeUnit.MILLISECONDS);
    }
    do {
      noteOpenConnection(serverStats, request);
      Stopwatch w = tracer.start();
      try {
        response = execute(request);
        done = true;
      } catch (Exception e) {
        if (serverStats != null) {
          serverStats.addToFailureCount();
        }
        lastException = e;
        if (isCircuitBreakerException(e) && serverStats != null) {
          serverStats.incrementSuccessiveConnectionFailureCount();
        }
        boolean shouldRetry = retryOkayOnOperation && numRetries >= 0 && isRetriableException(e);
        if (shouldRetry) {
          retries = handleRetry(uri.toString(), retries, numRetries, e);
        } else {
          ClientException niwsClientException = generateNIWSException(uri.toString(), e);
          throw niwsClientException;
        }
      } finally {
        w.stop();
        noteRequestCompletion(
            serverStats, request, response, lastException, w.getDuration(TimeUnit.MILLISECONDS));
      }
    } while (!done);
    return response;
  }
Пример #9
0
/**
 * The custom {@link com.netflix.discovery.provider.Serializer} for serializing and deserializing
 * the registry information from and to the eureka server.
 *
 * <p>The {@link com.netflix.discovery.provider.Serializer} used here is an <tt>Xstream</tt>
 * serializer which uses the <tt>JSON</tt> format and custom fields.The XStream deserialization does
 * not handle removal of fields and hence this custom mechanism. Since then {@link Auto} annotation
 * introduced handles any fields that does not exist gracefully and is the recommended mechanism. If
 * the user wishes to override the whole XStream serialization/deserialization mechanism with their
 * own alternatives they can do so my implementing their own providers in {@link
 * EntityBodyConverter}.
 *
 * @author Karthik Ranganathan, Greg Kim
 */
public final class Converters {
  private static final String UNMARSHAL_ERROR = "UNMARSHAL_ERROR";
  public static final String NODE_LEASE = "leaseInfo";
  public static final String NODE_METADATA = "metadata";
  public static final String NODE_DATACENTER = "dataCenterInfo";
  public static final String NODE_INSTANCE = "instance";
  public static final String NODE_APP = "application";

  private static final Logger logger = LoggerFactory.getLogger(Converters.class);

  private static final Counter UNMARSHALL_ERROR_COUNTER = Monitors.newCounter(UNMARSHAL_ERROR);

  /** Serialize/deserialize {@link Applications} object types. */
  public static class ApplicationsConverter implements Converter {

    private static final String VERSIONS_DELTA = "versions_delta";
    private static final String APPS_HASHCODE = "apps_hashcode";

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
      return Applications.class == clazz;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      Applications apps = (Applications) source;
      writer.startNode(VERSIONS_DELTA);
      writer.setValue(apps.getVersion().toString());
      writer.endNode();
      writer.startNode(APPS_HASHCODE);
      writer.setValue(apps.getAppsHashCode());
      writer.endNode();
      for (Application app : apps.getRegisteredApplications()) {
        writer.startNode(NODE_APP);
        context.convertAnother(app);
        writer.endNode();
      }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      Applications apps = new Applications();
      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String nodeName = reader.getNodeName();

        if (NODE_APP.equals(nodeName)) {
          apps.addApplication((Application) context.convertAnother(apps, Application.class));
        } else if (VERSIONS_DELTA.equals(nodeName)) {
          apps.setVersion(Long.valueOf(reader.getValue()));
        } else if (APPS_HASHCODE.equals(nodeName)) {
          apps.setAppsHashCode(reader.getValue());
        }
        reader.moveUp();
      }
      return apps;
    }
  }

  /** Serialize/deserialize {@link Applications} object types. */
  public static class ApplicationConverter implements Converter {

    private static final String ELEM_NAME = "name";

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
      return Application.class == clazz;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      Application app = (Application) source;

      writer.startNode(ELEM_NAME);
      writer.setValue(app.getName());
      writer.endNode();

      for (InstanceInfo instanceInfo : app.getInstances()) {
        writer.startNode(NODE_INSTANCE);
        context.convertAnother(instanceInfo);
        writer.endNode();
      }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      Application app = new Application();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String nodeName = reader.getNodeName();

        if (ELEM_NAME.equals(nodeName)) {
          app.setName(reader.getValue());
        } else if (NODE_INSTANCE.equals(nodeName)) {
          app.addInstance((InstanceInfo) context.convertAnother(app, InstanceInfo.class));
        }
        reader.moveUp();
      }
      return app;
    }
  }

  /** Serialize/deserialize {@link InstanceInfo} object types. */
  public static class InstanceInfoConverter implements Converter {

    private static final String ELEM_OVERRIDDEN_STATUS = "overriddenstatus";
    private static final String ELEM_HOST = "hostName";
    private static final String ELEM_INSTANCE_ID = "instanceId";
    private static final String ELEM_APP = "app";
    private static final String ELEM_IP = "ipAddr";
    private static final String ELEM_SID = "sid";
    private static final String ELEM_STATUS = "status";
    private static final String ELEM_PORT = "port";
    private static final String ELEM_SECURE_PORT = "securePort";
    private static final String ELEM_COUNTRY_ID = "countryId";
    private static final String ELEM_IDENTIFYING_ATTR = "identifyingAttribute";
    private static final String ATTR_ENABLED = "enabled";

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
      return InstanceInfo.class == clazz;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      InstanceInfo info = (InstanceInfo) source;

      if (info.getInstanceId() != null) {
        writer.startNode(ELEM_INSTANCE_ID);
        writer.setValue(info.getInstanceId());
        writer.endNode();
      }

      writer.startNode(ELEM_HOST);
      writer.setValue(info.getHostName());
      writer.endNode();

      writer.startNode(ELEM_APP);
      writer.setValue(info.getAppName());
      writer.endNode();

      writer.startNode(ELEM_IP);
      writer.setValue(info.getIPAddr());
      writer.endNode();

      if (!("unknown".equals(info.getSID()) || "na".equals(info.getSID()))) {
        writer.startNode(ELEM_SID);
        writer.setValue(info.getSID());
        writer.endNode();
      }

      writer.startNode(ELEM_STATUS);
      writer.setValue(getStatus(info));
      writer.endNode();

      writer.startNode(ELEM_OVERRIDDEN_STATUS);
      writer.setValue(info.getOverriddenStatus().name());
      writer.endNode();

      writer.startNode(ELEM_PORT);
      writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.UNSECURE)));
      writer.setValue(String.valueOf(info.getPort()));
      writer.endNode();

      writer.startNode(ELEM_SECURE_PORT);
      writer.addAttribute(ATTR_ENABLED, String.valueOf(info.isPortEnabled(PortType.SECURE)));
      writer.setValue(String.valueOf(info.getSecurePort()));
      writer.endNode();

      writer.startNode(ELEM_COUNTRY_ID);
      writer.setValue(String.valueOf(info.getCountryId()));
      writer.endNode();

      if (info.getDataCenterInfo() != null) {
        writer.startNode(NODE_DATACENTER);
        // This is needed for backward compat. for now.
        if (info.getDataCenterInfo().getName() == Name.Amazon) {
          writer.addAttribute("class", "com.netflix.appinfo.AmazonInfo");
        } else {
          writer.addAttribute("class", "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo");
        }
        context.convertAnother(info.getDataCenterInfo());
        writer.endNode();
      }

      if (info.getLeaseInfo() != null) {
        writer.startNode(NODE_LEASE);
        context.convertAnother(info.getLeaseInfo());
        writer.endNode();
      }

      if (info.getMetadata() != null) {
        writer.startNode(NODE_METADATA);
        // for backward compat. for now
        if (info.getMetadata().size() == 0) {
          writer.addAttribute("class", "java.util.Collections$EmptyMap");
        }
        context.convertAnother(info.getMetadata());
        writer.endNode();
      }
      autoMarshalEligible(source, writer);
    }

    public String getStatus(InstanceInfo info) {
      return info.getStatus().name();
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String nodeName = reader.getNodeName();

        if (ELEM_HOST.equals(nodeName)) {
          builder.setHostName(reader.getValue());
        } else if (ELEM_INSTANCE_ID.equals(nodeName)) {
          builder.setInstanceId(reader.getValue());
        } else if (ELEM_APP.equals(nodeName)) {
          builder.setAppName(reader.getValue());
        } else if (ELEM_IP.equals(nodeName)) {
          builder.setIPAddr(reader.getValue());
        } else if (ELEM_SID.equals(nodeName)) {
          builder.setSID(reader.getValue());
        } else if (ELEM_IDENTIFYING_ATTR.equals(nodeName)) {
          // nothing;
        } else if (ELEM_STATUS.equals(nodeName)) {
          builder.setStatus(InstanceStatus.toEnum(reader.getValue()));
        } else if (ELEM_OVERRIDDEN_STATUS.equals(nodeName)) {
          builder.setOverriddenStatus(InstanceStatus.toEnum(reader.getValue()));
        } else if (ELEM_PORT.equals(nodeName)) {
          builder.setPort(Integer.valueOf(reader.getValue()).intValue());
          // Defaults to true
          builder.enablePort(PortType.UNSECURE, !"false".equals(reader.getAttribute(ATTR_ENABLED)));
        } else if (ELEM_SECURE_PORT.equals(nodeName)) {
          builder.setSecurePort(Integer.valueOf(reader.getValue()).intValue());
          // Defaults to false
          builder.enablePort(PortType.SECURE, "true".equals(reader.getAttribute(ATTR_ENABLED)));
        } else if (ELEM_COUNTRY_ID.equals(nodeName)) {
          builder.setCountryId(Integer.valueOf(reader.getValue()).intValue());
        } else if (NODE_DATACENTER.equals(nodeName)) {
          builder.setDataCenterInfo(
              (DataCenterInfo) context.convertAnother(builder, DataCenterInfo.class));
        } else if (NODE_LEASE.equals(nodeName)) {
          builder.setLeaseInfo((LeaseInfo) context.convertAnother(builder, LeaseInfo.class));
        } else if (NODE_METADATA.equals(nodeName)) {
          builder.setMetadata((Map<String, String>) context.convertAnother(builder, Map.class));
        } else {
          autoUnmarshalEligible(reader, builder.getRawInstance());
        }

        reader.moveUp();
      }

      return builder.build();
    }
  }

  /** Serialize/deserialize {@link DataCenterInfo} object types. */
  public static class DataCenterInfoConverter implements Converter {

    private static final String ELEM_NAME = "name";

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
      return DataCenterInfo.class.isAssignableFrom(clazz);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      DataCenterInfo info = (DataCenterInfo) source;

      writer.startNode(ELEM_NAME);
      // For backward compat. for now
      writer.setValue(info.getName().name());
      writer.endNode();

      if (info.getName() == Name.Amazon) {
        AmazonInfo aInfo = (AmazonInfo) info;
        writer.startNode(NODE_METADATA);
        // for backward compat. for now
        if (aInfo.getMetadata().size() == 0) {
          writer.addAttribute("class", "java.util.Collections$EmptyMap");
        }
        context.convertAnother(aInfo.getMetadata());
        writer.endNode();
      }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      DataCenterInfo info = null;
      while (reader.hasMoreChildren()) {
        reader.moveDown();

        if (ELEM_NAME.equals(reader.getNodeName())) {
          final String dataCenterName = reader.getValue();
          if (DataCenterInfo.Name.Amazon.name().equalsIgnoreCase(dataCenterName)) {
            info = new AmazonInfo();
          } else {
            final DataCenterInfo.Name name = DataCenterInfo.Name.valueOf(dataCenterName);
            info =
                new DataCenterInfo() {

                  @Override
                  public Name getName() {
                    return name;
                  }
                };
          }
        } else if (NODE_METADATA.equals(reader.getNodeName())) {
          if (info.getName() == Name.Amazon) {
            Map<String, String> metadataMap =
                (Map<String, String>) context.convertAnother(info, Map.class);
            Map<String, String> metadataMapInter = new HashMap<String, String>(metadataMap.size());
            for (Map.Entry<String, String> entry : metadataMap.entrySet()) {
              metadataMapInter.put(
                  StringCache.intern(entry.getKey()), StringCache.intern(entry.getValue()));
            }
            ((AmazonInfo) info).setMetadata(metadataMapInter);
          }
        }

        reader.moveUp();
      }
      return info;
    }
  }

  /** Serialize/deserialize {@link LeaseInfo} object types. */
  public static class LeaseInfoConverter implements Converter {

    private static final String ELEM_RENEW_INT = "renewalIntervalInSecs";
    private static final String ELEM_DURATION = "durationInSecs";
    private static final String ELEM_REG_TIMESTAMP = "registrationTimestamp";
    private static final String ELEM_LAST_RENEW_TIMETSTAMP = "lastRenewalTimestamp";
    private static final String ELEM_EVICTION_TIMESTAMP = "evictionTimestamp";
    private static final String ELEM_SERVICE_UP_TIMESTAMP = "serviceUpTimestamp";

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert(Class clazz) {
      return LeaseInfo.class == clazz;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      LeaseInfo info = (LeaseInfo) source;

      writer.startNode(ELEM_RENEW_INT);
      writer.setValue(String.valueOf(info.getRenewalIntervalInSecs()));
      writer.endNode();

      writer.startNode(ELEM_DURATION);
      writer.setValue(String.valueOf(info.getDurationInSecs()));
      writer.endNode();

      writer.startNode(ELEM_REG_TIMESTAMP);
      writer.setValue(String.valueOf(info.getRegistrationTimestamp()));
      writer.endNode();

      writer.startNode(ELEM_LAST_RENEW_TIMETSTAMP);
      writer.setValue(String.valueOf(info.getRenewalTimestamp()));
      writer.endNode();

      writer.startNode(ELEM_EVICTION_TIMESTAMP);
      writer.setValue(String.valueOf(info.getEvictionTimestamp()));
      writer.endNode();

      writer.startNode(ELEM_SERVICE_UP_TIMESTAMP);
      writer.setValue(String.valueOf(info.getServiceUpTimestamp()));
      writer.endNode();
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      LeaseInfo.Builder builder = LeaseInfo.Builder.newBuilder();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String nodeName = reader.getNodeName();
        String nodeValue = reader.getValue();
        if (nodeValue == null) {
          continue;
        }

        long longValue = 0;
        try {
          longValue = Long.valueOf(nodeValue).longValue();
        } catch (NumberFormatException ne) {
          continue;
        }

        if (ELEM_DURATION.equals(nodeName)) {
          builder.setDurationInSecs((int) longValue);
        } else if (ELEM_EVICTION_TIMESTAMP.equals(nodeName)) {
          builder.setEvictionTimestamp(longValue);
        } else if (ELEM_LAST_RENEW_TIMETSTAMP.equals(nodeName)) {
          builder.setRenewalTimestamp(longValue);
        } else if (ELEM_REG_TIMESTAMP.equals(nodeName)) {
          builder.setRegistrationTimestamp(longValue);
        } else if (ELEM_RENEW_INT.equals(nodeName)) {
          builder.setRenewalIntervalInSecs((int) longValue);
        } else if (ELEM_SERVICE_UP_TIMESTAMP.equals(nodeName)) {
          builder.setServiceUpTimestamp(longValue);
        }
        reader.moveUp();
      }
      return builder.build();
    }
  }

  /** Serialize/deserialize application metadata. */
  public static class MetadataConverter implements Converter {

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java
     * .lang.Class)
     */
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
      return Map.class.isAssignableFrom(type);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    @SuppressWarnings("unchecked")
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      Map<String, String> map = (Map<String, String>) source;

      for (Iterator<Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext(); ) {
        Entry<String, String> entry = iter.next();

        writer.startNode(entry.getKey());
        writer.setValue(entry.getValue());
        writer.endNode();
      }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      return unmarshalMap(reader, context);
    }

    private Map<String, String> unmarshalMap(
        HierarchicalStreamReader reader, UnmarshallingContext context) {

      Map<String, String> map = Collections.emptyMap();

      while (reader.hasMoreChildren()) {
        if (map == Collections.EMPTY_MAP) {
          map = new HashMap<String, String>();
        }
        reader.moveDown();
        String key = reader.getNodeName();
        String value = reader.getValue();
        reader.moveUp();

        map.put(StringCache.intern(key), value);
      }
      return map;
    }
  }

  /**
   * Marshal all the objects containing an {@link Auto} annotation automatically.
   *
   * @param o - The object's fields that needs to be marshalled.
   * @param writer - The writer for which to write the information to.
   */
  private static void autoMarshalEligible(Object o, HierarchicalStreamWriter writer) {
    try {
      Class c = o.getClass();
      Field[] fields = c.getDeclaredFields();
      Annotation annotation = null;
      for (Field f : fields) {
        annotation = f.getAnnotation(Auto.class);
        if (annotation != null) {
          f.setAccessible(true);
          if (f.get(o) != null) {
            writer.startNode(f.getName());
            writer.setValue(String.valueOf(f.get(o)));
            writer.endNode();
          }
        }
      }
    } catch (Throwable th) {
      logger.error("Error in marshalling the object", th);
    }
  }

  /**
   * Unmarshal all the elements to their field values if the fields have the {@link Auto} annotation
   * defined.
   *
   * @param reader - The reader where the elements can be read.
   * @param o - The object for which the value of fields need to be populated.
   */
  private static void autoUnmarshalEligible(HierarchicalStreamReader reader, Object o) {
    try {
      String nodeName = reader.getNodeName();
      Class c = o.getClass();
      Field f = null;
      try {
        f = c.getDeclaredField(nodeName);
      } catch (NoSuchFieldException e) {
        UNMARSHALL_ERROR_COUNTER.increment();
      }
      if (f == null) {
        return;
      }
      Annotation annotation = f.getAnnotation(Auto.class);
      if (annotation == null) {
        return;
      }
      f.setAccessible(true);

      String value = reader.getValue();
      Class returnClass = f.getType();
      if (value != null) {
        if (!String.class.equals(returnClass)) {

          Method method = returnClass.getDeclaredMethod("valueOf", java.lang.String.class);
          Object valueObject = method.invoke(returnClass, value);
          f.set(o, valueObject);
        } else {
          f.set(o, value);
        }
      }
    } catch (Throwable th) {
      logger.error("Error in unmarshalling the object:", th);
    }
  }
}
Пример #10
0
 /** Unregister all statistics from <tt>Servo</tt>. */
 public static void shutdown() {
   for (EurekaMonitors c : EurekaMonitors.values()) {
     DefaultMonitorRegistry.getInstance().unregister(Monitors.newObjectMonitor(c.name(), c));
   }
 }
Пример #11
0
 /** Register all statistics with <tt>Servo</tt>. */
 public static void registerAllStats() {
   for (EurekaMonitors c : EurekaMonitors.values()) {
     Monitors.registerObject(c.getName(), c);
   }
 }