001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.support;
018
019import java.io.InputStream;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.ExpressionIllegalSyntaxException;
024import org.apache.camel.IsSingleton;
025import org.apache.camel.spi.Language;
026import org.apache.camel.util.IOHelper;
027import org.apache.camel.util.ResourceHelper;
028
029/**
030 * Base language for {@link Language} implementations.
031 */
032public abstract class LanguageSupport implements Language, IsSingleton, CamelContextAware {
033
034    public static final String RESOURCE = "resource:";
035
036    private CamelContext camelContext;
037
038    public CamelContext getCamelContext() {
039        return camelContext;
040    }
041
042    public void setCamelContext(CamelContext camelContext) {
043        this.camelContext = camelContext;
044    }
045
046    @Override
047    public boolean isSingleton() {
048        return true;
049    }
050
051    /**
052     * Loads the resource if the given expression is referring to an external resource by using
053     * the syntax <tt>resource:scheme:uri<tt>.
054     * If the expression is not referring to a resource, then its returned as is.
055     * <p/>
056     * For example <tt>resource:classpath:mygroovy.groovy</tt> to refer to a groovy script on the classpath.
057     *
058     * @param expression the expression
059     * @return the expression
060     * @throws ExpressionIllegalSyntaxException is thrown if error loading the resource
061     */
062    protected String loadResource(String expression) throws ExpressionIllegalSyntaxException {
063        if (camelContext != null && expression.startsWith(RESOURCE)) {
064            String uri = expression.substring(RESOURCE.length());
065            InputStream is = null;
066            try {
067                is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uri);
068                expression = camelContext.getTypeConverter().mandatoryConvertTo(String.class, is);
069            } catch (Exception e) {
070                throw new ExpressionIllegalSyntaxException(expression, e);
071            } finally {
072                IOHelper.close(is);
073            }
074        }
075        return expression;
076    }
077}