GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — javaxt-express/src/javaxt/express/Config.java at master · javaxt-project/javaxt-express Expand file tree/ Copy pathConfig.javaMore file actions 319 lines (275 loc) · 11.1 KB / Copy pathConfig.javaFile metadata and controls319 lines (275 loc) · 11.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 package javaxt.express; import javaxt.json.*; import java.util.*; //****************************************************************************** //** Config Class //****************************************************************************** /** * Provides thread-safe methods used to get and set application variables. * ******************************************************************************/ public class Config { private List<JSONObject> config; //************************************************************************** //** Constructor //************************************************************************** public Config(){ config = new LinkedList<>(); config.add(new JSONObject()); } //************************************************************************** //** init //************************************************************************** /** Used to initialize the config with a given JSON document. This will * replace any previously assigned config values. */ public void init(JSONObject json){ synchronized(config){ config.set(0, json); config.notify(); } } //************************************************************************** //** get //************************************************************************** /** Returns the value for a given key. */ public JSONValue get(String key){ synchronized(config){ return config.get(0).get(key); } } //************************************************************************** //** get //************************************************************************** /** Returns a nested value associated with the given keys */ public JSONValue get(String... path){ synchronized(config){ return config.get(0).get(path); } } //************************************************************************** //** set //************************************************************************** /** Used to set the value for a given key. */ public void set(String key, Object value){ synchronized(config){ config.get(0).set(key, value); config.notify(); } } //************************************************************************** //** has //************************************************************************** /** Returns true if the config has a given key. */ public boolean has(String key){ synchronized(config){ return config.get(0).has(key); } } //************************************************************************** //** getKeys //************************************************************************** /** Returns a list of keys found in the config. */ public ArrayList<String> getKeys(){ ArrayList<String> keys = new ArrayList<>(); synchronized(config){ for (String key : config.get(0).keySet()){ keys.add(key); } } return keys; } //************************************************************************** //** isEmpty //************************************************************************** /** Returns true if there are no entries in the config. */ public boolean isEmpty(){ return getKeys().isEmpty(); } //************************************************************************** //** getDatabase //************************************************************************** /** Returns a Database assigned to a "database" key. */ public javaxt.sql.Database getDatabase(){ JSONValue val = get("database"); if (val==null) return null; if (val.toObject() instanceof javaxt.sql.Database){ return (javaxt.sql.Database) val.toObject(); } else{ javaxt.sql.Database database = getDatabase(val); if (database!=null) setDatabase(database); return database; } } //************************************************************************** //** getDatabase //************************************************************************** /** Used to parse database connection information found in a given JSONValue * and returns a Database object. */ public static javaxt.sql.Database getDatabase(JSONValue val){ return getDatabase(val.toJSONObject()); } //************************************************************************** //** getDatabase //************************************************************************** /** Used to parse database connection information found in a given JSONObject * and returns a Database object. Example: <pre> { "driver": "PostgreSQL", "host": "localhost:5432", "name": "test", "username": "postgres", "password": "xxxxxxxx", "maxConnections": 35 } </pre> */ public static javaxt.sql.Database getDatabase(JSONObject json){ if (json==null) return null; javaxt.sql.Database database = new javaxt.sql.Database(); database.setDriver(json.get("driver").toString()); database.setHost(json.get("host").toString()); database.setName(json.get("name").toString()); database.setUserName(json.get("username").toString()); database.setPassword(json.get("password").toString()); if (json.has("maxConnections")){ database.setConnectionPoolSize(json.get("maxConnections").toInteger()); } return database; } //************************************************************************** //** setDatabase //************************************************************************** /** Used to assign a Database to a "database" key. */ public void setDatabase(javaxt.sql.Database database){ set("database", database); } //************************************************************************** //** getEmail //************************************************************************** /** Returns an instance of an EmailService assigned to a "email" key. */ public javaxt.express.email.EmailService getEmail(){ JSONValue val = get("email"); if (val==null) return null; if (val.toObject() instanceof javaxt.express.email.EmailService){ return (javaxt.express.email.EmailService) val.toObject(); } else{ javaxt.express.email.EmailService email = getEmail(val); if (email!=null) setEmail(email); return email; } } //************************************************************************** //** getEmail //************************************************************************** /** Used to parse email connection information found in a given JSONValue * and returns an instance of an EmailService. */ public static javaxt.express.email.EmailService getEmail(JSONValue emailConfig){ return getEmail(emailConfig.toJSONObject()); } //************************************************************************** //** setEmail //************************************************************************** /** Used to parse email connection information found in a given JSONObject * and returns an instance of an EmailService. Example: <pre> { "host": "secure.emailsrvr.com", "port": 465, "username": "[email protected]", "password": "xxxxxxxx", } </pre> */ public static javaxt.express.email.EmailService getEmail(JSONObject emailConfig){ if (emailConfig==null) return null; javaxt.express.email.EmailService emailService = new javaxt.express.email.EmailService( emailConfig.get("host").toString(), emailConfig.get("port").toInteger(), emailConfig.get("username").toString(), emailConfig.get("password").toString() ); //Enable TLS as needed Boolean tls = emailConfig.get("tls").toBoolean(); if (tls!=null){ if (tls) emailService.enableTLS(); } return emailService; } //************************************************************************** //** setEmail //************************************************************************** /** Used to assign an EmailService to an "email" key. */ public void setEmail(javaxt.express.email.EmailService email){ set("email", email); } //************************************************************************** //** toJson //************************************************************************** /** Returns the current config in JSON notation. */ public JSONObject toJson(){ JSONObject json = new JSONObject(); synchronized(config){ JSONObject currConfig = config.get(0); for (String key : currConfig.keySet()){ JSONValue val = currConfig.get(key); Object obj = val.toObject(); if (obj instanceof javaxt.sql.Database){ javaxt.sql.Database database = (javaxt.sql.Database) obj; JSONObject db = new JSONObject(); String host = database.getHost(); Integer port = database.getPort(); if (port!=null && port>0) host += ":" + port; db.set("driver", database.getDriver().getVendor()); db.set("host", host); db.set("name", database.getName()); db.set("username", database.getUserName()); db.set("password", database.getPassword()); db.set("maxConnections", database.getConnectionPoolSize()); json.set(key, db); } else if (obj instanceof javaxt.express.email.EmailService){ javaxt.express.email.EmailService email = (javaxt.express.email.EmailService) obj; JSONObject emailConfig = new JSONObject(); emailConfig.set("host", email.getHost()); emailConfig.set("port", email.getPort()); emailConfig.set("username", email.getUserName()); emailConfig.set("password", email.getPassword()); Properties properties = email.getProperties(); for (Object p : properties.keySet()){ String prop = p.toString(); if (prop.equals("mail.smtp.starttls.enable") || prop.equals("mail.smtp.starttls.required")){ String v = properties.get(prop).toString(); if (v.equalsIgnoreCase("true")){ emailConfig.set("tls", true); break; } } } json.set(key, emailConfig); } else{ json.set(key, obj); } } } return json; } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |