1 | package net.deterlab.abac; |
---|
2 | |
---|
3 | import java.io.*; |
---|
4 | import java.util.*; |
---|
5 | import java.lang.reflect.*; |
---|
6 | |
---|
7 | |
---|
8 | /** |
---|
9 | * @author <a href="http://abac.deterlab.net">ISI ABAC team</a> |
---|
10 | * @version 1.3 |
---|
11 | */ |
---|
12 | public class CredentialFactory { |
---|
13 | static List<Constructor<? extends Credential> > defCons = |
---|
14 | new ArrayList<Constructor<? extends Credential> >(); |
---|
15 | static List<Constructor<? extends Credential> > cons = |
---|
16 | new ArrayList<Constructor<? extends Credential> >(); |
---|
17 | static String defName = "net.deterlab.abac.GENICredential"; |
---|
18 | public static final int maxSize = 50 * 1024; |
---|
19 | |
---|
20 | static void initDefaultConstructors() throws ABACException { |
---|
21 | try { |
---|
22 | Class c = Class.forName(defName); |
---|
23 | Constructor<? extends Credential> con = c.getConstructor( |
---|
24 | Class.forName("java.io.InputStream"), |
---|
25 | Class.forName("java.util.Collection")); |
---|
26 | defCons.add(con); |
---|
27 | } |
---|
28 | catch (Exception e) { |
---|
29 | throw new ABACException("Could not initialize default " + |
---|
30 | "credential parser. Something is very wrong", e); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | static Credential[] newInstance(InputStream is, Set<Identity> ids) |
---|
35 | throws ABACException { |
---|
36 | Credential rv = null; |
---|
37 | ABACException err = null; |
---|
38 | List<Constructor<? extends Credential> > useCons = |
---|
39 | (cons.size()> 0 ) ? cons : defCons; |
---|
40 | |
---|
41 | if ( defCons.size() < 1 ) initDefaultConstructors(); |
---|
42 | |
---|
43 | if (!is.markSupported() && useCons.size() > 1) |
---|
44 | is = new BufferedInputStream(is, maxSize); |
---|
45 | is.mark(maxSize); |
---|
46 | |
---|
47 | for (Constructor<? extends Credential> c : useCons ) { |
---|
48 | try { |
---|
49 | rv = c.newInstance(is, ids); |
---|
50 | } |
---|
51 | catch (InvocationTargetException ie ) { |
---|
52 | Throwable th = ie.getCause(); |
---|
53 | |
---|
54 | if ( th != null && th instanceof ABACException ) |
---|
55 | err = (ABACException) th; |
---|
56 | else |
---|
57 | err = new ABACException("Constructor error " + |
---|
58 | "(this is weird) " + ie.getMessage(), ie); |
---|
59 | rv = null; |
---|
60 | } |
---|
61 | catch (Exception e ) { |
---|
62 | err = new ABACException("Constructor error " + |
---|
63 | e.getMessage(), e); |
---|
64 | rv = null; |
---|
65 | } |
---|
66 | if ( rv != null ) |
---|
67 | break; |
---|
68 | |
---|
69 | try { |
---|
70 | if (useCons.size() > 1) is.reset(); |
---|
71 | } |
---|
72 | catch (IOException ie) { |
---|
73 | break; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if ( rv != null ) |
---|
78 | return new Credential[] { rv }; |
---|
79 | else |
---|
80 | throw (err != null) ? err : |
---|
81 | new ABACException("null exception and failed construction??"); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | static Credential[] newInstance(File f, Set<Identity> ids) |
---|
86 | throws ABACException { |
---|
87 | try { |
---|
88 | return newInstance(new FileInputStream(f), ids); |
---|
89 | } |
---|
90 | catch (FileNotFoundException e) { |
---|
91 | throw new CertInvalidException(e.getMessage(), e); |
---|
92 | } |
---|
93 | } |
---|
94 | static Credential[] newInstance(String fn, Set<Identity> ids) |
---|
95 | throws ABACException { |
---|
96 | return newInstance(new File(fn), ids); |
---|
97 | } |
---|
98 | |
---|
99 | static public void registerClass(String name) |
---|
100 | throws ClassNotFoundException, NoSuchMethodException, |
---|
101 | SecurityException { |
---|
102 | |
---|
103 | Class c = Class.forName(name); |
---|
104 | Constructor<? extends Credential> con = c.getConstructor( |
---|
105 | Class.forName("java.io.InputStream"), |
---|
106 | Class.forName("java.util.Collection")); |
---|
107 | |
---|
108 | cons.add(con); |
---|
109 | } |
---|
110 | } |
---|