001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.auth;
016:
017: import org.jmanage.core.util.CoreUtils;
018: import org.jmanage.core.util.Loggers;
019: import org.jmanage.util.StringUtils;
020:
021: import java.util.*;
022: import java.util.logging.Logger;
023: import java.io.*;
024:
025: /**
026: * Date: Mar 8, 2005 8:00:47 AM
027: * @author Shashank Bellary
028: */
029: public class ACLStore {
030:
031: private static final String ACL_CONFIG_FILE = "acl-config.properties";
032: private static final Logger logger = Loggers
033: .getLogger(ACLStore.class);
034: private static final ACLStore instance = new ACLStore();
035:
036: private Map aclNameToACLMap = new HashMap();
037:
038: /**
039: *
040: */
041: private ACLStore() {
042:
043: final String configFile = CoreUtils.getConfigDir()
044: + File.separator + ACL_CONFIG_FILE;
045:
046: try {
047: BufferedReader reader = new BufferedReader(new FileReader(
048: configFile));
049: String line = reader.readLine();
050: while (line != null) {
051: parse(line);
052: line = reader.readLine();
053: }
054: } catch (IOException e) {
055: throw new RuntimeException("Error reading: " + configFile,
056: e);
057: }
058: logger.info("Loaded ACLs");
059: }
060:
061: /**
062: * The only access to this instance.
063: *
064: * @return
065: */
066: public static ACLStore getInstance() {
067: return instance;
068: }
069:
070: public ACL getACL(String aclName) {
071: return (ACL) aclNameToACLMap.get(aclName);
072: }
073:
074: private void parse(String line) {
075: line = line.trim();
076: if (line.length() == 0 || line.startsWith("#")) {
077: return;
078: }
079:
080: int index = line.lastIndexOf('=');
081: if (index == -1) {
082: throw new RuntimeException("Invalid line format: " + line);
083: }
084: String acl = line.substring(0, index);
085: String authorizedList = line.substring(index + 1);
086: /* now seperate acl name from the context */
087: index = acl.indexOf('@');
088: // todo: ACLContext should be made abstract and should be constructed
089: // todo: based on the application that is using it.
090: String aclName = null;
091: String aclContext = null;
092: if (index != -1) {
093: aclName = acl.substring(0, index);
094: aclContext = acl.substring(index + 1);
095: } else {
096: aclName = acl;
097: }
098:
099: storeACL(aclName, aclContext, authorizedList);
100: }
101:
102: private void storeACL(String aclName, String aclContext,
103: String authorizedList) {
104: ACL acl = (ACL) aclNameToACLMap.get(aclName);
105: if (acl == null) {
106: acl = new ACL(aclName);
107: aclNameToACLMap.put(aclName, acl);
108: }
109: List authorizedListObj = StringUtils.csvToList(authorizedList);
110: if (aclContext == null) {
111: acl.setAuthorizedList(authorizedListObj);
112: } else {
113: acl.add(new ACLContext(aclContext), authorizedListObj);
114: }
115: logger.fine("Added ACL: " + aclName + " - " + aclContext
116: + " - " + authorizedList);
117: }
118: }
|