KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > system > SystemSettings


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 19, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.core.system;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
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     /**
38      *
39      */

40     private static final long serialVersionUID = 3727605230748352557L;
41
42     private static String JavaDoc LOG_NAME = Messages.getString("SYSTEMSETTINGS.CODE_LOG_NAME"); //$NON-NLS-1$
43

44     private static final Log logger = LogFactory.getLog(SystemSettings.class);
45
46     private IApplicationContext applicationContext;
47
48     private Map JavaDoc settingsDocumentMap = new HashMap JavaDoc();
49
50     String JavaDoc logId;
51
52     public SystemSettings(IApplicationContext applicationContext) {
53       // TODO apply session-base security to the system settings
54
this.applicationContext = applicationContext;
55       logId = LOG_NAME + ":"; //$NON-NLS-1$
56
Document doc = getSystemSettingsDocument(PENTAHOSETTINGSFILENAME);
57       if (doc == null) {
58         throw new IllegalArgumentException JavaDoc(Messages.getErrorString("SYSTEMSETTINGS.ERROR_0003_INVALID_OR_MISSING_FILE")); //$NON-NLS-1$
59
}
60     }
61
62     public String JavaDoc getSystemSetting(String JavaDoc path, String JavaDoc settingName, String JavaDoc defaultValue) {
63         debug(Messages.getString("SYSTEMSETTINGS.DEBUG_GET_SYSTEM_SETTING_PATH", File.separator + path)); //$NON-NLS-1$
64
Document doc = getSystemSettingsDocument(path);
65         if (doc == null) {
66             return null;
67         }
68         Node node = doc.selectSingleNode("//" + settingName); //$NON-NLS-1$
69
if (node == null) {
70             return defaultValue;
71         }
72         return node.getText();
73     }
74
75     public String JavaDoc getSystemSetting(String JavaDoc settingName, String JavaDoc defaultValue) {
76         return getSystemSetting(PENTAHOSETTINGSFILENAME, settingName, defaultValue);
77     }
78
79     public List JavaDoc getSystemSettings(String JavaDoc path, String JavaDoc settingName) {
80         Document doc = getSystemSettingsDocument(path);
81         if (doc == null) {
82             return null;
83         }
84         return doc.selectNodes("//" + settingName); //$NON-NLS-1$
85
}
86
87     public List JavaDoc getSystemSettings(String JavaDoc settingName) {
88         return getSystemSettings(PENTAHOSETTINGSFILENAME, settingName);
89     }
90
91     public Document getSystemSettingsDocument(String JavaDoc actionPath) {
92         // S logId =
93
// runtimeContext.getInstanceId()+":"+LOG_NAME+":"+runtimeContext.getActionName();
94
// //$NON-NLS-1$ //$NON-NLS-2$
95
Document systemSettingsDocument = (Document) settingsDocumentMap.get(actionPath);
96         if (systemSettingsDocument == null) {
97             File JavaDoc 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 JavaDoc getFile(String JavaDoc path) {
108         File JavaDoc f = new File JavaDoc(getAbsolutePath(path));
109
110         if (!f.exists()) {
111             error(Messages.getErrorString("SYSTEMSETTINGS.ERROR_0002_FILE_NOT_IN_SOLUTION", f.getAbsolutePath())); //$NON-NLS-1$
112
return null;
113         }
114         debug(Messages.getString("SYSTEMSETTINGS.DEBUG_SYSTEM_SETTINGS_GET_FILE", f.getAbsolutePath())); //$NON-NLS-1$
115

116         return f;
117     }
118
119     private String JavaDoc getAbsolutePath(String JavaDoc path) {
120         return applicationContext.getSolutionPath("system" + File.separator + path); //$NON-NLS-1$
121
}
122
123     public Log getLogger() {
124         return logger;
125     }
126
127     public void resetSettingsCache() {
128         settingsDocumentMap.clear();
129     }
130
131     public Properties JavaDoc getSystemSettingsProperties(String JavaDoc path) {
132         String JavaDoc fullPath = PentahoSystem.getApplicationContext().getSolutionPath("system" + File.separator + path); //$NON-NLS-1$
133
File JavaDoc propsFile = new File JavaDoc(fullPath);
134         if (!propsFile.exists()) {
135             return null;
136         }
137         try {
138             Properties JavaDoc props = new Properties JavaDoc();
139             InputStream JavaDoc iStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(propsFile));
140             props.load(iStream);
141             return props;
142
143         } catch (Exception JavaDoc e) {
144
145         }
146         return null;
147     }
148
149 }
150
Popular Tags