diff options
Diffstat (limited to 'id/server/idserverlib/src')
-rw-r--r-- | id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java | 99 |
1 files changed, 99 insertions, 0 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 new file mode 100644 index 000000000..36063ca2c --- /dev/null +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java @@ -0,0 +1,99 @@ +package at.gv.egovernment.moa.id.config.auth; + +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; + +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; +import org.codehaus.jackson.annotate.JsonMethod; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; + +import at.gv.egovernment.moa.id.config.ConfigurationException; +import at.gv.egovernment.moa.id.config.ConfigurationProvider; + +public class ConfigurationToJSONConverter { + + AuthConfigurationProvider config; + + public static void main(String[] args) { + + try { + ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); + + List<String> jsonResults = converter.convertConfigToJSONStrings(); + System.out.println(jsonResults); + + } catch (ConfigurationException e) { + e.printStackTrace(); + 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) { + // get JSON + // prettyprint and write to file + + } + + public void writeConfigToJSONDB() throws ConfigurationException { + ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); + List<String> jsonResults = converter.convertConfigToJSONStrings(); + // TODO: write to db + } + + public List<String> convertConfigToJSONStrings() { + + List<String> result = new ArrayList<String>(); + ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); + + int numberOfCalledGetterMethods = 0; + try { + System.out.println("=== Start ==="); + // find all getter methods + for (PropertyDescriptor pd : Introspector.getBeanInfo(AuthConfigurationProvider.class) + .getPropertyDescriptors()) { + if (pd.getReadMethod() != null && !"class".equals(pd.getName())) { + + try { + // get result of get method + Object tmp = pd.getReadMethod().invoke(config); + // result to JSON + String show = mapper.writeValueAsString(tmp); + System.out.println(show); + result.add(show); + numberOfCalledGetterMethods++; + } catch (JsonGenerationException | JsonMappingException e) { + e.printStackTrace(); + // System.out.println("Problems while writing JSON as String"); + // return new ArrayList<String>(); + } + } + } + + // TODO: handle static methods + + } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException 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 ArrayList<String>(); + } + + System.out.println("=== END called:'" + numberOfCalledGetterMethods + "' getter-methods ==="); + return result; + } + +} |