VinciFrame resolveLocal()

in jVinci/src/main/java/org/apache/vinci/transport/vns/service/VNS.java [586:711]


  VinciFrame resolveLocal(VinciFrame in) {

    Debug.p("Local resolve");

    VinciFrame out = new VinciFrame();

    String name = in.fgetString("SERVICE");
    if (strip(name) == null)
      return new ErrorFrame("No service name specified");

    String host = strip(in.fgetString("HOST"));
    String realhost = strip(in.fgetString("IP"));
    if (strip(realhost) == null && strip(host) != null) {
      try {
        // Try to resolve the IP if possible
        realhost = (InetAddress.getByName(host)).getHostAddress();
      } catch (Exception e) {
        // realhost = null;

        // return error message
        return new ErrorFrame("Could not resolve IP address for service " + name);
      }
    }

    String instance = in.fgetString("INSTANCE");
    int inst = -1;
    try {
      inst = Integer.parseInt(instance);
    } catch (Exception e) {
      inst = -1;
    }

    String level = in.fgetString("LEVEL");

    // Default as specified in SPEC.txt
    if (strip(level) == null || level.trim().toLowerCase().equals("none"))
      level = "-1";
    else if (level.toLowerCase().equals("all")) {
      Debug.p("Specific level must be given (not all)");
      out.fadd("vinci:ERROR", "Specific level must be given (not all)");
      return out;
    }

    synchronized (SR) {
      // Construct a valid number from the level string specified
      level = "" + SR.getLevel(name, level);
    }
    Service[] servicesList;

    // Check the cache for matches
    CachedItem citem = (CachedItem) checkCache(name + "[" + level + "]");
    if (citem == null) {
      synchronized (SR) {
        // Find all services that match the specified name and are <= level specified
        // Also, resolve all aliases to actual services
        servicesList = SR.getServices(name, level, true);
      }
      // Cache the result (if any matches found)
      if (servicesList != null && servicesList.length > 0) {
        // Cache the resolve result under the actual name and level returned
        cache(name + "[" + servicesList[0].level + "]", new CachedItem(servicesList));
        // Have a proxy pointer to the entry created if it is not the same
        if (!servicesList[0].level.equals(level)) {
          cache(name + "[" + level + "]",
                  new ProxyCachedItem(name + "[" + servicesList[0].level + "]"));
        }
      }
    } else
      servicesList = citem.servicesList;

    if (servicesList == null) {
      Debug.p("Service " + name + " not found");
      out.fadd("vinci:ERROR", "Service " + name + " not found");
      return out;
    }

    Debug.p("Number of services found with name = " + name + ", and level = " + level + " : "
            + servicesList.length);

    if (servicesList.length == 0)
      System.err.println("NO SERVICES FOUND WITH REALHOST = " + realhost + " name = " + name);

    ArrayList v = new ArrayList();

    // Filter the services with matching realhost and instance
    for (int i = 0; i < servicesList.length; i++) {
      if (realhost != null && !servicesList[i].realhost.equals(realhost))
        continue;
      if (inst > -1 && inst != servicesList[i].instance)
        continue;
      v.add(servicesList[i]);
    }

    servicesList = new Service[v.size()];

    for (int i = 0; i < v.size(); i++)
      servicesList[i] = (Service) v.get(i);

    if (servicesList == null || servicesList.length == 0) {
      Debug.p("Service " + name + " not found");
      out.fadd("vinci:ERROR", "Service " + name + " not found");
      return out;
    }

    VinciFrame temp;
    for (int j = 0; j < servicesList.length; j++) {
      temp = new VinciFrame();

      temp.fadd("HOST", (String) servicesList[j].getAttr("host"));
      temp.fadd("PORT", "" + servicesList[j].port);
      temp.fadd("INSTANCE", "" + servicesList[j].instance);

      if (servicesList[j].meta) {
        Object o = servicesList[j].dict.get("META");
        if (o instanceof String)
          temp.fadd("META", (String) o);
        if (o instanceof Frame)
          temp.fadd("META", (Frame) o);
      }

      out.fadd("SERVER", temp);
    }

    out.fadd("LEVEL", servicesList[0].level); // should all be the same
    return out;
  }