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());
}
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;
}
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);
}
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.");
}
}
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;
}
Aggregations