Skip to content

Commit

Permalink
CAMEL-7463: Routes should support prop: property placeholder for any …
Browse files Browse the repository at this point in the history
…attributes for expression/predicates/dataformats as well.
  • Loading branch information
davsclaus committed Jul 9, 2015
1 parent 678c294 commit 9f31902
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
Expand Up @@ -16,10 +16,13 @@
*/
package org.apache.camel.model;

import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;

import org.apache.camel.CamelContext;
import org.apache.camel.spi.DataFormat;
Expand All @@ -36,11 +39,14 @@
@Metadata(label = "dataformat,transformation")
@XmlType(name = "dataFormat")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataFormatDefinition extends IdentifiedType {
public class DataFormatDefinition extends IdentifiedType implements OtherAttributesAware {
@XmlTransient
private DataFormat dataFormat;
@XmlTransient
private String dataFormatName;
// use xs:any to support optional property placeholders
@XmlAnyAttribute
private Map<QName, Object> otherAttributes;

public DataFormatDefinition() {
}
Expand All @@ -66,7 +72,7 @@ public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefi
ObjectHelper.notNull(ref, "ref or type");

// try to let resolver see if it can resolve it, its not always possible
type = ((ModelCamelContext) routeContext.getCamelContext()).resolveDataFormatDefinition(ref);
type = routeContext.getCamelContext().resolveDataFormatDefinition(ref);

if (type != null) {
return type.getDataFormat(routeContext);
Expand Down Expand Up @@ -177,6 +183,14 @@ public void setDataFormat(DataFormat dataFormat) {
this.dataFormat = dataFormat;
}

public Map<QName, Object> getOtherAttributes() {
return otherAttributes;
}

public void setOtherAttributes(Map<QName, Object> otherAttributes) {
this.otherAttributes = otherAttributes;
}

public String getShortName() {
String name = getClass().getSimpleName();
if (name.endsWith("DataFormat")) {
Expand Down
@@ -0,0 +1,40 @@
/**
* 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.model;

import java.util.Map;
import javax.xml.namespace.QName;

/**
* Models can support being configured with any other attributes to shadow existing options to be used for property placeholders.
* <p/>
* For example to override attributes that are configured as a boolean or integer type. Then the any attributes can be used to
* override those existing attributes and supporting property placeholders.
*/
public interface OtherAttributesAware {

/**
* Adds optional attribute to use as property placeholder
*/
Map<QName, Object> getOtherAttributes();

/**
* Adds optional attribute to use as property placeholder
*/
void setOtherAttributes(Map<QName, Object> otherAttributes);

}
Expand Up @@ -78,7 +78,7 @@
* @version
*/
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>> extends OptionalIdentifiedDefinition<Type> implements Block {
public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>> extends OptionalIdentifiedDefinition<Type> implements Block, OtherAttributesAware {
@XmlTransient
private static final AtomicInteger COUNTER = new AtomicInteger();
@XmlTransient
Expand Down Expand Up @@ -3551,10 +3551,12 @@ public void setInheritErrorHandler(Boolean inheritErrorHandler) {
this.inheritErrorHandler = inheritErrorHandler;
}

@Override
public Map<QName, Object> getOtherAttributes() {
return otherAttributes;
}

@Override
public void setOtherAttributes(Map<QName, Object> otherAttributes) {
this.otherAttributes = otherAttributes;
}
Expand Down
Expand Up @@ -665,10 +665,10 @@ public static void resolvePropertyPlaceholders(RouteContext routeContext, Object
* Inspects the given definition and resolves any property placeholders from its properties.
* <p/>
* This implementation will check all the getter/setter pairs on this instance and for all the values
* (which is a String type) will be property placeholder resolved.
* (which is a String type) will be property placeholder resolved. The definition should implement {@link OtherAttributesAware}
*
* @param camelContext the Camel context
* @param definition the definition
* @param definition the definition which should implement {@link OtherAttributesAware}
* @throws Exception is thrown if property placeholders was used and there was an error resolving them
* @see org.apache.camel.CamelContext#resolvePropertyPlaceholders(String)
* @see org.apache.camel.component.properties.PropertiesComponent
Expand All @@ -680,17 +680,17 @@ public static void resolvePropertyPlaceholders(CamelContext camelContext, Object
Map<String, Object> properties = new HashMap<String, Object>();
IntrospectionSupport.getProperties(definition, properties, null);

ProcessorDefinition<?> processorDefinition = null;
if (definition instanceof ProcessorDefinition) {
processorDefinition = (ProcessorDefinition<?>) definition;
OtherAttributesAware other = null;
if (definition instanceof OtherAttributesAware) {
other = (OtherAttributesAware) definition;
}
// include additional properties which have the Camel placeholder QName
// and when the definition parameter is this (otherAttributes belong to this)
if (processorDefinition != null && processorDefinition.getOtherAttributes() != null) {
for (QName key : processorDefinition.getOtherAttributes().keySet()) {
if (other != null && other.getOtherAttributes() != null) {
for (QName key : other.getOtherAttributes().keySet()) {
if (Constants.PLACEHOLDER_QNAME.equals(key.getNamespaceURI())) {
String local = key.getLocalPart();
Object value = processorDefinition.getOtherAttributes().get(key);
Object value = other.getOtherAttributes().get(key);
if (value != null && value instanceof String) {
// enforce a properties component to be created if none existed
CamelContextHelper.lookupPropertiesComponent(camelContext, true);
Expand Down
Expand Up @@ -17,8 +17,10 @@
package org.apache.camel.model.language;

import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlRootElement;
Expand All @@ -27,12 +29,14 @@
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.namespace.QName;

import org.apache.camel.AfterPropertiesConfigured;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.model.OtherAttributesAware;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.Required;
Expand All @@ -49,7 +53,7 @@
@XmlRootElement
@XmlType(name = "expression") // must be named expression
@XmlAccessorType(XmlAccessType.FIELD)
public class ExpressionDefinition implements Expression, Predicate {
public class ExpressionDefinition implements Expression, Predicate, OtherAttributesAware {
@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
Expand All @@ -64,6 +68,9 @@ public class ExpressionDefinition implements Expression, Predicate {
private Expression expressionValue;
@XmlTransient
private ExpressionDefinition expressionType;
// use xs:any to support optional property placeholders
@XmlAnyAttribute
private Map<QName, Object> otherAttributes;

public ExpressionDefinition() {
}
Expand Down Expand Up @@ -241,6 +248,16 @@ public void setTrim(Boolean trim) {
this.trim = trim;
}

@Override
public Map<QName, Object> getOtherAttributes() {
return otherAttributes;
}

@Override
public void setOtherAttributes(Map<QName, Object> otherAttributes) {
this.otherAttributes = otherAttributes;
}

/**
* Returns some descriptive text to describe this node
*/
Expand Down

0 comments on commit 9f31902

Please sign in to comment.