001: /*
002: * Copyright 2000-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package org.apache.bcel.verifier;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Vector;
024:
025: /**
026: * This class produces instances of the Verifier class. Its purpose is to make
027: * sure that they are singleton instances with respect to the class name they
028: * operate on. That means, for every class (represented by a unique fully qualified
029: * class name) there is exactly one Verifier.
030: *
031: * @version $Id: VerifierFactory.java 386056 2006-03-15 11:31:56Z tcurdt $
032: * @author Enver Haase
033: * @see org.apache.bcel.verifier.Verifier
034: */
035: public class VerifierFactory {
036:
037: /**
038: * The HashMap that holds the data about the already-constructed Verifier instances.
039: */
040: private static Map hashMap = new HashMap();
041: /**
042: * The VerifierFactoryObserver instances that observe the VerifierFactory.
043: */
044: private static List observers = new Vector();
045:
046: /**
047: * The VerifierFactory is not instantiable.
048: */
049: private VerifierFactory() {
050: }
051:
052: /**
053: * Returns the (only) verifier responsible for the class with the given name.
054: * Possibly a new Verifier object is transparently created.
055: * @return the (only) verifier responsible for the class with the given name.
056: */
057: public static Verifier getVerifier(String fully_qualified_classname) {
058: Verifier v = (Verifier) (hashMap.get(fully_qualified_classname));
059: if (v == null) {
060: v = new Verifier(fully_qualified_classname);
061: hashMap.put(fully_qualified_classname, v);
062: notify(fully_qualified_classname);
063: }
064: return v;
065: }
066:
067: /**
068: * Notifies the observers of a newly generated Verifier.
069: */
070: private static void notify(String fully_qualified_classname) {
071: // notify the observers
072: Iterator i = observers.iterator();
073: while (i.hasNext()) {
074: VerifierFactoryObserver vfo = (VerifierFactoryObserver) i
075: .next();
076: vfo.update(fully_qualified_classname);
077: }
078: }
079:
080: /**
081: * Returns all Verifier instances created so far.
082: * This is useful when a Verifier recursively lets
083: * the VerifierFactory create other Verifier instances
084: * and if you want to verify the transitive hull of
085: * referenced class files.
086: */
087: public static Verifier[] getVerifiers() {
088: Verifier[] vs = new Verifier[hashMap.values().size()];
089: return (Verifier[]) (hashMap.values().toArray(vs)); // Because vs is big enough, vs is used to store the values into and returned!
090: }
091:
092: /**
093: * Adds the VerifierFactoryObserver o to the list of observers.
094: */
095: public static void attach(VerifierFactoryObserver o) {
096: observers.add(o);
097: }
098:
099: /**
100: * Removes the VerifierFactoryObserver o from the list of observers.
101: */
102: public static void detach(VerifierFactoryObserver o) {
103: observers.remove(o);
104: }
105: }
|