@Override
  public Status investigate(final long hostId) {
    final HostVO host = _hostDao.findById(hostId);
    if (host == null) {
      return null;
    }

    final Enumeration<Investigator> en = _investigators.enumeration();
    Status hostState = null;
    Investigator investigator = null;
    while (en.hasMoreElements()) {
      investigator = en.nextElement();
      hostState = investigator.isAgentAlive(host);
      if (hostState != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              investigator.getName()
                  + " was able to determine host "
                  + hostId
                  + " is in "
                  + hostState.toString());
        }
        return hostState;
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            investigator.getName() + " unable to determine the state of the host.  Moving on.");
      }
    }

    return null;
  }
  @Override
  public Status investigate(final long hostId) {
    final HostVO host = _hostDao.findById(hostId);
    if (host == null) {
      return null;
    }

    Status hostState = null;
    for (Investigator investigator : investigators) {
      hostState = investigator.isAgentAlive(host);
      if (hostState != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              investigator.getName()
                  + " was able to determine host "
                  + hostId
                  + " is in "
                  + hostState.toString());
        }
        return hostState;
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            investigator.getName() + " unable to determine the state of the host.  Moving on.");
      }
    }

    return null;
  }
 @Override
 public boolean processDisconnect(long agentId, Status state) {
   if (s_logger.isDebugEnabled()) {
     s_logger.debug("Disconnected called on " + agentId + " with status " + state.toString());
   }
   return true;
 }
public class DirectAgentManagerSimpleImpl extends ManagerBase implements AgentManager {
  private static final Logger logger = Logger.getLogger(DirectAgentManagerSimpleImpl.class);
  private final Map<Long, ServerResource> hostResourcesMap = new HashMap<Long, ServerResource>();
  @Inject HostDao hostDao;
  @Inject ClusterDao clusterDao;
  @Inject ClusterDetailsDao clusterDetailsDao;
  @Inject HostDao _hostDao;
  protected StateMachine2<Status, Event, Host> _statusStateMachine = Status.getStateMachine();

  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public void rescan() {}

  @Override
  public boolean start() {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean stop() {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public String getName() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Answer easySend(Long hostId, Command cmd) {
    try {
      return this.send(hostId, cmd);
    } catch (AgentUnavailableException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (OperationTimedoutException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }

  protected void loadResource(Long hostId) {
    HostVO host = hostDao.findById(hostId);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("guid", host.getGuid());
    params.put("ipaddress", host.getPrivateIpAddress());
    params.put("username", "root");
    params.put("password", "password");
    params.put("zone", String.valueOf(host.getDataCenterId()));
    params.put("pod", String.valueOf(host.getPodId()));

    ServerResource resource = null;
    if (host.getHypervisorType() == HypervisorType.XenServer) {
      resource = new XcpOssResource();
      try {
        resource.configure(host.getName(), params);

      } catch (ConfigurationException e) {
        logger.debug("Failed to load resource:" + e.toString());
      }
    } else if (host.getHypervisorType() == HypervisorType.KVM) {
      resource = new LibvirtComputingResource();
      try {
        params.put("public.network.device", "cloudbr0");
        params.put("private.network.device", "cloudbr0");
        resource.configure(host.getName(), params);
      } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } else if (host.getHypervisorType() == HypervisorType.VMware) {
      ClusterVO cluster = clusterDao.findById(host.getClusterId());
      String url = clusterDetailsDao.findDetail(cluster.getId(), "url").getValue();
      URI uri;
      try {
        uri = new URI(url);
        String userName = clusterDetailsDao.findDetail(cluster.getId(), "username").getValue();
        String password = clusterDetailsDao.findDetail(cluster.getId(), "password").getValue();
        VmwareServerDiscoverer discover = new VmwareServerDiscoverer();

        Map<? extends ServerResource, Map<String, String>> resources =
            discover.find(
                host.getDataCenterId(),
                host.getPodId(),
                host.getClusterId(),
                uri,
                userName,
                password,
                null);
        for (Map.Entry<? extends ServerResource, Map<String, String>> entry :
            resources.entrySet()) {
          resource = entry.getKey();
        }
        if (resource == null) {
          throw new CloudRuntimeException("can't find resource");
        }
      } catch (DiscoveryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    hostResourcesMap.put(hostId, resource);
    HostEnvironment env = new HostEnvironment();
    SetupCommand cmd = new SetupCommand(env);
    cmd.setNeedSetup(true);

    resource.executeRequest(cmd);
  }

  @Override
  public synchronized Answer send(Long hostId, Command cmd)
      throws AgentUnavailableException, OperationTimedoutException {
    ServerResource resource = hostResourcesMap.get(hostId);
    if (resource == null) {
      loadResource(hostId);
      resource = hostResourcesMap.get(hostId);
    }

    if (resource == null) {
      return null;
    }

    Answer answer = resource.executeRequest(cmd);
    return answer;
  }

  @Override
  public Answer[] send(Long hostId, Commands cmds)
      throws AgentUnavailableException, OperationTimedoutException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Answer[] send(Long hostId, Commands cmds, int timeout)
      throws AgentUnavailableException, OperationTimedoutException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public long send(Long hostId, Commands cmds, Listener listener) throws AgentUnavailableException {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public int registerForHostEvents(
      Listener listener, boolean connections, boolean commands, boolean priority) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public int registerForInitialConnects(StartupCommandProcessor creator, boolean priority) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public void unregisterForHostEvents(int id) {
    // TODO Auto-generated method stub

  }

  @Override
  public Answer sendTo(Long dcId, HypervisorType type, Command cmd) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public boolean agentStatusTransitTo(HostVO host, Event e, long msId) {
    try {
      return _statusStateMachine.transitTo(host, e, host.getId(), _hostDao);
    } catch (NoTransitionException e1) {
      e1
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    return true;
  }

  @Override
  public void disconnectWithoutInvestigation(long hostId, Event event) {
    // TODO Auto-generated method stub

  }

  @Override
  public void pullAgentToMaintenance(long hostId) {
    // TODO Auto-generated method stub

  }

  @Override
  public void pullAgentOutMaintenance(long hostId) {
    // TODO Auto-generated method stub

  }

  @Override
  public boolean reconnect(long hostId) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean isAgentAttached(long hostId) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean handleDirectConnectAgent(
      Host host, StartupCommand[] cmds, ServerResource resource, boolean forRebalance)
      throws ConnectionException {
    // TODO Auto-generated method stub
    return false;
  }
}