FileDocCategorySizeDatePackage
APIRepository.javaAPI DocGlassfish v2 API17616Fri May 04 22:33:26 BST 2007com.sun.enterprise.tools.verifier.apiscan.stdapis

APIRepository

public class APIRepository extends Object
This class represents a repository of APIs. It is backed by a XML file with a predefined schema. This class is resoponsible for parsing the XML file and getting populated by the information available in that file. In verifier, this class is used in conjunction with an XML file where all the standard APIs are listed along with their respective version number. Refer to standard-apis.xml file to see how we list APIs in that file.
author
Sanjeeb.Sahoo@Sun.COM

Fields Summary
private static Logger
logger
private static String
myClsName
private HashMap
apis
private static APIRepository
me
Constructors Summary
private APIRepository(String fileName)

        logger.entering(myClsName, "init<>", fileName); // NOI18N
        final File file = new File(fileName);
        Document d = getDocumentBuilder().parse(file);
        traverseTree(d.getDocumentElement());
    
Methods Summary
public static synchronized voidInitialize(java.lang.String fileName)
Initialize the singleton instance.

param
fileName An XML file which contains the details of APIs.

        logger.logp(Level.FINE, myClsName, "Initialize", fileName); // NOI18N
        //Pl refer to bug#6174887
//        assert(me==null);
//        if(me==null){
        me = new APIRepository(fileName);
//        }else throw new RuntimeException("Already Initialized");
    
public static com.sun.enterprise.tools.verifier.apiscan.stdapis.APIRepositoryInstance()
Return the singleton instance.


             
        
        assert(me != null);
        return me;
    
protected java.util.CollectiongetClassesFor(java.lang.String api_name_version)

        return ((API) apis.get(api_name_version)).getClasses();
    
private javax.xml.parsers.DocumentBuildergetDocumentBuilder()

        DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
        bf.setValidating(true);
        bf.setIgnoringComments(false);
        bf.setIgnoringElementContentWhitespace(true);
        bf.setCoalescing(true);
        bf.setNamespaceAware(true);
        bf.setAttribute(
                "http://java.sun.com/xml/jaxp/properties/schemaLanguage", // NOI18N
                "http://www.w3.org/2001/XMLSchema"); // NOI18N
        DocumentBuilder builder = bf.newDocumentBuilder();
        builder.setErrorHandler(new DefaultHandler() {
            public void error(SAXParseException e) throws SAXException {
                throw e;
            }
        });
        return builder;
    
private static java.lang.StringgetPackageName(java.lang.String externalClassName)

        int idx = externalClassName.lastIndexOf('.");
        if (idx != -1) {
            return externalClassName.substring(0, idx);
        } else
            return "";
    
protected java.util.CollectiongetPackagesFor(java.lang.String api_name_version)

        return ((API) apis.get(api_name_version)).getPackages();
    
protected java.util.CollectiongetPatternsFor(java.lang.String api_name_version)

        return ((API) apis.get(api_name_version)).getPatterns();
    
public booleanisClassPartOf(java.lang.String class_name, java.lang.String api_name_version)
This method is used to find out if a particular class is part of a standard API or not. e.g. to find out if an EJB 2.0 application is allowed to use javax.ejb.Timer.class, call this method as
isClassPartOf("javax.ejb.Timer","ejb_jar_2.0")

param
class_name name of the class (in external class name format)
param
api_name_version is the name of the API along with version. It must already be defined in the XML file.
return
true iff the given class is part of this API.

        if (getClassesFor(api_name_version).contains(class_name)) {
            return true;
        } else if (getPackagesFor(api_name_version).
                contains(getPackageName(class_name))) {
            return true;
        } else {
            for (String pattern : getPatternsFor(api_name_version)) {
                if (class_name.startsWith(pattern)) {
                    return true;
                }
            }
        }
        return false;
    
public booleanisPackagePartOf(java.lang.String pkg_name, java.lang.String api_name_version)
This method is used to find out if a particular package is part of a standard API or not. e.g. to find out if an appclient (v 1.4) is allowed to import javax.persistence.* , call this method as
isPackagePartOf("javax.persistence","appclient_1.4")

param
pkg_name name of the package
param
api_name_version is the name of the API along with version. It must already be defined in the XML file.
return
true iff the given package is part of this API.

        if (getPackagesFor(api_name_version).contains(pkg_name)) {
            return true;
        } else {
            for (String pattern : getPatternsFor(api_name_version)) {
                if (pkg_name.startsWith(pattern)) {
                    return true;
                }
            }
        }
        return false;
    
public static voidmain(java.lang.String[] args)

        if (args.length < 1) {
            usage();
        }
        Logger logger = Logger.getLogger("apiscan.stdapis"); // NOI18N
        try {
            APIRepository.Initialize(args[0]);
            APIRepository apiRep = APIRepository.Instance();
            switch(args.length) {
                case 1:
                    System.out.println(apiRep);
                    break;
                case 2:
                    System.out.println(apiRep.apis.get(args[1]));
                    break;
                case 3:
                    System.out.println(apiRep.isClassPartOf(args[2], args[1]));
                    break;
                default:
                    usage();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public java.lang.StringtoString()

        StringBuffer sb = new StringBuffer();
        for (Iterator i = apis.values().iterator(); i.hasNext();) {
            sb.append("\n").append(i.next().toString()); // NOI18N
        }
        return sb.toString();
    
private voidtraverseTree(org.w3c.dom.Node node)

        if (node == null) {
            return;
        }
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) node;
            String tagName = e.getTagName();
            if (tagName.equals("api")) { // NOI18N
                API a = new API(e);
                apis.put(a.name_version, a);
            }
            NodeList childNodes = node.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                traverseTree(childNodes.item(i));
            }
        }
    
private static voidusage()

        System.out.println(
                "Usage: java " + APIRepository.class.getName() + // NOI18N
                " <file_name> [api_name_version] [class_name]"); // NOI18N
        System.out.println("\nExamples:\n"); // NOI18N
        System.out.println(
                "java " + APIRepository.class.getName() + // NOI18N
                " src/standard-apis.xml ejb_jar_2.0 javax.ejb.Timer"); // NOI18N
        System.out.println("The above command prints true if javax.ejb.Timer is part of ejb_api_2.0 API.\n"); // NOI18N
        System.out.println(
                "java " + APIRepository.class.getName() + // NOI18N
                " src/standard-apis.xml ejb_jar_2.0"); // NOI18N
        System.out.println("The above command prints details about all classes and packages for ejb_api_2.0 API.\n"); // NOI18N
        System.out.println(
                "java " + APIRepository.class.getName() + // NOI18N
                " src/standard-apis.xml"); // NOI18N
        System.out.println("The above command prints details about all APIs.\n"); // NOI18N
        System.exit(1);