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 */ 017 package org.apache.camel.component.language; 018 019 import java.io.InputStream; 020 021 import org.apache.camel.CamelExchangeException; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Expression; 024 import org.apache.camel.impl.DefaultProducer; 025 import org.apache.camel.util.IOHelper; 026 import org.apache.camel.util.ObjectHelper; 027 import org.apache.camel.util.ResourceHelper; 028 import org.apache.camel.util.ServiceHelper; 029 030 /** 031 * Language producer. 032 * 033 * @version 034 */ 035 public class LanguageProducer extends DefaultProducer { 036 037 public LanguageProducer(LanguageEndpoint endpoint) { 038 super(endpoint); 039 } 040 041 public void process(Exchange exchange) throws Exception { 042 // is there a custom expression in the header? 043 Expression exp = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, Expression.class); 044 if (exp == null) { 045 String script = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, String.class); 046 if (script != null) { 047 // the script may be a file: so resolve it before using 048 script = getEndpoint().resolveScript(script); 049 exp = getEndpoint().getLanguage().createExpression(script); 050 } 051 } 052 // if not fallback to use expression from endpoint 053 if (exp == null && getEndpoint().isCacheScript()) { 054 exp = getEndpoint().getExpression(); 055 } 056 057 // fallback and use resource uri from endpoint 058 if (exp == null) { 059 String script = getEndpoint().getScript(); 060 061 if (script == null && getEndpoint().getResourceUri() == null) { 062 // no script to execute 063 throw new CamelExchangeException("No script to evaluate", exchange); 064 } 065 066 // the script can be a resource from the endpoint, 067 // or refer to a resource itself 068 // or just be a plain string 069 InputStream is = null; 070 if (script == null) { 071 is = getEndpoint().getResourceAsInputStream(); 072 } else if (ResourceHelper.hasScheme(script)) { 073 is = ResourceHelper.resolveMandatoryResourceAsInputStream(getEndpoint().getCamelContext().getClassResolver(), script); 074 } 075 if (is != null) { 076 try { 077 script = getEndpoint().getCamelContext().getTypeConverter().convertTo(String.class, exchange, is); 078 } finally { 079 IOHelper.close(is); 080 } 081 } 082 083 if (script != null) { 084 // create the expression from the script 085 exp = getEndpoint().getLanguage().createExpression(script); 086 // expression was resolved from resource 087 getEndpoint().setContentResolvedFromResource(true); 088 // if we cache then set this as expression on endpoint so we don't re-create it again 089 if (getEndpoint().isCacheScript()) { 090 getEndpoint().setExpression(exp); 091 } 092 } else { 093 // no script to execute 094 throw new CamelExchangeException("No script to evaluate", exchange); 095 } 096 } 097 098 ObjectHelper.notNull(exp, "expression"); 099 100 Object result; 101 try { 102 result = exp.evaluate(exchange, Object.class); 103 log.debug("Evaluated expression as: {} with: {}", result, exchange); 104 } finally { 105 if (!getEndpoint().isCacheScript()) { 106 // some languages add themselves as a service which we then need to remove if we are not cached 107 ServiceHelper.stopService(exp); 108 getEndpoint().getCamelContext().removeService(exp); 109 } 110 } 111 112 // set message body if transform is enabled 113 if (getEndpoint().isTransform()) { 114 if (exchange.hasOut()) { 115 exchange.getOut().setBody(result); 116 } else { 117 exchange.getIn().setBody(result); 118 } 119 } 120 } 121 122 @Override 123 public LanguageEndpoint getEndpoint() { 124 return (LanguageEndpoint) super.getEndpoint(); 125 } 126 }