Search in sources :

Example 1 with Language

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

the class OsgiLanguageResolverTest method testOsgiResolverFindLanguageTest.

@Test
public void testOsgiResolverFindLanguageTest() throws IOException {
    CamelContext camelContext = new DefaultCamelContext();
    OsgiLanguageResolver resolver = new OsgiLanguageResolver(getBundleContext());
    Language language = resolver.resolveLanguage("simple", camelContext);
    assertNotNull("We should find simple language", language);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Language(org.apache.camel.spi.Language) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 2 with Language

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

the class CamelMockBundleContext method getService.

public Object getService(@SuppressWarnings("rawtypes") ServiceReference reference) {
    String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS);
    String classNames0 = classNames != null ? classNames[0] : null;
    String pid = (String) reference.getProperty(Constants.SERVICE_PID);
    if (classNames0 != null && classNames0.equals("org.apache.camel.core.osgi.test.MyService")) {
        return new MyService();
    } else if (pid != null && pid.equals(SERVICE_PID_PREFIX + "org.apache.camel.core.osgi.test.MyService")) {
        return new MyService();
    } else if (classNames0 != null && classNames0.equals(ComponentResolver.class.getName())) {
        return new ComponentResolver() {

            public Component resolveComponent(String name, CamelContext context) throws Exception {
                if (name.equals("file_test")) {
                    return new FileComponent();
                }
                return null;
            }
        };
    } else if (classNames0 != null && classNames0.equals(LanguageResolver.class.getName())) {
        return new LanguageResolver() {

            public Language resolveLanguage(String name, CamelContext context) {
                if (name.equals("simple")) {
                    return new SimpleLanguage();
                }
                return null;
            }
        };
    } else {
        return null;
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) LanguageResolver(org.apache.camel.spi.LanguageResolver) SimpleLanguage(org.apache.camel.language.simple.SimpleLanguage) Language(org.apache.camel.spi.Language) MyService(org.apache.camel.core.osgi.test.MyService) FileComponent(org.apache.camel.component.file.FileComponent) ComponentResolver(org.apache.camel.spi.ComponentResolver) SimpleLanguage(org.apache.camel.language.simple.SimpleLanguage)

Example 3 with Language

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

the class OsgiLanguageResolver method resolveLanguage.

public Language resolveLanguage(String name, CamelContext context) {
    // lookup in registry first
    Language lang = ResolverHelper.lookupLanguageInRegistryWithFallback(context, name);
    if (lang != null) {
        return lang;
    }
    lang = getLanguage(name, context);
    if (lang != null) {
        return lang;
    }
    LanguageResolver resolver = getLanguageResolver("default", context);
    if (resolver != null) {
        return resolver.resolveLanguage(name, context);
    }
    throw new NoSuchLanguageException(name);
}
Also used : LanguageResolver(org.apache.camel.spi.LanguageResolver) Language(org.apache.camel.spi.Language) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException)

Example 4 with Language

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

the class ExpressionMapper method convert.

@Override
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
    try {
        if (currentExchange.get() == null) {
            throw new IllegalStateException("Current exchange has not been set for ExpressionMapper");
        }
        Expression exp;
        // Resolve the language being used for this expression and evaluate
        Exchange exchange = currentExchange.get();
        Language expLang = exchange.getContext().resolveLanguage(getLanguagePart());
        String scheme = getSchemePart();
        if (scheme != null && (scheme.equalsIgnoreCase("classpath") || scheme.equalsIgnoreCase("file") || scheme.equalsIgnoreCase("http"))) {
            String path = getPathPart();
            try {
                exp = expLang.createExpression(resolveScript(scheme + ":" + path));
            } catch (IOException e) {
                throw new IllegalStateException("Expression script specified but not found", e);
            }
        } else {
            exp = expLang.createExpression(getExpressionPart());
        }
        return exp.evaluate(exchange, destinationClass);
    } finally {
        done();
    }
}
Also used : Exchange(org.apache.camel.Exchange) Language(org.apache.camel.spi.Language) Expression(org.apache.camel.Expression) IOException(java.io.IOException)

Example 5 with Language

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

the class LanguageComponent method createEndpoint.

protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    String name = ObjectHelper.before(remaining, ":");
    String script = ObjectHelper.after(remaining, ":");
    // no script then remaining is the language name
    if (name == null && script == null) {
        name = remaining;
    }
    if (ObjectHelper.isEmpty(name)) {
        throw new IllegalArgumentException("Illegal syntax. Name of language not given in uri: " + uri);
    }
    Language language = getCamelContext().resolveLanguage(name);
    String resourceUri = null;
    String resource = script;
    if (resource != null) {
        if (resource.startsWith(RESOURCE)) {
            resource = resource.substring(RESOURCE.length());
        }
        if (ResourceHelper.hasScheme(resource)) {
            // the script is a uri for a resource
            resourceUri = resource;
            // then the script should be null
            script = null;
        } else {
            // the script is provided as text in the uri, so decode to utf-8
            script = URLDecoder.decode(script, "UTF-8");
            // then the resource should be null
            resourceUri = null;
        }
    }
    LanguageEndpoint endpoint = new LanguageEndpoint(uri, this, language, null, resourceUri);
    endpoint.setScript(script);
    setProperties(endpoint, parameters);
    return endpoint;
}
Also used : Language(org.apache.camel.spi.Language)

Aggregations

Language (org.apache.camel.spi.Language)44 Expression (org.apache.camel.Expression)15 Test (org.junit.Test)13 Exchange (org.apache.camel.Exchange)6 CamelContext (org.apache.camel.CamelContext)5 Predicate (org.apache.camel.Predicate)5 NoSuchLanguageException (org.apache.camel.NoSuchLanguageException)4 SqlTest (org.apache.camel.builder.sql.SqlTest)4 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 File (java.io.File)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 IOException (java.io.IOException)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)2 LanguageResolver (org.apache.camel.spi.LanguageResolver)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1