1 17 18 package org.pentaho.core.system; 19 20 import java.io.BufferedInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.dom4j.Document; 32 import org.dom4j.Node; 33 import org.pentaho.core.util.XmlHelper; 34 import org.pentaho.messages.Messages; 35 36 public class SystemSettings extends PentahoBase implements ISystemSettings { 37 40 private static final long serialVersionUID = 3727605230748352557L; 41 42 private static String LOG_NAME = Messages.getString("SYSTEMSETTINGS.CODE_LOG_NAME"); 44 private static final Log logger = LogFactory.getLog(SystemSettings.class); 45 46 private IApplicationContext applicationContext; 47 48 private Map settingsDocumentMap = new HashMap (); 49 50 String logId; 51 52 public SystemSettings(IApplicationContext applicationContext) { 53 this.applicationContext = applicationContext; 55 logId = LOG_NAME + ":"; Document doc = getSystemSettingsDocument(PENTAHOSETTINGSFILENAME); 57 if (doc == null) { 58 throw new IllegalArgumentException (Messages.getErrorString("SYSTEMSETTINGS.ERROR_0003_INVALID_OR_MISSING_FILE")); } 60 } 61 62 public String getSystemSetting(String path, String settingName, String defaultValue) { 63 debug(Messages.getString("SYSTEMSETTINGS.DEBUG_GET_SYSTEM_SETTING_PATH", File.separator + path)); Document doc = getSystemSettingsDocument(path); 65 if (doc == null) { 66 return null; 67 } 68 Node node = doc.selectSingleNode("//" + settingName); if (node == null) { 70 return defaultValue; 71 } 72 return node.getText(); 73 } 74 75 public String getSystemSetting(String settingName, String defaultValue) { 76 return getSystemSetting(PENTAHOSETTINGSFILENAME, settingName, defaultValue); 77 } 78 79 public List getSystemSettings(String path, String settingName) { 80 Document doc = getSystemSettingsDocument(path); 81 if (doc == null) { 82 return null; 83 } 84 return doc.selectNodes("//" + settingName); } 86 87 public List getSystemSettings(String settingName) { 88 return getSystemSettings(PENTAHOSETTINGSFILENAME, settingName); 89 } 90 91 public Document getSystemSettingsDocument(String actionPath) { 92 Document systemSettingsDocument = (Document) settingsDocumentMap.get(actionPath); 96 if (systemSettingsDocument == null) { 97 File f = getFile(actionPath); 98 if (f == null) { 99 return null; 100 } 101 systemSettingsDocument = XmlHelper.getDocFromFile(f); 102 settingsDocumentMap.put(actionPath, systemSettingsDocument); 103 } 104 return systemSettingsDocument; 105 } 106 107 private File getFile(String path) { 108 File f = new File (getAbsolutePath(path)); 109 110 if (!f.exists()) { 111 error(Messages.getErrorString("SYSTEMSETTINGS.ERROR_0002_FILE_NOT_IN_SOLUTION", f.getAbsolutePath())); return null; 113 } 114 debug(Messages.getString("SYSTEMSETTINGS.DEBUG_SYSTEM_SETTINGS_GET_FILE", f.getAbsolutePath())); 116 return f; 117 } 118 119 private String getAbsolutePath(String path) { 120 return applicationContext.getSolutionPath("system" + File.separator + path); } 122 123 public Log getLogger() { 124 return logger; 125 } 126 127 public void resetSettingsCache() { 128 settingsDocumentMap.clear(); 129 } 130 131 public Properties getSystemSettingsProperties(String path) { 132 String fullPath = PentahoSystem.getApplicationContext().getSolutionPath("system" + File.separator + path); File propsFile = new File (fullPath); 134 if (!propsFile.exists()) { 135 return null; 136 } 137 try { 138 Properties props = new Properties (); 139 InputStream iStream = new BufferedInputStream (new FileInputStream (propsFile)); 140 props.load(iStream); 141 return props; 142 143 } catch (Exception e) { 144 145 } 146 return null; 147 } 148 149 } 150
| Popular Tags
|