Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.regex.PatternSyntaxException;
import java.util.stream.Stream;

import org.apache.camel.catalog.impl.AbstractCamelCatalog;
import org.apache.camel.catalog.impl.AbstractCachingCamelCatalog;
import org.apache.camel.catalog.impl.CatalogHelper;
import org.apache.camel.tooling.model.ArtifactModel;
import org.apache.camel.tooling.model.BaseModel;
Expand All @@ -56,7 +56,7 @@
/**
* Default {@link CamelCatalog}.
*/
public class DefaultCamelCatalog extends AbstractCamelCatalog implements CamelCatalog {
public class DefaultCamelCatalog extends AbstractCachingCamelCatalog implements CamelCatalog {

private static final String MODELS_CATALOG = "org/apache/camel/catalog/models.properties";
private static final String SCHEMAS_XML = "org/apache/camel/catalog/schemas";
Expand Down Expand Up @@ -99,10 +99,6 @@ public class DefaultCamelCatalog extends AbstractCamelCatalog implements CamelCa
private final Map<String, String> extraDataFormats = new HashMap<>();
private final Map<String, String> extraDataFormatsJSonSchema = new HashMap<>();

// cache of operation -> result
private final Map<String, Object> cache = new HashMap<>();

private boolean caching;
private VersionManager versionManager = new DefaultVersionManager(this);
private RuntimeProvider runtimeProvider = new DefaultRuntimeProvider(this);

Expand All @@ -119,7 +115,8 @@ public DefaultCamelCatalog() {
* @param caching whether to use cache
*/
public DefaultCamelCatalog(boolean caching) {
this.caching = caching;
super(caching);

setJSonSchemaResolver(new CamelCatalogJSonSchemaResolver(
this, extraComponents, extraComponentsJSonSchema, extraDataFormats, extraDataFormatsJSonSchema));
}
Expand All @@ -136,17 +133,17 @@ public void setRuntimeProvider(RuntimeProvider runtimeProvider) {
this.runtimeProvider.setCamelCatalog(this);

// invalidate the cache
cache.clear();
super.clearCache();
}

@Override
public void enableCache() {
caching = true;
super.setCaching(true);
}

@Override
public boolean isCaching() {
return caching;
return super.isCaching();
}

@Override
Expand All @@ -163,11 +160,11 @@ public VersionManager getVersionManager() {
public void addComponent(String name, String className) {
extraComponents.put(name, className);
// invalidate the cache
cache.remove(FIND_COMPONENT_NAMES);
cache.remove(FIND_COMPONENT_LABELS);
cache.remove(LIST_COMPONENTS_AS_JSON);
getCache().remove(FIND_COMPONENT_NAMES);
getCache().remove(FIND_COMPONENT_LABELS);
getCache().remove(LIST_COMPONENTS_AS_JSON);

cache.remove(SUMMARY_AS_JSON);
getCache().remove(SUMMARY_AS_JSON);
}

@Override
Expand All @@ -182,11 +179,11 @@ public void addComponent(String name, String className, String jsonSchema) {
public void addDataFormat(String name, String className) {
extraDataFormats.put(name, className);
// invalidate the cache
cache.remove(FIND_DATA_FORMAT_NAMES);
cache.remove(FIND_DATA_FORMAT_LABELS);
cache.remove(LIST_DATA_FORMATS_AS_JSON);
getCache().remove(FIND_DATA_FORMAT_NAMES);
getCache().remove(FIND_DATA_FORMAT_LABELS);
getCache().remove(LIST_DATA_FORMATS_AS_JSON);

cache.remove(SUMMARY_AS_JSON);
getCache().remove(SUMMARY_AS_JSON);
}

@Override
Expand All @@ -208,7 +205,7 @@ public boolean loadVersion(String version) {
return true;
} else if (versionManager.loadVersion(version)) {
// invalidate existing cache if we loaded a new version
cache.clear();
super.clearCache();
return true;
}
return false;
Expand Down Expand Up @@ -640,46 +637,6 @@ private static boolean matchArtifact(ArtifactModel<?> am, String groupId, String
&& (version == null || version.isBlank() || version.equals(am.getVersion()));
}

@SuppressWarnings("unchecked")
private <T> T cache(String name, Supplier<T> loader) {
if (caching) {
T t = (T) cache.get(name);
if (t == null) {
t = loader.get();
if (t != null) {
cache.put(name, t);
}
}
return t;
} else {
return loader.get();
}
}

private <T> T cache(String key, String name, Function<String, T> loader) {
return doGetCache(key, name, loader);
}

@SuppressWarnings("unchecked")
private <T> T doGetCache(String key, String name, Function<String, T> loader) {
if (caching) {
T t = (T) cache.get(key);
if (t == null) {
t = loader.apply(name);
if (t != null) {
cache.put(key, t);
}
}
return t;
} else {
return loader.apply(name);
}
}

private <T> T cache(String name, Function<String, T> loader) {
return doGetCache(name, name, loader);
}

private String loadResource(String file) {
try (InputStream is = versionManager.getResourceAsStream(file)) {
return is != null ? CatalogHelper.loadText(is) : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.catalog.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public abstract class AbstractCachingCamelCatalog extends AbstractCamelCatalog {
private final Map<String, Object> cache = new ConcurrentHashMap<>();
private boolean caching;

/**
* Creates the {@link AbstractCachingCamelCatalog} without caching enabled.
*/
protected AbstractCachingCamelCatalog() {
this(false);
}

/**
* Creates the {@link AbstractCachingCamelCatalog}
*
* @param caching whether to use cache
*/
protected AbstractCachingCamelCatalog(boolean caching) {
this.caching = caching;
}

/**
* To turn caching on or off
*/
public boolean isCaching() {
return caching;
}

/**
* To turn caching on or off
*/
public void setCaching(boolean caching) {
this.caching = caching;

if (!this.caching) {
clearCache();
}
}

protected Map<String, Object> getCache() {
return this.cache;
}

protected void clearCache() {
cache.clear();
}

protected <T> T cache(String key, String name, Function<String, T> loader) {
return doGetCache(key, name, loader);
}

protected <T> T cache(String name, Function<String, T> loader) {
return doGetCache(name, name, loader);
}

@SuppressWarnings("unchecked")
protected <T> T cache(String name, Supplier<T> loader) {
if (caching) {
T t = (T) cache.get(name);
if (t == null) {
t = loader.get();
if (t != null) {
cache.put(name, t);
}
}
return t;
} else {
return loader.get();
}
}

@SuppressWarnings("unchecked")
protected <T> T doGetCache(String key, String name, Function<String, T> loader) {
if (caching) {
T t = (T) cache.get(key);
if (t == null) {
t = loader.apply(name);
if (t != null) {
cache.put(key, t);
}
}
return t;
} else {
return loader.apply(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
/**
* Base class for both the runtime RuntimeCamelCatalog from camel-core and the complete CamelCatalog from camel-catalog.
*/
@SuppressWarnings("unused")
public abstract class AbstractCamelCatalog {

private static final Pattern SYNTAX_PATTERN = Pattern.compile("([\\w.]+)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
package org.apache.camel.catalog.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.apache.camel.CamelContext;
import org.apache.camel.catalog.RuntimeCamelCatalog;
import org.apache.camel.spi.annotations.JdkService;
Expand All @@ -35,12 +31,17 @@
* Default {@link RuntimeCamelCatalog}.
*/
@JdkService(RuntimeCamelCatalog.FACTORY)
public class DefaultRuntimeCamelCatalog extends AbstractCamelCatalog implements RuntimeCamelCatalog {
public class DefaultRuntimeCamelCatalog extends AbstractCachingCamelCatalog implements RuntimeCamelCatalog {

private CamelContext camelContext;
// cache of operation -> result
private final Map<String, Object> cache = new HashMap<>();
private boolean caching = true;

public DefaultRuntimeCamelCatalog() {
this(true);
}

public DefaultRuntimeCamelCatalog(boolean caching) {
super(caching);
}

@Override
public CamelContext getCamelContext() {
Expand All @@ -53,28 +54,14 @@ public void setCamelContext(CamelContext camelContext) {
this.setJSonSchemaResolver(new CamelContextJSonSchemaResolver(camelContext));
}

/**
* To turn caching on or off
*/
public boolean isCaching() {
return caching;
}

/**
* To turn caching on or off
*/
public void setCaching(boolean caching) {
this.caching = caching;
}

@Override
public void start() {
// noop
}

@Override
public void stop() {
cache.clear();
super.clearCache();
}

@Override
Expand Down Expand Up @@ -146,21 +133,4 @@ public String mainJSonSchema() {
public MainModel mainModel() {
return cache("main-model", "main-model", k -> super.mainModel());
}

@SuppressWarnings("unchecked")
private <T> T cache(String key, String name, Function<String, T> loader) {
if (caching) {
T t = (T) cache.get(key);
if (t == null) {
t = loader.apply(name);
if (t != null) {
cache.put(key, t);
}
}
return t;
} else {
return loader.apply(name);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Comparator<BaseModel<?>> compareTitle() {
return (m1, m2) -> m1.getTitle().compareToIgnoreCase(m2.getTitle());
}

public abstract String getKind();
public abstract Kind getKind();

public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public ComponentModel() {
}

@Override
public String getKind() {
return "component";
public Kind getKind() {
return Kind.component;
}

public String getScheme() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public DataFormatModel() {
}

@Override
public String getKind() {
return "dataformat";
public Kind getKind() {
return Kind.dataformat;
}

public String getModelName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void setGroup(String group) {
}

@Override
public String getKind() {
return "console";
public Kind getKind() {
return Kind.console;
}

}
Loading