[7] | 1 | package org.etsi.common.configuration; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileNotFoundException; |
---|
| 5 | import java.io.FileOutputStream; |
---|
| 6 | import java.io.IOException; |
---|
| 7 | import java.io.OutputStreamWriter; |
---|
| 8 | import java.util.ArrayList; |
---|
| 9 | |
---|
| 10 | import org.etsi.common.MiscTools; |
---|
| 11 | import org.etsi.common.exceptions.TerminationException; |
---|
| 12 | |
---|
| 13 | import com.thoughtworks.xstream.XStream; |
---|
| 14 | import com.thoughtworks.xstream.converters.ConversionException; |
---|
| 15 | |
---|
| 16 | public class ConfigTools { |
---|
| 17 | |
---|
| 18 | private ToolConfiguration config = null; |
---|
| 19 | private String configurationClassName; |
---|
| 20 | private String configurationProfileClassName; |
---|
| 21 | private String toolVersion; |
---|
| 22 | |
---|
| 23 | public ConfigTools(String configurationClassName, String configurationProfileClassName) { |
---|
| 24 | this.configurationClassName = configurationClassName; |
---|
| 25 | this.configurationProfileClassName = configurationProfileClassName; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | public void loadConfig(String configurationFilename) throws TerminationException, InstantiationException, IllegalAccessException, ClassNotFoundException { |
---|
| 30 | System.out.println("Loading configuration ..."); |
---|
| 31 | File configFile = new File(configurationFilename); |
---|
| 32 | if (configFile.exists()) { |
---|
| 33 | config = readConfig(configurationFilename); |
---|
| 34 | if (config == null) { |
---|
| 35 | System.out.println("ERROR: XML configuration could not be loaded! " |
---|
| 36 | + "\n Please check the configuration for consistency or use an auto-generated configuration file to migrate any custom settings!"); |
---|
| 37 | throw new TerminationException(""); |
---|
| 38 | } |
---|
| 39 | } else { |
---|
| 40 | System.out.println("ERROR: Configuration file \""+configurationFilename+"\" does not exist! Use the appropriate option to generate a new default configuration."); |
---|
| 41 | throw new TerminationException(""); |
---|
| 42 | // config = initializeNewDefaultConfig(configurationFilename); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public ToolConfiguration initializeNewDefaultConfig( |
---|
| 47 | String configurationFilename) throws TerminationException, InstantiationException, IllegalAccessException, ClassNotFoundException { |
---|
| 48 | System.out.println(" Generating new default configuration in: \"" |
---|
| 49 | + configurationFilename + "\" ..."); |
---|
| 50 | ToolConfiguration config = null; |
---|
| 51 | ConfigurationProfile configurationProfile = null; |
---|
| 52 | config = (ToolConfiguration) Class.forName(configurationClassName).newInstance(); |
---|
| 53 | configurationProfile = (ConfigurationProfile) Class.forName(configurationProfileClassName).newInstance(); |
---|
| 54 | configurationProfile.setProfileName("defaultProfile"); |
---|
| 55 | configurationProfile.setProfileVersion(getToolVersion()); |
---|
| 56 | config.setDefaultConfigurationProfile("all"); |
---|
| 57 | if (config.getConfigurationProfiles() == null) { |
---|
| 58 | ArrayList<ConfigurationProfile> configurationProfiles = new ArrayList<ConfigurationProfile>(); |
---|
| 59 | config.setConfigurationProfiles(configurationProfiles); |
---|
| 60 | } |
---|
| 61 | config.getConfigurationProfiles().add(configurationProfile); |
---|
| 62 | writeConfig(config, configurationFilename); |
---|
| 63 | return config; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // ------------------------------------------------------------------------- |
---|
| 67 | |
---|
| 68 | /* //TO BE DEPRECATED |
---|
| 69 | //TODO: Reorganize and reuse |
---|
| 70 | public static String findConfig(String filename) { |
---|
| 71 | File configPath = new File(getConfigurationPath()); |
---|
| 72 | if (configPath.exists()) { |
---|
| 73 | File configFile = null; |
---|
| 74 | if (filename.length() > 0) { |
---|
| 75 | configFile = new File(getConfigurationPath() + filename); |
---|
| 76 | } else { |
---|
| 77 | configFile = new File(getDefaultConfigurationFilePath()); |
---|
| 78 | } |
---|
| 79 | if (configFile.exists()) |
---|
| 80 | return configFile.getAbsolutePath(); |
---|
| 81 | else |
---|
| 82 | return null; |
---|
| 83 | } else { |
---|
| 84 | configPath.mkdir(); |
---|
| 85 | return null; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | //TODO: TO BE DEPRECATED |
---|
| 89 | public static String findConfig() { |
---|
| 90 | return findConfig(""); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | //TODO: TO BE DEPRECATED |
---|
| 95 | //TODO: FIX THESE HARDCODED PARTS |
---|
| 96 | public static String getConfigurationPath() { |
---|
| 97 | if (System.getProperty("os.name").toLowerCase().contains("windows")) { |
---|
| 98 | String appdata = System.getenv("appdata"); |
---|
| 99 | return appdata + "\\T3Q\\"; |
---|
| 100 | } else { // assume unix |
---|
| 101 | String home = System.getProperty("user.home"); |
---|
| 102 | return home + "/.t3q/"; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | //TODO: TO BE DEPRECATED |
---|
| 108 | public static String getDefaultConfigurationFilePath() { |
---|
| 109 | return getConfigurationPath() + "t3q.xml"; |
---|
| 110 | } |
---|
| 111 | */ |
---|
| 112 | // ------------------------------------------------------------------------- |
---|
| 113 | |
---|
| 114 | private ToolConfiguration readConfig(String filename) throws TerminationException { |
---|
| 115 | System.out.println(" Reading XML configuration from: " + filename); |
---|
| 116 | String str = MiscTools.readFile(filename); |
---|
| 117 | XStream xstream = new XStream(); |
---|
| 118 | initXstreamAliases(xstream); |
---|
| 119 | ToolConfiguration config = null; |
---|
| 120 | try{ |
---|
| 121 | config = (ToolConfiguration)xstream.fromXML(str); |
---|
| 122 | } catch (ConversionException ce){ |
---|
| 123 | String line = ""; |
---|
| 124 | line = ce.getMessage().split("line\\snumber[\\t\\s]+?:\\s")[1]; |
---|
| 125 | line = line.substring(0, line.indexOf("\n")); |
---|
| 126 | System.out.println(" Problem Occured while loading configuration: line: "+line); |
---|
| 127 | //TODO: add further debugging details? |
---|
| 128 | } |
---|
| 129 | return config; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | private void initXstreamAliases(XStream xstream) throws TerminationException { |
---|
| 133 | try { |
---|
| 134 | xstream.alias(Class.forName(configurationClassName).getSimpleName(), Class.forName(configurationClassName)); |
---|
| 135 | xstream.alias(Class.forName(configurationProfileClassName).getSimpleName(), Class.forName(configurationProfileClassName)); |
---|
| 136 | } catch (ClassNotFoundException e) { |
---|
| 137 | System.out.println(" Problem occurred while initiating XStream. Class not found: "+e.getMessage()); |
---|
| 138 | throw new TerminationException("Class not found: "+e.getMessage()); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | private void writeConfig(ToolConfiguration config, String filename) throws TerminationException { |
---|
| 143 | System.out.println(" Writing XML configuration to: "+filename); |
---|
| 144 | XStream xstream = new XStream(); |
---|
| 145 | initXstreamAliases(xstream); |
---|
| 146 | String xml = xstream.toXML(config); |
---|
| 147 | OutputStreamWriter oos; |
---|
| 148 | //write out an empty file |
---|
| 149 | MiscTools.writeFile(filename, ""); |
---|
| 150 | try { |
---|
| 151 | oos = new OutputStreamWriter (new FileOutputStream(filename)); |
---|
| 152 | oos.write(xml); |
---|
| 153 | oos.close(); |
---|
| 154 | } catch (FileNotFoundException e) { |
---|
| 155 | System.out.println(" ERROR: File could not be found! (\""+filename+"\")"); |
---|
| 156 | throw new TerminationException(e.getMessage()); |
---|
| 157 | } catch (IOException e) { |
---|
| 158 | System.out.println(" IO ERROR: Could not write to file! (\""+filename+"\")"); |
---|
| 159 | throw new TerminationException(e.getMessage()); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | public ConfigurationProfile selectProfile(String specifiedProfileName) throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
---|
| 164 | // add all default implicit profile |
---|
| 165 | ConfigurationProfile selectedProfile = null; |
---|
| 166 | ConfigurationProfile allEnabledProfile = (ConfigurationProfile) Class.forName(configurationProfileClassName).newInstance(); |
---|
| 167 | allEnabledProfile.setProfileName("all"); |
---|
| 168 | allEnabledProfile.setProfileVersion(getToolVersion()); |
---|
| 169 | config.getConfigurationProfiles().add(allEnabledProfile); |
---|
| 170 | if (specifiedProfileName != null){ |
---|
| 171 | System.out.println(" Selecting profile \"" + specifiedProfileName |
---|
| 172 | + "\"..."); |
---|
| 173 | selectedProfile = activateProfile(specifiedProfileName); |
---|
| 174 | } |
---|
| 175 | if (selectedProfile == null) { |
---|
| 176 | if (specifiedProfileName != null) { |
---|
| 177 | System.out.println(" Profile \"" |
---|
| 178 | + specifiedProfileName |
---|
| 179 | + "\" not found in configuration. Trying default profile \"" |
---|
| 180 | + config.getDefaultConfigurationProfile() + "\"."); |
---|
| 181 | } else { |
---|
| 182 | System.out.println(" No Profile specified. Trying default profile \"" |
---|
| 183 | + config.getDefaultConfigurationProfile() + "\"."); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | specifiedProfileName = config.getDefaultConfigurationProfile(); |
---|
| 187 | selectedProfile = activateProfile(specifiedProfileName); |
---|
| 188 | |
---|
| 189 | } |
---|
| 190 | if (selectedProfile == null) { |
---|
| 191 | System.out.println(" Profile \"" |
---|
| 192 | + specifiedProfileName |
---|
| 193 | + "\" not found in configuration. Auto-selecting \"all\" profile."); |
---|
| 194 | selectedProfile = allEnabledProfile; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | return selectedProfile; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // -------------------------------------------------------------------------- |
---|
| 201 | //TODO: Extract configuration management and possibly abstract it for reusability |
---|
| 202 | private ConfigurationProfile activateProfile(String specifiedProfile) { |
---|
| 203 | ConfigurationProfile activatedProfile = null; |
---|
| 204 | |
---|
| 205 | for (int i = 0; i < config.getConfigurationProfiles().size(); i++) { |
---|
| 206 | ConfigurationProfile configurationProfile = (ConfigurationProfile) config |
---|
| 207 | .getConfigurationProfiles().get(i); |
---|
| 208 | if (configurationProfile.getProfileName().toLowerCase().equals( |
---|
| 209 | specifiedProfile.toLowerCase())) { |
---|
| 210 | System.out.println(" Activating profile \"" + specifiedProfile |
---|
| 211 | + "\"..."); |
---|
| 212 | activatedProfile = configurationProfile; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | return activatedProfile; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | public void setToolVersion(String toolVersion) { |
---|
| 220 | this.toolVersion = toolVersion; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | public String getToolVersion() { |
---|
| 225 | return toolVersion; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | } |
---|