Search in sources :

Example 1 with ClassMediator

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

the class ClassMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ClassMediator classMediator = new ClassMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    if (name == null) {
        String msg = "The name of the actual mediator class is a required attribute";
        log.error(msg);
        throw new SynapseException(msg);
    }
    Class clazz = null;
    Mediator mediator;
    if (properties != null) {
        // load from synapse libs or dynamic class mediators
        ClassLoader libLoader = (ClassLoader) properties.get(SynapseConstants.SYNAPSE_LIB_LOADER);
        if (libLoader != null) {
            // load from synapse lib
            try {
                clazz = libLoader.loadClass(name.getAttributeValue());
            } catch (ClassNotFoundException e) {
                String msg = "Error loading class : " + name.getAttributeValue() + " from Synapse library";
                log.error(msg, e);
                throw new SynapseException(msg, e);
            }
        } else {
            // load from dynamic class mediators
            Map<String, ClassLoader> dynamicClassMediatorLoaderMap = (Map<String, ClassLoader>) properties.get(SynapseConstants.CLASS_MEDIATOR_LOADERS);
            if (dynamicClassMediatorLoaderMap != null) {
                // Has registered dynamic class mediator loaders in the deployment store.
                // Try to load class from them.
                Iterator<ClassLoader> dynamicClassMediatorLoaders = dynamicClassMediatorLoaderMap.values().iterator();
                while (dynamicClassMediatorLoaders.hasNext()) {
                    try {
                        clazz = dynamicClassMediatorLoaders.next().loadClass(name.getAttributeValue());
                        break;
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    }
    if (clazz == null) {
        try {
            clazz = getClass().getClassLoader().loadClass(name.getAttributeValue());
        } catch (ClassNotFoundException e) {
            String msg = "Error loading class : " + name.getAttributeValue() + " - Class not found";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
    }
    try {
        mediator = (Mediator) clazz.newInstance();
    } catch (Throwable e) {
        String msg = "Error in instantiating class : " + name.getAttributeValue();
        log.error(msg, e);
        throw new SynapseException(msg, e);
    }
    classMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    classMediator.setMediator(mediator);
    processAuditStatus(classMediator, elem);
    return classMediator;
}
Also used : ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException) Mediator(org.apache.synapse.Mediator) ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) OMAttribute(org.apache.axiom.om.OMAttribute) Map(java.util.Map)

Example 2 with ClassMediator

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

the class ClassMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof ClassMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    ClassMediator mediator = (ClassMediator) m;
    OMElement clazz = fac.createOMElement("class", synNS);
    saveTracingState(clazz, mediator);
    if (mediator.getMediator() != null && mediator.getMediator().getClass().getName() != null) {
        clazz.addAttribute(fac.createOMAttribute("name", nullNS, mediator.getMediator().getClass().getName()));
    } else {
        handleException("Invalid class mediator. The class name is required");
    }
    super.serializeProperties(clazz, mediator.getProperties());
    return clazz;
}
Also used : ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) OMElement(org.apache.axiom.om.OMElement)

Aggregations

ClassMediator (org.apache.synapse.mediators.ext.ClassMediator)2 Map (java.util.Map)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 Mediator (org.apache.synapse.Mediator)1 SynapseException (org.apache.synapse.SynapseException)1