Skip to content

Commit

Permalink
Merge 1319bb5 into edfe882
Browse files Browse the repository at this point in the history
  • Loading branch information
luan-cestari committed Sep 15, 2014
2 parents edfe882 + 1319bb5 commit 04b5b02
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.metadata.DataStore;
import com.redhat.lightblue.metadata.MetadataConstants;
import com.redhat.lightblue.metadata.parser.Extensions;
import com.redhat.lightblue.metadata.parser.HookConfigurationParser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.redhat.lightblue.metadata.parser.*;

import java.util.*;

import com.redhat.lightblue.util.Error;

/**
Expand All @@ -37,6 +37,8 @@
public abstract class AbstractMetadataConfiguration implements MetadataConfiguration {

private final List<HookConfigurationParser> hookConfigurationParsers = new ArrayList<>();
private final List<Map.Entry<String,DataStoreParser>> backendParsers = new ArrayList<>();
private final List<Map.Entry<String,PropertyParser>> propertyParsers = new ArrayList<>();

/**
* Register any common bits with the given Extensions instance.
Expand All @@ -45,6 +47,12 @@ protected void registerWithExtensions(Extensions ext) {
for (HookConfigurationParser parser : hookConfigurationParsers) {
ext.registerHookConfigurationParser(parser.getName(), parser);
}
for (Map.Entry<String, DataStoreParser> parser : backendParsers) {
ext.registerDataStoreParser(parser.getKey(), parser.getValue());
}
for (Map.Entry<String, PropertyParser> parser : propertyParsers) {
ext.registerPropertyParser(parser.getKey(), parser.getValue());
}
}

@Override
Expand Down Expand Up @@ -75,6 +83,50 @@ public void initializeFromJson(JsonNode node) {
}
}
}


ArrayNode backendParsersJs = (ArrayNode) node.get("backendParsers");
if(backendParsersJs != null) {
for (int i = 0; i < backendParsersJs.size(); i++) {
JsonNode jsonNode = backendParsersJs.get(i);
String name = jsonNode.get("name").asText();
String clazz = jsonNode.get("clazz").asText();

if (name == null || clazz == null) {
throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID, "Backend/DataStoreParser class was not informed: name='" + name + "' clazz=" + clazz);
}

try {
DataStoreParser instance = (DataStoreParser) Class.forName(clazz).newInstance();
AbstractMap.SimpleEntry<String, DataStoreParser> stringDataStoreParserSimpleEntry = new AbstractMap.SimpleEntry<>(name, instance);
backendParsers.add(stringDataStoreParserSimpleEntry);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID, "Class not instance of Backend/DataStoreParser: " + clazz);
}
}
}

ArrayNode propertyParserJs = (ArrayNode) node.get("propertyParsers");
if(propertyParserJs != null) {
for (int i = 0; i < propertyParserJs.size(); i++) {
JsonNode jsonNode = propertyParserJs.get(i);
String name = jsonNode.get("name").asText();
String clazz = jsonNode.get("clazz").asText();

if (name == null || clazz == null) {
throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID, "PropertyParser Name/Class not informed: name=" + name + " clazz=" + clazz);
}

try {
PropertyParser instance = (PropertyParser) Class.forName(clazz).newInstance();

AbstractMap.SimpleEntry<String, PropertyParser> stringPropertyParserSimpleEntry = new AbstractMap.SimpleEntry<>(name, instance);
propertyParsers.add(stringPropertyParserSimpleEntry);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID, "Class not instance of PropertyParser: " + clazz);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,8 @@ public void testExtensions() throws Exception {

// verify extensions
Assert.assertNotNull(metadata.extensions.getHookConfigurationParser(TestHookConfigurationParser.HOOK_NAME));

Assert.assertNotNull(metadata.extensions.getPropertyParser("TestPropertyParser"));
Assert.assertNotNull(metadata.extensions.getDataStoreParser("TestDataStoreParser"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2013 Red Hat, Inc. and/or its affiliates.
This file is part of lightblue.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.redhat.lightblue.config;

import com.redhat.lightblue.metadata.parser.DataStoreParser;
import com.redhat.lightblue.metadata.parser.MetadataParser;

public class TestDataStoreParser implements DataStoreParser {
@Override
public String getDefaultName() {
return null;
}

@Override
public Object parse(String name, MetadataParser p, Object node) {
return null;
}

@Override
public void convert(MetadataParser p, Object emptyNode, Object object) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2013 Red Hat, Inc. and/or its affiliates.
This file is part of lightblue.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.redhat.lightblue.config;

import com.redhat.lightblue.metadata.parser.MetadataParser;
import com.redhat.lightblue.metadata.parser.PropertyParser;

public class TestPropertyParser extends PropertyParser {
@Override
public Object parse(String name, MetadataParser p, Object node) {
return null;
}

@Override
public void convert(MetadataParser p, Object emptyNode, Object object) {

}
}
14 changes: 13 additions & 1 deletion config/src/test/resources/lightblue-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@
"type": "com.redhat.lightblue.config.TestConfig",
"hookConfigurationParsers": ["com.redhat.lightblue.config.TestHookConfigurationParser"],
"dataSource": "mongo",
"collection": "metadata"
"collection": "metadata",
"backendParsers": [
{
"name": "TestDataStoreParser",
"clazz": "com.redhat.lightblue.config.TestDataStoreParser"
}
],
"propertyParsers": [
{
"name": "TestPropertyParser",
"clazz": "com.redhat.lightblue.config.TestPropertyParser"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@
*/
package com.redhat.lightblue.metadata.parser;

import com.redhat.lightblue.metadata.DataStore;
import com.redhat.lightblue.metadata.EntityConstraint;
import com.redhat.lightblue.metadata.FieldConstraint;
import com.redhat.lightblue.metadata.HookConfiguration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.metadata.*;
import com.redhat.lightblue.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
* Parser extensions where T is the node type of the underlying object tree (for
* JSon, T is JsonNode).
*/
public class Extensions<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(Extensions.class);

private final ParserRegistry<T, DataStore> backendParsers = new ParserRegistry<>();

private final ParserRegistry<T, EntityConstraint> entityConstraintParsers = new ParserRegistry<>();
private final ParserRegistry<T, FieldConstraint> fieldConstraintParsers = new ParserRegistry<>();
private final ParserRegistry<T, HookConfiguration> hookConfigurationParsers = new ParserRegistry<>();
Expand All @@ -44,6 +48,7 @@ public class Extensions<T> {
public void addDefaultExtensions() {
fieldConstraintParsers.add(new DefaultFieldConstraintParsers<T>());
entityConstraintParsers.add(new DefaultEntityConstraintParsers<T>());
LOGGER.debug("Initialized addDefaultExtensions");
}

/**
Expand Down

0 comments on commit 04b5b02

Please sign in to comment.