Search in sources :

Example 1 with RegistryEntry

use of org.apache.synapse.registry.RegistryEntry in project wso2-synapse by wso2.

the class SimpleURLRegistry method getChildren.

public RegistryEntry[] getChildren(RegistryEntry entry) {
    URL url;
    if (entry == null) {
        RegistryEntryImpl entryImpl = new RegistryEntryImpl();
        entryImpl.setKey("");
        entry = entryImpl;
    }
    url = SynapseConfigUtils.getURLFromPath(root + entry.getKey(), properties.get(SynapseConstants.SYNAPSE_HOME) != null ? properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
    if (url == null) {
        return null;
    }
    if (url.getProtocol().equals("file")) {
        File file = new File(url.getFile());
        if (!file.isDirectory()) {
            return null;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader((InputStream) url.getContent()));
            ArrayList<RegistryEntry> entryList = new ArrayList<RegistryEntry>();
            String key;
            while ((key = reader.readLine()) != null) {
                RegistryEntryImpl registryEntryImpl = new RegistryEntryImpl();
                if (entry.getKey().equals("")) {
                    registryEntryImpl.setKey(key);
                } else {
                    if (entry.getKey().endsWith("/")) {
                        registryEntryImpl.setKey(entry.getKey() + key);
                    } else {
                        registryEntryImpl.setKey(entry.getKey() + "/" + key);
                    }
                }
                entryList.add(registryEntryImpl);
            }
            RegistryEntry[] entries = new RegistryEntry[entryList.size()];
            for (int i = 0; i < entryList.size(); i++) {
                entries[i] = entryList.get(i);
            }
            return entries;
        } catch (Exception e) {
            throw new SynapseException("Error in reading the URL.");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) {
                }
            }
        }
    } else {
        throw new SynapseException("Invalid protocol.");
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) ArrayList(java.util.ArrayList) RegistryEntryImpl(org.apache.synapse.registry.RegistryEntryImpl) RegistryEntry(org.apache.synapse.registry.RegistryEntry) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) SynapseException(org.apache.synapse.SynapseException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with RegistryEntry

use of org.apache.synapse.registry.RegistryEntry in project wso2-synapse by wso2.

the class RegistryResourceFetcher method getResource.

private Object getResource(Entry entry, Properties properties) {
    OMNode omNode;
    RegistryEntry re = registry.getRegistryEntry(entry.getKey());
    omNode = registry.lookup(entry.getKey());
    if (re == null) {
        return null;
    }
    if ((!entry.isCached() || (re.getVersion() == Long.MIN_VALUE || re.getVersion() != entry.getVersion())) || re.getLastModified() >= lastExecutionTime) {
        entry.setEntryProperties(registry.getResourceProperties(entry.getKey()));
        entry.setVersion(re.getVersion());
        // 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());
                seq.init(synapseEnvironment);
            } else if (entry.getValue() instanceof Endpoint) {
                Endpoint ep = (Endpoint) entry.getValue();
                ep.init(synapseEnvironment);
            }
        } else {
            // if the type of the object is known to have a mapper, create the
            // resultant Object using the known mapper, and cache this Object
            // else cache the raw OMNode
            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();
            }
        }
        entry.setVersion(re.getVersion());
    }
    // new getRegistryEntry() call
    if (re.getCachableDuration() > 0) {
        entry.setExpiryTime(System.currentTimeMillis() + re.getCachableDuration());
    } else {
        entry.setExpiryTime(-1);
    }
    return entry.getValue();
}
Also used : OMNode(org.apache.axiom.om.OMNode) Endpoint(org.apache.synapse.endpoints.Endpoint) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) RegistryEntry(org.apache.synapse.registry.RegistryEntry)

Aggregations

RegistryEntry (org.apache.synapse.registry.RegistryEntry)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMNode (org.apache.axiom.om.OMNode)1 SynapseException (org.apache.synapse.SynapseException)1 Endpoint (org.apache.synapse.endpoints.Endpoint)1 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)1 RegistryEntryImpl (org.apache.synapse.registry.RegistryEntryImpl)1