diff options
5 files changed, 357 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); + // } + // + // } + // + // } } diff --git a/id/server/moa-id-commons/pom.xml b/id/server/moa-id-commons/pom.xml index c16c68a5f..85bd82dd4 100644 --- a/id/server/moa-id-commons/pom.xml +++ b/id/server/moa-id-commons/pom.xml @@ -123,6 +123,7 @@ <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.java}</version> </dependency> + <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> @@ -133,6 +134,30 @@ <artifactId>spring-orm</artifactId> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + + <!-- for testing only ? --> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + <version>2.2.2</version> + </dependency> + + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>1.4.178</version> + </dependency> + + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.4</version> + </dependency> + </dependencies> <build> diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java new file mode 100644 index 000000000..da2c4b7e6 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java @@ -0,0 +1,162 @@ +package com.datentechnik.moa.id.conf.persistence; + +import java.io.IOException; +import java.util.List; + +import org.springframework.beans.factory.annotation.Required; +import org.springframework.stereotype.Component; + +import com.datentechnik.moa.id.conf.persistence.dal.ConfigProperty; +import com.datentechnik.moa.id.conf.persistence.dal.ConfigPropertyDao; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +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; + +/** + * + * + */ +@Component +public class ConfigurationImpl implements Configuration { + + ConfigPropertyDao configPropertyDao; + private JsonMapper mapper = new JsonMapper(); + + /** + * Sets the {@link ConfigPropertyDao}. + * @param configPropertyDao the ConfigPropertyDao + */ + @Required + public void setConfigPropertyDao(ConfigPropertyDao configPropertyDao) { + this.configPropertyDao = configPropertyDao; + } + + @Override + public Object get(String key) { + // return null if key does not exist + + try { + return mapper.deserialize(getPropertyValue(key), null); + } catch (Exception e) { + return null; + } + } + + @Override + public <T> T get(String key, Class<T> clazz) { + // return null if key does not exist + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + try { + return clazz.cast(mapper.deserialize(property.getValue(), clazz)); + } catch (IOException e) { + return null; + } + } else { + return null; + } + } + + @Override + public void set(String key, Object value) { + + ConfigProperty keyValue = new ConfigProperty(); + keyValue.setKey(key); + try { + keyValue.setValue(mapper.serialize(value)); + // System.out.println(">> key - value: " + keyValue.getKey() + " - " + keyValue.getValue() + "\n"); + configPropertyDao.saveProperty(keyValue); + + } catch (JsonProcessingException e) { + // TODO do proper error handling + e.printStackTrace(); + } + + } + + @Override + public <T> T get(String key, Class<T> clazz, Object defaultValue) { + // TODO complete the method + T value = get(key, clazz); + if (value != null) { + return value; + } else { + return clazz.cast(defaultValue); + } + } + + @Override + public <T> List<T> getList(String key, Class<T> clazz) { + // return empty list if key does not exist + // TODO Auto-generated method stub + + return null; + } + + private String getPropertyValue(String key) { + ConfigProperty property = configPropertyDao.getProperty(key); + return property.getValue(); + } + + /** + * + * + */ + private class JsonMapper { + + private ObjectMapper mapper = new ObjectMapper(); + + public JsonMapper() { + this(false); + } + + /** + * + * @param prettyPrint + */ + 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); + } + } + + /** + * + * @param value + * @return + * @throws JsonProcessingException + */ + public String serialize(Object value) throws JsonProcessingException { + return mapper.writeValueAsString(value); + } + + /** + * + * @param value + * @param clazz + * @return + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public <T> Object deserialize(String value, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { + + ObjectMapper mapper = new ObjectMapper(); + if (clazz != null) { + return mapper.readValue(value, clazz); + } else { + return mapper.readValue(value, Object.class); + } + + } + + }; + +} diff --git a/id/server/moa-id-commons/src/main/resources/META-INF/persistence.xml b/id/server/moa-id-commons/src/main/resources/META-INF/persistence.xml index e77d6c49e..640c1504c 100644 --- a/id/server/moa-id-commons/src/main/resources/META-INF/persistence.xml +++ b/id/server/moa-id-commons/src/main/resources/META-INF/persistence.xml @@ -7,6 +7,11 @@ http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" <persistence-unit name="at.gv.egovernment.moa.id.commons.db.dao.config" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> + <class>com.datentechnik.moa.id.conf.persistence.dal.SOME_CLASS</class> + </persistence-unit> + + <persistence-unit name="moaidconf" transaction-type="RESOURCE_LOCAL"> + <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.datentechnik.moa.id.conf.persistence.dal.ConfigProperty</class> <properties> </properties> diff --git a/id/server/moa-id-commons/src/main/resources/configuration.beans.xml b/id/server/moa-id-commons/src/main/resources/configuration.beans.xml new file mode 100644 index 000000000..762c55507 --- /dev/null +++ b/id/server/moa-id-commons/src/main/resources/configuration.beans.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:tx="http://www.springframework.org/schema/tx" + xmlns:aop="http://www.springframework.org/schema/aop" + xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> + + <tx:annotation-driven /> + <context:annotation-config /> + + <context:property-placeholder location="${location}"/> + + + <bean id="configPropertyDao" + class="com.datentechnik.moa.id.conf.persistence.dal.ConfigPropertyDaoImpl" /> + <bean id="config" class="com.datentechnik.moa.id.conf.persistence.ConfigurationImpl"> + <property name="configPropertyDao" ref="configPropertyDao" /> + </bean> + + + <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true" destroy-method="close"> + <aop:scoped-proxy/> + <property name="driverClassName" value="${dataSource.driverClassName}" /> + <property name="url" value="${dataSource.url}"/> + <property name="username" value="${dataSource.username}" /> + <property name="password" value="${dataSource.password}" /> + <property name="testOnBorrow" value="true" /> + <property name="validationQuery" value="SELECT 1" /> + </bean> + + + <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> + <property name="showSql" value="true" /> + <property name="generateDdl" value="${jpaVendorAdapter.generateDdl}" /> + <property name="databasePlatform" value="${jpaVendorAdapter.databasePlatform}" /> + </bean> + + + <bean name="moaidconf" id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> + <property name="dataSource" ref="dataSource" /> + <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> + <property name="persistenceUnitName" value="moaidconf" /> + </bean> + + <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + +</beans>
\ No newline at end of file |