1 16 17 package org.jmanage.core.auth; 18 19 import org.jmanage.core.util.CoreUtils; 20 import org.jmanage.core.util.Loggers; 21 import org.jmanage.util.StringUtils; 22 23 import java.util.*; 24 import java.util.logging.Logger ; 25 import java.io.*; 26 27 31 public class ACLStore { 32 33 private static final String ACL_CONFIG_FILE = "acl-config.properties"; 34 private static final Logger logger = Loggers.getLogger(ACLStore.class); 35 private static final ACLStore instance = new ACLStore(); 36 37 private Map aclNameToACLMap = new HashMap(); 38 39 42 private ACLStore() { 43 44 final String configFile = CoreUtils.getConfigDir() + File.separator + 45 ACL_CONFIG_FILE; 46 47 try { 48 BufferedReader reader = new BufferedReader(new FileReader(configFile)); 49 String line = reader.readLine(); 50 while(line != null){ 51 parse(line); 52 line = reader.readLine(); 53 } 54 } catch (IOException e) { 55 throw new RuntimeException ("Error reading: " + configFile, e); 56 } 57 logger.info("Loaded ACLs"); 58 } 59 60 61 66 public static ACLStore getInstance() { 67 return instance; 68 } 69 70 public ACL getACL(String aclName){ 71 return (ACL)aclNameToACLMap.get(aclName); 72 } 73 74 private void parse(String line){ 75 line = line.trim(); 76 if(line.length() == 0 || line.startsWith("#")){ 77 return; 78 } 79 80 int index = line.lastIndexOf('='); 81 if(index == -1){ 82 throw new RuntimeException ("Invalid line format: " + line); 83 } 84 String acl = line.substring(0, index); 85 String authorizedList = line.substring(index + 1); 86 87 index = acl.indexOf('@'); 88 String aclName = null; 91 String aclContext = null; 92 if(index != -1){ 93 aclName = acl.substring(0, index); 94 aclContext = acl.substring(index+1); 95 }else{ 96 aclName = acl; 97 } 98 99 storeACL(aclName, aclContext, authorizedList); 100 } 101 102 private void storeACL(String aclName, 103 String aclContext, 104 String authorizedList){ 105 ACL acl = (ACL)aclNameToACLMap.get(aclName); 106 if(acl == null){ 107 acl = new ACL(aclName); 108 aclNameToACLMap.put(aclName, acl); 109 } 110 List authorizedListObj = StringUtils.csvToList(authorizedList); 111 if(aclContext == null){ 112 acl.setAuthorizedList(authorizedListObj); 113 }else{ 114 acl.add(new ACLContext(aclContext), authorizedListObj); 115 } 116 logger.fine("Added ACL: " + aclName + " - " + 117 aclContext + " - " + authorizedList); 118 } 119 } | Popular Tags |