public void init() {
    BaseCmd.setComponents(new ApiResponseHelper());
    BaseListCmd.configure();

    _systemAccount = _accountMgr.getSystemAccount();
    _systemUser = _accountMgr.getSystemUser();
    _dispatcher = ApiDispatcher.getInstance();

    Integer apiPort = null; // api port, null by default
    ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    SearchCriteria<ConfigurationVO> sc = configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, "integration.api.port");
    List<ConfigurationVO> values = configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Set<Class<?>> cmdClasses =
        ReflectUtil.getClassesWithAnnotation(
            APICommand.class, new String[] {"org.apache.cloudstack.api", "com.cloud.api"});

    for (Class<?> cmdClass : cmdClasses) {
      String apiName = cmdClass.getAnnotation(APICommand.class).name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }
  }
Example #2
0
  @Override
  public boolean start() {
    Integer apiPort = null; // api port, null by default
    SearchCriteria<ConfigurationVO> sc = _configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, Config.IntegrationAPIPort.key());
    List<ConfigurationVO> values = _configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Map<String, String> configs = _configDao.getConfiguration();
    String strSnapshotLimit = configs.get(Config.ConcurrentSnapshotsThresholdPerHost.key());
    if (strSnapshotLimit != null) {
      Long snapshotLimit = NumbersUtil.parseLong(strSnapshotLimit, 1L);
      if (snapshotLimit.longValue() <= 0) {
        s_logger.debug(
            "Global config parameter "
                + Config.ConcurrentSnapshotsThresholdPerHost.toString()
                + " is less or equal 0; defaulting to unlimited");
      } else {
        _dispatcher.setCreateSnapshotQueueSizeLimit(snapshotLimit);
      }
    }

    Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
    for (PluggableService pluggableService : _pluggableServices) {
      cmdClasses.addAll(pluggableService.getCommands());
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("Discovered plugin " + pluggableService.getClass().getSimpleName());
      }
    }

    for (Class<?> cmdClass : cmdClasses) {
      APICommand at = cmdClass.getAnnotation(APICommand.class);
      if (at == null) {
        throw new CloudRuntimeException(
            String.format(
                "%s is claimed as a API command, but it doesn't have @APICommand annotation",
                cmdClass.getName()));
      }
      String apiName = at.name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(_configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = _configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }

    return true;
  }