Program: 5946
Project: 5946
Buggy source:
/** 
 * Inspects and builds plugin and application information for the given artifact, adding an additional set of plugin classes to the plugins found through inspection. This method is used when all plugin classes cannot be derived by inspecting the artifact but need to be explicitly set. This is true for 3rd party plugins like jdbc drivers.
 * @param artifactId the id of the artifact to inspect and store
 * @param artifactFile the artifact to inspect and store
 * @param parentArtifacts artifacts the given artifact extends.If null, the given artifact does not extend another artifact
 * @param additionalPlugins the set of additional plugin classes to add to the plugins found through inspection.If null, no additional plugin classes will be added
 * @param properties properties for the artifact
 * @throws IOException if there was an exception reading from the artifact store
 * @throws ArtifactRangeNotFoundException if none of the parent artifacts could be found
 * @throws UnauthorizedException if the user is not authorized to add an artifact in the specified namespace. To addan artifact, a user must have  {@link Action#WRITE} on the namespace in whichthe artifact is being added. If authorization is successful, and the artifact is added successfully, then the user gets  {@link Action#ALL} privilegeson the added artifact.
 */
@VisibleForTesting public ArtifactDetail addArtifact(final Id.Artifact artifactId,final File artifactFile,@Nullable Set<ArtifactRange> parentArtifacts,@Nullable Set<PluginClass> additionalPlugins,Map<String,String> properties) throws Exception {
  if (additionalPlugins != null) {
    validatePluginSet(additionalPlugins);
  }
  parentArtifacts=parentArtifacts == null ? Collections.<ArtifactRange>emptySet() : parentArtifacts;
  CloseableClassLoader parentClassLoader;
  NamespacedImpersonator namespacedImpersonator=new NamespacedImpersonator(artifactId.getNamespace().toEntityId(),impersonator);
  if (parentArtifacts.isEmpty()) {
    parentClassLoader=createArtifactClassLoader(Locations.toLocation(artifactFile),namespacedImpersonator);
  }
 else {
    validateParentSet(artifactId,parentArtifacts);
    parentClassLoader=createParentClassLoader(artifactId,parentArtifacts,namespacedImpersonator);
  }
  try {
    ArtifactClasses artifactClasses=inspectArtifact(artifactId,artifactFile,additionalPlugins,parentClassLoader);
    ArtifactMeta meta=new ArtifactMeta(artifactClasses,parentArtifacts,properties);
    ArtifactDetail artifactDetail=artifactStore.write(artifactId,meta,Files.newInputStreamSupplier(artifactFile),namespacedImpersonator);
    ArtifactDescriptor descriptor=artifactDetail.getDescriptor();
    ArtifactInfo artifactInfo=new ArtifactInfo(descriptor.getArtifactId(),artifactDetail.getMeta().getClasses(),artifactDetail.getMeta().getProperties());
    writeSystemMetadata(artifactId,artifactInfo);
    return artifactDetail;
  }
  finally {
    parentClassLoader.close();
  }
}

Fixed version (ground truth):
/** 
 * Inspects and builds plugin and application information for the given artifact, adding an additional set of plugin classes to the plugins found through inspection. This method is used when all plugin classes cannot be derived by inspecting the artifact but need to be explicitly set. This is true for 3rd party plugins like jdbc drivers.
 * @param artifactId the id of the artifact to inspect and store
 * @param artifactFile the artifact to inspect and store
 * @param parentArtifacts artifacts the given artifact extends.If null, the given artifact does not extend another artifact
 * @param additionalPlugins the set of additional plugin classes to add to the plugins found through inspection.If null, no additional plugin classes will be added
 * @param properties properties for the artifact
 * @throws IOException if there was an exception reading from the artifact store
 * @throws ArtifactRangeNotFoundException if none of the parent artifacts could be found
 * @throws UnauthorizedException if the user is not authorized to add an artifact in the specified namespace. To addan artifact, a user must have  {@link Action#WRITE} on the namespace in whichthe artifact is being added. If authorization is successful, and the artifact is added successfully, then the user gets all  {@link Action privileges}on the added artifact.
 */
@VisibleForTesting public ArtifactDetail addArtifact(final Id.Artifact artifactId,final File artifactFile,@Nullable Set<ArtifactRange> parentArtifacts,@Nullable Set<PluginClass> additionalPlugins,Map<String,String> properties) throws Exception {
  if (additionalPlugins != null) {
    validatePluginSet(additionalPlugins);
  }
  parentArtifacts=parentArtifacts == null ? Collections.<ArtifactRange>emptySet() : parentArtifacts;
  CloseableClassLoader parentClassLoader;
  NamespacedImpersonator namespacedImpersonator=new NamespacedImpersonator(artifactId.getNamespace().toEntityId(),impersonator);
  if (parentArtifacts.isEmpty()) {
    parentClassLoader=createArtifactClassLoader(Locations.toLocation(artifactFile),namespacedImpersonator);
  }
 else {
    validateParentSet(artifactId,parentArtifacts);
    parentClassLoader=createParentClassLoader(artifactId,parentArtifacts,namespacedImpersonator);
  }
  try {
    ArtifactClasses artifactClasses=inspectArtifact(artifactId,artifactFile,additionalPlugins,parentClassLoader);
    ArtifactMeta meta=new ArtifactMeta(artifactClasses,parentArtifacts,properties);
    ArtifactDetail artifactDetail=artifactStore.write(artifactId,meta,Files.newInputStreamSupplier(artifactFile),namespacedImpersonator);
    ArtifactDescriptor descriptor=artifactDetail.getDescriptor();
    ArtifactInfo artifactInfo=new ArtifactInfo(descriptor.getArtifactId(),artifactDetail.getMeta().getClasses(),artifactDetail.getMeta().getProperties());
    writeSystemMetadata(artifactId,artifactInfo);
    return artifactDetail;
  }
  finally {
    parentClassLoader.close();
  }
}