From c48e8bd4eca3c469546054a5721424b3d481e117 Mon Sep 17 00:00:00 2001 From: Chris Dennis Date: Fri, 18 Mar 2016 14:34:25 -0400 Subject: [PATCH] Issue #50 Add configuration parsing logic --- offheap-resource/pom.xml | 30 ++++++- .../OffHeapResourceConfigurationParser.java | 72 ++++++++++++++++ .../OffHeapResourcesConfiguration.java | 43 ++++++++++ .../OffHeapResourcesProvider.java | 39 +++++++++ ...ffHeapResourceConfigurationParserTest.java | 84 +++++++++++++++++++ .../test/resources/configs/no-resources.xml | 23 +++++ .../src/test/resources/configs/valid.xml | 25 ++++++ 7 files changed, 313 insertions(+), 3 deletions(-) create mode 100644 offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java create mode 100644 offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java create mode 100644 offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesProvider.java create mode 100644 offheap-resource/src/test/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParserTest.java create mode 100644 offheap-resource/src/test/resources/configs/no-resources.xml create mode 100644 offheap-resource/src/test/resources/configs/valid.xml diff --git a/offheap-resource/pom.xml b/offheap-resource/pom.xml index dcf6b8f6a8..78f59ee9fe 100644 --- a/offheap-resource/pom.xml +++ b/offheap-resource/pom.xml @@ -12,15 +12,20 @@ - org.terracotta - standard-cluster-services - provided + org.terracotta.internal + tc-config-parser + 10.0-SNAPSHOT junit junit test + + org.hamcrest + hamcrest-all + 1.3 + org.mockito mockito-all @@ -45,6 +50,25 @@ + + org.codehaus.mojo + jaxb2-maven-plugin + 2.2 + + + xjc + + xjc + + + + + org.terracotta.offheapresource.config + + src/main/resources/offheap-resource.xsd + + + diff --git a/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java new file mode 100644 index 0000000000..688bc62a0b --- /dev/null +++ b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java @@ -0,0 +1,72 @@ +/* + * The contents of this file are subject to the Terracotta Public License Version + * 2.0 (the "License"); You may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://terracotta.org/legal/terracotta-public-license. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for + * the specific language governing rights and limitations under the License. + * + * The Covered Software is OffHeap Resource. + * + * The Initial Developer of the Covered Software is + * Terracotta, Inc., a Software AG company + */ + +package org.terracotta.offheapresource; + +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.SchemaFactory; +import org.terracotta.config.service.ServiceConfigParser; +import org.terracotta.entity.ServiceProviderConfiguration; +import org.w3c.dom.Element; + +import org.terracotta.offheapresource.config.OffheapResourcesType; +import org.xml.sax.SAXException; + +public class OffHeapResourceConfigurationParser implements ServiceConfigParser { + + private static final URL XML_SCHEMA = OffHeapResourceConfigurationParser.class.getResource("/offheap-resource.xsd"); + private static final URI NAMESPACE = URI.create("http://www.terracotta.org/config/offheap-resource"); + + @Override + public Source getXmlSchema() throws IOException { + return new StreamSource(XML_SCHEMA.openStream()); + } + + @Override + public URI getNamespace() { + return NAMESPACE; + } + + @Override + public ServiceProviderConfiguration parse(Element elmnt, String string) { + try { + JAXBContext jaxbContext = JAXBContext.newInstance(OffheapResourcesType.class.getPackage().getName(), OffHeapResourceConfigurationParser.class.getClassLoader()); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(getXmlSchema())); + JAXBElement parsed = (JAXBElement) unmarshaller.unmarshal(elmnt); + return new OffHeapResourcesConfiguration(parsed.getValue()); + } catch (JAXBException e) { + throw new IllegalArgumentException(e); + } catch (SAXException e) { + throw new AssertionError(e); + } catch (IOException e) { + throw new AssertionError(e); + } + } + +} diff --git a/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java new file mode 100644 index 0000000000..431360e1f0 --- /dev/null +++ b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java @@ -0,0 +1,43 @@ +/* + * The contents of this file are subject to the Terracotta Public License Version + * 2.0 (the "License"); You may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://terracotta.org/legal/terracotta-public-license. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for + * the specific language governing rights and limitations under the License. + * + * The Covered Software is OffHeap Resource. + * + * The Initial Developer of the Covered Software is + * Terracotta, Inc., a Software AG company + */ + +package org.terracotta.offheapresource; + +import java.util.Collection; +import org.terracotta.entity.ServiceProvider; +import org.terracotta.entity.ServiceProviderConfiguration; +import org.terracotta.offheapresource.config.OffheapResourcesType; +import org.terracotta.offheapresource.config.ResourceType; + +public class OffHeapResourcesConfiguration implements ServiceProviderConfiguration { + + private final OffheapResourcesType xmlConfig; + + OffHeapResourcesConfiguration(OffheapResourcesType xmlConfig) { + this.xmlConfig = xmlConfig; + } + + public Collection getResources() { + return xmlConfig.getResource(); + } + + @Override + public Class getServiceProviderType() { + return OffHeapResourcesProvider.class; + } + +} diff --git a/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesProvider.java b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesProvider.java new file mode 100644 index 0000000000..19e05643c2 --- /dev/null +++ b/offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesProvider.java @@ -0,0 +1,39 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.terracotta.offheapresource; + +import java.io.IOException; +import java.util.Collection; +import org.terracotta.entity.ServiceConfiguration; +import org.terracotta.entity.ServiceProvider; +import org.terracotta.entity.ServiceProviderConfiguration; + +/** + * + * @author cdennis + */ +class OffHeapResourcesProvider implements ServiceProvider { + + @Override + public boolean initialize(ServiceProviderConfiguration configuration) { + throw new UnsupportedOperationException(); + } + + @Override + public T getService(long consumerID, ServiceConfiguration configuration) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection> getProvidedServiceTypes() { + throw new UnsupportedOperationException(); + } + + @Override + public void close() throws IOException { + throw new UnsupportedOperationException(); + } +} diff --git a/offheap-resource/src/test/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParserTest.java b/offheap-resource/src/test/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParserTest.java new file mode 100644 index 0000000000..d51b80d277 --- /dev/null +++ b/offheap-resource/src/test/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParserTest.java @@ -0,0 +1,84 @@ +/* + * The contents of this file are subject to the Terracotta Public License Version + * 2.0 (the "License"); You may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://terracotta.org/legal/terracotta-public-license. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for + * the specific language governing rights and limitations under the License. + * + * The Covered Software is OffHeap Resource. + * + * The Initial Developer of the Covered Software is + * Terracotta, Inc., a Software AG company + */ + +package org.terracotta.offheapresource; + +import java.math.BigInteger; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.validation.SchemaFactory; +import org.hamcrest.Matcher; +import org.hamcrest.core.IsCollectionContaining; +import org.junit.Test; +import org.terracotta.offheapresource.config.MemoryUnit; +import org.terracotta.offheapresource.config.ResourceType; +import org.w3c.dom.Document; + +import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; +import static org.hamcrest.core.AllOf.allOf; +import static org.hamcrest.core.Is.is; +import org.junit.Assert; +import static org.junit.Assert.assertThat; + +/** + * + * @author cdennis + */ +public class OffHeapResourceConfigurationParserTest { + + @Test + public void testValidParse() throws Exception { + OffHeapResourceConfigurationParser parser = new OffHeapResourceConfigurationParser(); + + DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance(); + domBuilderFactory.setNamespaceAware(true); + domBuilderFactory.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(parser.getXmlSchema())); + + Document dom = domBuilderFactory.newDocumentBuilder().parse(getClass().getResourceAsStream("/configs/valid.xml")); + + OffHeapResourcesConfiguration config = (OffHeapResourcesConfiguration) parser.parse(dom.getDocumentElement(), "what is this thing?"); + + assertThat(config.getResources(), IsCollectionContaining.hasItems( + resource("primary", 128, MemoryUnit.GB), + resource("secondary", 1024, MemoryUnit.MB))); + } + + @Test + public void testNoResources() throws Exception { + OffHeapResourceConfigurationParser parser = new OffHeapResourceConfigurationParser(); + + DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance(); + domBuilderFactory.setNamespaceAware(true); + domBuilderFactory.setValidating(true); + domBuilderFactory.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(parser.getXmlSchema())); + + Document dom = domBuilderFactory.newDocumentBuilder().parse(getClass().getResourceAsStream("/configs/no-resources.xml")); + + try { + parser.parse(dom.getDocumentElement(), "what is this thing?"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + //expected + } + } + + + private static Matcher resource(String name, long size, MemoryUnit unit) { + return allOf(hasProperty("name", is(name)), hasProperty("unit", is(unit)), + hasProperty("value", is(BigInteger.valueOf(size)))); + } +} diff --git a/offheap-resource/src/test/resources/configs/no-resources.xml b/offheap-resource/src/test/resources/configs/no-resources.xml new file mode 100644 index 0000000000..001a63d886 --- /dev/null +++ b/offheap-resource/src/test/resources/configs/no-resources.xml @@ -0,0 +1,23 @@ + + + + + diff --git a/offheap-resource/src/test/resources/configs/valid.xml b/offheap-resource/src/test/resources/configs/valid.xml new file mode 100644 index 0000000000..2ce63d9096 --- /dev/null +++ b/offheap-resource/src/test/resources/configs/valid.xml @@ -0,0 +1,25 @@ + + + + + 128 + 1024 +