Skip to content

Commit

Permalink
Issue #50 Add configuration parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdennis committed Mar 18, 2016
1 parent 3ae793b commit c48e8bd
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 3 deletions.
30 changes: 27 additions & 3 deletions offheap-resource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@

<dependencies>
<dependency>
<groupId>org.terracotta</groupId>
<artifactId>standard-cluster-services</artifactId>
<scope>provided</scope>
<groupId>org.terracotta.internal</groupId>
<artifactId>tc-config-parser</artifactId>
<version>10.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand All @@ -45,6 +50,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.terracotta.offheapresource.config</packageName>
<sources>
<source>src/main/resources/offheap-resource.xsd</source>
</sources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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<OffheapResourcesType> parsed = (JAXBElement<OffheapResourcesType>) 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<ResourceType> getResources() {
return xmlConfig.getResource();
}

@Override
public Class<? extends ServiceProvider> getServiceProviderType() {
return OffHeapResourcesProvider.class;
}

}
Original file line number Diff line number Diff line change
@@ -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> T getService(long consumerID, ServiceConfiguration<T> configuration) {
throw new UnsupportedOperationException();
}

@Override
public Collection<Class<?>> getProvidedServiceTypes() {
throw new UnsupportedOperationException();
}

@Override
public void close() throws IOException {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -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.<ResourceType>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<ResourceType> resource(String name, long size, MemoryUnit unit) {
return allOf(hasProperty("name", is(name)), hasProperty("unit", is(unit)),
hasProperty("value", is(BigInteger.valueOf(size))));
}
}
23 changes: 23 additions & 0 deletions offheap-resource/src/test/resources/configs/no-resources.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->

<ohr:offheap-resources
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ohr='http://www.terracotta.org/config/offheap-resource'
xsi:schemaLocation='http://www.terracotta.org/config/offheap-resource ../../../../src/main/resources/offheap-resource.xsd'>
</ohr:offheap-resources>
25 changes: 25 additions & 0 deletions offheap-resource/src/test/resources/configs/valid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->

<ohr:offheap-resources
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ohr='http://www.terracotta.org/config/offheap-resource'
xsi:schemaLocation='http://www.terracotta.org/config/offheap-resource ../../../../src/main/resources/offheap-resource.xsd'>
<ohr:resource name="primary" unit="GB">128</ohr:resource>
<ohr:resource name="secondary" unit="MB">1024</ohr:resource>
</ohr:offheap-resources>

0 comments on commit c48e8bd

Please sign in to comment.