001: package com.jat.business.privileges;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import com.jat.business.BusinessException;
007: import com.jat.business.JatUser;
008: import com.jat.core.config.Config;
009: import com.jat.core.log.LogManager;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description: </p>
014: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
015: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
016: * @author stf
017: * @version 1.0
018: * @since 1.2
019: */
020:
021: public class ConfigPrivilegeLoader implements PrivilegesLoader {
022:
023: public PrivilegeList getPrivileges(JatUser user)
024: throws BusinessException {
025: return this .getPrivileges(user.getProfile());
026: }
027:
028: public String getDataSource() {
029: return "config_privilege";
030: }
031:
032: public PrivilegeList getPrivileges(String profile)
033: throws BusinessException {
034: try {
035: PrivilegeList privileges = new PrivilegeList(this
036: .getDataSource());
037: Vector pKeys = this .getConfig().getSubKeys(SECTION,
038: PROFILE_KEY);
039: for (Enumeration e = pKeys.elements(); e.hasMoreElements();) {
040: String key = (String) e.nextElement();
041: String prof = this .getConfig().getValue(SECTION,
042: key + PROFILE_NAME);
043: if (prof.equalsIgnoreCase(profile)) {
044: Vector priKeys = this .getConfig().getSubKeys(
045: SECTION, key + PRIVILEGE_KEY);
046: for (Enumeration el = priKeys.elements(); el
047: .hasMoreElements();) {
048: String priKey = (String) el.nextElement();
049: Privilege privilege = new Privilege(this
050: .getDataSource());
051: privilege.setName(this .getConfig().getValue(
052: SECTION, priKey + PRIVILEGE_NAME));
053: String conditionName = null;
054: try {
055: conditionName = this .getConfig().getValue(
056: SECTION,
057: priKey + PRIVILEGE_CONDITION);
058: } catch (Exception ignored) {
059: }
060: if (conditionName != null)
061: privilege
062: .setCondition((Privilegeable) Class
063: .forName(conditionName)
064: .newInstance());
065: privileges.addElement(privilege);
066: }
067: }
068: }
069: return privileges;
070: } catch (Exception ex) {
071: throw new BusinessException(this .getClass().getName()
072: + "::getPrivileges: " + ex.getMessage());
073: }
074: }
075:
076: public Vector profiles() throws BusinessException {
077: Vector ret = new Vector();
078: try {
079: PrivilegeList privileges = new PrivilegeList(this
080: .getDataSource());
081: Vector pKeys = this .getConfig().getSubKeys(SECTION,
082: PROFILE_KEY);
083: for (Enumeration e = pKeys.elements(); e.hasMoreElements();) {
084: String key = (String) e.nextElement();
085: String profile = this .getConfig().getValue(SECTION,
086: key + PROFILE_NAME);
087: ret.addElement(profile);
088: }
089: return ret;
090: } catch (Exception ex) {
091: throw new BusinessException(this .getClass().getName()
092: + "::profiles: " + ex.getMessage());
093: }
094: }
095:
096: public void persist() throws Exception {
097: LogManager.sendDebug(this .getClass().getName()
098: + "::persist: start");
099: Config config = this .getConfig();
100: config.save();
101: LogManager.sendDebug(this .getClass().getName()
102: + "::persist: end");
103: }
104:
105: public void remove(Profile profile) throws Exception {
106: LogManager.sendDebug(this .getClass().getName()
107: + "::remove: start for profile: " + profile);
108: String key = this .getIndexKey(profile.getName());
109: this .removeProfile(key);
110: LogManager.sendDebug(this .getClass().getName()
111: + "::remove: end");
112: }
113:
114: public void update(Profile profile) throws Exception {
115: LogManager.sendDebug(this .getClass().getName()
116: + "::update: start for profile: " + profile);
117: String key = this .getIndexKey(profile.getName());
118: this .removeProfile(key);
119: this .addProfile(profile, key);
120: LogManager.sendDebug(this .getClass().getName()
121: + "::update: end");
122: }
123:
124: public void insert(Profile profile) throws Exception {
125: LogManager.sendDebug(this .getClass().getName()
126: + "::insert: start for profile: " + profile);
127: int index = 1;
128: String key = null;
129: while (key == null) {
130: String k = PROFILE_KEY + (index++);
131: if (!this .getConfig()
132: .existsValue(SECTION, k + PROFILE_NAME))
133: key = k;
134: }
135: this .addProfile(profile, key);
136: LogManager.sendDebug(this .getClass().getName()
137: + "::insert: end");
138: }
139:
140: private void addProfile(Profile profile, String key)
141: throws Exception {
142: try {
143: Config config = this .getConfig();
144: config.addValue(SECTION, key + PROFILE_NAME, profile
145: .getName());
146: int index = 1;
147: for (Enumeration e = profile.getPrivileges().elements(); e
148: .hasMoreElements();) {
149: Privilege privilege = (Privilege) e.nextElement();
150: config.addValue(SECTION, key + PRIVILEGE_KEY + index
151: + PRIVILEGE_NAME, privilege.getName());
152: if (privilege.getCondition() != null)
153: config.addValue(SECTION, key + PRIVILEGE_KEY
154: + index + PRIVILEGE_CONDITION, privilege
155: .getName());
156: index++;
157: }
158: } catch (Exception ex) {
159: throw new Exception(this .getClass().getName()
160: + "::addProfile: exception: " + ex);
161: }
162: }
163:
164: private void removeProfile(String key) throws Exception {
165: try {
166: Config config = this .getConfig();
167: config.removeKeys(SECTION, key);
168: } catch (Exception ex) {
169: throw new Exception(this .getClass().getName()
170: + "::removeProfile: exception: " + ex);
171: }
172: }
173:
174: private Config getConfig() throws Exception {
175: String name = Config.getCurrent().getValue(SECTION,
176: "config.name");
177: Config config = Config.getCurrent().getConfig(name);
178: if (config == null) {
179: LogManager.sendError(this .getClass().getName()
180: + "::getConfig: exception: Config file " + name
181: + " not found");
182: throw new Exception("Config file " + name + " not found");
183: }
184: return config;
185: }
186:
187: private String getIndexKey(String name) throws Exception {
188: for (Enumeration e = this .getConfig().getSubKeys(SECTION,
189: PROFILE_KEY).elements(); e.hasMoreElements();) {
190: String key = (String) e.nextElement();
191: if (this .getConfig().getValue(SECTION, key + PROFILE_NAME)
192: .equalsIgnoreCase(name))
193: return key;
194: }
195: throw new Exception("config key not found for " + name);
196: }
197:
198: public static final String SECTION = "privileges";
199: public static final String PRIVILEGE_KEY = ".privilege";
200: public static final String PRIVILEGE_NAME = ".name";
201: public static final String PRIVILEGE_CONDITION = ".condition";
202: public static final String PROFILE_KEY = "profile";
203: public static final String PROFILE_NAME = ".name";
204: }
|