Search in sources :

Example 11 with ComponentContext

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

the class OakVirtualInstanceBuilder method createViewChecker.

@Override
protected ViewChecker createViewChecker() throws Exception {
    getOakViewChecker();
    return new ViewChecker() {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        private SimulatedLease lease = new SimulatedLease(getResourceResolverFactory(), leaseCollection, getSlingId());

        protected void activate(ComponentContext c) throws Throwable {
            OakViewChecker pinger = getOakViewChecker();
            PrivateAccessor.invoke(pinger, "activate", new Class[] { ComponentContext.class }, new Object[] { c });
        }

        @Override
        public void checkView() {
            try {
                lease.updateDescriptor(getConfig());
            } catch (Exception e) {
                logger.error("run: could not update lease: " + e);
            }
        }

        public void run() {
            heartbeatAndCheckView();
        }

        @Override
        public void heartbeatAndCheckView() {
            //                as soon as I see others the descriptor is updated
            try {
                lease.updateLeaseAndDescriptor(getConfig());
            } catch (Exception e) {
                logger.error("run: could not update lease: " + e, e);
            }
            try {
                getOakViewChecker().run();
            } catch (Exception e) {
                logger.error("run: could not ping: " + e, e);
            }
            if (!getIdMapService().isInitialized()) {
                if (!getIdMapService().waitForInit(1500)) {
                    fail("init didnt work");
                }
            }
        }
    };
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) ViewChecker(org.apache.sling.discovery.base.commons.ViewChecker) OakViewChecker(org.apache.sling.discovery.oak.pinger.OakViewChecker) OakViewChecker(org.apache.sling.discovery.oak.pinger.OakViewChecker) Logger(org.slf4j.Logger)

Example 12 with ComponentContext

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

the class CompositeHealthCheck method execute.

@Override
public Result execute() {
    final ComponentContext localCtx = this.componentContext;
    final ServiceReference referenceToThis = localCtx == null ? null : localCtx.getServiceReference();
    Result result = referenceToThis == null ? null : checkForRecursion(referenceToThis, new HashSet<String>());
    if (result != null) {
        // return recursion error
        return result;
    }
    FormattingResultLog resultLog = new FormattingResultLog();
    List<HealthCheckExecutionResult> executionResults = healthCheckExecutor.execute(HealthCheckSelector.tags(filterTags));
    resultLog.debug("Executing {} HealthChecks selected by tags {}", executionResults.size(), Arrays.asList(filterTags));
    result = new CompositeResult(resultLog, executionResults);
    return result;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HashSet(java.util.HashSet)

Example 13 with ComponentContext

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

the class HealthCheckTestsProviderTest method setup.

@Before
public void setup() throws InvalidSyntaxException {
    setupTimestamp = System.currentTimeMillis();
    final ComponentContext ctx = Mockito.mock(ComponentContext.class);
    // context properties
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HealthCheckTestsProvider.PROP_TAG_GROUPS, TAG_GROUPS);
    props.put(Constants.SERVICE_PID, getClass().getName());
    Mockito.when(ctx.getProperties()).thenReturn(props);
    // bundle context
    final BundleContext bc = Mockito.mock(BundleContext.class);
    Mockito.when(ctx.getBundleContext()).thenReturn(bc);
    // HealthCheck ServiceReferences mocks
    Mockito.when(bc.getServiceReferences(Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<ServiceReference[]>() {

        @Override
        public ServiceReference[] answer(InvocationOnMock invocation) throws Throwable {
            return getMockReferences(bc, (String) invocation.getArguments()[1]);
        }
    });
    provider = new HealthCheckTestsProvider() {

        {
            activate(ctx);
        }
    };
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Hashtable(java.util.Hashtable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HealthCheckTestsProvider(org.apache.sling.hc.junitbridge.HealthCheckTestsProvider) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 14 with ComponentContext

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

the class RhinoJavaScriptEngineFactory method activate.

// ---------- SCR integration
@Activate
protected void activate(final ComponentContext context, final RhinoJavaScriptEngineFactoryConfiguration configuration) {
    Dictionary<?, ?> props = context.getProperties();
    boolean debugging = getProperty("org.apache.sling.scripting.javascript.debug", props, context.getBundleContext(), false);
    optimizationLevel = readOptimizationLevel(configuration);
    // setup the wrap factory
    wrapFactory = new SlingWrapFactory();
    // initialize the Rhino Context Factory
    SlingContextFactory.setup(this);
    Context cx = Context.enter();
    setEngineName(getEngineName() + " (" + cx.getImplementationVersion() + ")");
    languageVersion = String.valueOf(cx.getLanguageVersion());
    Context.exit();
    setExtensions(ECMA_SCRIPT_EXTENSION, ESP_SCRIPT_EXTENSION);
    setMimeTypes("text/javascript", "application/ecmascript", "application/javascript");
    setNames("javascript", ECMA_SCRIPT_EXTENSION, ESP_SCRIPT_EXTENSION);
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    if (contextFactory instanceof SlingContextFactory) {
        ((SlingContextFactory) contextFactory).setDebugging(debugging);
    }
    // set the dynamic class loader as the application class loader
    final DynamicClassLoaderManager dclm = this.dynamicClassLoaderManager;
    if (dclm != null) {
        contextFactory.initApplicationClassLoader(dynamicClassLoaderManager.getDynamicClassLoader());
    }
    log.info("Activated with optimization level {}", optimizationLevel);
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory) ContextFactory(org.mozilla.javascript.ContextFactory) SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory) SlingWrapFactory(org.apache.sling.scripting.javascript.helper.SlingWrapFactory) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager) Activate(org.osgi.service.component.annotations.Activate)

Example 15 with ComponentContext

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

the class LanguageDetectionEngineTest method testNonTextContent.

@Test
public void testNonTextContent() throws EngineException, ConfigurationException, LangDetectException, IOException {
    LanguageDetectionEnhancementEngine langIdEngine = new LanguageDetectionEnhancementEngine();
    ComponentContext context = new MockComponentContext();
    context.getProperties().put(EnhancementEngine.PROPERTY_NAME, "langdetect");
    langIdEngine.activate(context);
    ContentItem ci = ciFactory.createContentItem(new StringSource("123"));
    langIdEngine.computeEnhancements(ci);
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) 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)35 Test (org.junit.Test)16 Dictionary (java.util.Dictionary)14 BundleContext (org.osgi.framework.BundleContext)11 Hashtable (java.util.Hashtable)5 Expectations (org.jmock.Expectations)4 HashMap (java.util.HashMap)3 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)3 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)3 BeforeClass (org.junit.BeforeClass)3 ServiceReference (org.osgi.framework.ServiceReference)3 IRI (org.apache.clerezza.commons.rdf.IRI)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 LuceneLabelTokenizer (org.apache.stanbol.enhancer.engines.entitylinking.labeltokenizer.lucene.LuceneLabelTokenizer)2 Mockery (org.jmock.Mockery)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1