Program: 91806
Project: 91806
Buggy source:
/** 
 * Returns the data source for the SIS-wide "SpatialMetadata" database. This method returns the first of the following steps that succeed: <ol> <li>If a JNDI context exists, the data source registered under the   {@code jdbc/SpatialMetadata} name.</li><li>If the  {@code SIS_DATA} environment variable is defined, {@code jdbc:derby:$SIS_DATA/Databases/SpatialMetadata}. This database will be created if it does not exist. Note that this is the only case where we allow database creation since we are in the directory managed by SIS.</li> <li>If the   {@code derby.system.home} property is defined, the data source for {@code jdbc:derby:SpatialMetadata}. This database will <strong>not</strong> be created if it does not exist.</li> <li>Otherwise (no JNDI, no environment variable, no Derby property set),   {@code null}.</li> </ol>
 * @return The data source for the {@code $SIS_DATA/Databases/SpatialMetadata} or equivalent database, or {@code null} if none.
 * @throws javax.naming.NamingException     if an error occurred while fetching the data source from a JNDI context.
 * @throws java.net.MalformedURLException   if an error occurred while converting the {@code derby.jar} file to URL.
 * @throws java.lang.ClassNotFoundException if {@code derby.jar} has not been found on the JDK installation directory.
 * @throws java.lang.InstantiationException if an error occurred while creating {@code org.apache.derby.jdbc.EmbeddedDataSource}.
 * @throws java.lang.NoSuchMethodException  if a JDBC bean property has not been found on the data source.
 * @throws java.lang.IllegalAccessException if a JDBC bean property of the data source is not public.
 * @throws java.lang.reflect.InvocationTargetException if an error occurred while setting a data source bean property.
 * @throws Exception for any other kind of errors. This include {@link RuntimeException} not documented above like{@link IllegalArgumentException},   {@link ClassCastException},   {@link SecurityException}, <i>etc.</i>
 */
public static synchronized DataSource getDataSource() throws Exception {
  if (source == null) {
    if (hasJNDI())     try {
      final Context env=(Context)InitialContext.doLookup("String_Node_Str");
      source=(DataSource)env.lookup(JNDI);
      if (env instanceof EventContext) {
        Listener.register((EventContext)env);
      }
      return source;
    }
 catch (    NameNotFoundException e) {
      final LogRecord record=Messages.getResources(null).getLogRecord(Level.CONFIG,Messages.Keys.JNDINotSpecified_1,JNDI);
      record.setLoggerName(Loggers.SQL);
      Logging.log(Initializer.class,"String_Node_Str",record);
    }
    final boolean create;
    final String home=AccessController.doPrivileged(new PrivilegedAction<String>(){
      @Override public String run(){
        return System.getProperty(DERBY_HOME_KEY);
      }
    }
);
    final Path dir=DataDirectory.DATABASES.getDirectory();
    if (dir != null) {
      Path path=dir.resolve(DATABASE);
      if (home != null)       try {
        path=Paths.get(home).relativize(path);
      }
 catch (      IllegalArgumentException|SecurityException e) {
        Logging.recoverableException(Logging.getLogger(Loggers.SQL),Initializer.class,"String_Node_Str",e);
      }
      path=path.normalize();
      create=!Files.exists(path);
      source=forJavaDB(path.toString());
    }
 else     if (home != null) {
      final Path path=Paths.get(home);
      create=!Files.exists(path.resolve(DATABASE)) && Files.isDirectory(path);
      source=forJavaDB(DATABASE);
    }
 else {
      return null;
    }
    Shutdown.register(new Callable<Object>(){
      @Override public Object call() throws ReflectiveOperationException {
        shutdown();
        return null;
      }
    }
);
    if (create) {
      final Method m=source.getClass().getMethod("String_Node_Str",String.class);
      m.invoke(source,"String_Node_Str");
      try (Connection c=source.getConnection()){
        for (        Initializer init : DefaultFactories.createServiceLoader(Initializer.class)) {
          init.createSchema(c);
        }
      }
  finally {
        m.invoke(source,"String_Node_Str");
      }
    }
  }
  return source;
}

Fixed version (ground truth):
/** 
 * Returns the data source for the SIS-wide "SpatialMetadata" database. This method returns the first of the following steps that succeed: <ol> <li>If a JNDI context exists, the data source registered under the   {@code jdbc/SpatialMetadata} name.</li><li>If the  {@code SIS_DATA} environment variable is defined, {@code jdbc:derby:$SIS_DATA/Databases/SpatialMetadata}. This database will be created if it does not exist. Note that this is the only case where we allow database creation since we are in the directory managed by SIS.</li> <li>If the   {@code derby.system.home} property is defined, the data source for {@code jdbc:derby:SpatialMetadata}. This database will <strong>not</strong> be created if it does not exist.</li> <li>Otherwise (no JNDI, no environment variable, no Derby property set),   {@code null}.</li> </ol>
 * @return the data source for the {@code $SIS_DATA/Databases/SpatialMetadata} or equivalent database, or {@code null} if none.
 * @throws javax.naming.NamingException     if an error occurred while fetching the data source from a JNDI context.
 * @throws java.net.MalformedURLException   if an error occurred while converting the {@code derby.jar} file to URL.
 * @throws java.lang.ClassNotFoundException if {@code derby.jar} has not been found on the JDK installation directory.
 * @throws java.lang.InstantiationException if an error occurred while creating {@code org.apache.derby.jdbc.EmbeddedDataSource}.
 * @throws java.lang.NoSuchMethodException  if a JDBC bean property has not been found on the data source.
 * @throws java.lang.IllegalAccessException if a JDBC bean property of the data source is not public.
 * @throws java.lang.reflect.InvocationTargetException if an error occurred while setting a data source bean property.
 * @throws Exception for any other kind of errors. This include {@link RuntimeException} not documented above like{@link IllegalArgumentException},   {@link ClassCastException},   {@link SecurityException}, <i>etc.</i>
 */
public static synchronized DataSource getDataSource() throws Exception {
  if (source == null) {
    if (hasJNDI())     try {
      final Context env=(Context)InitialContext.doLookup("String_Node_Str");
      source=(DataSource)env.lookup(JNDI);
      if (env instanceof EventContext) {
        Listener.register((EventContext)env);
      }
      return source;
    }
 catch (    NameNotFoundException e) {
      final LogRecord record=Messages.getResources(null).getLogRecord(Level.CONFIG,Messages.Keys.JNDINotSpecified_1,JNDI);
      record.setLoggerName(Loggers.SQL);
      Logging.log(Initializer.class,"String_Node_Str",record);
    }
    final boolean create;
    final String home=AccessController.doPrivileged(new PrivilegedAction<String>(){
      @Override public String run(){
        return System.getProperty(DERBY_HOME_KEY);
      }
    }
);
    final Path dir=DataDirectory.DATABASES.getDirectory();
    if (dir != null) {
      Path path=dir.resolve(DATABASE);
      if (home != null)       try {
        path=Paths.get(home).relativize(path);
      }
 catch (      IllegalArgumentException|SecurityException e) {
        Logging.recoverableException(Logging.getLogger(Loggers.SQL),Initializer.class,"String_Node_Str",e);
      }
      path=path.normalize();
      create=!Files.exists(path);
      source=forJavaDB(path.toString());
    }
 else     if (home != null) {
      final Path path=Paths.get(home);
      create=!Files.exists(path.resolve(DATABASE)) && Files.isDirectory(path);
      source=forJavaDB(DATABASE);
    }
 else {
      return null;
    }
    Shutdown.register(new Callable<Object>(){
      @Override public Object call() throws ReflectiveOperationException {
        shutdown();
        return null;
      }
    }
);
    if (create) {
      final Method m=source.getClass().getMethod("String_Node_Str",String.class);
      m.invoke(source,"String_Node_Str");
      try (Connection c=source.getConnection()){
        for (        Initializer init : DefaultFactories.createServiceLoader(Initializer.class)) {
          init.createSchema(c);
        }
      }
  finally {
        m.invoke(source,"String_Node_Str");
      }
    }
  }
  return source;
}