1 18 package sync4j.server.config; 19 20 import java.io.IOException ; 21 import java.io.File ; 22 23 import java.net.NetworkInterface ; 24 import java.util.Enumeration ; 25 import java.net.InetAddress ; 26 import java.net.*; 27 28 import java.util.Iterator ; 29 import java.util.logging.ConsoleHandler ; 30 import java.util.logging.FileHandler ; 31 import java.util.logging.Handler ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import java.util.logging.LogManager ; 35 36 import sync4j.framework.config.*; 37 import sync4j.framework.engine.SyncStrategy; 38 import sync4j.framework.engine.pipeline.PipelineManager; 39 import sync4j.framework.security.Officer; 40 import sync4j.framework.server.session.SessionHandler; 41 import sync4j.framework.server.store.PersistentStore; 42 import sync4j.framework.tools.beans.BeanFactory; 43 import sync4j.framework.tools.beans.BeanException; 44 45 import sync4j.server.admin.UserManager; 46 47 64 public class Configuration 65 implements java.io.Serializable , ConfigurationConstants { 66 67 69 72 private ServerConfiguration serverConfig; 73 74 private static Configuration singleton; 75 private transient ClassLoader classLoader = null; 76 77 private static transient Logger log = Logger.getAnonymousLogger(); 83 84 86 90 public ClassLoader getClassLoader() { 91 return classLoader; 92 } 93 94 98 public void setClassLoader(ClassLoader classLoader) { 99 this.classLoader = classLoader; 100 } 101 102 104 107 protected Configuration() { 108 } 109 110 116 public static synchronized Configuration getConfiguration() { 117 if (singleton == null) { 118 singleton = new Configuration(); 119 try { 120 URL configPath = ConfigTools.fixURL(getConfigPath()); 121 ConfigClassLoader cl = new ConfigClassLoader( 122 new URL[] { configPath }, 123 Configuration.class.getClassLoader() 124 125 ); 126 singleton.serverConfig = 127 (ServerConfiguration)ConfigTools.getBeanInstance(cl, BEAN_SERVER_CONFIGURATION); 128 129 singleton.setClassLoader(cl); 130 131 singleton.setLoggingConfiguration(); 132 133 singleton.setServerURI(); 134 135 } catch (Exception e) { 136 if (log.isLoggable(Level.SEVERE)) { 137 log.severe("Fatal error creating the configuration object: " + e.getMessage()); 138 } 139 log.throwing(Configuration.class.getName(), "getConfiguration", e); 140 141 singleton = new Configuration(); 145 } 146 } 147 return singleton; 148 } 149 150 152 156 public ServerConfiguration getServerConfig() { 157 return serverConfig; 158 } 159 160 163 public void loadServerConfig() { 164 singleton = null; 171 getConfiguration(); 172 } 173 174 180 public SyncStrategy getStrategy() { 181 try { 182 return (SyncStrategy)ConfigTools.getBeanInstance( 183 classLoader, 184 serverConfig.getEngineConfiguration().getStrategy() 185 ); 186 } catch (Exception e) { 187 if (log.isLoggable(Level.SEVERE)) { 188 log.severe("Error creating the SyncStrategy object: " + e.getMessage()); 189 } 190 log.throwing(getClass().getName(), "getStartegy", e); 191 } 192 193 return null; 194 } 195 196 202 public PersistentStore getStore() { 203 try { 204 return (PersistentStore)ConfigTools.getBeanInstance( 205 classLoader, 206 serverConfig.getEngineConfiguration().getStore() 207 ); 208 } catch (Exception e) { 209 if (log.isLoggable(Level.SEVERE)) { 210 log.severe("Error creating the PersistentStore object: " + e.getMessage()); 211 } 212 log.throwing(getClass().getName(), "getStore", e); 213 } 214 215 return null; 216 } 217 218 224 public Officer getOfficer() { 225 try { 226 return (Officer)ConfigTools.getBeanInstance( 227 classLoader, 228 serverConfig.getEngineConfiguration().getOfficer() 229 ); 230 } catch (Exception e) { 231 if (log.isLoggable(Level.SEVERE)) { 232 log.severe("Error creating the SecurityOfficer object: " + e.getMessage()); 233 } 234 log.throwing(getClass().getName(), "getOfficer", e); 235 } 236 237 return null; 238 } 239 240 241 247 public SessionHandler getSessionHandler() { 248 try { 249 return (SessionHandler)ConfigTools.getBeanInstance( 250 classLoader, 251 serverConfig.getEngineConfiguration().getSessionHandler() 252 ); 253 } catch (Exception e) { 254 if (log.isLoggable(Level.SEVERE)) { 255 log.severe("Error creating the SessionHandler object: " + e.getMessage()); 256 } 257 log.throwing(getClass().getName(), "getSessionHandler", e); 258 } 259 260 return null; 261 } 262 263 269 public PipelineManager getPipelineManager() { 270 try { 271 return (PipelineManager)ConfigTools.getBeanInstance( 272 classLoader, 273 serverConfig.getEngineConfiguration().getPipelineManager() 274 ); 275 } catch (Exception e) { 276 if (log.isLoggable(Level.SEVERE)) { 277 log.severe("Error creating the PipelineManager object: " + e.getMessage()); 278 } 279 log.throwing(getClass().getName(), "getPipelineManager", e); 280 } 281 282 return null; 283 } 284 285 291 public UserManager getUserManager() { 292 try { 293 return (UserManager)ConfigTools.getBeanInstance( 294 classLoader, 295 serverConfig.getEngineConfiguration().getUserManager() 296 ); 297 } catch (Exception e) { 298 if (log.isLoggable(Level.SEVERE)) { 299 log.severe("Error creating the UserManager object: " + e.getMessage()); 300 } 301 log.throwing(getClass().getName(), "getUserManager", e); 302 } 303 304 return null; 305 } 306 307 313 public LoggingConfiguration getLoggingConfiguration() { 314 try { 315 return (LoggingConfiguration)ConfigTools.getBeanInstance( 316 classLoader, 317 serverConfig.getEngineConfiguration().getLoggingConfiguration() 318 ); 319 } catch (Exception e) { 320 if (log.isLoggable(Level.SEVERE)) { 321 log.severe("Error creating the LoggingConfiguration object: " + e.getMessage()); 322 } 323 log.throwing(getClass().getName(), "getLoggingConfiguration", e); 324 } 325 return null; 326 } 327 328 335 public void setLoggingConfiguration() throws IOException { 336 337 LogManager lm = LogManager.getLogManager(); 341 lm.reset(); 342 lm.readConfiguration(); 343 344 Logger l; 348 LoggerConfiguration conf; 349 LoggingConfiguration lc = getLoggingConfiguration(); 350 351 Handler h; 352 Iterator i = lc.getLoggers().iterator(); 353 while(i.hasNext()) { 354 355 conf = (LoggerConfiguration)i.next(); 356 357 if (conf.isInherit()) { 362 continue; 363 } 364 365 l = Logger.getLogger(conf.getName()); 366 367 Handler [] handlers = l.getHandlers(); 371 for (int j=0; ((handlers != null) && (j<handlers.length)); ++j) { 372 handlers[j].flush(); 373 handlers[j].close(); 374 l.removeHandler(handlers[j]); 375 } 376 l.setUseParentHandlers(false); 377 378 if (conf.isConsoleOutput()) { 384 h = new ConsoleHandler (); 385 h.setFormatter(new sync4j.framework.logging.SimpleFormatter()); 386 h.setLevel(Level.ALL); 387 l.addHandler(h); 388 } 389 390 if (conf.isFileOutput()) { 391 String pattern = conf.getPattern(); 392 393 if (new File (pattern).isAbsolute() == false) { 394 pattern = getSync4jHome() + File.separator + pattern; 395 } 396 397 if ((pattern == null) || (pattern.trim().length() == 0)) { 398 pattern = "logs/sync4j%t.log"; 399 } 400 h = new FileHandler ( 401 pattern , 402 conf.getLimit()*1048576, 403 conf.getCount(), 404 conf.isAppend() 405 ); 406 h.setFormatter(new sync4j.framework.logging.SimpleFormatter()); 407 h.setLevel(Level.ALL); 408 l.addHandler(h); 409 } 410 411 if (LoggerConfiguration.LEVEL_OFF.equalsIgnoreCase(conf.getLevel())) { 412 l.setLevel(Level.OFF); 413 } else if (LoggerConfiguration.LEVEL_SEVERE.equalsIgnoreCase(conf.getLevel())) { 414 l.setLevel(Level.SEVERE); 415 } else if (LoggerConfiguration.LEVEL_INFO.equalsIgnoreCase(conf.getLevel())) { 416 l.setLevel(Level.INFO); 417 } else if (LoggerConfiguration.LEVEL_ALL.equalsIgnoreCase(conf.getLevel())) { 418 l.setLevel(Level.ALL); 419 } 420 421 } 422 } 423 424 441 public Object getBeanInstanceByName(String beanName) 442 throws BeanException { 443 return BeanFactory.getBeanInstance(classLoader, beanName); 444 } 445 446 456 public void setBeanInstance(String beanName, Object o) 457 throws BeanException { 458 BeanFactory.saveBeanInstance(o, new File (getConfigPath(), beanName)); 459 } 460 461 471 public static String getSync4jHome() { 472 return System.getProperty(PROPERTY_SYSTEM_SYNC4J_HOME, "."); 473 } 474 475 480 public static String getConfigPath() { 481 return getSync4jHome() + CONFIG_PATH; 482 } 483 484 public String toString() { 485 StringBuffer sb = new StringBuffer (); 486 487 sb.append(getClass().getName()).append('\n') 488 .append("serverInfo: ").append(serverConfig.getServerInfo()).append('\n') 489 .append("engineConfiguration: ").append(serverConfig.getEngineConfiguration()).append('\n') 490 .append("classLoader: " + classLoader).append('\n'); 491 492 return sb.toString(); 493 } 494 495 502 private void setServerURI() { 503 String serverURI = null; 504 try { 505 serverURI = 506 singleton.getServerConfig().getEngineConfiguration().getServerURI(); 507 if (log.isLoggable(Level.FINEST)) { 508 log.finest("Server URI set: " + serverURI); 509 } 510 511 if (serverURI == null || serverURI.equals("")) { 512 513 if (log.isLoggable(Level.WARNING)) { 514 log.warning("serverURI property not provided; looking for local ip address."); 515 } 516 517 serverURI = "http://" 518 + getServerAddress() 519 + ":8080/sync4j/sync" 520 ; 521 522 singleton.getServerConfig().getEngineConfiguration().setServerURI(serverURI); 523 } 524 } catch (Exception e) { 525 String msg = "Error in setting default server URI"; 526 log.severe(msg + ": " + e.getMessage()); 527 log.throwing(Configuration.class.getName(), "setServerURI", e); 528 throw new ConfigurationException(msg); 529 } 530 531 if (log.isLoggable(Level.INFO)) { 532 log.info("ServerURI: " + serverURI); 533 } 534 } 535 536 542 private String getServerAddress() { 543 NetworkInterface iface = null; 544 InetAddress ia = null; 545 String address = null; 546 547 try { 548 for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); 549 ifaces.hasMoreElements(); ) { 550 iface = (NetworkInterface )ifaces.nextElement(); 551 for (Enumeration ips = iface.getInetAddresses(); ips.hasMoreElements(); ) { 552 ia = (InetAddress )ips.nextElement(); 553 if (!ia.isLoopbackAddress() && (ia.getAddress().length == 4)) { 554 address = ia.getHostAddress(); 555 556 if (log.isLoggable(Level.WARNING)) { 557 log.warning("Local address <" + address + "> found on interface <" + 558 iface + ">"); 559 } 560 return address; 561 } 562 563 } 564 } 565 } catch (SocketException ex) { 566 if (log.isLoggable(Level.SEVERE)) { 567 log.severe(ex.getMessage()); 568 } 569 return null; 570 } 571 572 if (address == null) { 573 if (log.isLoggable(Level.WARNING)) { 574 log.warning("Non loopback address found, using local host name. " + 575 "If you want to access the server from an external network, please check your network " + 576 "configuration and/or provide a valid serverURI " + 577 "property in Sync4j.xml. For local access, ignore this message."); 578 } 579 580 try { 581 address = java.net.InetAddress.getLocalHost().getHostName(); 582 } catch (UnknownHostException ex) { 583 if (log.isLoggable(Level.SEVERE)) { 584 log.severe(ex.getMessage()); 585 } 586 return null; 587 } 588 } 589 return address; 590 } 591 } 592 | Popular Tags |