alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (InstalledLibrariesResolver.java)

This example Glassfish source code file (InstalledLibrariesResolver.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

extension, extension, file, io, ioexception, jar, jarfile, log, logging, manifest, map, object, object, set, set, string, string, text, util, vector

The Glassfish InstalledLibrariesResolver.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package org.glassfish.deployment.common;

import com.sun.logging.LogDomains;

import java.io.*;
import java.text.MessageFormat;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.logging.*;
import java.util.*;

import org.glassfish.api.deployment.archive.ReadableArchive;

/**
 * This class resolves the dependencies between optional packages (installed libraries) and also between
 * apps/stand-alone modules that depend on optional packages (installed libraries)
 * @author Sheetal Vartak
 */

public class InstalledLibrariesResolver {

    //optional packages are stored in this map that is keyed by the extension
    //(accounts only for ext dir jars)
    private static Map<Extension, String> extDirsLibsStore = new HashMap();

    // Fully qualified names of all jar files in all ext dirs.  Note that
    // this will include even jars that do not explicitly declare the
    // extension name and specification version in their manifest.
    private static Set extDirJars = new LinkedHashSet();

    //installed libraries list (accounts only for "domainRoot/lib/applibs" and not any
    //of "java.ext.dirs" entries)
    private static Map<Extension, String> appLibsDirLibsStore = new HashMap();

    private static Logger logger = LogDomains.getLogger(DeploymentUtils.class, LogDomains.DPL_LOGGER);

    /**
     * resolves installed library dependencies
     * @param manifest Manifest File
     * @param archiveUri archive
     * @return status indicating whether all depencencies (transitive) is resolved or not
     */
    public static boolean resolveDependencies(Manifest manifest, String archiveUri) {

        try{
            getInstalledLibraries(archiveUri, manifest, true, extDirsLibsStore);

        }catch(MissingResourceException e){
            //let us try app-libs directory
            try{
                getInstalledLibraries(archiveUri, manifest, true, appLibsDirLibsStore);
            }catch(MissingResourceException e1){
                logger.log(Level.WARNING,
                    "enterprise.deployment.backend.optionalpkg.dependency.notexist",
                    new Object[] {e1.getClass(), archiveUri});
                return false;
            }
        }
        logger.log(Level.INFO,
                "enterprise.deployment.backend.optionalpkg.dependency.satisfied",
                new Object[] {archiveUri});
        return true;
    }

    /**
     * check whether the optional packages have all their 
     * internal dependencies resolved
     * @param libDir libraryDirectory
     */
    public static void initializeInstalledLibRegistry(String libDir ) {
        initializeInstalledLibRegistryForExtDirs();
        initializeInstalledLibRegistryForApplibs(libDir);
    }

    private static void initializeInstalledLibRegistryForExtDirs() {
        String ext_dirStr = System.getProperty("java.ext.dirs");
        logger.fine("ext-Dir-Str : " + ext_dirStr);

        Vector extDirs = new Vector();
        StringTokenizer st = new StringTokenizer(ext_dirStr, File.pathSeparator);
        while (st.hasMoreTokens()) {
            String next = st.nextToken();
            logger.log(Level.FINE,"string tokens..." + next);
            extDirs.addElement(next);
        }

        for (int v = 0; v < extDirs.size(); v++) {

            File extDir = new File((String)extDirs.elementAt(v));

            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE,"extension dir..." + extDir);
            }

            /*
            *Records the files that are legitimate JARs.
            */
            ArrayList<File> validExtDirLibFiles = new ArrayList();

            File[] installedLibraries = extDir.listFiles();
            if (installedLibraries != null) {
                try {
                    Map<Extension, String> installedLibrariesList =
                            getInstalledLibraries(extDir.getAbsolutePath(), extDirJars, validExtDirLibFiles);
                    extDirsLibsStore.putAll(installedLibrariesList);

                    for (File file : validExtDirLibFiles) {
                        JarFile jarFile = null;
                        try {
                            jarFile = new JarFile(file);
                            Manifest m = jarFile.getManifest();
                            if (m!=null) {
                                try{
                                    getInstalledLibraries(file.getAbsolutePath(), m, true, extDirsLibsStore);
                                }catch(MissingResourceException e){
                                    logger.log(Level.WARNING,
                                        "enterprise.deployment.backend.optionalpkg.dependency.notexist",
                                        new Object[] {e.getClass(), file.getAbsolutePath()});
                                }
                            }
                        } catch (IOException ioe) {
                            logger.log(Level.WARNING,
                                "enterprise.deployment.backend.optionalpkg.invalid.zip",
                                    new Object[] {file.getAbsolutePath(), ioe.getMessage()});
                        }finally {
                            if (jarFile!=null)
                                jarFile.close();
                        }
                    }
                } catch (IOException e) {
                    logger.log(Level.WARNING,
                            "enterprise.deployment.backend.optionalpkg.dependency.exception",
                            new Object[] {e.getMessage()});
                }
            }
        }
    }

    /**
     * Adds all the jar files in all of the ext dirs into a single string
     * in classpath format.  Returns the empty string if there are no
     * jar files in any ext dirs.
     */
    public static String getExtDirFilesAsClasspath() {
       
        StringBuffer classpath = new StringBuffer();

        for(Iterator iter = extDirJars.iterator(); iter.hasNext();) {
            String next = (String) iter.next();
            if( classpath.length() > 0 ) {
                classpath.append(File.pathSeparator);                
            }
            classpath.append(next);
        }

        return classpath.toString();
    }

    public static Set<String> getInstalledLibraries(ReadableArchive archive) throws IOException {
        Set<String> libraries = new HashSet();
        if (archive != null) {
            Manifest manifest = archive.getManifest();

            //we are looking for libraries only in "applibs" directory, hence strict=false
            Set<String> installedLibraries =
                    getInstalledLibraries(archive.getURI().toString(), manifest, false, appLibsDirLibsStore);
            libraries.addAll(installedLibraries);

            // now check my libraries.
            Vector<String> libs = getArchiveLibraries(archive);
            if (libs != null) {
                for (String libUri : libs) {
                    JarInputStream jis = null;
                    try {
                        InputStream libIs = archive.getEntry(libUri);
                        if(libIs == null) {
                            //libIs can be null if reading an exploded archive where directories are also exploded. See FileArchive.getEntry()  
                            continue;
                        }
                        jis = new JarInputStream(libIs);
                        manifest = jis.getManifest();
                        if (manifest != null) {
                            //we are looking for libraries only in "applibs" directory, hence strict=false
                            Set<String> jarLibraries =
                                    getInstalledLibraries(archive.getURI().toString(), manifest, false,
                                            appLibsDirLibsStore);
                            libraries.addAll(jarLibraries);
                        }
                    } finally {
                        if (jis != null)
                            jis.close();
                    }
                }
            }
        }
        return libraries;
    }

    private static Set<String> getInstalledLibraries(String archiveURI, Manifest manifest, boolean strict,
                                                     Map<Extension, String> libraryStore) {
        Set<String> libraries = new HashSet();
        String extensionList = null;
        try {
            extensionList = manifest.getMainAttributes().
                    getValue(Attributes.Name.EXTENSION_LIST);
            if(logger.isLoggable(Level.FINE)){
                logger.fine("Extension-List for archive [" + archiveURI + "] : " + extensionList);
            }
        } catch (Exception e) {
            //ignore this exception
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE,
                        "InstalledLibrariesResolver : exception occurred : " + e.toString());
            }
        }

        if (extensionList != null) {
            StringTokenizer st =
                    new StringTokenizer(extensionList, " ");
            while (st.hasMoreTokens()) {

                String token = st.nextToken().trim();
                String extName = manifest.getMainAttributes().
                        getValue(token + "-" + Attributes.Name.EXTENSION_NAME);
                if (extName != null) {
                    extName = extName.trim();
                }

                String specVersion = manifest.getMainAttributes().
                        getValue(token + "-" + Attributes.Name.SPECIFICATION_VERSION);

                Extension extension = new Extension(extName);
                if (specVersion != null) {
                    extension.setSpecVersion(specVersion);
                }
                //TODO possible NPE when extension is unspecified
                boolean isLibraryInstalled = libraryStore.containsKey(extension);
                if (isLibraryInstalled) {
                    String libraryName = libraryStore.get(extension);
                    libraries.add(libraryName);
                } else {
                    if(strict){
                        throw new MissingResourceException(extName + " not found", extName, null);
                    }else{
                        // no action needed
                    }
                }
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, " is library installed [" + extName + "] " +
                            "for archive [" + archiveURI + "]: " + isLibraryInstalled);
                }
            }
        }
        return libraries;
    }


    /**
     * @return a list of libraries included in the archivist
     */
    private static Vector getArchiveLibraries(ReadableArchive archive) {

        Enumeration<String> entries = archive.entries();
        if (entries == null)
            return null;

        Vector libs = new Vector();
        while (entries.hasMoreElements()) {

            String entryName = entries.nextElement();
            if (entryName.indexOf('/') != -1) {
                continue; // not on the top level
            }
            if (entryName.endsWith(".jar")) {
                libs.add(entryName);
            }
        }
        return libs;
    }


    /**
     * initialize the "applibs" part of installed libraries ie.,
     * any library within "applibs" which represents an extension (EXTENSION_NAME in MANIFEST.MF)
     * that can be used by applications (as EXTENSION_LIST in their MANIFEST.MF)
     * @param domainLibDir library directory (of a domain)
     */
    private static void initializeInstalledLibRegistryForApplibs(String domainLibDir) {

        String applibsDirString = domainLibDir + File.separator + "applibs";

        logger.fine("applib-Dir-String..." + applibsDirString);
        ArrayList<File> validApplibsDirLibFiles = new ArrayList();
        Map<Extension, String> installedLibraries = getInstalledLibraries(applibsDirString, null, validApplibsDirLibFiles);
        appLibsDirLibsStore.putAll(installedLibraries);


        for (File file : validApplibsDirLibFiles) {
            JarFile jarFile = null;
            try {
                jarFile = new JarFile(file);
                Manifest m = jarFile.getManifest();
                if (m!=null) {
                    boolean found = true;
                    try{
                        getInstalledLibraries(file.getAbsolutePath(), m, true, appLibsDirLibsStore);
                    }catch(MissingResourceException mre ){
                        found = false;
                    }
                    //it is possible that an library in "applibs" specified dependency on an library in "ext-dirs"
                    if(!found){
                        try{
                            getInstalledLibraries(file.getAbsolutePath(), m, true, extDirsLibsStore);
                        }catch(MissingResourceException mre ){
                            logger.log(Level.WARNING,
                                "enterprise.deployment.backend.optionalpkg.dependency.notexist",
                                new Object[] {mre.getClass(), file.getAbsolutePath()});
                        }
                    }
                }
            } catch (IOException ioe) {
                logger.log(Level.WARNING,
                    "enterprise.deployment.backend.optionalpkg.invalid.zip",
                        new Object[] {file.getAbsolutePath(), ioe.getMessage()});
            }finally {
                if (jarFile!=null)
                    try {
                        jarFile.close();
                    } catch (IOException e) {
                        logger.log(Level.WARNING,
                                "enterprise.deployment.backend.optionalpkg.dependency.exception",
                                new Object[] {e.getMessage()});
                    }
            }
        }

    }

    private static Map<Extension, String> getInstalledLibraries(String libraryDirectoryName, Set processedLibraryNames,
                                                                List processedLibraries) {
        Map<Extension, String> installedLibraries = new HashMap();

        File dir = new File(libraryDirectoryName);

        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "installed library directory : " + dir);
        }

        File[] libraries = dir.listFiles();
        if (libraries != null) {
            try {
                for (int i = 0; i < libraries.length; i++) {

                    if (logger.isLoggable(Level.FINE)) {
                        logger.log(Level.FINE, "installed library : " + libraries[i]);
                    }
                    /*
                     *Skip any candidate that does not end with .jar or is a
                     *directory.
                     */
                    if (libraries[i].isDirectory()) {
                        logger.log(Level.FINE,
                                "Skipping installed library processing on " +
                                        libraries[i].getAbsolutePath() +
                                        "; it is a directory");
                        continue;
                    } else if (!libraries[i].getName().toLowerCase().endsWith(".jar")) {
                        logger.log(Level.FINE,
                                "Skipping installed library processing on " +
                                        libraries[i].getAbsolutePath() +
                                        "; it does not appear to be a JAR file based on its file type");
                        continue;
                    }
                    JarFile jarFile = null;
                    try {
                        jarFile = new JarFile(libraries[i]);

                        Manifest manifest = jarFile.getManifest();
                        if(processedLibraryNames != null){
                            processedLibraryNames.add(libraries[i].toString());
                        }
                        if(processedLibraries != null){
                            processedLibraries.add(libraries[i]);
                        }

                        //Extension-Name of optional package
                        if (manifest != null) {
                            String extName = manifest.getMainAttributes().
                                    getValue(Attributes.Name.EXTENSION_NAME);
                            String specVersion = manifest.getMainAttributes().
                                    getValue(Attributes.Name.SPECIFICATION_VERSION);
                            logger.fine("Extension " + libraries[i].getAbsolutePath() +
                                    ", extNameOfOPtionalPkg..." + extName +
                                    ", specVersion..." + specVersion);
                            if (extName != null) {
                                if (specVersion == null) {
                                    logger.log(Level.WARNING,
                                            "enterprise.deployment.backend.optionalpkg.dependency.specversion.null",
                                            new Object[]{extName, jarFile.getName()});
                                    specVersion = new String("");
                                }

                                Extension extension = new Extension(extName);
                                extension.setSpecVersion(specVersion);

                                installedLibraries.put(extension, libraries[i].getName());
                            }
                        }
                    } catch (Throwable thr) {
                        String msg = logger.getResourceBundle().getString(
                                "enterprise.deployment.backend.optionalpkg.dependency.error");
                        if (logger.isLoggable(Level.FINE)) {
                            logger.log(Level.FINE, MessageFormat.format(
                                    msg, libraries[i].getAbsolutePath(), thr.getMessage()), thr);
                        } else {
                            logger.log(Level.INFO, MessageFormat.format(
                                    msg, libraries[i].getAbsolutePath(), thr.getMessage()));
                        }
                    } finally {
                        if (jarFile != null) {
                            jarFile.close();
                        }
                    }
                }
            } catch (IOException e) {
                logger.log(Level.WARNING,
                        "enterprise.deployment.backend.optionalpkg.dependency.exception", new Object[]{e.getMessage()});
            }
        }
        return installedLibraries;
    }

    static class Extension {
        private String extensionName;
        private String specVersion = "";
        private String specVendor = "";
        private String implVersion = "";
        private String implVendor = "";

        public Extension(String name){
            this.extensionName = name;
        }
        
        public String getExtensionName() {
            return extensionName;
        }

        public void setExtensionName(String extensionName) {
            this.extensionName = extensionName;
        }

        public String getSpecVersion() {
            return specVersion;
        }

        public void setSpecVersion(String specVersion) {
            this.specVersion = specVersion;
        }

        public String getSpecVendor() {
            return specVendor;
        }

        public void setSpecVendor(String specVendor) {
            this.specVendor = specVendor;
        }

        public String getImplVersion() {
            return implVersion;
        }

        public void setImplVersion(String implVersion) {
            this.implVersion = implVersion;
        }

        public String getImplVendor() {
            return implVendor;
        }

        public void setImplVendor(String implVendor) {
            this.implVendor = implVendor;
        }

        public boolean equals(Object o){
            if(o != null && o instanceof Extension){
                Extension e = (Extension)o;
                if((o == this) ||
                    (e.getExtensionName().equals(extensionName) &&
                        (e.getImplVendor().equals(implVendor)    || (e.getImplVendor().equals(""))  ) &&
                        (e.getImplVersion().equals(implVersion)  || (e.getImplVersion().equals("")) ) &&
                        (e.getSpecVendor().equals(specVendor)    || (e.getSpecVendor().equals(""))  ) &&
                        (e.getSpecVersion().equals(specVersion)  || (e.getSpecVersion().equals("")) )
                    )){
                    return true;
                }else{
                    return false;
                }
            }else {
                return false;
            }
        }

        public int hashCode() {
            int result = 17;
            result = 37*result + extensionName.hashCode();
            return result;
        }
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish InstalledLibrariesResolver.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.