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

Glassfish example source code file (CLIManFileFinder.java)

This example Glassfish source code file (CLIManFileFinder.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

arraylist, classloader, inputstream, inputstreamreader, io, iterator, iterator, list, locale, locale, log, logging, nosuchelementexception, reader, reader, string, string, util

The Glassfish CLIManFileFinder.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2011 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 com.sun.enterprise.admin.cli;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.logging.Logger;

/**
 *  A utility class that gets the plain text man page for the
 *  given command.  It searches (using Class.getResource()) for
 *  the pages, and returns the first one found.
 *
 *  For any given man page multiple instances of that page can exist.
 *  Man pages are come in sections (1 through 9, 1m through 9m),
 *  locales (language, country, variant), and by command version.
 *  These instances are ordered by section number (1 - 9, 1m *  - 9m),
 *  local specificity (most specific to least specific) and then by
 *  version (later versions before earlier versions).
 *
 *  This is probably <em>not what is wanted (I think what is
 *  wanted is versions before sections before language specificity),
 *  but is this way because of the way the Java Class.getResource()
 *  mechanism works.
 *
 *  All methods will throw a NullPointerException if given null object
 *  arguments.
 *
 *  All methods will throw an IllegalArgumentException if their
 *  arguments are non-null but are otherwise meaningless.
 */

public class CLIManFileFinder {
    private static final String[] sections = {
        "1", "1m", "2", "2m", "3", "3m", "4", "4m", "5", "5m",
        "6", "6m", "7", "7m", "8", "8m", "9", "9m", "5asc" };

    private static final Logger logger =
        Logger.getLogger(CLIManFileFinder.class.getPackage().getName());

    /**
     * Get the man page for the given command, using the default locale
     *
     * @param cmd the command
     */
    public static Reader getCommandManFile(CLICommand cmd) {
        return getCommandManFile(cmd, Locale.getDefault());
    }

    /**
     * Get the man page for the given command for the given locale
     * @param cmd the command
     * @param currentLocale the locale to be used to find the man page
     */
    public static Reader getCommandManFile(CLICommand cmd,
            Locale currentLocale) {
        return getCommandManFile(cmd, currentLocale,
                                cmd.getClass().getClassLoader());
    }

    /**
     * Get the man page for the given command for the given locale
     * using the given classloader.
     *
     * @param cmd the command
     * @param locale the locale to be used to find the man page
     * @param classLoader the class loader to be used to find the man page
     */
    public static Reader getCommandManFile(CLICommand cmd, Locale locale,
            ClassLoader classLoader) {
        return getCommandManFile(getCommandName(cmd), cmd.getClass().getName(),
                            locale, classLoader);
    }

    /**
     * Get the man page for the given command for the given locale
     * using the given classloader.
     *
     * @param cmdName the command name
     * @param cmdClass the command class
     * @param locale the locale to be used to find the man page
     * @param classLoader the class loader to be used to find the man page
     */
    public static Reader getCommandManFile(String cmdName, String cmdClass,
            Locale locale, ClassLoader classLoader) {

        InputStream s = null;
 
        Iterator it = getPossibleLocations(cmdName, cmdClass, locale);
        while (s == null && it.hasNext()) {
            s = classLoader.getResourceAsStream((String)it.next());
        }
 
        if (s == null)
            return null;
        Reader r;
        try {
            r = new InputStreamReader(s, "utf-8");
        } catch (UnsupportedEncodingException ex) {
            r = new InputStreamReader(s);
        }
        return r;
    }

    private static Iterator getPossibleLocations(final String cmdName,
            final String cmdClass, final Locale locale) {
        return new Iterator() {
            final String[] locales = getLocaleLocations(locale);
            private int i = 0;
            private int j = 0;
            private String helpdir = getHelpDir(cmdClass);
            private String commandName = cmdName;

            public boolean hasNext() {
                return i < locales.length && j < sections.length;
            }

            public Object next() throws NoSuchElementException{
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                final String result = helpdir + locales[i] + "/" +
                                        commandName + "." + sections[j++];

                if (j == sections.length) {
                    i++;
                    if (i < locales.length)
                        j = 0;
                }
                logger.finer("Trying to get this manpage: " + result);
                return result;
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    private static String getCommandName(CLICommand cmd) {
        String commandName = cmd.getName();
        if (commandName.length() == 0)
            throw new IllegalArgumentException(
                                "Command name cannot be empty");

        // special case "help" --> "asadmin"
        if (commandName.equals("help"))
            commandName = "asadmin";

        return commandName;
    }

    private static String getHelpDir(String cmdClass) {
        // The man page is assumed to be packaged with the
        // command class.
        String pkgname =
            cmdClass.substring(0, cmdClass.lastIndexOf('.'));
        return pkgname.replace('.', '/');
    }

    private static String[] getLocaleLocations(Locale locale) {
        String language = locale.getLanguage();
        String country = locale.getCountry();
        String variant = locale.getVariant();
        List<String> l = new ArrayList();
        l.add("");

        if (language != null && language.length() > 0) {
            l.add("/" + language);
            if (country != null && country.length() > 0) {
                l.add("/" + language + "_" + country);
                if (variant != null && variant.length() > 0)
                    l.add("/" + language + "_" + country + "_" + variant);
            }
        }
        Collections.reverse(l);
        return l.toArray(new String[l.size()]);
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish CLIManFileFinder.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.