Search in sources :

Example 1 with Yard

use of org.apache.stanbol.entityhub.servicesapi.yard.Yard in project stanbol by apache.

the class ManagedSiteComponent method activate.

/**
 * Activates this {@link ManagedSiteComponent}. This might be overridden to
 * perform additional configuration. In such cases super should be called
 * before the additional configuration steps.
 * @param context
 * @throws ConfigurationException
 * @throws YardException
 * @throws InvalidSyntaxException
 */
@Activate
protected void activate(final ComponentContext context) throws ConfigurationException, YardException, InvalidSyntaxException {
    this.bundleContext = context.getBundleContext();
    // NOTE that the constructor also validation of the parsed configuration
    this.siteConfiguration = new ManagedSiteConfigurationImpl(context.getProperties());
    if (PROHIBITED_SITE_IDS.contains(siteConfiguration.getId().toLowerCase())) {
        throw new ConfigurationException(SiteConfiguration.ID, String.format("The ID '%s' of this Referenced Site is one of the following " + "prohibited IDs: {} (case insensitive)", siteConfiguration.getId(), PROHIBITED_SITE_IDS));
    }
    log.info(" > initialise Managed Site {}", siteConfiguration.getId());
    SiteUtils.extractSiteMetadata(siteConfiguration, InMemoryValueFactory.getInstance());
    // Initialise the Yard
    final String yardId = siteConfiguration.getYardId();
    String yardFilterString = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Yard.class.getName(), Yard.ID, yardId);
    Filter yardFilter = bundleContext.createFilter(yardFilterString);
    yardTracker = new ServiceTracker(bundleContext, yardFilter, new ServiceTrackerCustomizer() {

        @Override
        public void removedService(ServiceReference reference, Object service) {
            synchronized (yardReferenceLock) {
                if (reference.equals(yardReference)) {
                    deactivateManagedSite();
                    yardReference = null;
                }
                bundleContext.ungetService(reference);
            }
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
        }

        @Override
        public Object addingService(ServiceReference reference) {
            Yard yard = (Yard) bundleContext.getService(reference);
            synchronized (yardReferenceLock) {
                if (yardReference == null) {
                    if (yard != null) {
                        activateManagedSite(yard);
                        yardReference = reference;
                    } else {
                        log.warn("Unable to addService for ServiceReference because" + "unable to obtain referenced Yard via the BundleContext!");
                    }
                } else {
                    log.warn("Tracking two Yard instances with the Yard ID '{}' " + "configured for ManagedSite '{}'", yardId, siteConfiguration.getId());
                    log.warn("used  : {}", yardReference.getProperty(Constants.SERVICE_PID));
                    log.warn("unused: {}", reference.getProperty(Constants.SERVICE_PID));
                }
            }
            return yard;
        }
    });
    yardTracker.open();
// will be moved to a Solr specific implementation
// //chaeck if we are allowed to init an yard with the provided id
// boolean allowInit = false;
// if(configAdmin!= null){
// Configuration[] configs;
// try {
// String yardIdFilter = String.format("(%s=%s)",
// Yard.ID,yardId);
// configs = configAdmin.listConfigurations(yardIdFilter);
// if(configs == null || configs.length < 1){
// allowInit = true;
// }
// } catch (IOException e) {
// log.warn("Unable to access ManagedService configurations ",e);
// }
// } else if (yardTracker.getService() == null){
// log.warn("Unable to check for Yard configuration of ManagedSite {} "
// + "Because the ConfigurationAdmin service is not available");
// log.warn(" -> unable to create YardConfiguration");
// }
// if(allowInit){
// //TODO: This has SolrYard specific code - this needs to be refactored
// String factoryPid = "org.apache.stanbol.entityhub.yard.solr.impl.SolrYard";
// try {
// Configuration config = configAdmin.createFactoryConfiguration(factoryPid,null);
// //configure the required properties
// Dictionary<String,Object> yardConfig = new Hashtable<String,Object>();
// yardConfig.put(Yard.ID, siteConfiguration.getYardId());
// yardConfig.put(Yard.NAME, siteConfiguration.getYardId());
// yardConfig.put(Yard.DESCRIPTION, "Yard for the ManagedSite "+siteConfiguration.getId());
// yardConfig.put("org.apache.stanbol.entityhub.yard.solr.solrUri", siteConfiguration.getId());
// yardConfig.put("org.apache.stanbol.entityhub.yard.solr.useDefaultConfig", true);
// config.update(yardConfig); //this create the solrYard
// } catch (IOException e) {
// log.warn("Unable to create SolrYard configuration for MnagedSite "+siteConfiguration.getId(),e);
// }
// }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ConfigurationException(org.osgi.service.cm.ConfigurationException) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 2 with Yard

use of org.apache.stanbol.entityhub.servicesapi.yard.Yard in project stanbol by apache.

the class YardSite method store.

/**
 * Stores the parsed representation to the Yard and also applies the
 * configured {@link #getFieldMapper() FieldMappings}.
 * @param The representation to store
 */
@Override
public void store(Representation representation) throws ManagedSiteException {
    try {
        Yard yard = getYard();
        fieldMapper.applyMappings(representation, representation, yard.getValueFactory());
        yard.store(representation);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)

Example 3 with Yard

use of org.apache.stanbol.entityhub.servicesapi.yard.Yard in project stanbol by apache.

the class YardSite method store.

/**
 * Stores the parsed representations to the Yard and also applies the
 * configured {@link #getFieldMapper() FieldMappings}.
 * @param The representations to store
 */
@Override
public void store(final Iterable<Representation> representations) throws ManagedSiteException {
    try {
        Yard yard = getYard();
        final ValueFactory vf = yard.getValueFactory();
        yard.store(new Iterable<Representation>() {

            @Override
            public Iterator<Representation> iterator() {
                return new Iterator<Representation>() {

                    Iterator<Representation> it = representations.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public Representation next() {
                        Representation next = it.next();
                        fieldMapper.applyMappings(next, next, vf);
                        return next;
                    }

                    @Override
                    public void remove() {
                        it.remove();
                    }
                };
            }
        });
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) Iterator(java.util.Iterator) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) InMemoryValueFactory(org.apache.stanbol.entityhub.core.model.InMemoryValueFactory) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 4 with Yard

use of org.apache.stanbol.entityhub.servicesapi.yard.Yard in project stanbol by apache.

the class EntityhubComponent method activate.

@Activate
protected void activate(final ComponentContext context) throws ConfigurationException {
    this.bc = context.getBundleContext();
    Dictionary<?, ?> properties = context.getProperties();
    log.info("Activate Entityhub Component:");
    this.entityhubID = OsgiUtils.checkProperty(properties, ID).toString();
    if (entityhubID == null || entityhubID.isEmpty()) {
        throw new ConfigurationException(ID, "The id for the Entityhub MUST NOT be empty!");
    } else {
        log.debug("   + id: {}", entityhubID);
    }
    this.entityhubName = OsgiUtils.checkProperty(properties, NAME, this.entityhubID).toString();
    if (entityhubName.isEmpty()) {
        throw new ConfigurationException(NAME, "The name for the Entityhub MUST NOT be empty!");
    } else {
        log.debug("   + name: {}", entityhubName);
    }
    Object entityhubDescriptionObject = properties.get(DESCRIPTION);
    this.entityhubDescription = entityhubDescriptionObject == null ? null : entityhubDescriptionObject.toString();
    log.debug("   + description: {}", entityhubDescription == null ? "<none>" : entityhubDescription);
    this.entityhubPrefix = OsgiUtils.checkProperty(properties, PREFIX).toString();
    if (entityhubPrefix.isEmpty()) {
        throw new ConfigurationException(PREFIX, "The UIR preix for the Entityub MUST NOT be empty!");
    }
    try {
        new URI(entityhubPrefix);
        log.info("   + prefix: " + entityhubPrefix);
    } catch (URISyntaxException e) {
        throw new ConfigurationException(PREFIX, "The URI prefix for the Entityhub " + "MUST BE an valid URI (prefix=" + entityhubPrefix + ")", e);
    }
    Object defaultSymbolState = properties.get(DEFAULT_SYMBOL_STATE);
    if (defaultSymbolState == null) {
        this.defaultSymblStateString = ManagedEntity.DEFAULT_SYMBOL_STATE.name();
    } else {
        this.defaultSymblStateString = defaultSymbolState.toString();
    }
    Object defaultMappingState = properties.get(DEFAULT_MAPPING_STATE);
    if (defaultMappingState == null) {
        this.defaultMappingStateString = EntityMapping.DEFAULT_MAPPING_STATE.name();
    } else {
        this.defaultMappingStateString = defaultMappingState.toString();
    }
    Object fieldMappingConfigObject = OsgiUtils.checkProperty(properties, FIELD_MAPPINGS);
    if (fieldMappingConfigObject instanceof String[]) {
        this.fieldMappingConfig = (String[]) fieldMappingConfigObject;
    } else {
        throw new ConfigurationException(FIELD_MAPPINGS, "Values for this property must be of type Stirng[]!");
    }
    String entityhubYardId = OsgiUtils.checkProperty(properties, ENTITYHUB_YARD_ID).toString();
    String filterString = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Yard.class.getName(), Yard.ID, entityhubYardId);
    log.debug(" ... tracking EntityhubYard by Filter:" + filterString);
    Filter filter;
    try {
        filter = context.getBundleContext().createFilter(filterString);
    } catch (InvalidSyntaxException e) {
        throw new ConfigurationException(ENTITYHUB_YARD_ID, "Unable to parse OSGI filter '" + filterString + "' for configured Yard id '" + entityhubYardId + "'!", e);
    }
    entityhubYardTracker = new ServiceTracker(context.getBundleContext(), filter, new ServiceTrackerCustomizer() {

        final BundleContext bc = context.getBundleContext();

        @Override
        public void removedService(ServiceReference reference, Object service) {
            if (service.equals(entityhubYard)) {
                entityhubYard = (Yard) entityhubYardTracker.getService();
                updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
            }
            bc.ungetService(reference);
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
            // the service.ranking might have changed ... so check if the
            // top ranked yard is a different one
            Yard newYard = (Yard) entityhubYardTracker.getService();
            if (newYard == null || !newYard.equals(entityhubYard)) {
                // set the new yard
                entityhubYard = newYard;
                // and update the service registration
                updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
            }
        }

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = bc.getService(reference);
            if (service != null) {
                if (// the first added Service or
                entityhubYardTracker.getServiceReference() == null || // the new service as higher ranking as the current
                (reference.compareTo(entityhubYardTracker.getServiceReference()) > 0)) {
                    entityhubYard = (Yard) service;
                    updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
                }
            // else the new service has lower ranking as the currently use one
            }
            // else service == null -> ignore
            return service;
        }
    });
    // start the tracking
    entityhubYardTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ServiceReference(org.osgi.framework.ServiceReference) Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ConfigurationException(org.osgi.service.cm.ConfigurationException) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 5 with Yard

use of org.apache.stanbol.entityhub.servicesapi.yard.Yard in project stanbol by apache.

the class SolrYardIndexingDestinationTest method validateSolrDestination.

/**
 * Checks if the SolrYardIndexingDestination returned by the
 * {@link IndexingConfig} is valid and functional
 * @param config the configuration
 * @throws YardException indicates problems while working with the {@link SolrYard}
 * returned by {@link IndexingDestination#getYard()}
 * @throws IOException indicates problems while validating the SolrArchives
 * created by the {@link IndexingDestination#finalise()} method
 */
private void validateSolrDestination(IndexingConfig config) throws YardException, IOException {
    // get the destination
    IndexingDestination destination = config.getIndexingDestination();
    assertNotNull(destination);
    assertEquals(destination.getClass(), SolrYardIndexingDestination.class);
    // initialise
    assertTrue(destination.needsInitialisation());
    destination.initialise();
    // test that the returned Yard instance is functional
    Yard yard = destination.getYard();
    assertNotNull(yard);
    assertEquals(yard.getClass(), SolrYard.class);
    Representation rep = yard.getValueFactory().createRepresentation("http://www.example.com/entity#123");
    rep.add(NamespaceEnum.rdfs + "label", "test");
    rep.add(NamespaceEnum.rdfs + "description", "Representation to test storage while indexing");
    rep.add(RdfResourceEnum.entityRank.getUri(), Float.valueOf(0.8f));
    yard.store(rep);
    // finalise
    destination.finalise();
    // test the archives
    File expectedSolrArchiveFile = new File(config.getDistributionFolder(), config.getName() + ".solrindex.zip");
    assertTrue(expectedSolrArchiveFile.isFile());
    // validate the archive
    ZipFile archive = new ZipFile(expectedSolrArchiveFile);
    Set<String> expected = new HashSet<String>(EXPECTED_INDEX_ARCHIVE_FILE_NAMES);
    for (Enumeration<? extends ZipEntry> entries = archive.entries(); entries.hasMoreElements(); ) {
        ZipEntry entry = entries.nextElement();
        // the name of the index MUST be the root folder within the Archive!
        assertTrue(entry.getName().startsWith(config.getName()));
        String name = FilenameUtils.getName(entry.getName());
        if (expected.remove(name)) {
            log.info("found expected Entry '{}'", entry.getName());
        }
        Assert.assertFalse("found unexpected Entry '" + entry.getName() + "' in " + "SolrIndexArchive", UNEXPECTED_INDEX_ARCHIVE_FILE_NAMES.contains(name));
    }
    assertTrue("missing Files in index archive: " + expected, expected.isEmpty());
// TODO: reimplement to validate the created bundle!
// //check for the solrArchive reference file and validate required properties
// File expectedSolrArchiveReferenceFile =
// new File(,config.getName()+".solrindex.ref");
// assertTrue(expectedSolrArchiveReferenceFile.isFile());
// Properties solrRefProperties = new Properties();
// solrRefProperties.load(new FileInputStream(expectedSolrArchiveReferenceFile));
// assertTrue(solrRefProperties.getProperty("Index-Archive").equals(expectedSolrArchiveFile.getName()));
// assertTrue(solrRefProperties.getProperty("Name") != null);
}
Also used : SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IndexingDestination(org.apache.stanbol.entityhub.indexing.core.IndexingDestination) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ZipFile(java.util.zip.ZipFile) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)30 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)25 Test (org.junit.Test)23 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)6 SolrYard (org.apache.stanbol.entityhub.yard.solr.impl.SolrYard)4 Activate (org.apache.felix.scr.annotations.Activate)3 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)3 ServiceReference (org.osgi.framework.ServiceReference)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3 ServiceTracker (org.osgi.util.tracker.ServiceTracker)3 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)3 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)2 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)2 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)2 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2 SesameYard (org.apache.stanbol.entityhub.yard.sesame.SesameYard)2 Filter (org.osgi.framework.Filter)2 File (java.io.File)1