Implementing a Custom Artifact Type Handler

Infinity provides the interface IArtifactHandler, which can be implemented to handle artifacts of a specific artifact type.

..
import org.eclipse.stardust.common.annotations.SPI;
import org.eclipse.stardust.common.annotations.Status;
import org.eclipse.stardust.common.annotations.UseRestriction;
import org.eclipse.stardust.engine.api.runtime.ArtifactType;
import org.eclipse.stardust.engine.api.runtime.DeployedRuntimeArtifact;
import org.eclipse.stardust.engine.api.runtime.RuntimeArtifact;

@SPI(status=Status.Stable, useRestriction=UseRestriction.Public)
public interface IArtifactHandler
{

   public interface Factory
   {
      IArtifactHandler getInstance();
   }

   ArtifactType getArtifactType();

   String getArtifactContentType(RuntimeArtifact runtimeArtifact);

   RuntimeArtifact preProcess(RuntimeArtifact runtimeArtifact);

   void afterOverwrite(DeployedRuntimeArtifact deployedRuntimeArtifact);

   void beforeDelete(DeployedRuntimeArtifact deployedRuntimeArtifact);

   void afterDelete(long oid);
}

Implementation Methods

The artifact handler provides implementation methods for the following purposes:

Returning the supported Artifact Type

To retrieve the ArtifactType of the artifact, implement the following method:

ArtifactType getArtifactType();

For example:

   public static final ArtifactType ARTIFACT_TYPE = new BenchmarkDefinitionArtifactType();

   public ArtifactType getArtifactType()
   {
      return ARTIFACT_TYPE;
   }

Getting the Artifact Content Type

The following method returns the MIME-type of the artifact.

String getArtifactContentType(RuntimeArtifact runtimeArtifact);

For example:

   public static final String MIME_TYPE = "application/json";

   public String getArtifactContentType(RuntimeArtifact runtimeArtifact)
   {
      return MIME_TYPE;
   }

Pre-processing the artifact before Deployment

The following method is called before deploy and overwrite:

RuntimeArtifact preProcess(RuntimeArtifact runtimeArtifact);

Parameter is the input artifact. The handler can pre-process this artifact before it is deployed, for example compile, convert or validate it. It is possible to change all fields of the RuntimeArtifact prior to deployment, e.g. the artifactId to change the file ending for a compiled or processed artifact. The pre-processed artifact is returned.

   public RuntimeArtifact preProcess(RuntimeArtifact runtimeArtifact)
   {
      return runtimeArtifact;
   }

Notifying Overwrite

Implement the following method to notify after a runtime artifact is overwritten:

void afterOverwrite(DeployedRuntimeArtifact deployedRuntimeArtifact);

For example:

   public void afterOverwrite(DeployedRuntimeArtifact deployedRuntimeArtifact)
   {
      BenchmarkUtils.removeBenchmarkFromCache(deployedRuntimeArtifact.getOid());
   }

Performing Actions before Deletion

With method beforeDelete, the artifact handler can e.g. check referential integrity and prevent a deletion if the artifact is still being required.

void beforeDelete(DeployedRuntimeArtifact deployedRuntimeArtifact);

Purging Caches after Deletion

Using method afterDelete the artifact handler can purge caches after the artifact was deleted.

void afterDelete(long oid);

Handling Artifacts of Type Benchmark Definition

The BenchmarkDefinitionArtifactHandler is an implementation of the IArtifactHandler, which handles artifacts of type BenchmarkDefinitionArtifactType.

...

import org.eclipse.stardust.engine.api.runtime.ArtifactType;
import org.eclipse.stardust.engine.api.runtime.DeployedRuntimeArtifact;
import org.eclipse.stardust.engine.api.runtime.RuntimeArtifact;
import org.eclipse.stardust.engine.core.benchmark.BenchmarkUtils;
import org.eclipse.stardust.engine.core.spi.artifact.IArtifactHandler;

/**
 * This {@link IArtifactHandler} handles artifacts of type
 * {@link BenchmarkDefinitionArtifactType}.
 * <p>
 * The handled artifacts are all of content type
 * {@link BenchmarkDefinitionArtifactHandler#MIME_TYPE}.<br>
 * Internal caches for benchmark definitions are flushed if a benchmark definition runtime
 * artifact is overwritten or deleted.
 *
 * @author Roland.Stamm
 */
public class BenchmarkDefinitionArtifactHandler
      implements IArtifactHandler, IArtifactHandler.Factory
{

   public static final ArtifactType ARTIFACT_TYPE = new BenchmarkDefinitionArtifactType();

   public static final String MIME_TYPE = "application/json";

   @Override
   public IArtifactHandler getInstance()
   {
      return new BenchmarkDefinitionArtifactHandler();
   }

   @Override
   public ArtifactType getArtifactType()
   {
      return ARTIFACT_TYPE;
   }

   @Override
   public String getArtifactContentType(RuntimeArtifact runtimeArtifact)
   {
      // all benchmarks are in json format.
      return MIME_TYPE;
   }

   @Override
   public RuntimeArtifact preProcess(RuntimeArtifact runtimeArtifact)
   {
      return runtimeArtifact;
   }

   @Override
   public void afterOverwrite(DeployedRuntimeArtifact deployedRuntimeArtifact)
   {
      BenchmarkUtils.removeBenchmarkFromCache(deployedRuntimeArtifact.getOid());
   }

   @Override
   public void beforeDelete(DeployedRuntimeArtifact deployedRuntimeArtifact)
   {
      ProcessInstanceQuery query = ProcessInstanceQuery.findAlive();
      
      query.where(ProcessInstanceQuery.BENCHMARK_OID.greaterThan(0L));
      
      ResultIterator rawResult = new ProcessInstanceQueryEvaluator(query, QueryServiceUtils.getDefaultEvaluationContext()).executeFetch();
      
      if (rawResult.hasNext())
      {
      throw new IllegalOperationException(BpmRuntimeError.ATDB_RUNTIME_ARTIFACT_IN_USE.raise(deployedRuntimeArtifact.getOid()));
      }
   }

   @Override
   public void afterDelete(long oid)
   {
      BenchmarkUtils.removeBenchmarkFromCache(oid);
   }

}