protected void readCatalog(Catalog catalog, XStreamPersister xp) throws Exception { // we are going to synch up the catalogs and need to preserve listeners, // but these two fellas are attached to the new catalog as well catalog.removeListeners(ResourcePool.CacheClearingListener.class); catalog.removeListeners(GeoServerPersister.class); List<CatalogListener> listeners = new ArrayList<CatalogListener>(catalog.getListeners()); // look for catalog.xml, if it exists assume we are dealing with // an old data directory File f = resourceLoader.find("catalog.xml"); if (f == null) { // assume 2.x style data directory CatalogImpl catalog2 = (CatalogImpl) readCatalog(xp); // make to remove the old resource pool catalog listener ((CatalogImpl) catalog).sync(catalog2); } else { // import old style catalog, register the persister now so that we start // with a new version of the catalog CatalogImpl catalog2 = (CatalogImpl) readLegacyCatalog(f, xp); ((CatalogImpl) catalog).sync(catalog2); } // attach back the old listeners for (CatalogListener listener : listeners) { catalog.addListener(listener); } }
/** Reads the legacy (1.x) catalog from disk. */ Catalog readLegacyCatalog(File f, XStreamPersister xp) throws Exception { Catalog catalog2 = new CatalogImpl(); catalog2.setResourceLoader(resourceLoader); // add listener now as a converter which will convert from the old style // data directory to the new GeoServerPersister p = new GeoServerPersister(resourceLoader, xp); if (!legacy) { catalog2.addListener(p); } LegacyCatalogImporter importer = new LegacyCatalogImporter(catalog2); importer.setResourceLoader(resourceLoader); importer.imprt(resourceLoader.getBaseDirectory()); if (!legacy) { catalog2.removeListener(p); } if (!legacy) { // copy files from old feature type directories to new File featureTypesDir = resourceLoader.find("featureTypes"); if (featureTypesDir != null) { LegacyCatalogReader creader = new LegacyCatalogReader(); creader.read(f); Map<String, Map<String, Object>> dataStores = creader.dataStores(); for (File featureTypeDir : featureTypesDir.listFiles()) { if (!featureTypeDir.isDirectory()) { continue; } File featureTypeInfo = new File(featureTypeDir, "info.xml"); if (!featureTypeInfo.exists()) { continue; } LegacyFeatureTypeInfoReader reader = new LegacyFeatureTypeInfoReader(); reader.read(featureTypeInfo); Map<String, Object> dataStore = dataStores.get(reader.dataStore()); if (dataStore == null) { continue; } String namespace = (String) dataStore.get("namespace"); File destFeatureTypeDir = resourceLoader.find("workspaces", namespace, reader.dataStore(), reader.name()); if (destFeatureTypeDir != null) { // copy all the files over for (File file : featureTypeDir.listFiles()) { if (file.isFile() && !featureTypeInfo.equals(file)) { FileUtils.copyFile(file, new File(destFeatureTypeDir, file.getName())); } } } } } // rename catalog.xml f.renameTo(new File(f.getParentFile(), "catalog.xml.old")); } return catalog2; }