/**
   * Handles a {@link GetFeature} request by delegating the contained {@link Query} objects to
   * different Threads.
   *
   * <p>If at least one query fails an exception will be thrown and all running threads will be
   * stopped.
   *
   * @param getFeature
   * @return result of the request
   * @throws OGCWebServiceException
   */
  FeatureResult handleRequest(GetFeature getFeature) throws OGCWebServiceException {
    LOG.entering();

    if (getFeature.getMaxFeatures() > maxFeatures) {
      getFeature.setMaxFeatures(maxFeatures);
    }

    LOG.logDebug("maxFeatures " + getFeature.getMaxFeatures());

    Query[] queries = getFeature.getQuery();
    List<Callable<FeatureCollection>> queryTasks =
        new ArrayList<Callable<FeatureCollection>>(queries.length);

    for (Query query : queries) {
      QualifiedName[] ftNames = query.getTypeNames();
      // TODO joins between feature types
      if (ftNames.length > 1) {
        String msg =
            "Multiple feature types in a Query (joins over feature types) "
                + "are not the supported by this WFS implementation.";
        throw new OGCWebServiceException(this.getClass().getName(), msg);
      }
      QualifiedName ftName = ftNames[0];

      MappedFeatureType ft = this.service.getMappedFeatureType(ftName);

      if (ft == null) {
        String msg = Messages.getMessage("WFS_FEATURE_TYPE_UNKNOWN", ftName);
        throw new OGCWebServiceException(this.getClass().getName(), msg);
      }
      if (!ft.isVisible()) {
        String msg = Messages.getMessage("WFS_FEATURE_TYPE_INVISIBLE", ftName);
        throw new OGCWebServiceException(this.getClass().getName(), msg);
      }

      // check and normalized requested SRS
      String srsName = query.getSrsName();
      if (srsName != null) {
        WFSFeatureType wfsFT =
            this.service.getCapabilities().getFeatureTypeList().getFeatureType(ftName);
        String normalizedSrsName = normalizeSrsName(srsName);
        query.setSrsName(normalizedSrsName);

        if (!(wfsFT.supportsSrs(normalizedSrsName))) {
          String msg = Messages.getMessage("WFS_FEATURE_TYPE_SRS_UNSUPPORTED", ftName, srsName);
          throw new OGCWebServiceException(this.getClass().getName(), msg);
        }
      }

      QueryTask task = new QueryTask(query, ft);
      queryTasks.add(task);
    }

    WFSConfiguration conf = (WFSConfiguration) service.getCapabilities();
    long timeout = conf.getDeegreeParams().getRequestTimeLimit() * 1000;
    if (timeout > MAX_TIMEOUT_MILLIS) {
      // limit max timeout
      timeout = MAX_TIMEOUT_MILLIS;
    }

    List<ExecutionFinishedEvent<FeatureCollection>> finishedEvents = null;
    try {
      finishedEvents = Executor.getInstance().performSynchronously(queryTasks, timeout);
    } catch (InterruptedException e) {
      String msg = "Exception occured while waiting for the GetFeature results: " + e.getMessage();
      throw new OGCWebServiceException(this.getClass().getName(), msg);
    }

    // use id of the request as id of the result feature collection
    // to allow identification of the original request that produced
    // the feature collection
    FeatureCollection fc = null;
    if (getFeature.getResultType() == RESULT_TYPE.RESULTS) {
      fc = mergeResults(getFeature.getId(), finishedEvents);
    } else {
      fc = mergeHits(getFeature.getId(), finishedEvents);
    }

    FeatureResult fr = new FeatureResult(getFeature, fc);

    LOG.exiting();
    return fr;
  }