1 28 package net.sf.jguard.ext.principals; 29 30 import java.lang.reflect.Constructor ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.security.Principal ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 40 public class PrincipalUtils { 41 42 private static final Logger logger = Logger.getLogger(PrincipalUtils.class.getName()); 43 44 public PrincipalUtils() { 45 super(); 46 } 47 48 54 public static Principal getPrincipal(String className, String name){ 55 Principal ppal = null; 56 Class clazz = null; 57 58 try { 59 clazz = Class.forName(className); 60 } catch (ClassNotFoundException e) { 61 logger.log(Level.SEVERE,"",e); 62 } 63 64 Constructor constructor = null; 65 try { 66 constructor = clazz.getConstructor(new Class []{String .class}); 67 } catch (SecurityException e) { 68 logger.log(Level.SEVERE,"",e); 69 } catch (NoSuchMethodException e) { 70 logger.log(Level.SEVERE,"",e); 71 } 72 73 if(constructor!= null){ 74 try { 75 ppal = (Principal )constructor.newInstance(new Object []{name}); 76 } catch (IllegalArgumentException e) { 77 logger.log(Level.SEVERE,"",e); 78 } catch (InstantiationException e) { 79 logger.log(Level.SEVERE,"",e); 80 } catch (IllegalAccessException e) { 81 logger.log(Level.SEVERE,"",e); 82 } catch (InvocationTargetException e) { 83 logger.log(Level.SEVERE,"",e); 84 } 85 }else{ 86 throw new IllegalArgumentException (" the provided Class="+className+" has'nt got any constructor with a String argument "); 87 } 88 89 return ppal; 90 } 91 92 99 public static Principal getPrincipal(Class clazz,Class [] parameterTypes, Object [] parameterValues){ 100 Principal ppal = null; 101 102 Constructor constructor = null; 103 try { 104 constructor = clazz.getConstructor(parameterTypes); 105 } catch (SecurityException e) { 106 logger.log(Level.SEVERE,"",e); 107 } catch (NoSuchMethodException e) { 108 logger.log(Level.SEVERE,"",e); 109 } 110 111 if(constructor!= null){ 112 try { 113 ppal = (Principal )constructor.newInstance(parameterValues); 114 } catch (IllegalArgumentException e) { 115 logger.log(Level.SEVERE,"",e); 116 } catch (InstantiationException e) { 117 logger.log(Level.SEVERE,"",e); 118 } catch (IllegalAccessException e) { 119 logger.log(Level.SEVERE,"",e); 120 } catch (InvocationTargetException e) { 121 logger.log(Level.SEVERE,"",e); 122 } 123 } 124 125 return ppal; 126 } 127 } 128 | Popular Tags |