Esempio n. 1
0
  /**
   * Initialize Jena.
   *
   * <p>This function is cheap to call when already initialized so can be called to be sure. A
   * commonly used idiom in jena is a static initializer in key classes.
   *
   * <p>By default, initialization happens by using {@code ServiceLoader.load} to find {@link
   * JenaSubsystemLifecycle} objects. See {@link #setSubsystemRegistry} to intercept that choice.
   */
  public static void init() {
    // Any other thread attempting to initialize as well will
    // first test the volatile outside the lock; if it's
    // not INITIALIZED, the thread will attempt to grab the lock
    // and hence wait, then see initialized as true.

    // But we need to cope with recursive calls of JenaSystem.init() as well.
    // The same thread will not stop at the lock.
    // Set initialized to true before a recursive call is possible
    // handles this.  The recursive call will see initialized true and
    // and returnn on the first test.

    // Net effect:
    // After a top level call of JenaSystem.init() returns, tjena has
    // finishes initialization.
    // Recursive calls do not have this property.

    if (initialized) return;
    synchronized (initLock) {
      if (initialized) {
        logLifecycle("JenaSystem.init - return");
        return;
      }
      // Catches recursive calls, same thread.
      initialized = true;
      logLifecycle("JenaSystem.init - start");

      if (get() == null) setSubsystemRegistry(new JenaSubsystemRegistryBasic());

      get().load();

      // Debug : what did we find?
      if (JenaSystem.DEBUG_INIT) {
        logLifecycle("Found:");
        get()
            .snapshot()
            .forEach(
                mod -> logLifecycle("  %-20s [%d]", mod.getClass().getSimpleName(), mod.level()));
      }

      get().add(new JenaInitLevel0());

      if (JenaSystem.DEBUG_INIT) {
        logLifecycle("Initialization sequence:");
        JenaSystem.forEach(
            module ->
                logLifecycle("  %-20s [%d]", module.getClass().getSimpleName(), module.level()));
      }

      JenaSystem.forEach(
          module -> {
            logLifecycle("Init: %s", module.getClass().getSimpleName());
            module.start();
          });
      logLifecycle("JenaSystem.init - finish");
    }
  }