davsclaus commented on code in PR #23813: URL: https://github.com/apache/camel/pull/23813#discussion_r3371549323
########## core/camel-support/src/main/java/org/apache/camel/support/GroupVariableRepository.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.support; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Stream; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.StreamCache; +import org.apache.camel.StreamCacheException; +import org.apache.camel.spi.BrowsableVariableRepository; +import org.apache.camel.spi.StreamCachingStrategy; +import org.apache.camel.spi.VariableRepository; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.StringHelper; + +/** + * Group {@link VariableRepository} which stores variables in-memory per named group. + * <p> + * Variables are scoped by group name using the syntax {@code groupId:variableName}. This allows sharing variables + * across a subset of routes — wider than per-route but narrower than global scope. + * + * @since 4.21 + */ +public final class GroupVariableRepository extends ServiceSupport implements BrowsableVariableRepository, CamelContextAware { + + private final Map<String, Map<String, Object>> groups = new ConcurrentHashMap<>(); + private CamelContext camelContext; + private StreamCachingStrategy strategy; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public Object getVariable(String name) { + String id = StringHelper.before(name, ":"); + String key = StringHelper.after(name, ":"); + if (id == null || key == null) { + throw new IllegalArgumentException("Name must be groupId:name syntax"); + } + Object answer = null; + Map<String, Object> variables = groups.get(id); + if (variables != null) { + answer = variables.get(key); + } + if (answer instanceof StreamCache sc) { + // reset so the cache is ready to be used as a variable + sc.reset(); + } + return answer; + } + + @Override + public void setVariable(String name, Object value) { + String id = StringHelper.before(name, ":"); + String key = StringHelper.after(name, ":"); + if (id == null || key == null) { + throw new IllegalArgumentException("Name must be groupId:name syntax"); + } + + if (value != null && strategy != null) { + StreamCache sc = convertToStreamCache(value); + if (sc != null) { + value = sc; + } + } + if (value != null) { + Map<String, Object> variables = groups.computeIfAbsent(id, s -> new ConcurrentHashMap<>(8)); + // avoid the NullPointException + variables.put(key, value); + } else { + // if the value is null, we just remove the key from the map + Map<String, Object> variables = groups.get(id); + if (variables != null) { + variables.remove(key); + } + } + } + + public boolean hasVariables() { Review Comment: Minor observation: when `value` is `null`, the key is removed from the inner map, but if the inner map becomes empty, the group entry remains in the `groups` outer map. This means `getGroupIds()` could return group names with zero variables. This is the same behavior as `RouteVariableRepository`, so it's consistent — just noting it since `getGroupIds()` is a new user-facing API. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
