1 package com.dotmarketing.util; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.net.URL ; 8 import java.util.Calendar ; 9 import java.util.Date ; 10 import java.util.GregorianCalendar ; 11 import java.util.Iterator ; 12 import java.util.NoSuchElementException ; 13 14 import org.apache.commons.configuration.PropertiesConfiguration; 15 16 import com.dotmarketing.db.DbConnectionFactory; 17 18 public class Config { 19 20 public static javax.servlet.ServletContext CONTEXT = null; 22 public static String CONTEXT_PATH = null; 23 24 public static final int PERMISSION_READ = 1; 26 public static final int PERMISSION_WRITE = 2; 27 public static final int PERMISSION_PUBLISH = 4; 28 29 private static int refreshInterval = 1; private static Date lastRefreshTime = new Date (); 32 33 private static PropertiesConfiguration props = null; 34 private static ClassLoader classLoader = null; 35 private static URL url = null; 36 37 public static void initializeConfig () { 39 classLoader = Thread.currentThread().getContextClassLoader(); 40 _loadProperties(); 41 } 42 43 private static void _loadProperties () { 44 if (classLoader == null) { 45 classLoader = Thread.currentThread().getContextClassLoader(); 46 Logger.warn(Config.class, "Exception loading properties: classloader not found, the config was never initialized."); 47 } 48 if (url == null) 49 url = classLoader.getResource("dotmarketing-config.properties"); 50 51 if (url != null) { 52 File f = new File (url.getPath()); 53 Date lastModified = new Date (f.lastModified()); 54 if (lastModified.after(lastRefreshTime) || props == null) { 55 try { 56 Logger.info(Config.class, "Loading DOTMARKETING Properties..."); 57 InputStream propsInputStream = new FileInputStream (f); 58 Config.props = new PropertiesConfiguration (); 59 props.load(new InputStreamReader (propsInputStream)); 60 propsInputStream.close(); 61 try { 62 int interval = props.getInt("config.refreshinterval"); 63 refreshInterval = interval; 64 Logger.info(Config.class, "Assigned custom refresh interval: " + interval + " minutes."); 65 } catch (NoSuchElementException e) { 66 Logger.info(Config.class, "Assigned default refresh interval: " + refreshInterval + " minutes."); 67 } 68 Logger.info(Config.class, "DOTMARKETING Properties Loaded"); 69 } catch (Exception e) { 70 Logger.fatal(Config.class, "Exception loading properties", e); 71 props = null; 72 } 73 } 74 Config.lastRefreshTime = new Date (); 75 } else { 76 Logger.fatal(Config.class, "DOTMARKETING Properties file (dotmarketing-config.properties) not found."); 77 } 78 } 79 80 private static void _refreshProperties () { 81 Calendar cal = new GregorianCalendar (); 82 Calendar calNow = new GregorianCalendar (); 83 calNow.setTimeInMillis(System.currentTimeMillis()); 84 cal.setTime(lastRefreshTime); 85 cal.add(Calendar.MINUTE, refreshInterval); 86 if (cal.before(calNow) || props == null) { 87 _loadProperties(); 88 } 89 } 90 91 public static String getStringProperty (String name) { 93 _refreshProperties (); 94 String [] propsArr = props.getStringArray(name); 95 String property = new String (); 96 int i = 0; 97 for (String propItem : propsArr) { 98 if (i > 0) 99 property += ","; 100 property += propItem; 101 i++; 102 } 103 return property; 104 } 105 106 public static String [] getStringArrayProperty (String name) { 107 _refreshProperties (); 108 return props.getStringArray(name); 109 } 110 111 public static int getIntProperty (String name) { 112 _refreshProperties (); 113 return props.getInt(name); 114 } 115 116 public static int getIntProperty (String name, int defaultVal) { 117 _refreshProperties (); 118 return props.getInt(name, defaultVal); 119 } 120 121 public static float getFloatProperty (String name) { 122 _refreshProperties (); 123 return props.getFloat(name); 124 } 125 126 public static float getFloatProperty (String name, float defaultVal) { 127 _refreshProperties (); 128 return props.getFloat(name, defaultVal); 129 } 130 131 public static boolean getBooleanProperty (String name) { 132 _refreshProperties (); 133 return props.getBoolean(name); 134 } 135 136 public static boolean getBooleanProperty (String name, boolean defaultVal) { 137 _refreshProperties (); 138 return props.getBoolean(name, defaultVal); 139 } 140 141 public static Iterator getKeys () { 142 _refreshProperties (); 143 return props.getKeys(); 144 } 145 146 148 public static final java.util.List reservedFolderNames = new java.util.ArrayList (); 149 150 @SuppressWarnings ("unchecked") 151 public static void setReservedFolders() { 152 reservedFolderNames.add("WEB-INF"); 153 reservedFolderNames.add("META-INF"); 154 } 155 156 public static void setMyApp(javax.servlet.ServletContext myApp) { 158 CONTEXT = myApp; 159 CONTEXT_PATH = myApp.getRealPath("/"); 160 } 161 162 public static String getLimitOffsetQuery(int limit, int offset) { 163 String db = DbConnectionFactory.getDBType(); 164 165 if (db.equals("PostgreSQL")){ 166 return " limit " + limit + " offset " + offset; 167 } 168 else if (db.equals("MySQL")){ 169 return " limit " + offset + " ," + limit; 170 } 171 return ""; 172 } 173 174 175 176 } 177 | Popular Tags |