Search in sources :

Example 71 with ComponentContext

use of org.osgi.service.component.ComponentContext in project spring-roo by spring-projects.

the class PomManagementServiceImplTest method setUpWorkingDirectory.

/**
 * Sets the working directory for the service under test
 *
 * @param relativePath the desired working directory relative to the
 *            "o.s.r.project" package
 * @throws IOException
 */
private void setUpWorkingDirectory(final String relativePath) throws IOException {
    final ComponentContext mockComponentContext = mock(ComponentContext.class);
    final BundleContext mockBundleContext = mock(BundleContext.class);
    when(mockComponentContext.getBundleContext()).thenReturn(mockBundleContext);
    final File workingDirectory = new File("target/test-classes/org/springframework/roo/project", relativePath);
    when(mockBundleContext.getProperty(OSGiUtils.ROO_WORKING_DIRECTORY_PROPERTY)).thenReturn(workingDirectory.getCanonicalPath());
    service.activate(mockComponentContext);
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 72 with ComponentContext

use of org.osgi.service.component.ComponentContext in project stanbol by apache.

the class CacheComponent method activate.

@Activate
protected void activate(final ComponentContext context) throws ConfigurationException, YardException, IllegalStateException, InvalidSyntaxException {
    if (context == null || context.getProperties() == null) {
        throw new IllegalStateException(String.format("Invalid ComponentContext parsed in activate (context=%s)", context));
    }
    this.cc = context;
    Object value = context.getProperties().get(Cache.ADDITIONAL_MAPPINGS);
    if (value instanceof String[]) {
        this.additionalMappings = (String[]) value;
    } else if (value instanceof String) {
        this.additionalMappings = new String[] { (String) value };
    } else if (value instanceof Collection<?>) {
        try {
            additionalMappings = ((Collection<?>) value).toArray(new String[((Collection<?>) value).size()]);
        } catch (ArrayStoreException e) {
            throw new ConfigurationException(Cache.ADDITIONAL_MAPPINGS, "Additional Mappings MUST BE a String, String[] or Collection<String>!", e);
        }
    } else {
        additionalMappings = null;
    }
    String yardId = OsgiUtils.checkProperty(context.getProperties(), Cache.CACHE_YARD).toString();
    String cacheFilter = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Yard.class.getName(), Yard.ID, yardId);
    yardTracker = new ServiceTracker(context.getBundleContext(), context.getBundleContext().createFilter(cacheFilter), new ServiceTrackerCustomizer() {

        // store the reference to the ComponentContext to avoid NPE if deactivate
        // is called for the CacheComponent
        final ComponentContext cc = context;

        @Override
        public void removedService(ServiceReference reference, Object service) {
            if (service.equals(yard)) {
                yard = (Yard) yardTracker.getService();
                updateServiceRegistration(cc, yard, additionalMappings, nsPrefixService);
            }
            cc.getBundleContext().ungetService(reference);
        }

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

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = cc.getBundleContext().getService(reference);
            if (service != null) {
                if (// the first added Service or
                yardTracker.getServiceReference() == null || // the new service as higher ranking as the current
                (reference.compareTo(yardTracker.getServiceReference()) > 0)) {
                    yard = (Yard) service;
                    updateServiceRegistration(cc, yard, additionalMappings, nsPrefixService);
                }
            // else the new service has lower ranking as the currently use one
            }
            // else service == null -> ignore
            return service;
        }
    });
    yardTracker.open();
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ComponentContext(org.osgi.service.component.ComponentContext) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Collection(java.util.Collection) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 73 with ComponentContext

use of org.osgi.service.component.ComponentContext in project stanbol by apache.

the class LangIdEngineTest method testEngine.

/**
 * Test the engine and validates the created enhancements
 * @throws EngineException
 * @throws IOException
 * @throws ConfigurationException
 */
@Test
public void testEngine() throws EngineException, IOException, ConfigurationException {
    LangIdEnhancementEngine langIdEngine = new LangIdEnhancementEngine();
    ComponentContext context = new MockComponentContext();
    context.getProperties().put(EnhancementEngine.PROPERTY_NAME, "langid");
    langIdEngine.activate(context);
    ContentItem ci = ciFactory.createContentItem(new StringSource(text));
    langIdEngine.computeEnhancements(ci);
    HashMap<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(langIdEngine.getClass().getName()));
    int textAnnotationCount = validateAllTextAnnotations(ci.getMetadata(), text, expectedValues);
    assertEquals("A single TextAnnotation is expected", 1, textAnnotationCount);
    // even through this tests do not validate service quality but rather
    // the correct integration of the CELI service as EnhancementEngine
    // we expect the "en" is detected for the parsed text
    assertEquals("The detected language for text '" + text + "' MUST BE 'en'", "en", EnhancementEngineHelper.getLanguage(ci));
    int entityAnnoNum = validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    assertEquals("No EntityAnnotations are expected", 0, entityAnnoNum);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) LangIdEnhancementEngine(org.apache.stanbol.enhancer.engines.langid.LangIdEnhancementEngine) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 74 with ComponentContext

use of org.osgi.service.component.ComponentContext in project stanbol by apache.

the class LanguageDetectionEngineTest method testEngine.

/**
 * Test the engine and validates the created enhancements
 * @throws EngineException
 * @throws IOException
 * @throws ConfigurationException
 * @throws LangDetectException
 */
@Test
public void testEngine() throws EngineException, ConfigurationException, LangDetectException, IOException {
    LOG.info("Testing engine: {}", TEST_FILE_NAMES[0]);
    InputStream in = LanguageDetectionEngineTest.class.getClassLoader().getResourceAsStream(TEST_FILE_NAMES[0]);
    assertNotNull("failed to load resource " + TEST_FILE_NAMES[0], in);
    String text = IOUtils.toString(in, "UTF-8");
    in.close();
    LanguageDetectionEnhancementEngine langIdEngine = new LanguageDetectionEnhancementEngine();
    ComponentContext context = new MockComponentContext();
    context.getProperties().put(EnhancementEngine.PROPERTY_NAME, "langdetect");
    langIdEngine.activate(context);
    ContentItem ci = ciFactory.createContentItem(new StringSource(text));
    langIdEngine.computeEnhancements(ci);
    HashMap<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(langIdEngine.getClass().getName()));
    int textAnnotationCount = validateAllTextAnnotations(ci.getMetadata(), text, expectedValues);
    assertTrue("A TextAnnotation is expected", textAnnotationCount > 0);
    // even through this tests do not validate detection quality
    // we expect the "en" is detected as best guess for the parsed text
    assertEquals("The detected language for text '" + text + "' MUST BE 'en'", "en", EnhancementEngineHelper.getLanguage(ci));
    int entityAnnoNum = validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    assertEquals("No EntityAnnotations are expected", 0, entityAnnoNum);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) InputStream(java.io.InputStream) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Aggregations

ComponentContext (org.osgi.service.component.ComponentContext)74 BundleContext (org.osgi.framework.BundleContext)46 Test (org.junit.Test)32 Dictionary (java.util.Dictionary)17 Before (org.junit.Before)12 JaxbUser (org.opencastproject.security.api.JaxbUser)11 SecurityService (org.opencastproject.security.api.SecurityService)11 Hashtable (java.util.Hashtable)10 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)10 JaxbRole (org.opencastproject.security.api.JaxbRole)10 File (java.io.File)9 Bundle (org.osgi.framework.Bundle)9 URI (java.net.URI)8 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)7 User (org.opencastproject.security.api.User)7 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)7 ServiceReference (org.osgi.framework.ServiceReference)7 Workspace (org.opencastproject.workspace.api.Workspace)6 Version (org.osgi.framework.Version)6 IOException (java.io.IOException)5