|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Intergral GmbH |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.intergral.deep.agent.api.plugin; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * This type allows the evaluator to be loaded only if it is needed. ie. if we have no watches or conditions there is no need for an |
| 24 | + * evaluator. |
| 25 | + */ |
| 26 | +public class LazyEvaluator extends AbstractEvaluator { |
| 27 | + |
| 28 | + private static final Exception NO_EVALUATOR_EXCEPTION = new RuntimeException( |
| 29 | + "No evaluator available."); |
| 30 | + private final IEvaluatorLoader loader; |
| 31 | + private IEvaluator evaluator; |
| 32 | + |
| 33 | + public LazyEvaluator(final IEvaluatorLoader loader) { |
| 34 | + this.loader = loader; |
| 35 | + } |
| 36 | + |
| 37 | + private IEvaluator load() { |
| 38 | + if (this.evaluator == null) { |
| 39 | + try { |
| 40 | + this.evaluator = this.loader.load(); |
| 41 | + } catch (Exception e) { |
| 42 | + this.evaluator = new AbstractEvaluator() { |
| 43 | + @Override |
| 44 | + public Object evaluateExpression(final String expression, final Map<String, Object> values) throws Throwable { |
| 45 | + throw NO_EVALUATOR_EXCEPTION; |
| 46 | + } |
| 47 | + }; |
| 48 | + } |
| 49 | + } |
| 50 | + return this.evaluator; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Object evaluateExpression(final String expression, final Map<String, Object> values) throws Throwable { |
| 55 | + return load().evaluateExpression(expression, values); |
| 56 | + } |
| 57 | + |
| 58 | + public interface IEvaluatorLoader { |
| 59 | + |
| 60 | + IEvaluator load(); |
| 61 | + } |
| 62 | +} |
0 commit comments