Search in sources :

Example 1 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty in project wso2-synapse by wso2.

the class XSLTMediator method addAttribute.

/**
 * Add an attribute to be set on the {@link TransformerFactory} used by this mediator instance.
 * This method can also be used to enable some Synapse specific optimizations and
 * enhancements as described in the documentation of this class.
 *
 * @param name The name of the feature
 * @param value should this feature enable?
 *
 * @see TransformerFactory#setAttribute(String, Object)
 * @see XSLTMediator
 */
public void addAttribute(String name, String value) {
    MediatorProperty mp = new MediatorProperty();
    mp.setName(name);
    mp.setValue(value);
    transformerFactoryAttributes.add(mp);
    if (SOURCE_BUILDER_FACTORY.equals(name) || RESULT_BUILDER_FACTORY.equals(name)) {
        Object instance;
        try {
            instance = Class.forName(value).newInstance();
        } catch (ClassNotFoundException e) {
            String msg = "The class specified by the " + name + " attribute was not found";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        } catch (Exception e) {
            String msg = "The class " + value + " could not be instantiated";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
        if (SOURCE_BUILDER_FACTORY.equals(name)) {
            sourceBuilderFactory = (SourceBuilderFactory) instance;
        } else {
            resultBuilderFactory = (ResultBuilderFactory) instance;
        }
    } else {
        try {
            transFact.setAttribute(name, value);
        } catch (IllegalArgumentException e) {
            String msg = "Error occurred when setting attribute to the TransformerFactory";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
    }
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SynapseException(org.apache.synapse.SynapseException)

Example 2 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty in project wso2-synapse by wso2.

the class WeightedRRLCAlgorithm method intialize.

/**
 * Initialize the algorithm reading the configurations from the endpoints.
 */
private void intialize() {
    // get the global properties
    if (loadBalanceEndpoint != null && loadBalanceEndpoint instanceof PropertyInclude) {
        PropertyInclude include = (PropertyInclude) loadBalanceEndpoint;
        MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_ROUNDS_PER_RECAL);
        if (val != null) {
            roundsPerRecalculation = Integer.parseInt(val.getValue());
        }
    }
    // initialize the states list, this runs only once
    list = new WeightedState[endpoints.size()];
    int totalWeight = 0;
    for (Endpoint endpoint : endpoints) {
        if (endpoint instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) endpoint;
            MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            if (val == null) {
                String msg = "Parameter " + "loadbalance.weighted.weight should be specified for every " + "endpoint in the load balance group";
                log.error(msg);
                throw new SynapseException(msg);
            }
            totalWeight += Integer.parseInt(val.getValue());
        }
    }
    this.totalWeight = totalWeight;
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint e = endpoints.get(i);
        if (e instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) e;
            MediatorProperty weight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            String key;
            URL url;
            if (e instanceof AddressEndpoint) {
                AddressEndpoint addressEndpoint = (AddressEndpoint) e;
                try {
                    url = new URL(addressEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else if (e instanceof WSDLEndpoint) {
                WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) e;
                try {
                    url = new URL(wsdlEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else {
                String msg = "Only AddressEndpoint and WSDLEndpoint can be used " + "with WeightedRRLCAlgorithm";
                log.error(msg);
                throw new SynapseException(msg);
            }
            // construct the key
            key = url.getHost() + ":" + url.getPort();
            WeightedState state = new WeightedState(Integer.parseInt(weight.getValue()), i, key);
            MediatorProperty minimumWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MIN);
            if (minimumWeight != null) {
                state.setMinWeight(Integer.parseInt(minimumWeight.getValue()));
            }
            MediatorProperty maxWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MAX);
            if (maxWeight != null) {
                state.setMaxWeight(Integer.parseInt(maxWeight.getValue()));
            }
            list[i] = state;
        }
    }
    // sort the states according to the initial fixed weights
    Arrays.sort(list, new Comparator<WeightedState>() {

        public int compare(WeightedState o1, WeightedState o2) {
            return o2.getFixedWeight() - o1.getFixedWeight();
        }
    });
}
Also used : WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) MalformedURLException(java.net.MalformedURLException) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) SynapseException(org.apache.synapse.SynapseException) PropertyInclude(org.apache.synapse.PropertyInclude) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) URL(java.net.URL)

Example 3 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty in project wso2-synapse by wso2.

the class WeightedRoundRobin method init.

public void init(SynapseEnvironment se) {
    if (endpoints == null) {
        String msg = "Endpoints are not set, cannot initialize the algorithm";
        log.error(msg);
        throw new SynapseException(msg);
    }
    endpointStates = new EndpointState[endpoints.size()];
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint endpoint = endpoints.get(i);
        if (!(endpoint instanceof PropertyInclude)) {
            EndpointState state = new EndpointState(i, DEFAULT_WEIGHT);
            endpointStates[i] = state;
        } else {
            MediatorProperty property = ((PropertyInclude) endpoint).getProperty(LOADBALANCE_WEIGHT);
            EndpointState state;
            if (property != null) {
                int weight = Integer.parseInt(property.getValue());
                if (weight <= 0) {
                    String msg = "Weight must be greater than zero";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
                state = new EndpointState(i, weight);
            } else {
                state = new EndpointState(i, DEFAULT_WEIGHT);
            }
            endpointStates[i] = state;
        }
    }
    if (loadBalanceEndpoint instanceof PropertyInclude) {
        MediatorProperty threadLocalProperty = ((PropertyInclude) loadBalanceEndpoint).getProperty(LOADBALANCE_ThEADLOCAL);
        if (threadLocalProperty != null && threadLocalProperty.getValue().equals("true")) {
            isThreadLocal = true;
        }
    }
    view = new WeightedRoundRobinView(this);
    MBeanRegistrar.getInstance().registerMBean(view, "LBAlgorithms", loadBalanceEndpoint.getName() != null ? loadBalanceEndpoint.getName() : "LBEpr");
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) Endpoint(org.apache.synapse.endpoints.Endpoint) PropertyInclude(org.apache.synapse.PropertyInclude) Endpoint(org.apache.synapse.endpoints.Endpoint)

Example 4 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty in project wso2-synapse by wso2.

the class PropertyMediatorTest method testMediatorPropertiesExpression.

public void testMediatorPropertiesExpression() throws Exception {
    // set a local property to the synapse context
    PropertyMediator propMediator = new PropertyMediator();
    propMediator.setName("name");
    propMediator.setValue("value");
    MessageContext synCtx = TestUtils.getTestContext("<empty/>");
    propMediator.mediate(synCtx);
    // read property through a mediator property
    MediatorProperty medProp = new MediatorProperty();
    medProp.setExpression(new SynapseXPath("synapse:get-property('name')"));
    assertTrue("value".equals(medProp.getEvaluatedExpression(synCtx)));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 5 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty in project wso2-synapse by wso2.

the class XQueryMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof XQueryMediator)) {
        handleException("Invalid Mediator has passed to serializer");
    }
    XQueryMediator queryMediator = (XQueryMediator) m;
    OMElement xquery = fac.createOMElement("xquery", synNS);
    Value key = queryMediator.getQueryKey();
    if (key != null) {
        // Serialize Key using KeySerializer
        ValueSerializer keySerializer = new ValueSerializer();
        keySerializer.serializeValue(key, XMLConfigConstants.KEY, xquery);
    }
    saveTracingState(xquery, queryMediator);
    SynapseXPath targetXPath = queryMediator.getTarget();
    if (targetXPath != null && !SourceXPathSupport.DEFAULT_XPATH.equals(targetXPath.toString())) {
        SynapseXPathSerializer.serializeXPath(targetXPath, xquery, "target");
    }
    List<MediatorProperty> pros = queryMediator.getProcessorProperties();
    if (pros != null && !pros.isEmpty()) {
        OMElement dataSource = fac.createOMElement("dataSource", synNS);
        serializeProperties(dataSource, pros);
        xquery.addChild(dataSource);
    }
    List list = queryMediator.getVariables();
    if (list != null && !list.isEmpty()) {
        for (Object o : list) {
            if (o instanceof MediatorBaseVariable) {
                MediatorBaseVariable variable = (MediatorBaseVariable) o;
                QName name = variable.getName();
                Object value = variable.getValue();
                if (name != null && value != null) {
                    OMElement baseElement = fac.createOMElement("variable", synNS);
                    baseElement.addAttribute(fac.createOMAttribute("name", nullNS, name.getLocalPart()));
                    baseElement.addAttribute(fac.createOMAttribute("value", nullNS, (String) value));
                    String type = null;
                    ItemType variableType = variable.getType();
                    XdmNodeKind nodeKind = variable.getNodeKind();
                    if (ItemType.INT == variableType) {
                        type = "INT";
                    } else if (ItemType.INTEGER == variableType) {
                        type = "INTEGER";
                    } else if (ItemType.BOOLEAN == variableType) {
                        type = "BOOLEAN";
                    } else if (ItemType.BYTE == variableType) {
                        type = "BYTE";
                    } else if (ItemType.DOUBLE == variableType) {
                        type = "DOUBLE";
                    } else if (ItemType.SHORT == variableType) {
                        type = "SHORT";
                    } else if (ItemType.LONG == variableType) {
                        type = "LONG";
                    } else if (ItemType.FLOAT == variableType) {
                        type = "FLOAT";
                    } else if (ItemType.STRING == variableType) {
                        type = "STRING";
                    } else if (XdmNodeKind.DOCUMENT == nodeKind) {
                        type = "DOCUMENT";
                    } else if (XdmNodeKind.ELEMENT == nodeKind) {
                        type = "ELEMENT";
                    } else {
                        handleException("Unknown Type " + variableType);
                    }
                    if (type != null) {
                        baseElement.addAttribute(fac.createOMAttribute("type", nullNS, type));
                    }
                    xquery.addChild(baseElement);
                }
            } else if (o instanceof MediatorCustomVariable) {
                MediatorCustomVariable variable = (MediatorCustomVariable) o;
                QName name = variable.getName();
                if (name != null) {
                    OMElement customElement = fac.createOMElement("variable", synNS);
                    customElement.addAttribute(fac.createOMAttribute("name", nullNS, name.getLocalPart()));
                    String regkey = variable.getRegKey();
                    if (regkey != null) {
                        customElement.addAttribute(fac.createOMAttribute("key", nullNS, regkey));
                    }
                    SynapseXPath expression = variable.getExpression();
                    if (expression != null && !SourceXPathSupport.DEFAULT_XPATH.equals(expression.toString())) {
                        SynapseXPathSerializer.serializeXPath(expression, customElement, "expression");
                    }
                    String type = null;
                    ItemType variableType = variable.getType();
                    XdmNodeKind nodeKind = variable.getNodeKind();
                    if (XdmNodeKind.DOCUMENT == nodeKind) {
                        type = "DOCUMENT";
                    } else if (XdmNodeKind.ELEMENT == nodeKind) {
                        type = "ELEMENT";
                    } else if (ItemType.INT == variableType) {
                        type = "INT";
                    } else if (ItemType.INTEGER == variableType) {
                        type = "INTEGER";
                    } else if (ItemType.BOOLEAN == variableType) {
                        type = "BOOLEAN";
                    } else if (ItemType.BYTE == variableType) {
                        type = "BYTE";
                    } else if (ItemType.DOUBLE == variableType) {
                        type = "DOUBLE";
                    } else if (ItemType.SHORT == variableType) {
                        type = "SHORT";
                    } else if (ItemType.LONG == variableType) {
                        type = "LONG";
                    } else if (ItemType.FLOAT == variableType) {
                        type = "FLOAT";
                    } else if (ItemType.STRING == variableType) {
                        type = "STRING";
                    } else {
                        handleException("Unknown Type " + variableType);
                    }
                    if (type != null) {
                        customElement.addAttribute(fac.createOMAttribute("type", nullNS, type));
                    }
                    xquery.addChild(customElement);
                }
            }
        }
    }
    return xquery;
}
Also used : XdmNodeKind(net.sf.saxon.s9api.XdmNodeKind) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) QName(javax.xml.namespace.QName) ItemType(net.sf.saxon.s9api.ItemType) OMElement(org.apache.axiom.om.OMElement) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) Value(org.apache.synapse.mediators.Value) List(java.util.List) ValueSerializer(org.apache.synapse.config.xml.ValueSerializer)

Aggregations

MediatorProperty (org.apache.synapse.mediators.MediatorProperty)21 OMElement (org.apache.axiom.om.OMElement)8 SynapseException (org.apache.synapse.SynapseException)8 ArrayList (java.util.ArrayList)5 Endpoint (org.apache.synapse.endpoints.Endpoint)4 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)3 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)3 Test (org.junit.Test)3 Iterator (java.util.Iterator)2 QName (javax.xml.namespace.QName)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 PropertyInclude (org.apache.synapse.PropertyInclude)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 LoadbalanceEndpoint (org.apache.synapse.endpoints.LoadbalanceEndpoint)2 Value (org.apache.synapse.mediators.Value)2 UriTemplate (com.damnhandy.uri.template.UriTemplate)1 ExpressionParseException (com.damnhandy.uri.template.impl.ExpressionParseException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1