001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name$
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.ext.principals;
029:
030: import java.lang.reflect.Constructor;
031: import java.lang.reflect.InvocationTargetException;
032: import java.security.Principal;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: /**
037: * Utility class to instantiate a Principal implementation.
038: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
039: */
040: public class PrincipalUtils {
041:
042: private static final Logger logger = Logger
043: .getLogger(PrincipalUtils.class.getName());
044:
045: public PrincipalUtils() {
046: super ();
047: }
048:
049: /**
050: * instantiate Principal implementations.
051: * @param className implementation class
052: * @param name
053: * @return Principal implementation instance
054: */
055: public static Principal getPrincipal(String className, String name) {
056: Principal ppal = null;
057: Class clazz = null;
058:
059: try {
060: clazz = Class.forName(className);
061: } catch (ClassNotFoundException e) {
062: logger.log(Level.SEVERE, "", e);
063: }
064:
065: Constructor constructor = null;
066: try {
067: constructor = clazz
068: .getConstructor(new Class[] { String.class });
069: } catch (SecurityException e) {
070: logger.log(Level.SEVERE, "", e);
071: } catch (NoSuchMethodException e) {
072: logger.log(Level.SEVERE, "", e);
073: }
074:
075: if (constructor != null) {
076: try {
077: ppal = (Principal) constructor
078: .newInstance(new Object[] { name });
079: } catch (IllegalArgumentException e) {
080: logger.log(Level.SEVERE, "", e);
081: } catch (InstantiationException e) {
082: logger.log(Level.SEVERE, "", e);
083: } catch (IllegalAccessException e) {
084: logger.log(Level.SEVERE, "", e);
085: } catch (InvocationTargetException e) {
086: logger.log(Level.SEVERE, "", e);
087: }
088: } else {
089: throw new IllegalArgumentException(
090: " the provided Class="
091: + className
092: + " has'nt got any constructor with a String argument ");
093: }
094:
095: return ppal;
096: }
097:
098: /**
099: * instantiate Principal implementations
100: * @param clazz implementation class
101: * @param parameterTypes
102: * @param parameterValues
103: * @return instantiated principal
104: */
105: public static Principal getPrincipal(Class clazz,
106: Class[] parameterTypes, Object[] parameterValues) {
107: Principal ppal = null;
108:
109: Constructor constructor = null;
110: try {
111: constructor = clazz.getConstructor(parameterTypes);
112: } catch (SecurityException e) {
113: logger.log(Level.SEVERE, "", e);
114: } catch (NoSuchMethodException e) {
115: logger.log(Level.SEVERE, "", e);
116: }
117:
118: if (constructor != null) {
119: try {
120: ppal = (Principal) constructor
121: .newInstance(parameterValues);
122: } catch (IllegalArgumentException e) {
123: logger.log(Level.SEVERE, "", e);
124: } catch (InstantiationException e) {
125: logger.log(Level.SEVERE, "", e);
126: } catch (IllegalAccessException e) {
127: logger.log(Level.SEVERE, "", e);
128: } catch (InvocationTargetException e) {
129: logger.log(Level.SEVERE, "", e);
130: }
131: }
132:
133: return ppal;
134: }
135: }
|