Commits

Joe Clark committed 84195fb

Fix potential NullPointerException in JobArtifactRetriever.

Comments (0)

Files changed (1)

src/main/java/com/atlassian/bamboo/plugins/confdeploy/config/JobArtifactRetriever.java

 import com.atlassian.bamboo.plugins.confdeploy.Failure;
 import com.atlassian.bamboo.plugins.confdeploy.Result;
 import com.atlassian.fugue.Either;
+import com.atlassian.fugue.Iterables;
+import com.atlassian.fugue.Option;
+import com.google.common.base.Predicate;
 import org.apache.commons.lang.StringUtils;
 
 import java.io.File;
         this.artifactContext = artifactContext;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public Either<File, Failure> getFileFromArtifact(String artifactString)
+    public Either<File, Failure> getFileFromArtifact(final String artifactString)
     {
         AvailableArtifact artifactDesc = null;
 
         // May need to update the stored artifact definition to a newer format.
         if (!StringUtils.startsWith(artifactString, "v2:"))
         {
-            for (ArtifactSubscriptionContext artifactSubscriptionContext : artifactContext.getSubscriptionContexts())
+            // The artifact configuration was serialized in the old format, where it is identified by name.
+            Option<ArtifactSubscriptionContext> maybeArtifact = Iterables.findFirst(artifactContext.getSubscriptionContexts(), new Predicate<ArtifactSubscriptionContext>()
             {
-                final ArtifactDefinitionContext artifactDefinitionContext = artifactSubscriptionContext.getArtifactDefinitionContext();
-                if (artifactDefinitionContext.getName().equals(artifactString))
+                @Override
+                public boolean apply(ArtifactSubscriptionContext artifactSubscriptionContext)
                 {
-                    artifactDesc = AvailableArtifact.from(artifactSubscriptionContext);
-                    break;
+                    final ArtifactDefinitionContext artifactDefinitionContext = artifactSubscriptionContext.getArtifactDefinitionContext();
+                    return artifactDefinitionContext.getName().equals(artifactString);
+
                 }
+            });
+            if (maybeArtifact.isEmpty())
+            {
+                return Either.right(Result.failure("Unable to locate Artifact subscription named " + artifactString));
             }
-        } else
+            else
+            {
+                artifactDesc = AvailableArtifact.from(maybeArtifact.get());
+            }
+        }
+        else
         {
             artifactDesc = new AvailableArtifact(artifactString);
         }