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.freemarker;
018
019 import java.net.URL;
020 import java.util.Map;
021
022 import freemarker.cache.URLTemplateLoader;
023 import freemarker.template.Configuration;
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.component.ResourceBasedComponent;
026 import org.apache.camel.util.ObjectHelper;
027
028 /**
029 * Freemarker component.
030 */
031 public class FreemarkerComponent extends ResourceBasedComponent {
032
033 private Configuration configuration;
034 private Configuration noCacheConfiguration;
035
036 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
037 FreemarkerEndpoint endpoint = new FreemarkerEndpoint(uri, this, remaining);
038
039 // should we use regular configuration or no cache (content cache is default true)
040 Configuration config;
041 String encoding = getAndRemoveParameter(parameters, "encoding", String.class);
042 if (ObjectHelper.isNotEmpty(encoding)) {
043 endpoint.setEncoding(encoding);
044 }
045 boolean cache = getAndRemoveParameter(parameters, "contentCache", Boolean.class, Boolean.TRUE);
046 if (cache) {
047 config = getConfiguration();
048 } else {
049 config = getNoCacheConfiguration();
050 }
051
052 endpoint.setConfiguration(config);
053 return endpoint;
054 }
055
056 public synchronized Configuration getConfiguration() {
057 if (configuration == null) {
058 configuration = new Configuration();
059 configuration.setTemplateLoader(new URLTemplateLoader() {
060 @Override
061 protected URL getURL(String name) {
062 return getResourceLoader().getClassLoader().getResource(name);
063 }
064 });
065 }
066 return (Configuration) configuration.clone();
067 }
068
069 public void setConfiguration(Configuration configuration) {
070 this.configuration = configuration;
071 }
072
073 private synchronized Configuration getNoCacheConfiguration() {
074 if (noCacheConfiguration == null) {
075 // create a clone of the regular configuration
076 noCacheConfiguration = (Configuration) getConfiguration().clone();
077 // set this one to not use cache
078 noCacheConfiguration.setCacheStorage(new NoCacheStorage());
079 }
080 return noCacheConfiguration;
081 }
082
083 }