1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package net.sf.jguard.core.principals;
29
30
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.security.Principal;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.Set;
37 import java.util.logging.Level;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42
43
44
45
46 public class PrincipalUtils {
47
48 private static final Logger logger = LoggerFactory.getLogger(PrincipalUtils.class.getName());
49
50
51
52
53
54
55
56
57 public static Principal getPrincipal(String className, String name){
58 Principal ppal = null;
59 Class clazz = null;
60
61 try {
62 clazz = Class.forName(className);
63 } catch (ClassNotFoundException e) {
64 logger.error("",e);
65 }
66
67 Constructor constructor = null;
68 try {
69 constructor = clazz.getConstructor(new Class[]{String.class});
70 } catch (SecurityException e) {
71 logger.error("",e);
72 } catch (NoSuchMethodException e) {
73 logger.error("",e);
74 }
75
76 if(constructor!= null){
77 try {
78 ppal = (Principal)constructor.newInstance(new Object[]{name});
79 } catch (IllegalArgumentException e) {
80 logger.error("",e);
81 } catch (InstantiationException e) {
82 logger.error("",e);
83 } catch (IllegalAccessException e) {
84 logger.error("",e);
85 } catch (InvocationTargetException e) {
86 logger.error("",e);
87 }
88 }else{
89 throw new IllegalArgumentException(" the provided Class="+className+" has'nt got any constructor with a String argument ");
90 }
91
92 return ppal;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public static Principal getPrincipal(Class clazz,Class[] parameterTypes, Object[] parameterValues){
106 Principal ppal = null;
107
108 Constructor constructor = null;
109 try {
110 constructor = clazz.getConstructor(parameterTypes);
111 } catch (SecurityException e) {
112 logger.error("",e);
113 } catch (NoSuchMethodException e) {
114 logger.error("",e);
115 }
116
117 if(constructor!= null){
118 try {
119 ppal = (Principal)constructor.newInstance(parameterValues);
120 } catch (IllegalArgumentException e) {
121 logger.error("",e);
122 } catch (InstantiationException e) {
123 logger.error("",e);
124 } catch (IllegalAccessException e) {
125 logger.error("",e);
126 } catch (InvocationTargetException e) {
127 logger.error("",e);
128 }
129 }
130
131 return ppal;
132 }
133
134
135
136
137 public static Set clonePrincipalsSet(Set principals) throws CloneNotSupportedException{
138 Set clonedPrincipals = new HashSet();
139 Iterator principalsIterator = principals.iterator();
140 while(principalsIterator.hasNext()){
141 BasePrincipal ppal = (BasePrincipal)principalsIterator.next();
142 clonedPrincipals.add(ppal.clone());
143 }
144 return clonedPrincipals;
145 }
146
147
148
149
150
151
152 public static void checkPrincipals(Set globalPermissions, Set principals) {
153 Iterator itPrincipals = principals.iterator();
154 while(itPrincipals.hasNext()){
155 RolePrincipal tempPrincipal = (RolePrincipal)itPrincipals.next();
156 Set permissionsFromTemplate = tempPrincipal.getAllPermissions();
157 if(!globalPermissions.containsAll(permissionsFromTemplate)){
158
159 logger.warn(" principal called "+tempPrincipal.getLocalName()+" has been removed from the SubjectTemplate ");
160 logger.warn(" because it contains permissions not owned by this organization throw its Principals ");
161 itPrincipals.remove();
162 }
163
164 }
165 }
166 }