private void initializeCatalogServices() throws Exception {
    CatalogDAO c = CatalogDAOFactory.newInstance();
    try {
      c.recoverTransactions();

      // Create and configure all Dynamic Site Providers
      for (Provider provider : c.findAllProviders()) {
        SiteProviderContext ctxt = new SiteProviderContextInitialisation(provider.getName(), c);
        SiteProviderFactory.addProvider(
            ctxt,
            provider.getPlugin(),
            provider.getName(),
            provider.isEnabled(),
            provider.getConfig());
      }

      // Create and configure External Services
      for (ExternalService es : c.findAllExternalServices()) {
        // enclose in try/catch in case an early service fails we still
        // start the later ones
        try {
          ExternalServiceFactory.register(es.getName(), es.getPlugin());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      //			GlobalConfigVariableHandler.initializeGlobalVariables(getGlobalVariables(c));
    } finally {
      c.close();
    }
  }
 // you'll have to set the def db, project - the session context would do that for us -
 // i.e. this test does not handle use statements correctly
 @SuppressWarnings({"rawtypes", "unchecked"})
 private Pair<SchemaContext, Persistable<?, ?>> roundTrip(String sql, String defaultDatabase)
     throws Exception {
   PECreateStatement firstTime = null;
   PECreateStatement secondTime = null;
   CatalogDAO catalog = CatalogDAOFactory.newInstance();
   SchemaContext secondPC = null;
   try {
     catalog.begin();
     SchemaContext firstPC = buildContext(catalog);
     if (defaultDatabase != null)
       firstPC.setCurrentDatabase(firstPC.findDatabase(defaultDatabase));
     firstTime = trip(firstPC, catalog, sql, defaultDatabase);
     // don't persist yet
     String gen = firstTime.getSQL(firstPC, true, false);
     echo(gen);
     secondPC = buildContext(catalog);
     if (defaultDatabase != null)
       secondPC.setCurrentDatabase(secondPC.findDatabase(defaultDatabase));
     secondTime = trip(secondPC, catalog, gen, defaultDatabase);
     // make sure the objects are the same
     String diffs = firstTime.getCreated().differs(firstPC, secondTime.getCreated(), false);
     if (diffs != null) fail(diffs);
     persistToCatalog(catalog, secondPC, secondTime);
     catalog.commit();
     // now, we're going to load that persistable object and compare it to the second time
     catalog.begin();
     checkLoad(catalog, secondTime.getCreated(), defaultDatabase);
     catalog.rollbackNoException();
   } finally {
     catalog.close();
   }
   return new Pair<SchemaContext, Persistable<?, ?>>(secondPC, secondTime.getCreated());
 }
 public String[] generateCatalog() throws PEException {
   CatalogDAO c = CatalogDAOFactory.newInstance();
   String[] current = null;
   try {
     CatalogGenerator generator = Singletons.require(CatalogGenerator.class);
     current =
         generator.buildCreateCurrentSchema(
             c,
             Singletons.require(HostService.class)
                 .getProperties()); // TODO: this looks like we are only looking up the host to get
                                    // something for the catalog. -sgossard
   } finally {
     c.close();
   }
   return current;
 }
  public static void stopServices() throws Exception {
    MySqlPortal.stop();

    TemporaryTable.onStopServices();

    if (GroupManager.getCoordinationServices() != null)
      GroupManager.getCoordinationServices().unRegisterWithGroup();

    Host.hostStop();

    Agent.stopServices();
    CatalogDAOFactory.shutdown();
    AutoIncrementTracker.clearCache();
    RangeDistributionModel.clearCache();
    RandomDistributionModel.clearCache();
    SiteProviderFactory.closeSiteProviders();
    ExternalServiceFactory.closeExternalServices();
    GroupManager.shutdown();
    DirectConnectionCache.clearConnectionCache();
  }
  public static <T> BootstrapHost startServices(Class<T> bootstrapClass, Properties props)
      throws Exception {

    String url = props.getProperty(DBHelper.CONN_URL);
    String database = props.getProperty(DBHelper.CONN_DBNAME, PEConstants.CATALOG);

    // Fail to start if we don't have a valid catalog connection URL
    if (StringUtils.isBlank(url))
      throw new PEException(
          "Value for " + DBHelper.CONN_URL + " not specified in server configuration");

    if (StringUtils.isBlank(database))
      throw new PEException(
          "Value for " + DBHelper.CONN_DBNAME + " not specified in server configuration");

    // Attempt to load the JDBC driver - fail early if it isn't available
    DBHelper.loadDriver(props.getProperty(DBHelper.CONN_DRIVER_CLASS));

    props.put(
        DBHelper.CONN_URL,
        CatalogURL.buildCatalogBaseUrlFrom(props.getProperty(DBHelper.CONN_URL)).toString());
    props.put(DBHelper.CONN_DBNAME, database);
    DBHelper helper = new DBHelper(props);
    int catalogAccessible = 1;
    try {
      // Check that we can connect to the database and that the catalog database exists
      helper.connect();

      // and also check that it is a valid catalog
      helper.executeQuery("SELECT name FROM " + database + ".user_table");
    } catch (Exception e) {
      String message = e.getMessage();
      if (message != null && message.startsWith("Error using")) catalogAccessible = -1;
      else catalogAccessible = 0;
      if (catalogAccessible == 0)
        throw new Exception(
            "A DVE catalog couldn't be found at '"
                + url
                + "' with name '"
                + database
                + "' - use dve_config to set the connection credentials for the server (error was "
                + message
                + ")");
    } finally {
      helper.disconnect();
    }

    if (catalogAccessible == -1) {
      new CatalogHelper(bootstrapClass).createBootstrapCatalog();
    }

    // Temporarily set the log level to info so that the below message will print and force the
    // header to print
    Level logLevel =
        logger.getLevel(); // this is needed in case an explicit level is set on this category
    Level effectiveLevel =
        logger
            .getEffectiveLevel(); // if an explicit level isn't set, this gets the level from the
                                  // hierarchy
    logger.setLevel(Level.INFO);

    if (catalogAccessible == -1) {
      logger.info("INSTALLED BOOTSTRAP CATALOG " + database + " AT " + url);
    }

    logger.info("Starting DVE server using:");
    logger.info("... Catalog URL      : " + url);
    logger.info("... Catalog database : " + database);
    logger.info("... Catalog User     : "******"Resetting log level back to: " + effectiveLevel);
    logger.setLevel(logLevel);

    // We have to initialize the Group Manager before anything else.
    GroupManager.initialize(props);
    GroupManager.getCoordinationServices().registerWithGroup(props);

    CatalogDAOFactory.setup(props);

    SchemaSourceFactory.reset();

    ErrorMapper.initialize();

    Agent.setPluginProvider(SimpleMQPlugin.PROVIDER);
    Agent.startServices(props);

    BootstrapHost host = new BootstrapHost(bootstrapClass.getName(), props);

    ClusterLock genLock =
        GroupManager.getCoordinationServices()
            .getClusterLock(RangeDistributionModel.GENERATION_LOCKNAME);
    String reason = "boostrapping system";
    genLock.sharedLock(null, reason);
    genLock.sharedUnlock(null, reason);

    TemporaryTable.onStartServices();

    MySqlPortal.start(props);

    return host;
  }