1 /** 2 * Copyright 2005 Steve Molloy 3 * 4 * This file is part of OV4J. 5 * 6 * OV4J is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 8 * 9 * OV4J is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License along with OV4J; if not, write to the Free Software 13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 14 * 15 */ 16 package org.ov4j; 17 18 import java.util.MissingResourceException; 19 import java.util.ResourceBundle; 20 import java.util.Timer; 21 import java.util.TimerTask; 22 import java.util.logging.Level; 23 import java.util.logging.Logger; 24 25 /** 26 * Configuration bundle, will load the first ov4j.properties file found in the classpath. 27 * 28 * @author smolloy 29 * 30 */ 31 public class Config { 32 /** 33 * Logger for this class 34 */ 35 private static final Logger logger = Logger.getLogger(Config.class.getName()); 36 37 /** Name of the bundle to load. */ 38 private static final String BUNDLE_NAME = "ov4j"; 39 40 /** The actual resource bundle. */ 41 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Config.BUNDLE_NAME); 42 43 public static final long MIN_MEMORY = Long.parseLong(System.getProperty("ov4j.memory.min", "0")); 44 45 private static boolean GC_RUNNING = false; 46 47 public static final Object GC_LOCK = new Object(); 48 49 public static boolean checkMemory() { 50 if (Config.logger.isLoggable(Level.FINEST)) { 51 Config.logger.entering("Config", "checkMemory()", "start"); 52 } 53 54 if (!Config.GC_RUNNING && !Config.enoughFree()) { 55 synchronized (Config.GC_LOCK) { 56 Config.GC_RUNNING = true; 57 } 58 final TimerTask task = new TimerTask() { 59 @Override 60 public void run() { 61 synchronized (Config.GC_LOCK) { 62 Config.GC_RUNNING = false; 63 } 64 } 65 }; 66 final Timer t = new Timer(); 67 t.schedule(task, 5000); 68 try { 69 Thread.sleep(500); 70 } catch (final InterruptedException e) { 71 if (Config.logger.isLoggable(Level.FINE)) { 72 Config.logger.logp(Level.FINE, "Config", "checkMemory()", "exception ignored", e); 73 } 74 } 75 } 76 final boolean returnboolean = Config.enoughFree(); 77 78 if (Config.logger.isLoggable(Level.FINEST)) { 79 Config.logger.exiting("Config", "checkMemory()", "end - return value=" + returnboolean); 80 } 81 return returnboolean; 82 } 83 84 public static boolean enoughFree() { 85 if (Config.logger.isLoggable(Level.FINEST)) { 86 Config.logger.entering("Config", "enoughFree()", "start"); 87 } 88 89 final boolean returnboolean = (Config.getAvailableMemory() >= Config.MIN_MEMORY); 90 91 if (Config.logger.isLoggable(Level.FINEST)) { 92 Config.logger.exiting("Config", "enoughFree()", "end - return value=" + returnboolean); 93 } 94 return returnboolean; 95 } 96 97 public static long getAvailableMemory() { 98 if (Config.logger.isLoggable(Level.FINEST)) { 99 Config.logger.entering("Config", "getAvailableMemory()", "start"); 100 } 101 102 final long stillAvailable = Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory(); 103 final long returnlong = (stillAvailable + Runtime.getRuntime().freeMemory()); 104 105 if (Config.logger.isLoggable(Level.FINEST)) { 106 Config.logger.exiting("Config", "getAvailableMemory()", "end - return value=" + returnlong); 107 } 108 return returnlong; 109 } 110 111 /** 112 * Retrieve a string from the resource bundle. 113 * 114 * @param key 115 * Key to search for. 116 * @return String value associated with the key. 117 */ 118 public static String getString(final String key) { 119 if (Config.logger.isLoggable(Level.FINEST)) { 120 Config.logger.entering("Config", "getString(String=" + key + ")", "start"); 121 } 122 123 try { 124 final String returnString = 125 (System.getProperty(key) == null) ? (Config.RESOURCE_BUNDLE.containsKey(key)) ? Config.RESOURCE_BUNDLE 126 .getString(key) : '!' + key + '!' : System.getProperty(key); 127 128 if (Config.logger.isLoggable(Level.FINEST)) { 129 Config.logger.exiting("Config", "getString(String=" + key + ")", "end - return value=" + returnString); 130 } 131 return returnString; 132 } catch (final MissingResourceException e) { 133 if (Config.logger.isLoggable(Level.FINE)) { 134 Config.logger.logp(Level.FINE, "Config", "getString(String=" + key + ")", "Exception caught", e); 135 } 136 137 final String returnString = '!' + key + '!'; 138 139 if (Config.logger.isLoggable(Level.FINEST)) { 140 Config.logger.exiting("Config", "getString(String=" + key + ")", "end - return value=" + returnString); 141 } 142 return returnString; 143 } 144 } 145 146 /** 147 * Private constructor to avoid instantiation. 148 * 149 */ 150 private Config() { 151 } 152 }