Package org.apache.uima.ducc.ws.registry

Source Code of org.apache.uima.ducc.ws.registry.ServicesRegistry

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.uima.ducc.ws.registry;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.uima.ducc.common.persistence.services.IStateServices;
import org.apache.uima.ducc.common.persistence.services.StateServices;
import org.apache.uima.ducc.common.persistence.services.StateServicesDirectory;
import org.apache.uima.ducc.common.persistence.services.StateServicesSet;
import org.apache.uima.ducc.common.utils.DuccLogger;
import org.apache.uima.ducc.common.utils.DuccLoggerComponents;
import org.apache.uima.ducc.common.utils.id.DuccId;
import org.springframework.util.StringUtils;

public class ServicesRegistry {
 
  private static DuccLogger logger = DuccLoggerComponents.getWsLogger(ServicesRegistry.class.getName());
 
  private static ServicesRegistry instance = new ServicesRegistry();

  private ServicesRegistryMap map = new ServicesRegistryMap();
 
  private AtomicBoolean inProgress = new AtomicBoolean(false);
 
  public static ServicesRegistry getInstance() {
    return instance;
  }
 
  private ServicesRegistry() {
    refreshCache();
  }
 
  public void update() {
    String location = "update";
    DuccId jobid = null;
    if(inProgress.compareAndSet(false, true)) {
      try {
        refreshCache();
        logger.debug(location, jobid, "size:"+map.size());
      }   
      catch(Exception e) {
        logger.error(location, jobid, e);
      }
    }
    else {
      logger.warn(location, jobid, "skipping: already in progress...");
    }
    inProgress.set(false);
  }
 
  public void refreshCache() {
    try {
      ServicesRegistryMap mapRevised = new ServicesRegistryMap();
      IStateServices iss = StateServices.getInstance();
      StateServicesDirectory ssd = iss.getStateServicesDirectory();
      if(!ssd.getDescendingKeySet().isEmpty()) {
        for(Integer key : ssd.getDescendingKeySet()) {
          StateServicesSet entry = ssd.get(key);
          Properties propertiesSvc = entry.get(IServicesRegistry.svc);
          Properties propertiesMeta = entry.get(IServicesRegistry.meta);
          ServicesRegistryMapPayload value = new ServicesRegistryMapPayload(propertiesSvc, propertiesMeta);
          mapRevised.put(key, value);
        }
      }
      map = mapRevised;
    }
    catch(IOException e) {
      e.printStackTrace();
    }
  }
 
  public ServicesRegistryMap getMap() {
    return map;
  }
 
  public ServicesRegistryMap getCurrentMap() {
    refreshCache();
    return map;
  }
 
  public String[] getList(String string) {
    String[] retVal = new String[0];
    if(string != null) {
      string = string.trim();
      if(string.length() > 0) {
        retVal = StringUtils.delimitedListToStringArray(string, " ");
      }
    }
    return retVal;
  }
 
  public ArrayList<String> getArrayList(String list) {
    ArrayList<String> retVal = new ArrayList<String>();
    for(String string : getList(list)) {
      retVal.add(string);
    }
    return retVal;
  }
 
  public ServicesRegistryMapPayload findService(String name) {
    ServicesRegistryMapPayload retVal = null;
    try {
      for(Integer key : map.keySet()) {
        ServicesRegistryMapPayload payload = map.get(key);
        Properties meta = payload.meta;
        if(meta != null) {
          if(meta.containsKey(IServicesRegistry.endpoint)) {
            String endpoint = meta.getProperty(IServicesRegistry.endpoint);
            if(name.equals(endpoint)) {
              retVal = payload;
              break;
            }
          }
        }
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
 
  public String findServiceUser(String id) {
    String retVal = null;
    try {
      for(Integer key : map.keySet()) {
        ServicesRegistryMapPayload payload = map.get(key);
        Properties meta = payload.meta;
        if(meta != null) {
          if(meta.containsKey(IServicesRegistry.numeric_id)) {
            String sid = meta.getProperty(IServicesRegistry.numeric_id);
            if(id.equals(sid)) {
              retVal = meta.getProperty(IServicesRegistry.user).trim();
              break;
            }
          }
        }
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
 
  public String findServiceName(DuccId duccId) {
    String retVal = null;
    try {
      long id = duccId.getFriendly();
      for(Integer key : map.keySet()) {
        ServicesRegistryMapPayload payload = map.get(key);
        Properties meta = payload.meta;
        if(meta != null) {
          String implementors = meta.getProperty(IServicesRegistry.implementors);
          String[] list = getList(implementors);
          for( String member : list ) {
            if(member.equals(id+"")) {
              if(meta.containsKey(IServicesRegistry.endpoint)) {
                retVal = meta.getProperty(IServicesRegistry.endpoint);
              }
              break;
            }
          }
        }
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
 
  public String getServiceState(String name) {
    String retVal = IServicesRegistry.constant_NotKnown;
    try {
      ServicesRegistryMapPayload payload = findService(name);
      Properties properties = payload.meta;
      String service_state = properties.getProperty(IServicesRegistry.service_state).trim();
      if(service_state.equalsIgnoreCase(IServicesRegistry.constant_Available)) {
        String ping_active = properties.getProperty(IServicesRegistry.ping_active).trim();
        if(ping_active.equalsIgnoreCase(IServicesRegistry.constant_true)) {
          String service_healthy = properties.getProperty(IServicesRegistry.service_healthy).trim();
          if(service_healthy.equalsIgnoreCase(IServicesRegistry.constant_true)) {
            retVal = IServicesRegistry.constant_OK;
          }
          else {
            retVal = IServicesRegistry.constant_NotHealthy;
          }
        }
        else {
          retVal = IServicesRegistry.constant_NotPinging;
        }
      }
      else {
        retVal = IServicesRegistry.constant_NotAvailable;
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    return retVal;
  }
}
TOP

Related Classes of org.apache.uima.ducc.ws.registry.ServicesRegistry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.