-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #50 Add configuration parsing logic
- Loading branch information
1 parent
e5989f2
commit 4ba8f13
Showing
8 changed files
with
318 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...urce/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 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); | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.ServiceProviderCleanupException; | ||
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(); | ||
} | ||
|
||
@Override | ||
public void clear() throws ServiceProviderCleanupException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ce/src/main/resources/META-INF/services/org.terracotta.config.service.ServiceConfigParser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.terracotta.offheapresource.OffHeapResourceConfigurationParser |
84 changes: 84 additions & 0 deletions
84
.../src/test/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
offheap-resource/src/test/resources/configs/no-resources.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |