diff options
author | Christian Wagner <c.wagner@datentechnik-innovation.com> | 2015-01-13 16:12:36 +0100 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:07:45 +0200 |
commit | c20d8f3da0b112864130031d39800e60289380d4 (patch) | |
tree | e378d9016f46dfe70e8ad49acabc482a04f32eec /id/server/idserverlib/src/main/java | |
parent | 3ee292090346f27e7e23d4943b74ada281f20552 (diff) | |
download | moa-id-spss-c20d8f3da0b112864130031d39800e60289380d4.tar.gz moa-id-spss-c20d8f3da0b112864130031d39800e60289380d4.tar.bz2 moa-id-spss-c20d8f3da0b112864130031d39800e60289380d4.zip |
add raw configuration persistence functionality
some problems: * all tables annotated via '@Table' are created in every database connected
* loading of 'Configuration' via Spring isn't pretty at all
Diffstat (limited to 'id/server/idserverlib/src/main/java')
-rw-r--r-- | id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java | 228 |
1 files changed, 112 insertions, 116 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java index 00a685b5e..321cb150c 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java @@ -3,178 +3,174 @@ package at.gv.egovernment.moa.id.config.auth; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.Map; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; import at.gv.egovernment.moa.id.config.ConfigurationException; import at.gv.egovernment.moa.id.config.ConfigurationProvider; -import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.datentechnik.moa.id.conf.persistence.Configuration; +import com.datentechnik.moa.id.conf.persistence.ConfigurationImpl; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; public class ConfigurationToJSONConverter { AuthConfigurationProvider config; + Configuration configuration; public static void main(String[] args) { try { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - Map<String, String> jsonResults = converter.convertConfigToJSONStrings(true); - for (String name : jsonResults.keySet()) { - System.out.println("Method: " + name + " = "); - System.out.println(jsonResults.get(name)); - converter.convertJsonStringToObject(jsonResults.get(name)); - } - - System.out.println("Try conversion from Json to object"); - for (String name : jsonResults.keySet()) { - Object obj = converter.convertJsonStringToObject(jsonResults.get(name)); - System.out.println(obj); - } + converter.writeConfigToJSONDB(); + System.out.println("====================================="); + System.out.println("====================================="); + converter.readConfigFromDB(); } catch (ConfigurationException e) { e.printStackTrace(); - System.out.println("Problems reading the configuration file in: " - + System.getProperty(ConfigurationProvider.CONFIG_PROPERTY_NAME)); + System.out.println("Problems reading the configuration file in: " + System.getProperty(ConfigurationProvider.CONFIG_PROPERTY_NAME)); System.exit(1); } + } public ConfigurationToJSONConverter() throws ConfigurationException { config = AuthConfigurationProvider.getInstance(); - } - public void writeConfigToJSONFile(String jsonFileName, boolean isPrettyPrint) throws FileNotFoundException, - IOException, ConfigurationException { - File out = new File(jsonFileName); - try (PrintWriter outStream = new PrintWriter(out)) { - // get pretty printed JSON - ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - Map<String, String> jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); - for (String name : jsonResults.keySet()) { - outStream.println(name); - outStream.println(jsonResults.get(name)); - } - } + System.getProperties().setProperty("location", "file:" + "c:\\Users\\cwagner\\dev\\temp\\moaid_test_db.properties"); - } + ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); + configuration = (ConfigurationImpl) context.getBean(ConfigurationImpl.class); - public void writeConfigToJSONDB() throws ConfigurationException { - ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - Map<String, String> jsonResults = converter.convertConfigToJSONStrings(false); - if (jsonResults.isEmpty()) { - System.out.println("WARN: writing EMPTY configuration to database"); - } - // TODO: write to db } - public Map<String, String> convertConfigToJSONStrings(boolean prettyPrint) { - - Map<String, String> result = new HashMap<String, String>(); - JsonMapper mapper = new JsonMapper(prettyPrint); + // public void writeConfigToJSONFile(String jsonFileName, boolean isPrettyPrint) throws FileNotFoundException, + // IOException, ConfigurationException { + // File out = new File(jsonFileName); + // try (PrintWriter outStream = new PrintWriter(out)) { + // // get pretty printed JSON + // ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); + // Map<String, String> jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); + // for (String name : jsonResults.keySet()) { + // outStream.println(name); + // outStream.println(jsonResults.get(name)); + // } + // } + // + // } + + public void writeConfigToJSONDB() { try { // find all getter methods - for (PropertyDescriptor pd : Introspector.getBeanInfo(AuthConfigurationProvider.class) - .getPropertyDescriptors()) { + for (PropertyDescriptor pd : Introspector.getBeanInfo(AuthConfigurationProvider.class).getPropertyDescriptors()) { // check if correct methods, and not annotated with @JsonIgnore - if ((pd.getReadMethod() != null) - && (!"class".equals(pd.getName())) - && (pd.getReadMethod().getAnnotation(JsonIgnore.class) == null)) { + if ((pd.getReadMethod() != null) && (!"class".equals(pd.getName())) && (pd.getReadMethod().getAnnotation(JsonIgnore.class) == null)) { + + JsonProperty name = pd.getReadMethod().getAnnotation(JsonProperty.class); + // get result of get method + Object tmp; try { - JsonProperty name = pd.getReadMethod().getAnnotation(JsonProperty.class); - // get result of get method - Object tmp = pd.getReadMethod().invoke(config); + tmp = pd.getReadMethod().invoke(config); + // convert result to JSON - String json = mapper.serialize(tmp); + if (name != null) { - result.put(name.value(), json); + + + configuration.set(name.value(), tmp); } else { System.out.println("CHECK if '" + pd.getDisplayName() + "' is NOT ANNOTATED"); } - - } catch (JsonGenerationException | JsonMappingException e) { - System.out.println("Problems while writing JSON as String"); - return new HashMap<String, String>(); + } catch (IllegalAccessException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } + } } // no static method handling needed - } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + } catch (IllegalArgumentException e) { System.out.println("Problems while using reflection to get all getter methods."); - } catch (IOException e) { - System.out.println("Problems while writing JSON as string"); - return new HashMap<String, String>(); + } catch (IntrospectionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - return result; } - public Object convertJsonStringToObject(String jsonString){ - JsonMapper mapper = new JsonMapper(); - Object result = null; + public void readConfigFromDB() { try { - result = mapper.deserialize(jsonString, null); - } catch (JsonParseException | JsonMappingException e) { - System.out.println("Problems parsing the JSON string"); - return null; - } catch (IOException e) { - System.out.println("Problems while reading JSON string"); - return null; - } - - return result; - } - - private class JsonMapper { - - private ObjectMapper mapper = new ObjectMapper(); - - public JsonMapper(){ - this(false); - } + // find all getter methods + for (PropertyDescriptor pd : Introspector.getBeanInfo(AuthConfigurationProvider.class).getPropertyDescriptors()) { + // check if correct methods, and not annotated with @JsonIgnore + if ((pd.getReadMethod() != null) && (!"class".equals(pd.getName())) && (pd.getReadMethod().getAnnotation(JsonIgnore.class) == null)) { + + JsonProperty name = pd.getReadMethod().getAnnotation(JsonProperty.class); + // get result of get method + Object tmp; + if (name != null) { + + Object o = configuration.get(name.value()); + + System.out.println(">>> OBJECT: " + o ); + + } else { + System.out.println("CHECK if '" + pd.getDisplayName() + "' is NOT ANNOTATED"); + } - public JsonMapper(boolean prettyPrint) { - mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); - mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); - mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); - if (prettyPrint) { - mapper.enable(SerializationFeature.INDENT_OUTPUT); + } } - } - - public String serialize(Object value) throws JsonProcessingException { - return mapper.writeValueAsString(value); - } - public Object deserialize(String value, Class type) throws JsonParseException, JsonMappingException, - IOException { - - ObjectMapper mapper = new ObjectMapper(); - if (type != null) { - return mapper.readValue(value, type); - } else { - return mapper.readValue(value, Object.class); - } + // no static method handling needed + } catch (IllegalArgumentException e) { + System.out.println("Problems while using reflection to get all getter methods."); + } catch (IntrospectionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - - } + } + // ============================================================================= + // private class JsonMapper { + // + // private ObjectMapper mapper = new ObjectMapper(); + // + // public JsonMapper(){ + // this(false); + // } + // + // public JsonMapper(boolean prettyPrint) { + // mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + // mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + // mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + // if (prettyPrint) { + // mapper.enable(SerializationFeature.INDENT_OUTPUT); + // } + // } + // + // public String serialize(Object value) throws JsonProcessingException { + // return mapper.writeValueAsString(value); + // } + // + // public Object deserialize(String value, Class type) throws JsonParseException, JsonMappingException, + // IOException { + // + // ObjectMapper mapper = new ObjectMapper(); + // if (type != null) { + // return mapper.readValue(value, type); + // } else { + // return mapper.readValue(value, Object.class); + // } + // + // } + // + // } } |