Search in sources :

Example 1 with UUIDIdBuilderImpl

use of org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl in project opencast by opencast.

the class IngestServiceImpl method addZippedMediaPackage.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#addZippedMediaPackage(java.io.InputStream, java.lang.String,
 *      java.util.Map, java.lang.Long)
 */
@Override
public WorkflowInstance addZippedMediaPackage(InputStream zipStream, String workflowDefinitionId, Map<String, String> workflowConfig, Long workflowInstanceId) throws MediaPackageException, IOException, IngestException, NotFoundException, UnauthorizedException {
    // Start a job synchronously. We can't keep the open input stream waiting around.
    Job job = null;
    if (StringUtils.isNotBlank(workflowDefinitionId)) {
        try {
            workflowService.getWorkflowDefinitionById(workflowDefinitionId);
        } catch (WorkflowDatabaseException e) {
            throw new IngestException(e);
        } catch (NotFoundException nfe) {
            logger.warn("Workflow definition {} not found, using default workflow {} instead", workflowDefinitionId, defaultWorkflowDefinionId);
            workflowDefinitionId = defaultWorkflowDefinionId;
        }
    }
    if (workflowInstanceId != null) {
        logger.warn("Deprecated method! Ingesting zipped mediapackage with workflow {}", workflowInstanceId);
    } else {
        logger.info("Ingesting zipped mediapackage");
    }
    ZipArchiveInputStream zis = null;
    Set<String> collectionFilenames = new HashSet<>();
    try {
        // We don't need anybody to do the dispatching for us. Therefore we need to make sure that the job is never in
        // QUEUED state but set it to INSTANTIATED in the beginning and then manually switch it to RUNNING.
        job = serviceRegistry.createJob(JOB_TYPE, INGEST_ZIP, null, null, false, ingestZipJobLoad);
        job.setStatus(Status.RUNNING);
        job = serviceRegistry.updateJob(job);
        // Create the working file target collection for this ingest operation
        String wfrCollectionId = Long.toString(job.getId());
        zis = new ZipArchiveInputStream(zipStream);
        ZipArchiveEntry entry;
        MediaPackage mp = null;
        Map<String, URI> uris = new HashMap<>();
        // Sequential number to append to file names so that, if two files have the same
        // name, one does not overwrite the other (see MH-9688)
        int seq = 1;
        // Folder name to compare with next one to figure out if there's a root folder
        String folderName = null;
        // Indicates if zip has a root folder or not, initialized as true
        boolean hasRootFolder = true;
        // While there are entries write them to a collection
        while ((entry = zis.getNextZipEntry()) != null) {
            try {
                if (entry.isDirectory() || entry.getName().contains("__MACOSX"))
                    continue;
                if (entry.getName().endsWith("manifest.xml") || entry.getName().endsWith("index.xml")) {
                    // Build the mediapackage
                    mp = loadMediaPackageFromManifest(new ZipEntryInputStream(zis, entry.getSize()));
                } else {
                    logger.info("Storing zip entry {}/{} in working file repository collection '{}'", job.getId(), entry.getName(), wfrCollectionId);
                    // Since the directory structure is not being mirrored, makes sure the file
                    // name is different than the previous one(s) by adding a sequential number
                    String fileName = FilenameUtils.getBaseName(entry.getName()) + "_" + seq++ + "." + FilenameUtils.getExtension(entry.getName());
                    URI contentUri = workingFileRepository.putInCollection(wfrCollectionId, fileName, new ZipEntryInputStream(zis, entry.getSize()));
                    collectionFilenames.add(fileName);
                    // Key is the zip entry name as it is
                    String key = entry.getName();
                    uris.put(key, contentUri);
                    ingestStatistics.add(entry.getSize());
                    logger.info("Zip entry {}/{} stored at {}", job.getId(), entry.getName(), contentUri);
                    // Figures out if there's a root folder. Does entry name starts with a folder?
                    int pos = entry.getName().indexOf('/');
                    if (pos == -1) {
                        // No, we can conclude there's no root folder
                        hasRootFolder = false;
                    } else if (hasRootFolder && folderName != null && !folderName.equals(entry.getName().substring(0, pos))) {
                        // Folder name different from previous so there's no root folder
                        hasRootFolder = false;
                    } else if (folderName == null) {
                        // Just initialize folder name
                        folderName = entry.getName().substring(0, pos);
                    }
                }
            } catch (IOException e) {
                logger.warn("Unable to process zip entry {}: {}", entry.getName(), e);
                throw e;
            }
        }
        if (mp == null)
            throw new MediaPackageException("No manifest found in this zip");
        // Determine the mediapackage identifier
        if (mp.getIdentifier() == null || isBlank(mp.getIdentifier().toString()))
            mp.setIdentifier(new UUIDIdBuilderImpl().createNew());
        String mediaPackageId = mp.getIdentifier().toString();
        logger.info("Ingesting mediapackage {} is named '{}'", mediaPackageId, mp.getTitle());
        // Make sure there are tracks in the mediapackage
        if (mp.getTracks().length == 0) {
            logger.warn("Mediapackage {} has no media tracks", mediaPackageId);
        }
        // Update the element uris to point to their working file repository location
        for (MediaPackageElement element : mp.elements()) {
            // Key has root folder name if there is one
            URI uri = uris.get((hasRootFolder ? folderName + "/" : "") + element.getURI().toString());
            if (uri == null)
                throw new MediaPackageException("Unable to map element name '" + element.getURI() + "' to workspace uri");
            logger.info("Ingested mediapackage element {}/{} located at {}", mediaPackageId, element.getIdentifier(), uri);
            URI dest = workingFileRepository.moveTo(wfrCollectionId, FilenameUtils.getName(uri.toString()), mediaPackageId, element.getIdentifier(), FilenameUtils.getName(element.getURI().toString()));
            element.setURI(dest);
            // TODO: This should be triggered somehow instead of being handled here
            if (MediaPackageElements.SERIES.equals(element.getFlavor())) {
                logger.info("Ingested mediapackage {} contains updated series information", mediaPackageId);
                updateSeries(element.getURI());
            }
        }
        // Now that all elements are in place, start with ingest
        logger.info("Initiating processing of ingested mediapackage {}", mediaPackageId);
        WorkflowInstance workflowInstance = ingest(mp, workflowDefinitionId, workflowConfig, workflowInstanceId);
        logger.info("Ingest of mediapackage {} done", mediaPackageId);
        job.setStatus(Job.Status.FINISHED);
        return workflowInstance;
    } catch (ServiceRegistryException e) {
        throw new IngestException(e);
    } catch (MediaPackageException e) {
        job.setStatus(Job.Status.FAILED, Job.FailureReason.DATA);
        throw e;
    } catch (Exception e) {
        if (e instanceof IngestException)
            throw (IngestException) e;
        throw new IngestException(e);
    } finally {
        IOUtils.closeQuietly(zis);
        finallyUpdateJob(job);
        for (String filename : collectionFilenames) {
            workingFileRepository.deleteFromCollection(Long.toString(job.getId()), filename, true);
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) IngestException(org.opencastproject.ingest.api.IngestException) HandleException(org.opencastproject.mediapackage.identifier.HandleException) ConfigurationException(org.opencastproject.util.ConfigurationException) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IngestException(org.opencastproject.ingest.api.IngestException) JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)

Example 2 with UUIDIdBuilderImpl

use of org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl in project opencast by opencast.

the class IngestRestServiceTest method setUp.

@Before
public void setUp() throws Exception {
    testDir = new File("./target", "ingest-rest-service-test");
    if (testDir.exists()) {
        FileUtils.deleteQuietly(testDir);
        logger.info("Removing  " + testDir.getAbsolutePath());
    } else {
        logger.info("Didn't Delete " + testDir.getAbsolutePath());
    }
    testDir.mkdir();
    restService = new IngestRestService();
    // Create a mock ingest service
    IngestService ingestService = EasyMock.createNiceMock(IngestService.class);
    EasyMock.expect(ingestService.createMediaPackage()).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.createMediaPackage("1a6f70ab-4262-4523-9f8e-babce22a1ea8")).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(new UUIDIdBuilderImpl().fromString("1a6f70ab-4262-4523-9f8e-babce22a1ea8")));
    EasyMock.expect(ingestService.addAttachment((URI) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addCatalog((URI) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addTrack((URI) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addTrack((URI) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (String[]) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addAttachment((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addAttachment((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (String[]) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addCatalog((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addCatalog((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (String[]) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addTrack((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addTrack((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), (String[]) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.expect(ingestService.addPartialTrack((InputStream) EasyMock.anyObject(), (String) EasyMock.anyObject(), (MediaPackageElementFlavor) EasyMock.anyObject(), EasyMock.anyLong(), (MediaPackage) EasyMock.anyObject())).andReturn(MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew());
    EasyMock.replay(ingestService);
    // Set the service, and activate the rest endpoint
    restService.setIngestService(ingestService);
    restService.activate(null);
}
Also used : IngestService(org.opencastproject.ingest.api.IngestService) File(java.io.File) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Before(org.junit.Before)

Example 3 with UUIDIdBuilderImpl

use of org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl in project opencast by opencast.

the class WorkflowServiceImplTest method testManyConcurrentWorkflows.

/**
 * Starts many concurrent workflows to test DB deadlock.
 *
 * @throws Exception
 */
@Test
public void testManyConcurrentWorkflows() throws Exception {
    int count = 10;
    Assert.assertEquals(0, service.countWorkflowInstances());
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    WorkflowStateListener stateListener = new WorkflowStateListener(WorkflowState.SUCCEEDED, WorkflowState.FAILED);
    service.addWorkflowListener(stateListener);
    for (int i = 0; i < count; i++) {
        MediaPackage mp = i % 2 == 0 ? mediapackage1 : mediapackage2;
        mp.setIdentifier(new UUIDIdBuilderImpl().createNew());
        instances.add(service.start(workingDefinition, mp, null));
    }
    while (stateListener.countStateChanges() < count) {
        synchronized (stateListener) {
            stateListener.wait();
        }
    }
    Assert.assertEquals(count, service.countWorkflowInstances());
    Assert.assertEquals(count, stateListener.countStateChanges(WorkflowState.SUCCEEDED));
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Example 4 with UUIDIdBuilderImpl

use of org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl in project opencast by opencast.

the class WorkflowStatisticsTest method testStatistics.

/**
 * Tests whether the workflow service statistics are gathered correctly.
 */
@Test
public void testStatistics() throws Exception {
    // Start the workflows and advance them in "random" order. With every definition, an instance is started for every
    // operation that is part of the definition. So we end up with an instance per definition and operation, and there
    // are no two workflows that are in the same operation.
    int total = 0;
    int paused = 0;
    int failed = 0;
    int failing = 0;
    int instantiated = 0;
    int running = 0;
    int stopped = 0;
    int succeeded = 0;
    WorkflowStateListener listener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(listener);
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    for (WorkflowDefinition def : workflowDefinitions) {
        for (int j = 0; j < def.getOperations().size(); j++) {
            mediaPackage.setIdentifier(new UUIDIdBuilderImpl().createNew());
            instances.add(service.start(def, mediaPackage));
            total++;
            paused++;
        }
    }
    // Wait for all the workflows to go into "paused" state
    synchronized (listener) {
        while (listener.countStateChanges() < WORKFLOW_DEFINITION_COUNT * OPERATION_COUNT) {
            listener.wait();
        }
    }
    service.removeWorkflowListener(listener);
    // Resume all of them, so some will be finished, some won't
    int j = 0;
    for (WorkflowInstance instance : instances) {
        WorkflowListener instanceListener = new IndividualWorkflowListener(instance.getId());
        service.addWorkflowListener(instanceListener);
        for (int k = 0; k <= (j % OPERATION_COUNT - 1); k++) {
            synchronized (instanceListener) {
                service.resume(instance.getId(), null);
                instanceListener.wait();
            }
        }
        j++;
    }
    // TODO: Add failed, failing, stopped etc. workflows as well
    // Get the statistics
    WorkflowStatistics stats = service.getStatistics();
    assertEquals(failed, stats.getFailed());
    assertEquals(failing, stats.getFailing());
    assertEquals(instantiated, stats.getInstantiated());
    assertEquals(succeeded, stats.getFinished());
    assertEquals(paused, stats.getPaused());
    assertEquals(running, stats.getRunning());
    assertEquals(stopped, stats.getStopped());
    assertEquals(total, stats.getTotal());
// TODO: Test the operations
// Make sure they are as expected
// for (WorkflowDefinitionReport report : stats.getDefinitions()) {
// 
// }
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowListener(org.opencastproject.workflow.api.WorkflowListener) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Example 5 with UUIDIdBuilderImpl

use of org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl in project opencast by opencast.

the class IngestServiceImpl method createMediaPackage.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#createMediaPackage()
 */
@Override
public MediaPackage createMediaPackage(String mediaPackageId) throws MediaPackageException, ConfigurationException, HandleException {
    MediaPackage mediaPackage;
    try {
        mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(new UUIDIdBuilderImpl().fromString(mediaPackageId));
    } catch (MediaPackageException e) {
        logger.error("INGEST:Failed to create media package " + e.getLocalizedMessage());
        throw e;
    }
    mediaPackage.setDate(new Date());
    logger.info("Created mediapackage {}", mediaPackage);
    return mediaPackage;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Date(java.util.Date) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)

Aggregations

UUIDIdBuilderImpl (org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)7 Test (org.junit.Test)4 MediaPackage (org.opencastproject.mediapackage.MediaPackage)4 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)4 WorkflowStateListener (org.opencastproject.workflow.api.WorkflowStateListener)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)2 NotFoundException (org.opencastproject.util.NotFoundException)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1 JDOMException (org.jdom.JDOMException)1 Before (org.junit.Before)1 IngestException (org.opencastproject.ingest.api.IngestException)1 IngestService (org.opencastproject.ingest.api.IngestService)1