1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 package org.apache.bcel.verifier; 19 20 import java.util.HashMap; 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$ 32 * @see Verifier 33 */ 34 public class VerifierFactory { 35 36 /** 37 * The HashMap that holds the data about the already-constructed Verifier instances. 38 */ 39 private static final Map<String, Verifier> hashMap = new HashMap<>(); 40 /** 41 * The VerifierFactoryObserver instances that observe the VerifierFactory. 42 */ 43 private static final List<VerifierFactoryObserver> observers = new Vector<>(); 44 45 46 /** 47 * The VerifierFactory is not instantiable. 48 */ VerifierFactory()49 private VerifierFactory() { 50 } 51 52 53 /** 54 * Returns the (only) verifier responsible for the class with the given name. 55 * Possibly a new Verifier object is transparently created. 56 * @return the (only) verifier responsible for the class with the given name. 57 */ getVerifier( final String fully_qualified_classname )58 public static Verifier getVerifier( final String fully_qualified_classname ) { 59 Verifier v = hashMap.get(fully_qualified_classname); 60 if (v == null) { 61 v = new Verifier(fully_qualified_classname); 62 hashMap.put(fully_qualified_classname, v); 63 notify(fully_qualified_classname); 64 } 65 return v; 66 } 67 68 69 /** 70 * Notifies the observers of a newly generated Verifier. 71 */ notify( final String fully_qualified_classname )72 private static void notify( final String fully_qualified_classname ) { 73 // notify the observers 74 for (final VerifierFactoryObserver vfo : observers) { 75 vfo.update(fully_qualified_classname); 76 } 77 } 78 79 80 /** 81 * Returns all Verifier instances created so far. 82 * This is useful when a Verifier recursively lets 83 * the VerifierFactory create other Verifier instances 84 * and if you want to verify the transitive hull of 85 * referenced class files. 86 */ getVerifiers()87 public static Verifier[] getVerifiers() { 88 final Verifier[] vs = new Verifier[hashMap.values().size()]; 89 return hashMap.values().toArray(vs); // Because vs is big enough, vs is used to store the values into and returned! 90 } 91 92 93 /** 94 * Adds the VerifierFactoryObserver o to the list of observers. 95 */ attach( final VerifierFactoryObserver o )96 public static void attach( final VerifierFactoryObserver o ) { 97 observers.add(o); 98 } 99 100 101 /** 102 * Removes the VerifierFactoryObserver o from the list of observers. 103 */ detach( final VerifierFactoryObserver o )104 public static void detach( final VerifierFactoryObserver o ) { 105 observers.remove(o); 106 } 107 } 108