Search in sources :

Example 1 with XMLToObjectMapper

use of org.apache.synapse.config.XMLToObjectMapper in project wso2-synapse by wso2.

the class AbstractRegistry method getResource.

/**
 * Get the resource for the given key from this registry
 * @param entry The Enrty instance that contains meta-data
 * @param properties bag of properties with additional information
 * @return the matching resultant object
 */
public Object getResource(Entry entry, Properties properties) {
    OMNode omNode = null;
    RegistryEntry re = null;
    // if we have an unexpired cached copy, return the cached object
    if (entry.isCached() && !entry.isExpired()) {
        return entry.getValue();
    // if we have not cached the referenced object, fetch it and its RegistryEntry
    } else if (!entry.isCached()) {
        try {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
        } catch (OMException e) {
            log.error("Error reading registry resource file : " + entry.getKey(), e);
        }
        if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
            // Collection
            re = getRegistryEntry(entry.getKey());
            if (re != null) {
                setExpiryTime(entry, re);
                entry.setVersion(re.getVersion());
            }
        }
        if (omNode == null) {
            return null;
        } else {
            re = getRegistryEntry(entry.getKey());
        }
    // if we have cached it before, and now the cache has expired
    // get its *new* registry entry and compare versions and pick new cache duration
    } else if (entry.isExpired()) {
        if (log.isDebugEnabled()) {
            log.debug("Cached object has expired for key : " + entry.getKey());
        }
        re = getRegistryEntry(entry.getKey());
        if (re.getVersion() != Long.MIN_VALUE && re.getVersion() == entry.getVersion()) {
            if (log.isDebugEnabled()) {
                log.debug("Expired version number is same as current version in registry");
            }
            // renew cache lease for another cachable duration (as returned by the
            // new getRegistryEntry() call
            setExpiryTime(entry, re);
            if (log.isDebugEnabled()) {
                log.debug("Renew cache lease for another " + re.getCachableDuration() / 1000 + "s");
            }
            // return cached object
            return entry.getValue();
        } else {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
            if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
                // Collection
                re = getRegistryEntry(entry.getKey());
                if (re != null) {
                    setExpiryTime(entry, re);
                    entry.setVersion(re.getVersion());
                }
            }
            if (omNode == null) {
                return null;
            }
        }
    }
    // if we get here, we have received the raw omNode from the
    // registry and our previous copy (if we had one) has expired or is not valid
    Object expiredValue = entry.getValue();
    // resource into the appropriate object - e.g. sequence or endpoint
    if (entry.getMapper() != null) {
        entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
        if (entry.getValue() instanceof SequenceMediator) {
            SequenceMediator seq = (SequenceMediator) entry.getValue();
            seq.setDynamic(true);
            seq.setRegistryKey(entry.getKey());
        } else if (entry.getValue() instanceof Endpoint) {
            Endpoint ep = (Endpoint) entry.getValue();
        } else if (entry.getValue() instanceof TemplateMediator) {
            ((TemplateMediator) entry.getValue()).setDynamic(true);
        }
    } else {
        // else cache the raw OMNode
        if (re != null && re.getType() != null) {
            XMLToObjectMapper mapper = getMapper(re.getType());
            if (mapper != null) {
                entry.setMapper(mapper);
                entry.setValue(mapper.getObjectFromOMNode(omNode, properties));
            } else {
                entry.setValue(omNode);
            }
        }
    }
    if (expiredValue != null) {
        // Destroy the old resource so that everything is properly cleaned up
        if (expiredValue instanceof SequenceMediator) {
            ((SequenceMediator) expiredValue).destroy();
        } else if (expiredValue instanceof Endpoint) {
            ((Endpoint) expiredValue).destroy();
        }
    }
    // increment cache expiry time as specified by the last getRegistryEntry() call
    if (re != null) {
        setExpiryTime(entry, re);
        entry.setVersion(re.getVersion());
    }
    return entry.getValue();
}
Also used : OMNode(org.apache.axiom.om.OMNode) Endpoint(org.apache.synapse.endpoints.Endpoint) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMException(org.apache.axiom.om.OMException) XMLToObjectMapper(org.apache.synapse.config.XMLToObjectMapper)

Aggregations

OMException (org.apache.axiom.om.OMException)1 OMNode (org.apache.axiom.om.OMNode)1 XMLToObjectMapper (org.apache.synapse.config.XMLToObjectMapper)1 Endpoint (org.apache.synapse.endpoints.Endpoint)1 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)1 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)1