/** * Translates a {@link ServiceCreationConfiguration} to an xml element * * @param serviceCreationConfiguration */ @Override public Element unparseServiceCreationConfiguration(final ServiceCreationConfiguration<ClusteringService> serviceCreationConfiguration) { Element rootElement = unparseConfig(serviceCreationConfiguration); return rootElement; }
@Override public Element unparseServiceConfiguration(ServiceConfiguration<ClusteredStore.Provider> serviceConfiguration) { return unparseConfig(serviceConfiguration); }
@Override public Element unparseResourcePool(ResourcePool resourcePool) { return unparseConfig(resourcePool); }
@Override protected Element createRootElement(Document doc, ClusteringServiceConfiguration clusteringServiceConfiguration) { Element rootElement; if (clusteringServiceConfiguration.getConnectionSource() instanceof ConnectionSource.ClusterUri) { rootElement = createRootUrlElement(doc, clusteringServiceConfiguration); } else { rootElement = createServerElement(doc, clusteringServiceConfiguration); } processTimeUnits(doc, rootElement, clusteringServiceConfiguration); Element serverSideConfigurationElem = processServerSideElements(doc, clusteringServiceConfiguration); rootElement.appendChild(serverSideConfigurationElem); return rootElement; }
protected Element processServerSideElements(Document doc, ClusteringServiceConfiguration clusteringServiceConfiguration) { Element serverSideConfigurationElem = createServerSideConfigurationElement(doc, clusteringServiceConfiguration); if (clusteringServiceConfiguration.getServerConfiguration() != null) { ServerSideConfiguration serverSideConfiguration = clusteringServiceConfiguration.getServerConfiguration(); String defaultServerResource = serverSideConfiguration.getDefaultServerResource(); if (!(defaultServerResource == null || defaultServerResource.trim().length() == 0)) { Element defaultResourceElement = createDefaultServerResourceElement(doc, defaultServerResource); serverSideConfigurationElem.appendChild(defaultResourceElement); } Map<String, ServerSideConfiguration.Pool> resourcePools = serverSideConfiguration.getResourcePools(); if (resourcePools != null) { resourcePools.forEach( (key, value) -> { Element poolElement = createSharedPoolElement(doc, key, value); serverSideConfigurationElem.appendChild(poolElement); } ); } } return serverSideConfigurationElem; }
private Element createRootUrlElement(Document doc, ClusteringServiceConfiguration clusteringServiceConfiguration) { Element rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + CLUSTER_ELEMENT_NAME); Element urlElement = createUrlElement(doc, clusteringServiceConfiguration); rootElement.appendChild(urlElement); return rootElement; }
@Test public void testTranslateServiceCreationConfigurationWithNoServerSideConfig() throws Exception { URI connectionUri = new URI("terracotta://localhost:9510/my-application"); ClusteringServiceConfiguration serviceConfig = ClusteringServiceConfigurationBuilder.cluster(connectionUri) .timeouts(Timeouts.DEFAULT) .build(); ClusteringCacheManagerServiceConfigurationParser parser = new ClusteringCacheManagerServiceConfigurationParser(); Element returnElement = parser.unparseServiceCreationConfiguration(serviceConfig); String inputString = "<tc:cluster xmlns:tc = \"http://www.ehcache.org/v3/clustered\">" + "<tc:connection url = \"terracotta://localhost:9510/my-application\"/>" + "<tc:read-timeout unit = \"seconds\">5</tc:read-timeout>" + "<tc:write-timeout unit = \"seconds\">5</tc:write-timeout>" + "<tc:connection-timeout unit = \"seconds\">150</tc:connection-timeout>" + "<tc:server-side-config auto-create = \"false\">" + "</tc:server-side-config></tc:cluster>"; assertElement(inputString, returnElement); }
@Test public void testTranslateClusteredResourcePoolConfiguration() { ClusteredResourceConfigurationParser configTranslator = new ClusteredResourceConfigurationParser(); ClusteredResourcePoolImpl clusteredResourcePool = new ClusteredResourcePoolImpl(); Node retElement = configTranslator.unparseResourcePool(clusteredResourcePool); String inputString = "<tc:clustered xmlns:tc = \"http://www.ehcache.org/v3/clustered\" />"; assertElement(inputString, retElement); }
@Override public ResourcePool parseResourceConfiguration(final Element fragment) { ResourcePool resourcePool = parseResourceConfig(fragment); if (resourcePool != null) { return resourcePool; } throw new XmlConfigurationException(String.format("XML configuration element <%s> in <%s> is not supported", fragment.getTagName(), (fragment.getParentNode() == null ? "null" : fragment.getParentNode().getLocalName()))); }
private void processTimeUnits(Document doc, Element parent, ClusteringServiceConfiguration clusteringServiceConfiguration) { if (clusteringServiceConfiguration.getTimeouts() != null) { Timeouts timeouts = clusteringServiceConfiguration.getTimeouts(); Element readTimeoutElem = createTimeoutElement(doc, READ_TIMEOUT_ELEMENT_NAME, timeouts.getReadOperationTimeout()); Element writeTimeoutElem = createTimeoutElement(doc, WRITE_TIMEOUT_ELEMENT_NAME, timeouts.getWriteOperationTimeout()); Element connectionTimeoutElem = createTimeoutElement(doc, CONNECTION_TIMEOUT_ELEMENT_NAME, timeouts.getConnectionTimeout()); /* Important: do not change the order of following three elements if corresponding change is not done in xsd */ parent.appendChild(readTimeoutElem); parent.appendChild(writeTimeoutElem); parent.appendChild(connectionTimeoutElem); } }
@Override protected Element createRootElement(Document doc, ClusteredStoreConfiguration clusteredStoreConfiguration) { Consistency consistency = clusteredStoreConfiguration.getConsistency(); Element rootElement = doc.createElementNS(getNamespace().toString(), TC_CLUSTERED_NAMESPACE_PREFIX + CLUSTERED_STORE_ELEMENT_NAME); rootElement.setAttribute(CONSISTENCY_ATTRIBUTE_NAME, consistency.name().toLowerCase()); return rootElement; }
@Test public void testTranslateServiceStoreConfiguration() { ClusteringCacheServiceConfigurationParser configurationTranslator = new ClusteringCacheServiceConfigurationParser(); Node retNode = configurationTranslator.unparseServiceConfiguration( ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG).build()); String inputString = "<tc:clustered-store consistency = \"strong\" " + "xmlns:tc = \"http://www.ehcache.org/v3/clustered\"></tc:clustered-store>"; assertElement(inputString, retNode); } }
@Test(expected = XmlConfigurationException.class) public void testServersOnly() throws Exception { final String[] config = new String[] { "<ehcache:config", " xmlns:ehcache=\"http://www.ehcache.org/v3\"", " xmlns:tc=\"http://www.ehcache.org/v3/clustered\">", "", " <ehcache:service>", " <tc:cluster>", " <tc:cluster-connection>", " <tc:server host=\"blah\" port=\"1234\" />", " </tc:cluster-connection>", " </tc:cluster>", " </ehcache:service>", "", "</ehcache:config>" }; new XmlConfiguration(makeConfig(config)); }
@Test public void testTranslateServiceCreationConfigurationWithInetSocketAddress() { InetSocketAddress firstServer = InetSocketAddress.createUnresolved("100.100.100.100", 9510); InetSocketAddress secondServer = InetSocketAddress.createUnresolved("server-2", 0); InetSocketAddress thirdServer = InetSocketAddress.createUnresolved("[::1]", 0); InetSocketAddress fourthServer = InetSocketAddress.createUnresolved("[fe80::1453:846e:7be4:15fe]", 9710); List<InetSocketAddress> servers = Arrays.asList(firstServer, secondServer, thirdServer, fourthServer); ClusteringServiceConfiguration serviceConfig = ClusteringServiceConfigurationBuilder.cluster(servers, "my-application") .timeouts(Timeouts.DEFAULT) .build(); ClusteringCacheManagerServiceConfigurationParser parser = new ClusteringCacheManagerServiceConfigurationParser(); Element returnElement = parser.unparseServiceCreationConfiguration(serviceConfig); String inputString = "<tc:cluster xmlns:tc = \"http://www.ehcache.org/v3/clustered\">" + "<tc:cluster-connection cluster-tier-manager = \"my-application\">" + "<tc:server host = \"100.100.100.100\" port = \"9510\"/>" + "<tc:server host = \"server-2\"/>" + "<tc:server host = \"[::1]\"/>" + "<tc:server host = \"[fe80::1453:846e:7be4:15fe]\" port = \"9710\"/>" + "</tc:cluster-connection>" + "<tc:read-timeout unit = \"seconds\">5</tc:read-timeout>" + "<tc:write-timeout unit = \"seconds\">5</tc:write-timeout>" + "<tc:connection-timeout unit = \"seconds\">150</tc:connection-timeout>" + "<tc:server-side-config auto-create = \"false\"/></tc:cluster>"; assertElement(inputString, returnElement); }
@Test public void testTranslateDedicatedResourcePoolConfiguration() { ClusteredResourceConfigurationParser configTranslator = new ClusteredResourceConfigurationParser(); DedicatedClusteredResourcePoolImpl dedicatedClusteredResourcePool = new DedicatedClusteredResourcePoolImpl("my-from", 12, MemoryUnit.GB); Node retElement = configTranslator.unparseResourcePool(dedicatedClusteredResourcePool); String inputString = "<tc:clustered-dedicated from = \"my-from\" unit = \"GB\" " + "xmlns:tc = \"http://www.ehcache.org/v3/clustered\">12</tc:clustered-dedicated>"; assertElement(inputString, retElement); }
@Test(expected = XmlConfigurationException.class) public void testUrlAndServers() throws Exception { final String[] config = new String[] { "<ehcache:config", " xmlns:ehcache=\"http://www.ehcache.org/v3\"", " xmlns:tc=\"http://www.ehcache.org/v3/clustered\">", "", " <ehcache:service>", " <tc:cluster>", " <tc:connection url=\"terracotta://example.com:9540/cachemanager\" />", " <tc:cluster-connection cluster-tier-manager=\"cM\">", " <tc:server host=\"blah\" port=\"1234\" />", " </tc:cluster-connection>", " </tc:cluster>", " </ehcache:service>", "", "</ehcache:config>" }; new XmlConfiguration(makeConfig(config)); }
@Test public void testTranslateServiceCreationConfigurationWithNoResourcePoolAndAutoCreateFalse() throws Exception { URI connectionUri = new URI("terracotta://localhost:9510/my-application"); ClusteringServiceConfiguration serviceConfig = ClusteringServiceConfigurationBuilder.cluster(connectionUri) .timeouts(Timeouts.DEFAULT) .expecting() .defaultServerResource("main") .build(); ClusteringCacheManagerServiceConfigurationParser parser = new ClusteringCacheManagerServiceConfigurationParser(); Element returnElement = parser.unparseServiceCreationConfiguration(serviceConfig); String inputString = "<tc:cluster xmlns:tc = \"http://www.ehcache.org/v3/clustered\">" + "<tc:connection url = \"terracotta://localhost:9510/my-application\"/>" + "<tc:read-timeout unit = \"seconds\">5</tc:read-timeout>" + "<tc:write-timeout unit = \"seconds\">5</tc:write-timeout>" + "<tc:connection-timeout unit = \"seconds\">150</tc:connection-timeout>" + "<tc:server-side-config auto-create = \"false\">" + "<tc:default-resource from = \"main\"/>" + "</tc:server-side-config></tc:cluster>"; assertElement(inputString, returnElement); }
@Test public void testTranslateSharedResourcePoolConfiguration() { ClusteredResourceConfigurationParser configTranslator = new ClusteredResourceConfigurationParser(); SharedClusteredResourcePoolImpl sharedResourcePool = new SharedClusteredResourcePoolImpl("shared-pool"); Node retElement = configTranslator.unparseResourcePool(sharedResourcePool); String inputString = "<tc:clustered-shared sharing = \"shared-pool\" " + "xmlns:tc = \"http://www.ehcache.org/v3/clustered\"></tc:clustered-shared>"; assertElement(inputString, retElement); }
@Test public void testGetTimeoutValueTooBig() throws Exception { final String[] config = new String[] { "<ehcache:config", " xmlns:ehcache=\"http://www.ehcache.org/v3\"", " xmlns:tc=\"http://www.ehcache.org/v3/clustered\">", "", " <ehcache:service>", " <tc:cluster>", " <tc:connection url=\"terracotta://example.com:9540/cachemanager\"/>", " <tc:read-timeout unit=\"seconds\">" + BigInteger.ONE.add(BigInteger.valueOf(Long.MAX_VALUE)) + "</tc:read-timeout>", " </tc:cluster>", " </ehcache:service>", "", "</ehcache:config>" }; try { new XmlConfiguration(makeConfig(config)); fail("Expecting XmlConfigurationException"); } catch (XmlConfigurationException e) { assertThat(e.getMessage(), containsString(" exceeds allowed value ")); } }
@Test public void testTranslateServiceCreationConfiguration() throws Exception { URI connectionUri = new URI("terracotta://localhost:9510/my-application"); ClusteringServiceConfiguration serviceConfig = ClusteringServiceConfigurationBuilder.cluster(connectionUri) .timeouts(Timeouts.DEFAULT) .autoCreate() .defaultServerResource("main") .resourcePool("primaryresource", 5, MemoryUnit.GB) .resourcePool("secondaryresource", 10, MemoryUnit.GB, "optional") .build(); ClusteringCacheManagerServiceConfigurationParser parser = new ClusteringCacheManagerServiceConfigurationParser(); Element returnElement = parser.unparseServiceCreationConfiguration(serviceConfig); String inputString = "<tc:cluster xmlns:tc = \"http://www.ehcache.org/v3/clustered\">" + "<tc:connection url = \"terracotta://localhost:9510/my-application\"/>" + "<tc:read-timeout unit = \"seconds\">5</tc:read-timeout>" + "<tc:write-timeout unit = \"seconds\">5</tc:write-timeout>" + "<tc:connection-timeout unit = \"seconds\">150</tc:connection-timeout>" + "<tc:server-side-config auto-create = \"true\">" + "<tc:default-resource from = \"main\"/>" + "<tc:shared-pool name = \"primaryresource\" unit = \"B\">5368709120</tc:shared-pool>" + "<tc:shared-pool from = \"optional\" name = \"secondaryresource\" unit = \"B\">10737418240</tc:shared-pool>" + "</tc:server-side-config></tc:cluster>"; assertElement(inputString, returnElement); }