Search in sources :

Example 11 with Effect0

use of org.opencastproject.util.data.Effect0 in project opencast by opencast.

the class EventCommentDatabaseServiceImpl method repopulate.

@Override
public void repopulate(final String indexName) throws Exception {
    final String destinationId = CommentItem.COMMENT_QUEUE_PREFIX + WordUtils.capitalize(indexName);
    try {
        final int total = countComments();
        final int[] current = new int[1];
        current[0] = 0;
        logger.info("Re-populating index '{}' with comments for events. There are {} events with comments to add", indexName, total);
        final int responseInterval = (total < 100) ? 1 : (total / 100);
        final Map<String, List<String>> eventsWithComments = getEventsWithComments();
        for (String orgId : eventsWithComments.keySet()) {
            Organization organization = organizationDirectoryService.getOrganization(orgId);
            SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

                @Override
                protected void run() {
                    for (String eventId : eventsWithComments.get(orgId)) {
                        try {
                            List<EventComment> comments = getComments(eventId);
                            boolean hasOpenComments = !Stream.$(comments).filter(filterOpenComments).toList().isEmpty();
                            boolean needsCutting = !Stream.$(comments).filter(filterNeedsCuttingComment).toList().isEmpty();
                            messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, CommentItem.update(eventId, !comments.isEmpty(), hasOpenComments, needsCutting));
                            current[0] += comments.size();
                            if (responseInterval == 1 || comments.size() > responseInterval || current[0] == total || current[0] % responseInterval < comments.size()) {
                                messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.update(indexName, IndexRecreateObject.Service.Comments, total, current[0]));
                            }
                        } catch (EventCommentDatabaseException e) {
                            logger.error("Unable to retrieve event comments for organization {}", orgId, e);
                        } catch (Throwable t) {
                            logger.error("Unable to update comment on event {} for organization {}", eventId, orgId, t);
                        }
                    }
                }
            });
        }
    } catch (Exception e) {
        logger.warn("Unable to index event comments", e);
        throw new ServiceException(e.getMessage());
    }
    Organization organization = new DefaultOrganization();
    SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

        @Override
        protected void run() {
            messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Comments));
        }
    });
}
Also used : Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) ServiceException(org.osgi.framework.ServiceException) NoResultException(javax.persistence.NoResultException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceException(org.osgi.framework.ServiceException) Effect0(org.opencastproject.util.data.Effect0) ArrayList(java.util.ArrayList) List(java.util.List) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Example 12 with Effect0

use of org.opencastproject.util.data.Effect0 in project opencast by opencast.

the class IoSupport method acquireLock.

/**
 * Acquire a lock on a file. Return a key to release the lock.
 *
 * @return a key to release the lock
 *
 * @throws NotFoundException
 *            if the path to the file, to create a lock for, does not exist
 * @throws IOException
 *            if the file lock can not be created due to access limitations
 */
private static Effect0 acquireLock(File file) throws NotFoundException, IOException {
    final RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "rw");
    } catch (FileNotFoundException e) {
        // make sure to create all parent directories before locking the file
        throw new NotFoundException("Error acquiring lock for " + file.getAbsolutePath(), e);
    }
    final FileLock lock = raf.getChannel().lock();
    return new Effect0() {

        @Override
        protected void run() {
            try {
                lock.release();
            } catch (IOException ignore) {
            }
            IoSupport.closeQuietly(raf);
        }
    };
}
Also used : RandomAccessFile(java.io.RandomAccessFile) Effect0(org.opencastproject.util.data.Effect0) FileNotFoundException(java.io.FileNotFoundException) FileLock(java.nio.channels.FileLock) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 13 with Effect0

use of org.opencastproject.util.data.Effect0 in project opencast by opencast.

the class EventsLoader method addArchiveEntry.

private void addArchiveEntry(final MediaPackage mediaPackage) {
    final User user = securityService.getUser();
    final Organization organization = securityService.getOrganization();
    singleThreadExecutor.execute(new Runnable() {

        @Override
        public void run() {
            SecurityUtil.runAs(securityService, organization, user, new Effect0() {

                @Override
                protected void run() {
                    assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
                }
            });
        }
    });
}
Also used : User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Effect0(org.opencastproject.util.data.Effect0)

Example 14 with Effect0

use of org.opencastproject.util.data.Effect0 in project opencast by opencast.

the class IndexServiceImpl method updateCommentCatalog.

@Override
public void updateCommentCatalog(final Event event, final List<EventComment> comments) throws Exception {
    final SecurityContext securityContext = new SecurityContext(securityService, securityService.getOrganization(), securityService.getUser());
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            securityContext.runInContext(new Effect0() {

                @Override
                protected void run() {
                    try {
                        MediaPackage mediaPackage = getEventMediapackage(event);
                        updateMediaPackageCommentCatalog(mediaPackage, comments);
                        switch(getEventSource(event)) {
                            case WORKFLOW:
                                logger.info("Update workflow mediapacakge {} with updated comments catalog.", event.getIdentifier());
                                Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
                                if (workflowInstance.isNone()) {
                                    logger.error("No workflow instance for event {} found!", event.getIdentifier());
                                    throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
                                }
                                WorkflowInstance instance = workflowInstance.get();
                                instance.setMediaPackage(mediaPackage);
                                updateWorkflowInstance(instance);
                                break;
                            case ARCHIVE:
                                logger.info("Update archive mediapacakge {} with updated comments catalog.", event.getIdentifier());
                                assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
                                break;
                            case SCHEDULE:
                                logger.info("Update scheduled mediapacakge {} with updated comments catalog.", event.getIdentifier());
                                schedulerService.updateEvent(event.getIdentifier(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
                                break;
                            default:
                                logger.error("Unkown event source {}!", event.getSource().toString());
                        }
                    } catch (Exception e) {
                        logger.error("Unable to update event {} comment catalog: {}", event.getIdentifier(), getStackTrace(e));
                    }
                }
            });
        }
    });
}
Also used : Effect0(org.opencastproject.util.data.Effect0) SecurityContext(org.opencastproject.security.util.SecurityContext) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException)

Example 15 with Effect0

use of org.opencastproject.util.data.Effect0 in project opencast by opencast.

the class ThemesServiceDatabaseImpl method repopulate.

@Override
public void repopulate(final String indexName) {
    final String destinationId = ThemeItem.THEME_QUEUE_PREFIX + WordUtils.capitalize(indexName);
    for (final Organization organization : organizationDirectoryService.getOrganizations()) {
        SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

            @Override
            protected void run() {
                try {
                    final List<Theme> themes = getThemes();
                    int total = themes.size();
                    int current = 1;
                    logger.info("Re-populating '{}' index with themes from organization {}. There are {} theme(s) to add to the index.", indexName, securityService.getOrganization().getId(), total);
                    for (Theme theme : themes) {
                        messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, ThemeItem.update(toSerializableTheme(theme)));
                        messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.update(indexName, IndexRecreateObject.Service.Themes, total, current));
                        current++;
                    }
                } catch (ThemesServiceDatabaseException e) {
                    logger.error("Unable to get themes from the database because: {}", ExceptionUtils.getStackTrace(e));
                    throw new IllegalStateException(e);
                }
            }
        });
    }
    Organization organization = new DefaultOrganization();
    SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {

        @Override
        protected void run() {
            messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Themes));
        }
    });
}
Also used : Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Effect0(org.opencastproject.util.data.Effect0) Theme(org.opencastproject.themes.Theme) SerializableTheme(org.opencastproject.message.broker.api.theme.SerializableTheme) ArrayList(java.util.ArrayList) List(java.util.List) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Aggregations

Effect0 (org.opencastproject.util.data.Effect0)17 Organization (org.opencastproject.security.api.Organization)10 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)9 NotFoundException (org.opencastproject.util.NotFoundException)6 SecurityContext (org.opencastproject.security.util.SecurityContext)5 IOException (java.io.IOException)4 List (java.util.List)4 CancellationException (java.util.concurrent.CancellationException)4 ExecutionException (java.util.concurrent.ExecutionException)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)4 SeriesException (org.opencastproject.series.api.SeriesException)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 ServiceException (org.osgi.framework.ServiceException)4 ArrayList (java.util.ArrayList)3 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)3 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)3 ValidationException (net.fortuna.ical4j.model.ValidationException)2 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)2