@Override
 protected void onSetUp(SystemTestData testData) throws Exception {
   GeoServer geoServer = getGeoServer();
   geoServer.addListener(
       new ServicePersister(
           (List) Arrays.asList(new ServiceLoader(getResourceLoader())), geoServer));
 }
 @Before
 public void removeFooService() {
   GeoServer geoServer = getGeoServer();
   WorkspaceInfo ws = getCatalog().getDefaultWorkspace();
   ServiceInfo s = geoServer.getServiceByName(ws, "foo", ServiceInfo.class);
   if (s != null) {
     geoServer.remove(s);
   }
 }
  @Test
  public void testRemoveWorkspaceLocalService() throws Exception {
    testAddWorkspaceLocalService();

    File dataDirRoot = getTestData().getDataDirectoryRoot();
    WorkspaceInfo ws = getCatalog().getDefaultWorkspace();

    File f = new File(dataDirRoot, "workspaces" + "/" + ws.getName() + "/service.xml");
    assertTrue(f.exists());

    ServiceInfo s = geoServer.getServiceByName(ws, "foo", ServiceInfo.class);
    geoServer.remove(s);
    assertFalse(f.exists());
  }
  public void reload() throws Exception {
    destroy();

    // reload catalog, make sure we reload the underlying catalog, not any wrappers
    Catalog catalog = geoserver.getCatalog();
    if (catalog instanceof Wrapper) {
      catalog = ((Wrapper) geoserver.getCatalog()).unwrap(Catalog.class);
    }

    XStreamPersister xp = xpf.createXMLPersister();
    xp.setCatalog(catalog);

    loadCatalog(catalog, xp);
    loadGeoServer(geoserver, xp);
  }
  @Test
  public void testAddWorkspaceLocalService() throws Exception {
    File dataDirRoot = getTestData().getDataDirectoryRoot();
    WorkspaceInfo ws = getCatalog().getDefaultWorkspace();

    ServiceInfo s = geoServer.getFactory().createService();
    s.setName("foo");
    s.setWorkspace(ws);

    File f = new File(dataDirRoot, "workspaces" + "/" + ws.getName() + "/service.xml");
    assertFalse(f.exists());

    geoServer.add(s);
    assertTrue(f.exists());
  }
  void loadServices(File directory, List<XStreamServiceLoader> loaders, GeoServer geoServer) {
    for (XStreamServiceLoader<ServiceInfo> l : loaders) {
      try {
        ServiceInfo s = l.load(geoServer, directory);
        if (directory != null && s.getWorkspace() == null) continue;

        geoServer.add(s);

        LOGGER.info(
            "Loaded service '" + s.getId() + "', " + (s.isEnabled() ? "enabled" : "disabled"));
      } catch (Throwable t) {
        LOGGER.log(
            Level.SEVERE,
            "Failed to load the service configuration in directory: " + directory.getPath(),
            t);
      }
    }
  }
  public final Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {
    if (bean instanceof Catalog) {
      // ensure this is not a wrapper but the real deal
      if (bean instanceof Wrapper && ((Wrapper) bean).isWrapperFor(Catalog.class)) {
        return bean;
      }

      // load
      try {
        Catalog catalog = (Catalog) bean;
        XStreamPersister xp = xpf.createXMLPersister();
        xp.setCatalog(catalog);
        loadCatalog(catalog, xp);

        // initialize styles
        initializeStyles(catalog, xp);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    if (bean instanceof GeoServer) {
      geoserver = (GeoServer) bean;
      try {
        XStreamPersister xp = xpf.createXMLPersister();
        xp.setCatalog(geoserver.getCatalog());
        loadGeoServer(geoserver, xp);

        // load initializers
        loadInitializers(geoserver);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      // initialize();
    }

    return bean;
  }
 public void destroy() throws Exception {
   // dispose
   geoserver.dispose();
 }
  protected void readConfiguration(GeoServer geoServer, XStreamPersister xp) throws Exception {
    // look for services.xml, if it exists assume we are dealing with
    // an old data directory
    File f = resourceLoader.find("services.xml");
    if (f == null) {
      // assume 2.x style
      f = resourceLoader.find("global.xml");
      if (f != null) {
        GeoServerInfo global = depersist(xp, f, GeoServerInfo.class);
        geoServer.setGlobal(global);
      }

      // load logging
      f = resourceLoader.find("logging.xml");
      if (f != null) {
        LoggingInfo logging = depersist(xp, f, LoggingInfo.class);
        geoServer.setLogging(logging);
      }

      // load workspace specific settings
      File workspaces = resourceLoader.find("workspaces");
      if (workspaces != null) {
        for (File dir : workspaces.listFiles()) {
          if (!dir.isDirectory() && !dir.isHidden()) continue;

          f = resourceLoader.find(dir, "settings.xml");
          if (f != null) {
            SettingsInfo settings = depersist(xp, f, SettingsInfo.class);
            geoServer.add(settings);
          }
        }
      }

      // load services
      final List<XStreamServiceLoader> loaders =
          GeoServerExtensions.extensions(XStreamServiceLoader.class);
      loadServices(null, loaders, geoServer);

      // load services specific to workspace
      if (workspaces != null) {
        for (File dir : workspaces.listFiles()) {
          if (!dir.isDirectory() && !dir.isHidden()) continue;

          loadServices(dir, loaders, geoServer);
        }
      }

    } else {
      // add listener now as a converter which will convert from the old style
      // data directory to the new
      GeoServerPersister p = new GeoServerPersister(resourceLoader, xp);
      geoServer.addListener(p);

      // import old style services.xml
      new LegacyConfigurationImporter(geoServer).imprt(resourceLoader.getBaseDirectory());

      geoServer.removeListener(p);

      // rename the services.xml file
      f.renameTo(new File(f.getParentFile(), "services.xml.old"));
    }
  }