Search in sources :

Example 6 with Registry

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

the class SimpleURLRegistryTest method testRegistry.

public void testRegistry() throws Exception {
    Registry reg = new SimpleURLRegistry();
    Properties props = new Properties();
    props.put("root", "file:./");
    props.put("cachableDuration", "1500");
    reg.init(props);
    Entry prop = new Entry();
    prop.setType(Entry.REMOTE_ENTRY);
    prop.setKey(FILE);
    // initial load of file from registry
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
    // sleep 1 sec
    Thread.sleep(1000);
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
    // sleep another 1 sec, has expired in cache, but content hasnt changed
    Thread.sleep(1000);
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
    // the renewed cache should be valid for another 1.5 secs
    // change the file now and change next cache duration
    writeToFile(TEXT_2);
    props.put("cachableDuration", "100");
    reg.init(props);
    // still cached content should be available and valid
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
    // now sleep ~1 sec, still cache should be valid
    Thread.sleep(800);
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
    // sleep another 1 sec.. cache should expire and new content should be loaded
    Thread.sleep(1000);
    assertEquals(TEXT_2, reg.getResource(prop, new Properties()).toString());
    // change content back to original
    writeToFile(TEXT_1);
    // sleep for .5 sec, now the new content should be loaded as new expiry time
    // is .1 sec
    Thread.sleep(500);
    assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
}
Also used : Entry(org.apache.synapse.config.Entry) Registry(org.apache.synapse.registry.Registry) Properties(java.util.Properties)

Example 7 with Registry

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

the class SynapseXMLConfigurationFactory method defineRegistry.

public static Registry defineRegistry(SynapseConfiguration config, OMElement elem, Properties properties) {
    if (config.getRegistry() != null) {
        handleException("Only one remote registry can be defined within a configuration");
    }
    Registry registry = RegistryFactory.createRegistry(elem, properties);
    config.setRegistry(registry);
    return registry;
}
Also used : Registry(org.apache.synapse.registry.Registry)

Example 8 with Registry

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

the class MultiXMLConfigurationSerializerTest method testSerialize3.

/**
 * Test serialize method with registry set for SynapseConfiguration and assert synapse.xml is created.
 */
@Test
public void testSerialize3() {
    MultiXMLConfigurationSerializer serializer = new MultiXMLConfigurationSerializer(TEST_DIRECTORY_NAME);
    SynapseConfiguration configuration = new SynapseConfiguration();
    Map<String, OMNode> data = new HashMap<>();
    data.put(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_1));
    data.put(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_1));
    Registry registry = new SimpleInMemoryRegistry(data, 8000L);
    configuration.setRegistry(registry);
    serializer.serialize(configuration);
    Assert.assertTrue("Error in serializing Synapse configuration.", new File(TEST_DIRECTORY_NAME + File.separator + SYNAPSE_XML).exists());
    removeTestFolder(TEST_DIRECTORY_NAME);
}
Also used : OMNode(org.apache.axiom.om.OMNode) HashMap(java.util.HashMap) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) Registry(org.apache.synapse.registry.Registry) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) File(java.io.File) Test(org.junit.Test)

Example 9 with Registry

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

the class RegistrySerializationTest method testRegistrySerialization.

public void testRegistrySerialization() {
    String regitryConfiguration = "<syn:registry xmlns:syn=\"http://ws.apache.org/ns/synapse\" " + "provider=\"org.apache.synapse.registry.url.SimpleURLRegistry\">" + "<syn:parameter name=\"root\">file:./../../repository/</syn:parameter>" + "<syn:parameter name=\"cachableDuration\">15000</syn:parameter>" + "</syn:registry>";
    OMElement registryElement = createOMElement(regitryConfiguration);
    Registry registry = RegistryFactory.createRegistry(registryElement, new Properties());
    OMElement serializedElement = RegistrySerializer.serializeRegistry(null, registry);
    try {
        assertTrue(compare(registryElement, serializedElement));
    } catch (Exception e) {
        fail("Exception in test.");
    }
}
Also used : OMElement(org.apache.axiom.om.OMElement) Registry(org.apache.synapse.registry.Registry) Properties(java.util.Properties) XMLComparisonException(org.apache.axiom.om.impl.exception.XMLComparisonException)

Example 10 with Registry

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

the class SynapseConfigurationBuilder method getConfiguration.

/**
 * Build a Synapse configuration from a given XML configuration file
 *
 * @param configFile Path to the Synapse configuration file or directory
 * @param properties bag of properties to be passed into the builder
 * @return the Synapse configuration model
 */
public static SynapseConfiguration getConfiguration(String configFile, Properties properties) {
    File synapseConfigLocation = new File(configFile);
    if (!synapseConfigLocation.exists()) {
        String message = "Unable to load the Synapse configuration from : " + configFile + ". Specified file not found";
        log.fatal(message);
        throw new SynapseException(message);
    }
    SynapseConfiguration synCfg = null;
    if (synapseConfigLocation.isFile()) {
        // build the Synapse configuration parsing the XML config file
        try {
            synCfg = XMLConfigurationBuilder.getConfiguration(new FileInputStream(configFile), properties);
            log.info("Loaded Synapse configuration from : " + configFile);
        } catch (Exception e) {
            handleException("Could not initialize Synapse : " + e.getMessage(), e);
        }
    } else if (synapseConfigLocation.isDirectory()) {
        // build the Synapse configuration by processing given directory hierarchy
        synCfg = MultiXMLConfigurationBuilder.getConfiguration(configFile, properties);
        log.info("Loaded Synapse configuration from the artifact " + "repository at : " + configFile);
    }
    assert synCfg != null;
    synCfg.setPathToConfigFile(new File(configFile).getAbsolutePath());
    Registry localConfigReg = synCfg.getRegistry();
    if (synCfg.getLocalRegistry().isEmpty() && synCfg.getProxyServices().isEmpty() && localConfigReg != null) {
        if (log.isDebugEnabled()) {
            log.debug("Only the registry is defined in the synapse configuration, trying " + "to fetch a configuration from the registry");
        }
        // TODO: support a artifact repository for registry as well instead of just the synapse.xml
        OMNode remoteConfigNode = localConfigReg.lookup("synapse.xml");
        if (remoteConfigNode != null) {
            try {
                synCfg = XMLConfigurationBuilder.getConfiguration(SynapseConfigUtils.getStreamSource(remoteConfigNode).getInputStream(), properties);
                // TODO: that, and should serialize the config to the registry
                if (synCfg.getRegistry() == null) {
                    synCfg.setRegistry(localConfigReg);
                } else {
                    log.warn("Registry declaration has been overwritten by the registry " + "declaration found at the remote configuration");
                }
            } catch (XMLStreamException xse) {
                throw new SynapseException("Problem loading remote synapse.xml ", xse);
            }
        } else if (log.isDebugEnabled()) {
            log.debug("Couldn't find a synapse configuration on the registry");
        }
    }
    // Check for the main sequence and add a default main sequence if not present
    if (synCfg.getMainSequence() == null) {
        SynapseConfigUtils.setDefaultMainSequence(synCfg);
    }
    // Check for the fault sequence and add a default fault sequence if not present
    if (synCfg.getFaultSequence() == null) {
        SynapseConfigUtils.setDefaultFaultSequence(synCfg);
    }
    return synCfg;
}
Also used : OMNode(org.apache.axiom.om.OMNode) SynapseException(org.apache.synapse.SynapseException) XMLStreamException(javax.xml.stream.XMLStreamException) Registry(org.apache.synapse.registry.Registry) File(java.io.File) FileInputStream(java.io.FileInputStream) SynapseException(org.apache.synapse.SynapseException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

Registry (org.apache.synapse.registry.Registry)10 Properties (java.util.Properties)4 HashMap (java.util.HashMap)3 OMElement (org.apache.axiom.om.OMElement)3 OMNode (org.apache.axiom.om.OMNode)3 File (java.io.File)2 Map (java.util.Map)2 MessageContext (org.apache.synapse.MessageContext)2 Entry (org.apache.synapse.config.Entry)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 SimpleInMemoryRegistry (org.apache.synapse.registry.SimpleInMemoryRegistry)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Set (java.util.Set)1 Stack (java.util.Stack)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1