Source code: org/apache/bcel/verifier/VerifierFactory.java
1 /*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 package org.apache.bcel.verifier;
18
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Vector;
24
25 /**
26 * This class produces instances of the Verifier class. Its purpose is to make
27 * sure that they are singleton instances with respect to the class name they
28 * operate on. That means, for every class (represented by a unique fully qualified
29 * class name) there is exactly one Verifier.
30 *
31 * @version $Id: VerifierFactory.java 386056 2006-03-15 11:31:56Z tcurdt $
32 * @author Enver Haase
33 * @see org.apache.bcel.verifier.Verifier
34 */
35 public class VerifierFactory {
36
37 /**
38 * The HashMap that holds the data about the already-constructed Verifier instances.
39 */
40 private static Map hashMap = new HashMap();
41 /**
42 * The VerifierFactoryObserver instances that observe the VerifierFactory.
43 */
44 private static List observers = new Vector();
45
46
47 /**
48 * The VerifierFactory is not instantiable.
49 */
50 private VerifierFactory() {
51 }
52
53
54 /**
55 * Returns the (only) verifier responsible for the class with the given name.
56 * Possibly a new Verifier object is transparently created.
57 * @return the (only) verifier responsible for the class with the given name.
58 */
59 public static Verifier getVerifier( String fully_qualified_classname ) {
60 Verifier v = (Verifier) (hashMap.get(fully_qualified_classname));
61 if (v == null) {
62 v = new Verifier(fully_qualified_classname);
63 hashMap.put(fully_qualified_classname, v);
64 notify(fully_qualified_classname);
65 }
66 return v;
67 }
68
69
70 /**
71 * Notifies the observers of a newly generated Verifier.
72 */
73 private static void notify( String fully_qualified_classname ) {
74 // notify the observers
75 Iterator i = observers.iterator();
76 while (i.hasNext()) {
77 VerifierFactoryObserver vfo = (VerifierFactoryObserver) i.next();
78 vfo.update(fully_qualified_classname);
79 }
80 }
81
82
83 /**
84 * Returns all Verifier instances created so far.
85 * This is useful when a Verifier recursively lets
86 * the VerifierFactory create other Verifier instances
87 * and if you want to verify the transitive hull of
88 * referenced class files.
89 */
90 public static Verifier[] getVerifiers() {
91 Verifier[] vs = new Verifier[hashMap.values().size()];
92 return (Verifier[]) (hashMap.values().toArray(vs)); // Because vs is big enough, vs is used to store the values into and returned!
93 }
94
95
96 /**
97 * Adds the VerifierFactoryObserver o to the list of observers.
98 */
99 public static void attach( VerifierFactoryObserver o ) {
100 observers.add(o);
101 }
102
103
104 /**
105 * Removes the VerifierFactoryObserver o from the list of observers.
106 */
107 public static void detach( VerifierFactoryObserver o ) {
108 observers.remove(o);
109 }
110 }