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);
}
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();
}
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);
}
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);
}
Aggregations