Package fb.rt

Source Code of fb.rt.FBManager

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name:   FBManager.java

package fb.rt;

import fb.datatype.ANY;
import fb.datatype.FBDataTypeException;
import fb.rt.events.E_RESTART;
import fb.rt.net.FBCommSI;
import fb.xml.FBElement;
import fb.xml.XMLException;
import fb.xml.mgt.*;

import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Vector;

// Referenced classes of package fb.rt:
//            FBInstance, FBRManagementException, EventOutput, Resource

public class FBManager {

  public FBManager(Resource r) {
    state = 0;
    fbs = new Vector(16);
    conns = new Vector(16);
    resource = r;
  }

  public void addFB(FBInstance fb, String fbname) {
    try {
      fb.initialize(fbname, resource);
    } catch (FBRManagementException fbrmanagementexception) {
    }
    fbs.addElement(fb);
  }

  public void connect(FBMConnection cnxn) throws XMLException,
      FBRManagementException {
    String src[] = unpackHierName(cnxn.getSource());
    String dst[] = unpackHierName(cnxn.getDestination());
    FBInstance srcFB = fbNamed(src[0]);
    FBInstance dstFB = fbNamed(dst[0]);
    EventOutput srcEVT = srcFB.eoNamed(src[1]);
    if (srcEVT != null) {
      EventServer dstEVT = dstFB.eiNamed(dst[1]);
      if (dstEVT != null) {
        srcEVT.connectTo(dstEVT);
        conns.addElement(cnxn);
      }
    } else {
      connectVariable(srcFB, src[1], dstFB, dst[1]);
      conns.addElement(cnxn);
    }
  }

  public void connectParameter(FBElement obj) throws FBRManagementException,
      FBDataTypeException, XMLException {
    if (!(obj instanceof FBMConnection)) {
      throw new XMLException("INVALID_OBJECT");
    } else {
      FBMConnection cnxn = (FBMConnection) obj;
      String dst[] = unpackHierName(cnxn.getDestination());
      fbNamed(dst[0]).connectParameter(dst[1], cnxn.getSource());
      return;
    }
  }

  private void connectVariable(FBInstance srcFB, String srcVarName,
      FBInstance dstFB, String dstVarName) throws FBRManagementException {
    ANY srcVar = srcFB.ovNamed(srcVarName);
    ANY dstVar = dstFB.ivNamed(dstVarName);
    if (srcVar.isANY() && !dstVar.isANY()) {
      srcFB.connectOV(srcVarName, dstVar);
      return;
    }
    if (srcVar.isARRAY() && !dstVar.isARRAY() && !dstVar.isANY()) {
      srcFB.connectOV(srcVarName, dstVar);
      return;
    } else {
      dstFB.connectIV(dstVarName, srcVar);
      return;
    }
  }

  public void create(FBElement obj) throws XMLException,
      FBRManagementException {
    if (obj instanceof FBMConnection)
      connect((FBMConnection) obj);
    else if (obj instanceof FBMFB)
      createFB((FBMFB) obj);
    else
      throw new XMLException("INVALID_OBJECT");
  }

  public FBInstance createFB(FBMFB obj) throws FBRManagementException {
    String fbName = obj.getName();
    String fbType = obj.getType();
    Class aClass = null;
    if (isDuplicate(fbName))
      throw FBRManagementException.INVALID_STATE;
    FBInstance fb = FBCommSI.newInstance(fbType);
    Vector packages = ANY.getPackages();
    if (fb == null) {
      for (int i = 0; i < packages.size();)
        try {
          aClass = Class.forName((new StringBuilder(String
              .valueOf((String) packages.elementAt(i)))).append(
              fbType).toString());
          break;
        } catch (ClassNotFoundException classnotfoundexception) {
          i++;
        }

      if (aClass == null)
        throw FBRManagementException.UNSUPPORTED_TYPE;
      Object inst;
      try {
        inst = aClass.newInstance();
      } catch (InstantiationException e) {
        throw FBRManagementException.UNSUPPORTED_TYPE;
      } catch (IllegalAccessException e) {
        throw FBRManagementException.UNSUPPORTED_TYPE;
      }
      if (!(inst instanceof FBInstance))
        throw FBRManagementException.UNSUPPORTED_TYPE;
      fb = (FBInstance) inst;
    }

    // //
    if(debugEnable)
      fb.initDebugClient(debugHost);

    fb.initialize(fbName, resource);
    fbs.addElement(fb);
    return fb;
  }

  public void delete(FBElement obj) throws XMLException,
      FBRManagementException {
    if (obj instanceof FBMFB)
      deleteFB((FBMFB) obj);
    else if (obj instanceof FBMConnection)
      deleteConnection((FBMConnection) obj);
    else
      throw new XMLException("INVALID_OBJECT");
  }

  public void deleteConnection(FBMConnection cnxn) throws XMLException,
      FBRManagementException {
    String src = cnxn.getSource();
    String dst = cnxn.getDestination();
    int n = src.lastIndexOf('.');
    if (n < 1 || n > src.length() - 1)
      throw FBRManagementException.INVALID_OBJECT;
    if (n < 1 || n > dst.length() - 1) {
      throw FBRManagementException.INVALID_OBJECT;
    } else {
      conns.removeElement(cnxn);
      return;
    }
  }

  public void deleteFB(FBMFB fb) throws XMLException, FBRManagementException {
    FBInstance fbi = fbNamed(fb.getName());
    if (!fbi.getType().equals(fb.getType())) {
      throw FBRManagementException.NO_SUCH_OBJECT;
    } else {
      fbi.stop();
      fbs.removeElement(fbi);
      return;
    }
  }

  public Enumeration enumFBs() {
    return fbs.elements();
  }

  public FBMResponse executeRequest(FBMRequest req) {
    String action = req.getAction();
    FBElement obj = req.getObject();
    FBMResponse rsp = new FBMResponse();
    rsp.setID(req.getID());
    String reason = "";
    try {
      if (action.equals("CREATE"))
        create(obj);
      else if (action.equals("READ"))
        rsp.setObject(read(obj));
      else if (action.equals("WRITE"))
        connectParameter(obj);
      else if (action.equals("START"))
        start();
      else if (action.equals("QUERY"))
        rsp.setObject(query(obj));
      else if (action.equals("DELETE"))
        delete(obj);
      else if (action.equals("DEBUG"))
        debug(obj);
      else
        reason = "UNSUPPORTED_CMD";
    } catch (FBDataTypeException dtx) {
      reason = dtx.getMessage();
    } catch (FBRManagementException mx) {
      reason = mx.getMessage();
    } catch (XMLException xmlx) {
      reason = xmlx.getMessage();
    }
    rsp.setReason(reason);
    return rsp;
  }

  public void debug(FBElement fbelement) {
  }

  public void updateObject() {
  }

  public FBInstance fbNamed(String name) throws FBRManagementException {
    if (name.equals(""))
      return resource;
    for (Enumeration e = fbs.elements(); e.hasMoreElements();) {
      FBInstance ans = (FBInstance) e.nextElement();
      if (ans.name.equals(name))
        return ans;
    }

    throw FBRManagementException.NO_SUCH_OBJECT;
  }

  public int getState() {
    return state;
  }

  public String getStateName() {
    return stateNames[getState()];
  }

  private boolean isDuplicate(String fbname) {
    try {
      fbNamed(fbname);
    } catch (FBRManagementException e) {
      return false;
    }
    return true;
  }

  public static boolean isFBType(String typename) {
    Class aClass;
    aClass = null;
    if (FBCommSI.getIOCount(typename)[0] > -1)
      return true;
    Vector packages = ANY.getPackages();
    for (int i = 0; i < packages.size();)
      try {
        aClass = Class.forName((new StringBuilder(String
            .valueOf((String) packages.elementAt(i)))).append(
            typename).toString());
        break;
      } catch (ClassNotFoundException classnotfoundexception) {
        i++;
      }

    try {
      return aClass != null
          && Class.forName("fb.rt.FBInstance").isAssignableFrom(
              aClass);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

    return false;
  }

  public FBElement query(FBElement obj) throws XMLException,
      FBRManagementException {
    if (obj instanceof FBMFB)
      return queryFB((FBMFB) obj);
    if (obj instanceof FBMConnection)
      return queryConnection((FBMConnection) obj);
    else
      throw new XMLException("INVALID_OBJECT");
  }

  private FBMFBList queryConnection(FBMConnection conn) {
    String dst = conn.getDestination();
    String src;
    boolean findDsts;
    StringBuffer buff;
    boolean isFirst;
    Enumeration e;
    FBMConnection c;
    String nextel;
    try {
      if (conns.contains(conn))
        return new FBMEndpointList(dst);
     
      src = conn.getSource();
      findDsts = dst.equals("*");
      if (!findDsts && !src.equals("*")){
        return conns.contains(conn) ? new FBMEndpointList(dst)
            : new FBMEndpointList();
      }
     
      buff = new StringBuffer(256);
      isFirst = true;
      for (e = conns.elements(); e.hasMoreElements();) {
        c = (FBMConnection) e.nextElement();
        nextel = "";
        if (findDsts) {
          if (c.getSource().equals(src))
            nextel = c.getDestination();
        } else if (c.getDestination().equals(dst))
          nextel = c.getSource();
        if (nextel.length() > 0) {
          if (!isFirst)
            buff.append(',');
          buff.append(nextel);
          isFirst = false;
        }
      }

      return new FBMEndpointList(buff.toString());
    } catch (XMLException ex) {
      return new FBMEndpointList();
    }
  }

  public FBElement queryFB(FBMFB fb) throws XMLException,
      FBRManagementException {
    String fbname = fb.getName();
    String fbtype = fb.getType();
    boolean wildtype = fbtype.equals("*");
    if (!fbname.equals("*")) {
      FBInstance fbi = fbNamed(fbname);
      String fbt2 = fbi.getType();
      if (wildtype)
        return new FBMFB(fbname, fbt2);
      if (fbt2.equals(fbtype))
        return new FBMFBStatus(getStateName());
      else
        throw FBRManagementException.NO_SUCH_OBJECT;
    }
    if (!wildtype && !isFBType(fbtype))
      throw new XMLException("UNSUPPORTED_TYPE");
    StringBuffer buff = new StringBuffer(256);
    boolean found = false;
    for (Enumeration e = fbs.elements(); e.hasMoreElements();) {
      FBInstance fbi = (FBInstance) e.nextElement();
      if (wildtype || fbi.getType().equals(fbtype)) {
        if (found)
          buff.append(',');
        else
          found = true;
        buff.append(fbi.getName());
      }
    }

    return new FBMFBList(buff.toString());
  }

  public FBElement read(FBElement obj) throws XMLException,
      FBRManagementException {
    if (!(obj instanceof FBMConnection)) {
      throw new XMLException("INVALID_OBJECT");
    } else {
      FBMConnection rsp = (FBMConnection) obj.copy();
      boolean isInput = rsp.getSource().equals("*");
      String endpoint[] = unpackHierName(isInput ? rsp.getDestination()
          : rsp.getSource());
      FBInstance fb = fbNamed(endpoint[0]);
      String value = (isInput ? fb.ivNamed(endpoint[1]) : fb
          .ovNamed(endpoint[1])).toString();
      rsp.setAttributeNoException(isInput ? "Source" : "Destination",
          value);
      return rsp;
    }
  }

  public void removeFB(FBInstance fb) {
    fbs.removeElement(fb);
  }

  public void restart() {
    doStart(true);
  }

  public void start() {
    doStart(false);
  }

  protected void doStart(boolean isRestart) {
    System.gc();
    Thread.yield();
    state = 1;
    for (int i = fbs.size() - 1; i >= 0; i--) {
      FBInstance fb = (FBInstance) fbs.elementAt(i);
      if (!(fb instanceof E_RESTART))
        if (isRestart)
          fb.restart();
        else
          fb.start();
    }

    for (int i = fbs.size() - 1; i >= 0; i--) {
      FBInstance fb = (FBInstance) fbs.elementAt(i);
      if (fb instanceof E_RESTART)
        if (isRestart)
          fb.restart();
        else
          fb.start();
    }

  }

  public void stop() {
    state = 2;
    for (Enumeration e = fbs.elements(); e.hasMoreElements(); ((FBInstance) e
        .nextElement()).stop())
      ;
  }

  public static String[] unpackHierName(String hname) {
    String ans[] = new String[2];
    int n = hname.lastIndexOf('.');
    if (n < 0) {
      ans[0] = "";
      ans[1] = hname;
    } else {
      ans[0] = hname.substring(0, n);
      ans[1] = n < hname.length() - 1 ? hname.substring(n + 1) : "";
    }
    return ans;
  }

  public void setDebugHost(String host) {
    debugHost = host;
    debugEnable = (host != null && debugHost.length() > 0);
  }
 
  public String getDebugHost(){
    return debugHost;
  }

  private static final String stateNames[] = { "IDLE", "RUNNING", "STOPPED" };

  public static final int IDLE = 0;

  public static final int RUNNING = 1;

  public static final int STOPPED = 2;

  private int state;

  private Resource resource;

  private Vector fbs;

  private Vector conns;

  private String debugHost = null;
 
  private boolean debugEnable = false;
}
TOP

Related Classes of fb.rt.FBManager

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.