BrightSide Workbench Full Report + Source Code
ProfileMap.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.security;
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.TreeMap;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantLock;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.jdom.Attribute;
30 import org.jdom.Document;
31 import org.jdom.Element;
32 import org.jdom.JDOMException;
33 import org.jdom.input.SAXBuilder;
34 import org.jdom.xpath.XPath;
35 import org.turro.elephant.context.ElephantContext;
36 
42 @Deprecated
43 public class ProfileMap extends TreeMap<String, RoleAttribute> {
44 
45  public boolean isInRoleByProfile(String profile, String role) {
46  if(profile != null) {
47  if(containsKey(profile)) {
48  return get(profile).isInRole(role);
49  }
50  }
51  return false;
52  }
53 
54  public List<String> getProfilesInRole(String role) {
55  List<String> profiles = new ArrayList<String>();
56  for(String s : keySet()) {
57  if(role == null) {
58  profiles.add(s);
59  }
60  else if(get(s).isInRole(role)) {
61  profiles.add(s);
62  }
63  }
64  return profiles;
65  }
66 
67  public static final String
68  ROLES_CONFIGURATION = "/WEB-INF/elephant/conf/xproles.xml",
69  ROLES_SITE_CONFIGURATION = "/WEB-INF/elephant/conf/xproles-site.xml";
70 
71  private static ProfileMap fullMap = null;
72  private static final Lock lock = new ReentrantLock();
73 
74  public static void resetProfiles() {
75  fullMap = null;
76  }
77 
78  public static ProfileMap loadProfiles() throws IOException {
79  if(fullMap == null) {
80  lock.lock();
81  try {
82  if(fullMap == null) {
83  fullMap = loadFromElementsList(loadRoleByExpression("/xproles/role"));
84  }
85  } finally {
86  lock.unlock();
87  }
88  }
89  return fullMap;
90  }
91 
92  private static ProfileMap loadFromElementsList(List<Element> elements) {
93  ProfileMap pm = new ProfileMap();
94 
95  for(Element el : elements) {
96  RoleAttribute ra = pm.get(el.getAttributeValue("name"));
97  if(ra != null) {
98  ra.addFromElementChildren(el.getChildren("resource"));
99  } else {
100  pm.put(el.getAttributeValue("name"), RoleAttribute.loadFromElementChildren(el.getChildren("resource")));
101  }
102  }
103 
104  return pm;
105  }
106 
107  public static List loadRoleByExpression(String expression) throws IOException {
108  List roles = new ArrayList();
109  List l = loadRoleFile(
111  expression);
112  if(l != null) roles.addAll(l);
113  l = loadRoleFile(
114  new File(ElephantContext.getRealPath(ROLES_SITE_CONFIGURATION)),
115  expression);
116  if(l != null) roles.addAll(l);
117  return roles;
118  }
119 
120  public static List loadRoleFile(File confFile, String expression) throws IOException {
121  if (confFile.exists()) {
122  SAXBuilder builder = new SAXBuilder();
123  try {
124  Document doc = builder.build(confFile);
125  return XPath.selectNodes(doc, expression);
126  } catch (IOException ex) {
127  Logger.getLogger(ProfileMap.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
128  } catch (JDOMException ex) {
129  Logger.getLogger(XpUser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
130  }
131  }
132  return null;
133  }
134 
135  public static List getRoleNames() throws IOException {
136  List<String> names = new ArrayList<String>();
137  for(Attribute a : (List<Attribute>) loadRoleByExpression("/xproles/role/@name")) {
138  names.add(a.getValue());
139  }
140  return names;
141  }
142 
143  public static List getRoleNamesByFilter(String filter) throws IOException {
144  List<String> names = new ArrayList<String>();
145  for(Attribute a : (List<Attribute>) loadRoleByExpression("/xproles/role[resource/@name='" + filter + "']/@name")) {
146  names.add(a.getValue());
147  }
148  return names;
149  }
150 
151 }
static List loadRoleByExpression(String expression)
static List loadRoleFile(File confFile, String expression)
boolean isInRoleByProfile(String profile, String role)
Definition: ProfileMap.java:45
static List getRoleNamesByFilter(String filter)
List< String > getProfilesInRole(String role)
Definition: ProfileMap.java:54
void addFromElementChildren(List< Element > children)