diff --git a/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishActionDescriptor.java b/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishActionDescriptor.java index 6308068..c4ac970 100644 --- a/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishActionDescriptor.java +++ b/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishActionDescriptor.java @@ -23,6 +23,20 @@ public class PublishActionDescriptor { */ private String publishSourceAbsolutePath = ""; + /** + * This is the path of any intermediary collection created as part of the + * publish operation. This will be set by the publish mechanism itself as a + * sort of 're-direct'. It may be a collection, or a file, if the + * pre-validate creates a bundle of some type. + *

+ * The publish mechanism will call the pre-validate. If an intermediary is + * created, that will be passed back in the {@link PublishResult} and set + * subsequently set in the {@link PublishActionDescriptor} passed to the + * actual publish method. The publish method will honor this intermediary + * path. + */ + private String publishIntermediaryAbsolutePath = ""; + /** * Map of free-form string parameters that may pass information to the * publish mechanism to tune the publish behavior @@ -76,6 +90,10 @@ public String toString() { builder.append("publishSourceAbsolutePath=") .append(publishSourceAbsolutePath).append(", "); } + if (publishIntermediaryAbsolutePath != null) { + builder.append("publishIntermediaryAbsolutePath=") + .append(publishIntermediaryAbsolutePath).append(", "); + } if (publishProperties != null) { builder.append("publishProperties=").append( toString(publishProperties.entrySet(), maxLen)); @@ -99,4 +117,20 @@ private String toString(Collection collection, int maxLen) { return builder.toString(); } + /** + * @return the publishIntermediaryAbsolutePath + */ + public String getPublishIntermediaryAbsolutePath() { + return publishIntermediaryAbsolutePath; + } + + /** + * @param publishIntermediaryAbsolutePath + * the publishIntermediaryAbsolutePath to set + */ + public void setPublishIntermediaryAbsolutePath( + String publishIntermediaryAbsolutePath) { + this.publishIntermediaryAbsolutePath = publishIntermediaryAbsolutePath; + } + } diff --git a/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishResult.java b/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishResult.java index bfbb8ca..0e6454f 100644 --- a/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishResult.java +++ b/publisher-api/src/main/java/org/iplantc/de/publish/mechanism/api/PublishResult.java @@ -37,7 +37,7 @@ public class PublishResult { * The publish system will pass this intermediate DIP location between the * result of the validate and the publication step. */ - private String intermediateDipAbsolutePath; + private String intermediateDipAbsolutePath = ""; private PublishStatusEnum publishStatus; diff --git a/publisher-sandbox/src/main/java/org/iplantc/de/publish/sandbox/PublisherEmulator.java b/publisher-sandbox/src/main/java/org/iplantc/de/publish/sandbox/PublisherEmulator.java index e78f0eb..57fa75e 100644 --- a/publisher-sandbox/src/main/java/org/iplantc/de/publish/sandbox/PublisherEmulator.java +++ b/publisher-sandbox/src/main/java/org/iplantc/de/publish/sandbox/PublisherEmulator.java @@ -1,5 +1,5 @@ /** - * + * */ package org.iplantc.de.publish.sandbox; @@ -7,26 +7,53 @@ import org.iplantc.de.publish.discovery.PublisherDiscoveryConfiguration; import org.iplantc.de.publish.discovery.PublisherDiscoveryService; +import org.iplantc.de.publish.mechanism.api.PublishActionDescriptor; +import org.iplantc.de.publish.mechanism.api.PublishContext; +import org.iplantc.de.publish.mechanism.api.PublishMechanism; +import org.iplantc.de.publish.mechanism.api.PublishResult; import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription; import org.iplantc.de.publish.mechanism.api.exception.PublicationException; +import org.iplantc.de.publish.mechanism.api.exception.PublicationRuntimeException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Black box host of publishing mechanisms for testing and validation purposes - * + * * @author Mike Conway - DICE * */ public class PublisherEmulator { + /** + * provided configuration to tune emulator + */ private SandboxConfiguration sandboxConfiguration; + /** + * service that will load plugin jars for publishers + */ private PublisherDiscoveryService publisherDiscoveryService; private List publisherPluginDescriptions; + private PublishContext publishContext; public static final Logger log = LoggerFactory .getLogger(PublisherEmulator.class); + /** + * @return the publishContext + */ + public PublishContext getPublishContext() { + return publishContext; + } + + /** + * @param publishContext + * the publishContext to set + */ + public void setPublishContext(final PublishContext publishContext) { + this.publishContext = publishContext; + } + /** * Default (no values) constructor, note that sandboxConfiguration must be * provided, and init() must be called @@ -47,17 +74,101 @@ public SandboxConfiguration getSandboxConfiguration() { * the sandboxConfiguration to set */ public void setSandboxConfiguration( - SandboxConfiguration sandboxConfiguration) { + final SandboxConfiguration sandboxConfiguration) { this.sandboxConfiguration = sandboxConfiguration; } + public PublishResult triggerPublicationLifecycle(String mechanismName, + PublishActionDescriptor publishActionDescriptor) + throws PublicationException { + + log.info("triggerPublicationLifecycle()"); + if (publishActionDescriptor == null) { + throw new IllegalArgumentException("null publishActionDescriptor"); + } + if (mechanismName == null || mechanismName.isEmpty()) { + throw new IllegalArgumentException("null mechanismName"); + } + validateInit(); + + log.info("looking for mechanism:{}", mechanismName); + + PublisherPluginDescription requestedPlugin = null; + + for (PublisherPluginDescription description : publisherPluginDescriptions) { + if (description.getPublisherName().equals(mechanismName)) { + requestedPlugin = description; + break; + } + } + + if (requestedPlugin == null) { + log.error("unable to find plugin with name:{}", mechanismName); + throw new PublicationException("unknown plugin requested"); + } + + try { + PublishMechanism publishMechanism = (PublishMechanism) requestedPlugin + .getPublisherClass().newInstance(); + + PublishResult result = publishMechanism.preValidate( + publishActionDescriptor, publishContext); + if (result.getResponseCode() != PublishResult.PUBLISH_RESULT_NORMAL) { + log.warn("invalid response:{}", result); + log.warn("for publication request:{}", publishActionDescriptor); + return result; + + } + + log.info("passed pre-validate...now do actual publish..."); + + // TODO: clarify this switching of publish 'target' if prevalidate + // creates an intermediary + + if (!result.getIntermediateDipAbsolutePath().isEmpty()) { + log.info("redirecting to intermediate DIP:{}", + result.getIntermediateDipAbsolutePath()); + publishActionDescriptor + .setPublishIntermediaryAbsolutePath(result + .getIntermediateDipAbsolutePath()); + } + + result = publishMechanism.publish(publishActionDescriptor, + publishContext); + log.info("result of publication:{}", result); + return result; + + } catch (InstantiationException | IllegalAccessException e) { + log.error("cannot create plugin instance from class:{}", + requestedPlugin); + throw new PublicationRuntimeException( + "cannot create plugin instance", e); + } + + } + /** * Initialize publisher based on (required) sandbox configuration - * + * * @throws PublicationException */ public void init() throws PublicationException { log.info("init()"); + validateInit(); + + PublisherDiscoveryConfiguration config = new PublisherDiscoveryConfiguration(); + config.setJarFilePluginDir(sandboxConfiguration.getJarFilePluginDir()); + publisherDiscoveryService = new PublisherDiscoveryService(); + publisherDiscoveryService.setPublisherDiscoveryConfiguration(config); + log.info("initializing plugin discovery service to load plugins"); + publisherDiscoveryService.init(); + publisherPluginDescriptions = publisherDiscoveryService + .listPublisherDescriptions(); + log.info("found plugins:{}", publisherPluginDescriptions); + + } + + private void validateInit() throws PublicationException { if (sandboxConfiguration == null) { throw new PublicationException( "init() cannot be called, no provided sandboxConfiguration"); @@ -69,16 +180,10 @@ public void init() throws PublicationException { "no jar file plugin directory specified"); } - PublisherDiscoveryConfiguration config = new PublisherDiscoveryConfiguration(); - config.setJarFilePluginDir(sandboxConfiguration.getJarFilePluginDir()); - publisherDiscoveryService = new PublisherDiscoveryService(); - publisherDiscoveryService.setPublisherDiscoveryConfiguration(config); - log.info("initializing plugin discovery service to load plugins"); - publisherDiscoveryService.init(); - this.publisherPluginDescriptions = publisherDiscoveryService - .listPublisherDescriptions(); - log.info("found plugins:{}", publisherPluginDescriptions); - + if (publishContext == null) { + log.error("cannot init, no publish context set"); + throw new PublicationException("cannot init, no publish context"); + } } public List listLoadedPlugins() diff --git a/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherEmulatorTest.java b/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherEmulatorTest.java index 276416e..085345d 100644 --- a/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherEmulatorTest.java +++ b/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherEmulatorTest.java @@ -8,11 +8,15 @@ import junit.framework.Assert; +import org.iplantc.de.publish.mechanism.api.PublishActionDescriptor; +import org.iplantc.de.publish.mechanism.api.PublishContext; +import org.iplantc.de.publish.mechanism.api.PublishResult; import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription; import org.iplantc.de.publish.mechanism.api.exception.PublicationException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mockito; /** * @author Mike Conway - DICE @@ -45,6 +49,9 @@ public void testInitWithConfig() throws PublicationException { .setJarFilePluginDir(testingProperties .getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY)); PublisherEmulator emulator = new PublisherEmulator(); + PublishContext publishContext = Mockito.mock(PublishContext.class); + + emulator.setPublishContext(publishContext); emulator.setSandboxConfiguration(sandboxConfiguration); emulator.init(); @@ -57,6 +64,9 @@ public void testListPublicationDescriptions() throws PublicationException { .setJarFilePluginDir(testingProperties .getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY)); PublisherEmulator emulator = new PublisherEmulator(); + PublishContext publishContext = Mockito.mock(PublishContext.class); + + emulator.setPublishContext(publishContext); emulator.setSandboxConfiguration(sandboxConfiguration); emulator.init(); List descriptions = emulator @@ -65,4 +75,24 @@ public void testListPublicationDescriptions() throws PublicationException { } + @Test + public void testPublish() throws PublicationException { + SandboxConfiguration sandboxConfiguration = new SandboxConfiguration(); + sandboxConfiguration + .setJarFilePluginDir(testingProperties + .getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY)); + PublisherEmulator emulator = new PublisherEmulator(); + PublishContext publishContext = Mockito.mock(PublishContext.class); + + emulator.setPublishContext(publishContext); + emulator.setSandboxConfiguration(sandboxConfiguration); + emulator.init(); + PublishActionDescriptor descriptor = new PublishActionDescriptor(); + descriptor.setPublishSourceAbsolutePath("/a/path"); + PublishResult result = emulator.triggerPublicationLifecycle( + PublisherTestingProperties.DUMMY_PUBLISHER, descriptor); + Assert.assertNotNull("null result", result); + + } + } diff --git a/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherTestingProperties.java b/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherTestingProperties.java index c628d98..32fb140 100644 --- a/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherTestingProperties.java +++ b/publisher-sandbox/src/test/java/org/iplantc/de/publish/sandbox/PublisherTestingProperties.java @@ -18,6 +18,7 @@ public class PublisherTestingProperties { public static final String TEST_PUBLISHER_DIR_PROPERTY = "driver.jar.dir"; + public static final String DUMMY_PUBLISHER = "Dummy Logging"; /** *