Search in sources :

Example 1 with Injector

use of org.apache.camel.spi.Injector in project camel by apache.

the class XsltEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final CamelContext ctx = getCamelContext();
    final ClassResolver resolver = ctx.getClassResolver();
    final Injector injector = ctx.getInjector();
    LOG.debug("{} using schema resource: {}", this, resourceUri);
    this.xslt = injector.newInstance(XsltBuilder.class);
    if (converter != null) {
        xslt.setConverter(converter);
    }
    boolean useSaxon = false;
    if (transformerFactoryClass == null && (saxon || saxonExtensionFunctions != null)) {
        useSaxon = true;
        transformerFactoryClass = SAXON_TRANSFORMER_FACTORY_CLASS_NAME;
    }
    TransformerFactory factory = transformerFactory;
    if (factory == null && transformerFactoryClass != null) {
        // provide the class loader of this component to work in OSGi environments
        Class<TransformerFactory> factoryClass = resolver.resolveMandatoryClass(transformerFactoryClass, TransformerFactory.class, XsltComponent.class.getClassLoader());
        LOG.debug("Using TransformerFactoryClass {}", factoryClass);
        factory = injector.newInstance(factoryClass);
        if (useSaxon) {
            XsltHelper.registerSaxonConfiguration(ctx, factoryClass, factory, saxonConfiguration);
            XsltHelper.registerSaxonConfigurationProperties(ctx, factoryClass, factory, saxonConfigurationProperties);
            XsltHelper.registerSaxonExtensionFunctions(ctx, factoryClass, factory, saxonExtensionFunctions);
        }
    }
    if (factory != null) {
        LOG.debug("Using TransformerFactory {}", factory);
        xslt.getConverter().setTransformerFactory(factory);
    }
    if (resultHandlerFactory != null) {
        xslt.setResultHandlerFactory(resultHandlerFactory);
    }
    if (errorListener != null) {
        xslt.errorListener(errorListener);
    }
    xslt.setFailOnNullBody(failOnNullBody);
    xslt.transformerCacheSize(transformerCacheSize);
    xslt.setUriResolver(uriResolver);
    xslt.setEntityResolver(entityResolver);
    xslt.setAllowStAX(allowStAX);
    xslt.setDeleteOutputFile(deleteOutputFile);
    configureOutput(xslt, output.name());
    // any additional transformer parameters then make a copy to avoid side-effects
    if (parameters != null) {
        Map<String, Object> copy = new HashMap<String, Object>(parameters);
        xslt.setParameters(copy);
    }
    // must load resource first which sets a template and do a stylesheet compilation to catch errors early
    loadResource(resourceUri);
    // the processor is the xslt builder
    setProcessor(xslt);
}
Also used : CamelContext(org.apache.camel.CamelContext) TransformerFactory(javax.xml.transform.TransformerFactory) HashMap(java.util.HashMap) Injector(org.apache.camel.spi.Injector) XsltBuilder(org.apache.camel.builder.xml.XsltBuilder) ClassResolver(org.apache.camel.spi.ClassResolver)

Example 2 with Injector

use of org.apache.camel.spi.Injector in project camel by apache.

the class InjectorDefaultsTest method testInjectorIsDefaultByDefault.

public void testInjectorIsDefaultByDefault() throws Exception {
    Injector injector = context.getInjector();
    assertIsInstanceOf(DefaultInjector.class, injector);
}
Also used : DefaultInjector(org.apache.camel.impl.DefaultInjector) Injector(org.apache.camel.spi.Injector)

Example 3 with Injector

use of org.apache.camel.spi.Injector in project camel by apache.

the class InjectorDefaultsTest method testNewInstance.

public void testNewInstance() throws Exception {
    Injector injector = context.getInjector();
    MyFoo foo = injector.newInstance(MyFoo.class);
    foo.setName("Claus");
    MyFoo foo2 = injector.newInstance(MyFoo.class);
    assertNotSame(foo, foo2);
    assertEquals("Claus", foo.getName());
    assertNull(foo2.getName());
}
Also used : DefaultInjector(org.apache.camel.impl.DefaultInjector) Injector(org.apache.camel.spi.Injector)

Example 4 with Injector

use of org.apache.camel.spi.Injector in project camel by apache.

the class DefaultFactoryFinderTest method shouldComplainIfInstanceTypeIsNotAsExpected.

@Test
public void shouldComplainIfInstanceTypeIsNotAsExpected() throws ClassNotFoundException, IOException {
    final Injector injector = createMock(Injector.class);
    final TestImplA expected = new TestImplA();
    expect(injector.newInstance(TestImplA.class)).andReturn(expected);
    replay(injector);
    try {
        factoryFinder.newInstances("TestImplA", injector, TestImplB.class);
        fail("ClassCastException should have been thrown");
    } catch (final ClassCastException e) {
        final String message = e.getMessage();
        assertThat(message, matchesPattern("Not instanceof org\\.apache\\.camel\\.impl\\.DefaultFactoryFinderTest\\$TestImplB " + "value: org\\.apache\\.camel\\.impl\\.DefaultFactoryFinderTest\\$TestImplA.*"));
    }
}
Also used : Injector(org.apache.camel.spi.Injector) Test(org.junit.Test)

Example 5 with Injector

use of org.apache.camel.spi.Injector in project camel by apache.

the class InjectorDefaultsTest method testSharedInstance.

public void testSharedInstance() throws Exception {
    Injector injector = context.getInjector();
    MyBarSingleton bar = injector.newInstance(MyBarSingleton.class, new MyBarSingleton());
    bar.setName("Claus");
    MyBarSingleton bar2 = injector.newInstance(MyBarSingleton.class, bar);
    assertSame(bar, bar2);
    assertEquals("Claus", bar.getName());
    assertEquals("Claus", bar2.getName());
}
Also used : DefaultInjector(org.apache.camel.impl.DefaultInjector) Injector(org.apache.camel.spi.Injector)

Aggregations

Injector (org.apache.camel.spi.Injector)6 DefaultInjector (org.apache.camel.impl.DefaultInjector)3 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 CamelContext (org.apache.camel.CamelContext)1 XsltBuilder (org.apache.camel.builder.xml.XsltBuilder)1 ClassResolver (org.apache.camel.spi.ClassResolver)1