@Override
  public GeoCouchFeatureStore create(URL configURL) throws ResourceInitException {

    GeoCouchFeatureStore fs = null;
    try {
      GeoCouchFeatureStoreConfig config =
          (GeoCouchFeatureStoreConfig)
              JAXBUtils.unmarshall(CONFIG_JAXB_PACKAGE, CONFIG_SCHEMA, configURL, workspace);

      XMLAdapter resolver = new XMLAdapter();
      resolver.setSystemId(configURL.toString());

      String srs = config.getStorageCRS();
      srs = srs.trim();
      ICRS crs = CRSManager.getCRSRef(srs);

      String couchUrl = config.getGeoCouchUrl();
      if (!couchUrl.endsWith("/")) {
        couchUrl += "/";
      }

      List<String> configSchemas = config.getGMLSchema();
      String[] schemas = new String[configSchemas.size()];
      int i = -1;
      for (String s : configSchemas) {
        schemas[++i] = resolver.resolve(s).toString();
      }

      GMLAppSchemaReader decoder = new GMLAppSchemaReader(null, null, schemas);
      AppSchema schema = decoder.extractAppSchema();

      fs = new GeoCouchFeatureStore(crs, schema, couchUrl);

    } catch (Throwable e) {

      String msg =
          "Error in feature store configuration file '" + configURL + "': " + e.getMessage();
      LOG.error(msg);
      throw new ResourceInitException(msg, e);
    }
    return fs;
  }
  @Override
  public FeatureStore build() {
    MemoryFeatureStore fs;
    ICRS storageCRS = null;
    AppSchema schema = null;
    try {
      String[] schemaURLs = new String[config.getGMLSchema().size()];
      int i = 0;
      GMLVersionType gmlVersionType = null;
      for (GMLSchema jaxbSchemaURL : config.getGMLSchema()) {
        schemaURLs[i++] =
            metadata
                .getLocation()
                .resolveToFile(jaxbSchemaURL.getValue().trim())
                .toURI()
                .toURL()
                .toString();
        // TODO what about different versions at the same time?
        gmlVersionType = jaxbSchemaURL.getVersion();
      }

      GMLAppSchemaReader decoder = null;
      if (schemaURLs.length == 1 && schemaURLs[0].startsWith("file:")) {
        File file = new File(new URL(schemaURLs[0]).toURI());
        decoder =
            new GMLAppSchemaReader(
                GMLVersion.valueOf(gmlVersionType.name()),
                getHintMap(config.getNamespaceHint()),
                file);
      } else {
        decoder =
            new GMLAppSchemaReader(
                GMLVersion.valueOf(gmlVersionType.name()),
                getHintMap(config.getNamespaceHint()),
                schemaURLs);
      }
      schema = decoder.extractAppSchema();
      if (config.getStorageCRS() != null) {
        storageCRS = CRSManager.lookup(config.getStorageCRS());
      }
    } catch (Exception e) {
      String msg = Messages.getMessage("STORE_MANAGER_STORE_SETUP_ERROR", e.getMessage());
      LOG.error(msg, e);
      throw new ResourceInitException(msg, e);
    }

    try {
      ConnectionProvider lockProvider =
          workspace.getResource(ConnectionProviderProvider.class, "LOCK_DB");
      fs = new MemoryFeatureStore(schema, storageCRS, metadata, lockProvider);
    } catch (FeatureStoreException ex) {
      throw new ResourceInitException(ex.getLocalizedMessage(), ex);
    }
    for (GMLFeatureCollection datasetFile : config.getGMLFeatureCollection()) {
      if (datasetFile != null) {
        try {
          GMLVersion version = GMLVersion.valueOf(datasetFile.getVersion().name());
          URL docURL =
              metadata.getLocation().resolveToFile(datasetFile.getValue().trim()).toURI().toURL();
          GMLStreamReader gmlStream = GMLInputFactory.createGMLStreamReader(version, docURL);
          gmlStream.setApplicationSchema(schema);
          LOG.info("Populating feature store with features from file '" + docURL + "'...");
          FeatureCollection fc = (FeatureCollection) gmlStream.readFeature();
          gmlStream.getIdContext().resolveLocalRefs();

          FeatureStoreTransaction ta = fs.acquireTransaction();
          int fids = ta.performInsert(fc, USE_EXISTING).size();
          LOG.info("Inserted " + fids + " features.");
          ta.commit();
        } catch (Exception e) {
          String msg = Messages.getMessage("STORE_MANAGER_STORE_SETUP_ERROR", e.getMessage());
          LOG.error(msg);
          LOG.trace("Stack trace:", e);
          throw new ResourceInitException(msg, e);
        }
      }
    }
    return fs;
  }