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.runtimecatalog;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.camel.CamelContext;
023
024/**
025 * Default {@link RuntimeCamelCatalog}.
026 */
027public class DefaultRuntimeCamelCatalog extends AbstractCamelCatalog implements RuntimeCamelCatalog {
028
029    // cache of operation -> result
030    private final Map<String, Object> cache = new HashMap<>();
031    private boolean caching;
032
033    /**
034     * Creates the {@link RuntimeCamelCatalog} without caching enabled.
035     *
036     * @param camelContext  the camel context
037     */
038    public DefaultRuntimeCamelCatalog(CamelContext camelContext) {
039        this(camelContext, false);
040    }
041
042    /**
043     * Creates the {@link RuntimeCamelCatalog}
044     *
045     * @param camelContext  the camel context
046     * @param caching  whether to use cache
047     */
048    public DefaultRuntimeCamelCatalog(CamelContext camelContext, boolean caching) {
049        this.caching = caching;
050        setJSonSchemaResolver(new CamelContextJSonSchemaResolver(camelContext));
051    }
052
053    @Override
054    public void start() throws Exception {
055        // noop
056    }
057
058    @Override
059    public void stop() throws Exception {
060        cache.clear();
061    }
062
063    @Override
064    public String modelJSonSchema(String name) {
065        String answer = null;
066        if (caching) {
067            answer = (String) cache.get("model-" + name);
068        }
069
070        if (answer == null) {
071            answer = getJSonSchemaResolver().getModelJSonSchema(name);
072            if (caching) {
073                cache.put("model-" + name, answer);
074            }
075        }
076
077        return answer;
078    }
079
080    @Override
081    public String componentJSonSchema(String name) {
082        String answer = null;
083        if (caching) {
084            answer = (String) cache.get("component-" + name);
085        }
086
087        if (answer == null) {
088            answer = getJSonSchemaResolver().getComponentJSonSchema(name);
089            if (caching) {
090                cache.put("component-" + name, answer);
091            }
092        }
093
094        return answer;
095    }
096
097    @Override
098    public String dataFormatJSonSchema(String name) {
099        String answer = null;
100        if (caching) {
101            answer = (String) cache.get("dataformat-" + name);
102        }
103
104        if (answer == null) {
105            answer = getJSonSchemaResolver().getDataFormatJSonSchema(name);
106            if (caching) {
107                cache.put("dataformat-" + name, answer);
108            }
109        }
110
111        return answer;
112    }
113
114    @Override
115    public String languageJSonSchema(String name) {
116        // if we try to look method then its in the bean.json file
117        if ("method".equals(name)) {
118            name = "bean";
119        }
120
121        String answer = null;
122        if (caching) {
123            answer = (String) cache.get("language-" + name);
124        }
125
126        if (answer == null) {
127            answer = getJSonSchemaResolver().getLanguageJSonSchema(name);
128            if (caching) {
129                cache.put("language-" + name, answer);
130            }
131        }
132
133        return answer;
134    }
135
136}