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.reifier;
018
019import java.time.Duration;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.Endpoint;
025import org.apache.camel.Expression;
026import org.apache.camel.NoSuchEndpointException;
027import org.apache.camel.Predicate;
028import org.apache.camel.Route;
029import org.apache.camel.model.ExpressionSubElementDefinition;
030import org.apache.camel.model.language.ExpressionDefinition;
031import org.apache.camel.reifier.language.ExpressionReifier;
032import org.apache.camel.spi.BeanRepository;
033import org.apache.camel.support.CamelContextHelper;
034import org.apache.camel.util.ObjectHelper;
035
036public abstract class AbstractReifier implements BeanRepository {
037
038    protected final org.apache.camel.Route route;
039    protected final CamelContext camelContext;
040
041    public AbstractReifier(Route route) {
042        this.route = ObjectHelper.notNull(route, "Route");
043        this.camelContext = route.getCamelContext();
044    }
045
046    public AbstractReifier(CamelContext camelContext) {
047        this.route = null;
048        this.camelContext = ObjectHelper.notNull(camelContext, "CamelContext");
049    }
050
051    protected String parseString(String text) {
052        return CamelContextHelper.parseText(camelContext, text);
053    }
054
055    protected Boolean parseBoolean(String text) {
056        return CamelContextHelper.parseBoolean(camelContext, text);
057    }
058
059    protected boolean parseBoolean(String text, boolean def) {
060        Boolean b = parseBoolean(text);
061        return b != null ? b : def;
062    }
063
064    protected Long parseLong(String text) {
065        return CamelContextHelper.parseLong(camelContext, text);
066    }
067
068    protected long parseLong(String text, long def) {
069        Long l = parseLong(text);
070        return l != null ? l : def;
071    }
072
073    protected Long parseDuration(String text) {
074        Duration d = CamelContextHelper.parseDuration(camelContext, text);
075        return d != null ? d.toMillis() : null;
076    }
077
078    protected long parseDuration(String text, long def) {
079        Duration d = CamelContextHelper.parseDuration(camelContext, text);
080        return d != null ? d.toMillis() : def;
081    }
082
083    protected Integer parseInt(String text) {
084        return CamelContextHelper.parseInteger(camelContext, text);
085    }
086
087    protected int parseInt(String text, int def) {
088        Integer i = parseInt(text);
089        return i != null ? i : def;
090    }
091
092    protected Float parseFloat(String text) {
093        return CamelContextHelper.parseFloat(camelContext, text);
094    }
095
096    protected float parseFloat(String text, float def) {
097        Float f = parseFloat(text);
098        return f != null ? f : def;
099    }
100
101    protected <T> T parse(Class<T> clazz, String text) {
102        return CamelContextHelper.parse(camelContext, clazz, text);
103    }
104
105    protected <T> T parse(Class<T> clazz, Object text) {
106        if (text instanceof String) {
107            text = parseString((String) text);
108        }
109        return CamelContextHelper.convertTo(camelContext, clazz, text);
110    }
111
112    protected Expression createExpression(ExpressionDefinition expression) {
113        return ExpressionReifier.reifier(camelContext, expression).createExpression();
114    }
115
116    protected Expression createExpression(ExpressionSubElementDefinition expression) {
117        return ExpressionReifier.reifier(camelContext, expression).createExpression();
118    }
119
120    protected Predicate createPredicate(ExpressionDefinition expression) {
121        return ExpressionReifier.reifier(camelContext, expression).createPredicate();
122    }
123
124    protected Predicate createPredicate(ExpressionSubElementDefinition expression) {
125        return ExpressionReifier.reifier(camelContext, expression).createPredicate();
126    }
127
128    protected Object or(Object a, Object b) {
129        return a != null ? a : b;
130    }
131
132    protected Object asRef(String s) {
133        return s != null ? s.startsWith("#") ? s : "#" + s : null;
134    }
135
136    protected BeanRepository getRegistry() {
137        return camelContext.getRegistry();
138    }
139
140    public <T> T mandatoryLookup(String name, Class<T> beanType) {
141        return CamelContextHelper.mandatoryLookup(camelContext, name, beanType);
142    }
143
144    public <T> T findSingleByType(Class<T> type) {
145        return CamelContextHelper.findByType(camelContext, type);
146    }
147
148    @Override
149    public Object lookupByName(String name) {
150        return getRegistry().lookupByName(name);
151    }
152
153    public <T> T lookup(String name, Class<T> type) {
154        return lookupByNameAndType(name, type);
155    }
156
157    public <T> T lookupByNameAndType(String name, Class<T> type) {
158        return getRegistry().lookupByNameAndType(name, type);
159    }
160
161    @Override
162    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
163        return getRegistry().findByTypeWithName(type);
164    }
165
166    @Override
167    public <T> Set<T> findByType(Class<T> type) {
168        return getRegistry().findByType(type);
169    }
170
171    @Override
172    public Object unwrap(Object value) {
173        return getRegistry().unwrap(value);
174    }
175
176    public Endpoint resolveEndpoint(String uri) throws NoSuchEndpointException {
177        return CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
178    }
179
180}