KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > principals > PrincipalUtils


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.principals;
29
30 import java.lang.reflect.Constructor JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * Utility class to instantiate a Principal implementation.
38  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
39  */

40 public class PrincipalUtils {
41
42     private static final Logger JavaDoc logger = Logger.getLogger(PrincipalUtils.class.getName());
43
44     public PrincipalUtils() {
45         super();
46     }
47
48     /**
49      * instantiate Principal implementations.
50      * @param className implementation class
51      * @param name
52      * @return Principal implementation instance
53      */

54     public static Principal JavaDoc getPrincipal(String JavaDoc className, String JavaDoc name){
55         Principal JavaDoc ppal = null;
56         Class JavaDoc clazz = null;
57
58         try {
59             clazz = Class.forName(className);
60         } catch (ClassNotFoundException JavaDoc e) {
61             logger.log(Level.SEVERE,"",e);
62         }
63
64         Constructor JavaDoc constructor = null;
65         try {
66             constructor = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
67         } catch (SecurityException JavaDoc e) {
68             logger.log(Level.SEVERE,"",e);
69         } catch (NoSuchMethodException JavaDoc e) {
70             logger.log(Level.SEVERE,"",e);
71         }
72
73         if(constructor!= null){
74             try {
75                 ppal = (Principal JavaDoc)constructor.newInstance(new Object JavaDoc[]{name});
76             } catch (IllegalArgumentException JavaDoc e) {
77                 logger.log(Level.SEVERE,"",e);
78             } catch (InstantiationException JavaDoc e) {
79                 logger.log(Level.SEVERE,"",e);
80             } catch (IllegalAccessException JavaDoc e) {
81                 logger.log(Level.SEVERE,"",e);
82             } catch (InvocationTargetException JavaDoc e) {
83                 logger.log(Level.SEVERE,"",e);
84             }
85         }else{
86             throw new IllegalArgumentException JavaDoc(" the provided Class="+className+" has'nt got any constructor with a String argument ");
87         }
88
89         return ppal;
90     }
91
92     /**
93      * instantiate Principal implementations
94      * @param clazz implementation class
95      * @param parameterTypes
96      * @param parameterValues
97      * @return instantiated principal
98      */

99     public static Principal JavaDoc getPrincipal(Class JavaDoc clazz,Class JavaDoc[] parameterTypes, Object JavaDoc[] parameterValues){
100         Principal JavaDoc ppal = null;
101
102         Constructor JavaDoc constructor = null;
103         try {
104             constructor = clazz.getConstructor(parameterTypes);
105         } catch (SecurityException JavaDoc e) {
106             logger.log(Level.SEVERE,"",e);
107         } catch (NoSuchMethodException JavaDoc e) {
108             logger.log(Level.SEVERE,"",e);
109         }
110
111         if(constructor!= null){
112             try {
113                 ppal = (Principal JavaDoc)constructor.newInstance(parameterValues);
114             } catch (IllegalArgumentException JavaDoc e) {
115                 logger.log(Level.SEVERE,"",e);
116             } catch (InstantiationException JavaDoc e) {
117                 logger.log(Level.SEVERE,"",e);
118             } catch (IllegalAccessException JavaDoc e) {
119                 logger.log(Level.SEVERE,"",e);
120             } catch (InvocationTargetException JavaDoc e) {
121                 logger.log(Level.SEVERE,"",e);
122             }
123         }
124
125         return ppal;
126     }
127 }
128
Popular Tags