Search in sources :

Example 11 with ServiceException

use of org.osgi.framework.ServiceException in project opencast by opencast.

the class SearchServiceImpl method populateIndex.

protected void populateIndex(String systemUserName) {
    long instancesInSolr = 0L;
    try {
        instancesInSolr = indexManager.count();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    if (instancesInSolr > 0) {
        logger.debug("Search index found");
        return;
    }
    if (instancesInSolr == 0L) {
        logger.info("No search index found");
        logger.info("Starting population of search index from database");
        Iterator<Tuple<MediaPackage, String>> mediaPackages;
        try {
            mediaPackages = persistence.getAllMediaPackages();
        } catch (SearchServiceDatabaseException e) {
            logger.error("Unable to load the search entries: {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        }
        int errors = 0;
        while (mediaPackages.hasNext()) {
            try {
                Tuple<MediaPackage, String> mediaPackage = mediaPackages.next();
                String mediaPackageId = mediaPackage.getA().getIdentifier().toString();
                Organization organization = organizationDirectory.getOrganization(mediaPackage.getB());
                securityService.setOrganization(organization);
                securityService.setUser(SecurityUtil.createSystemUser(systemUserName, organization));
                AccessControlList acl = persistence.getAccessControlList(mediaPackageId);
                Date modificationDate = persistence.getModificationDate(mediaPackageId);
                Date deletionDate = persistence.getDeletionDate(mediaPackageId);
                indexManager.add(mediaPackage.getA(), acl, deletionDate, modificationDate);
            } catch (Exception e) {
                logger.error("Unable to index search instances:", e);
                if (retryToPopulateIndex(systemUserName)) {
                    logger.warn("Trying to re-index search index later. Aborting for now.");
                    return;
                }
                errors++;
            } finally {
                securityService.setOrganization(null);
                securityService.setUser(null);
            }
        }
        if (errors > 0)
            logger.error("Skipped {} erroneous search entries while populating the search index", errors);
        logger.info("Finished populating search index");
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) Organization(org.opencastproject.security.api.Organization) ServiceException(org.osgi.framework.ServiceException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SearchServiceDatabaseException(org.opencastproject.search.impl.persistence.SearchServiceDatabaseException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Date(java.util.Date) SearchServiceDatabaseException(org.opencastproject.search.impl.persistence.SearchServiceDatabaseException) ServiceException(org.osgi.framework.ServiceException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Tuple(org.opencastproject.util.data.Tuple)

Example 12 with ServiceException

use of org.osgi.framework.ServiceException in project opencast by opencast.

the class SeriesServiceImpl method populateSolr.

/**
 * If the solr index is empty, but there are series in the database, populate the solr index.
 */
private void populateSolr(String systemUserName) {
    long instancesInSolr;
    try {
        instancesInSolr = index.count();
    } catch (Exception e) {
        throw new IllegalStateException("Repopulating series Solr index failed", e);
    }
    if (instancesInSolr != 0L) {
        return;
    }
    logger.info("The series index is empty. Populating it now with series");
    try {
        List<SeriesEntity> allSeries = persistence.getAllSeries();
        final int total = allSeries.size();
        if (total == 0) {
            logger.info("No series found. Repopulating index finished.");
            return;
        }
        int current = 0;
        for (SeriesEntity series : allSeries) {
            current++;
            // Run as the superuser so we get all series, regardless of organization or role
            Organization organization = orgDirectory.getOrganization(series.getOrganization());
            securityService.setOrganization(organization);
            securityService.setUser(SecurityUtil.createSystemUser(systemUserName, organization));
            index.updateIndex(DublinCoreXmlFormat.read(series.getDublinCoreXML()));
            String id = series.getSeriesId();
            AccessControlList acl = AccessControlParser.parseAcl(series.getAccessControl());
            if (acl != null) {
                index.updateSecurityPolicy(id, acl);
            }
            index.updateOptOutStatus(id, series.isOptOut());
            // log progress
            if (current % 100 == 0) {
                logger.info("Indexing series {}/{} ({} percent done)", current, total, current * 100 / total);
            }
        }
        logger.info("Finished populating series search index");
    } catch (Exception e) {
        logger.warn("Unable to index series instances", e);
        throw new ServiceException(e.getMessage());
    } finally {
        securityService.setOrganization(null);
        securityService.setUser(null);
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) ServiceException(org.osgi.framework.ServiceException) SeriesEntity(org.opencastproject.series.impl.persistence.SeriesEntity) ServiceException(org.osgi.framework.ServiceException) SeriesException(org.opencastproject.series.api.SeriesException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) FunctionException(org.opencastproject.util.data.FunctionException)

Example 13 with ServiceException

use of org.osgi.framework.ServiceException in project opencast by opencast.

the class WorkflowServiceSolrIndex method activate.

/**
 * Activates the index by configuring solr with the server url that must have been set previously.
 */
public void activate(String systemUserName) {
    // Set up the solr server
    if (solrServerUrl != null) {
        solrServer = SolrServerFactory.newRemoteInstance(solrServerUrl);
    } else {
        try {
            setupSolr(new File(solrRoot));
        } catch (IOException e) {
            throw new IllegalStateException("Unable to connect to solr at " + solrRoot, e);
        } catch (SolrServerException e) {
            throw new IllegalStateException("Unable to connect to solr at " + solrRoot, e);
        }
    }
    // If the solr is empty, add all of the existing workflows
    long instancesInSolr = 0;
    try {
        instancesInSolr = count();
    } catch (WorkflowDatabaseException e) {
        throw new IllegalStateException(e);
    }
    if (instancesInSolr == 0) {
        logger.info("The workflow index is empty, looking for workflows to index");
        // this may be a new index, so get all of the existing workflows and index them
        List<String> workflowPayloads;
        try {
            workflowPayloads = serviceRegistry.getJobPayloads(WorkflowServiceImpl.Operation.START_WORKFLOW.toString());
        } catch (ServiceRegistryException e) {
            logger.error("Unable to load the workflows jobs: {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        }
        final int total = workflowPayloads.size();
        if (total == 0) {
            logger.info("No workflows found. Repopulating index finished.");
            return;
        }
        logger.info("Populating the workflow index with {} workflows", total);
        int current = 0;
        for (String payload : workflowPayloads) {
            current++;
            WorkflowInstance instance = null;
            try {
                instance = WorkflowParser.parseWorkflowInstance(payload);
                Organization organization = instance.getOrganization();
                securityService.setOrganization(organization);
                securityService.setUser(SecurityUtil.createSystemUser(systemUserName, organization));
                index(instance);
            } catch (WorkflowParsingException | WorkflowDatabaseException e) {
                logger.warn("Skipping restoring of workflow {}", payload, e);
            }
            if (current % 100 == 0) {
                logger.info("Indexing workflow {}/{} ({} percent done)", current, total, current * 100 / total);
            }
        }
        logger.info("Finished populating the workflow search index");
    }
}
Also used : Organization(org.opencastproject.security.api.Organization) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ServiceException(org.osgi.framework.ServiceException) File(java.io.File) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 14 with ServiceException

use of org.osgi.framework.ServiceException in project ecf by eclipse.

the class Configuration method configure.

public Configuration configure() {
    PrintWriter writer = null;
    boolean isNewZookeeperData = false;
    try {
        String dataDirName = (String) getConfigProperties().get(ZOOKEEPER_DATADIR);
        // if no data directory name is specified, we randomly pick one.
        if (DATADIR_DEFAULT.equals(dataDirName)) {
            dataDirName = randomDirName();
        }
        this.zookeeperDataFile = new File(new File(getConfigProperties().get(ZOOKEEPER_TEMPDIR).toString()), dataDirName);
        isNewZookeeperData = this.zookeeperDataFile.mkdir();
        this.zookeeperDataFile.deleteOnExit();
        if (!isNewZookeeperData) {
            /*
				 * the same data directory is being reused, we try emptying it
				 * to avoid data corruption
				 */
            clean();
        }
        // $NON-NLS-1$
        this.zooConfFile = new File(this.zookeeperDataFile, "zoo.cfg");
        this.zooConfFile.createNewFile();
        this.zooConfFile.deleteOnExit();
        if (getConfigProperties().containsKey(ZOODISCOVERY_FLAVOR_CENTRALIZED)) {
            this.setFlavor(FLAVOR.CENTRALIZED);
            this.serverIps = parseIps();
            if (this.serverIps.size() != 1) {
                String msg = "ZooDiscovery property " + ZOODISCOVERY_FLAVOR_CENTRALIZED + " must contain exactly one IP address designating the location of the ZooDiscovery instance playing this central role.";
                Logger.log(LogService.LOG_ERROR, msg, null);
                throw new ServiceException(msg);
            }
        } else if (getConfigProperties().containsKey(ZOODISCOVERY_FLAVOR_REPLICATED)) {
            this.setFlavor(FLAVOR.REPLICATED);
            this.serverIps = parseIps();
            if (!this.serverIps.contains(Geo.getHost())) {
                this.serverIps.add(Geo.getHost());
            }
            if (this.serverIps.size() < 2) {
                String msg = // $NON-NLS-1$
                "Industrial Discovery property " + IDiscoveryConfig.ZOODISCOVERY_FLAVOR_REPLICATED + " must contain at least one IP address which is not localhost.";
                Logger.log(LogService.LOG_ERROR, msg, null);
                throw new ServiceException(msg);
            }
        } else if (getConfigProperties().containsKey(ZOODISCOVERY_FLAVOR_STANDALONE)) {
            this.setFlavor(FLAVOR.STANDALONE);
            this.serverIps = parseIps();
        }
        Collections.sort(this.serverIps);
        if (this.isQuorum()) {
            String myip = Geo.getHost();
            int myId = this.serverIps.indexOf(myip);
            // $NON-NLS-1$
            File myIdFile = new File(getZookeeperDataFile(), "myid");
            myIdFile.createNewFile();
            myIdFile.deleteOnExit();
            writer = new PrintWriter(myIdFile);
            writer.print(myId);
            writer.flush();
            writer.close();
        }
        writer = new PrintWriter(this.zooConfFile);
        if (this.isQuorum()) {
            for (int i = 0; i < this.serverIps.size(); i++) {
                writer.println(// $NON-NLS-1$
                "server." + i + // $NON-NLS-1$
                "=" + this.serverIps.get(i) + // $NON-NLS-1$
                ":" + getServerPort() + // $NON-NLS-1$
                ":" + getElectionPort());
            }
        }
        for (String k : getConfigProperties().keySet()) {
            if (k.startsWith(ZOODISCOVERY_PREFIX)) {
                /*
					 * Ignore properties that are not intended for ZooKeeper
					 * internal configuration
					 */
                continue;
            }
            // $NON-NLS-1$
            writer.println(k + "=" + getConfigProperties().get(k));
        }
        writer.flush();
        writer.close();
    } catch (IOException e) {
        Logger.log(LogService.LOG_ERROR, e.getMessage(), e);
    } finally {
        if (writer != null)
            writer.close();
    }
    return this;
}
Also used : ServiceException(org.osgi.framework.ServiceException) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 15 with ServiceException

use of org.osgi.framework.ServiceException in project aries by apache.

the class ServiceRegistryContextTest method checkServiceListLookup.

@Test
public void checkServiceListLookup() throws NamingException {
    BundleMock mock = new BundleMock("scooby.doo", new Properties());
    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
    InitialContext ctx = new InitialContext();
    String className = Runnable.class.getName();
    Runnable t = Skeleton.newMock(Runnable.class);
    // we don't want the default service
    reg.unregister();
    ServiceRegistration reg = bc.registerService(className, t, null);
    ServiceRegistration reg2 = bc.registerService("java.lang.Thread", new Thread(), null);
    Context ctx2 = (Context) ctx.lookup("osgi:servicelist/java.lang.Runnable");
    Runnable r = (Runnable) ctx2.lookup(String.valueOf(reg.getReference().getProperty(Constants.SERVICE_ID)));
    r.run();
    Skeleton.getSkeleton(t).assertCalled(new MethodCall(Runnable.class, "run"));
    reg.unregister();
    try {
        r.run();
        fail("Should have received a ServiceException");
    } catch (ServiceException e) {
        assertEquals("service exception has the wrong type", ServiceException.UNREGISTERED, e.getType());
    }
    try {
        ctx2.lookup(String.valueOf(reg2.getReference().getProperty(Constants.SERVICE_ID)));
        fail("Expected a NameNotFoundException");
    } catch (NameNotFoundException e) {
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) BundleContext(org.osgi.framework.BundleContext) ServiceException(org.osgi.framework.ServiceException) NameNotFoundException(javax.naming.NameNotFoundException) BundleMock(org.apache.aries.mocks.BundleMock) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialContext(javax.naming.InitialContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

ServiceException (org.osgi.framework.ServiceException)16 Organization (org.opencastproject.security.api.Organization)6 IOException (java.io.IOException)5 NotFoundException (org.opencastproject.util.NotFoundException)5 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)4 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)4 SeriesException (org.opencastproject.series.api.SeriesException)3 Effect0 (org.opencastproject.util.data.Effect0)3 File (java.io.File)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Properties (java.util.Properties)2 Context (javax.naming.Context)2 InitialContext (javax.naming.InitialContext)2 BundleMock (org.apache.aries.mocks.BundleMock)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 Test (org.junit.Test)2 AccessControlList (org.opencastproject.security.api.AccessControlList)2