    001/*****************************************************************************
    002 * Copyright by The HDF Group.                                               *
    003 * Copyright by the Board of Trustees of the University of Illinois.         *
    004 * All rights reserved.                                                      *
    005 *                                                                           *
    006 * This file is part of the HDF Java Products distribution.                  *
    007 * The full copyright notice, including terms governing use, modification,   *
    008 * and redistribution, is contained in the files COPYING and Copyright.html. *
    009 * COPYING can be found at the root of the source code distribution tree.    *
    010 * Or, see https://support.hdfgroup.org/products/licenses.html               *
    011 * If you do not have access to either file, you may request a copy from     *
    012 * help@hdfgroup.org.                                                        *
    013 ****************************************************************************/
    014
    015package hdf.view.DataView;
    016
    017import java.util.List;
    018
    019import hdf.view.Tools;
    020import hdf.view.ViewProperties;
    021import hdf.view.ViewProperties.DataViewType;
    022import hdf.view.ImageView.DefaultImageViewFactory;
    023import hdf.view.MetaDataView.DefaultMetaDataViewFactory;
    024import hdf.view.PaletteView.DefaultPaletteViewFactory;
    025import hdf.view.TableView.DefaultTableViewFactory;
    026import hdf.view.TreeView.DefaultTreeViewFactory;
    027
    028/**
    029 * Following the Abstract Factory Pattern, represents a class to produce
    030 * different types of DataView factory classes depending on the given
    031 * DataViewType enum value.
    032 *
    033 * @author jhenderson
    034 * @version 1.0 4/17/2018
    035 */
    036public class DataViewFactoryProducer {
    037
    038    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DataViewFactoryProducer.class);
    039
    040    public static DataViewFactory getFactory(DataViewType viewType) throws Exception {
    041        String factoryClassName = null;
    042        DataViewFactory theFactory = null;
    043        List<?> moduleList = null;
    044
    045        /*
    046         * First determine if we are using the default module for the requested DataViewFactory
    047         * class. If not, we will attempt to load the given DataViewFactory class.
    048         */
    049        switch (viewType) {
    050            case TABLE:
    051                /* Retrieve the "currently selected" TableViewFactory class to use */
    052                moduleList = ViewProperties.getTableViewList();
    053                if ((moduleList == null) || (moduleList.size() <= 0)) {
    054                    return null;
    055                }
    056
    057                factoryClassName = (String) moduleList.get(0);
    058
    059                if (factoryClassName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    060                    log.trace("getFactory(): returning default TableView factory instance");
    061                    return new DefaultTableViewFactory();
    062                }
    063
    064                break;
    065
    066            case IMAGE:
    067                /* Retrieve the "currently selected" ImageViewFactory class to use */
    068                moduleList = ViewProperties.getImageViewList();
    069                if ((moduleList == null) || (moduleList.size() <= 0)) {
    070                    return null;
    071                }
    072
    073                factoryClassName = (String) moduleList.get(0);
    074
    075                if (factoryClassName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    076                    log.trace("getFactory(): returning default ImageView factory instance");
    077                    return new DefaultImageViewFactory();
    078                }
    079
    080                break;
    081
    082            case PALETTE:
    083                /* Retrieve the "currently selected" PaletteViewFactory class to use */
    084                moduleList = ViewProperties.getPaletteViewList();
    085                if ((moduleList == null) || (moduleList.size() <= 0)) {
    086                    return null;
    087                }
    088
    089                factoryClassName = (String) moduleList.get(0);
    090
    091                if (factoryClassName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    092                    log.trace("getFactory(): returning default PaletteView factory instance");
    093                    return new DefaultPaletteViewFactory();
    094                }
    095
    096                break;
    097
    098            case METADATA:
    099                /* Retrieve the "currently selected" MetaDataViewFactory class to use */
    100                moduleList = ViewProperties.getMetaDataViewList();
    101                if ((moduleList == null) || (moduleList.size() <= 0)) {
    102                    return null;
    103                }
    104
    105                factoryClassName = (String) moduleList.get(0);
    106
    107                if (factoryClassName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    108                    log.trace("getFactory(): returning default MetaDataView factory instance");
    109                    return new DefaultMetaDataViewFactory();
    110                }
    111
    112                break;
    113
    114            case TREEVIEW:
    115                /* Retrieve the "currently selected" TreeViewFactory class to use */
    116                moduleList = ViewProperties.getTreeViewList();
    117                if ((moduleList == null) || (moduleList.size() <= 0)) {
    118                    return null;
    119                }
    120
    121                factoryClassName = (String) moduleList.get(0);
    122
    123                if (factoryClassName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    124                    log.trace("getFactory(): returning default TreeView factory instance");
    125                    return new DefaultTreeViewFactory();
    126                }
    127
    128                break;
    129
    130            default:
    131                throw new Exception("getFactory(): invalid DataViewType");
    132        }
    133
    134        Class<?> theClass = null;
    135        try {
    136            log.trace("getFactory(): ViewProperties.loadExtClass().loadClass({})", factoryClassName);
    137
    138            /* Attempt to load the class as an external module */
    139            theClass = ViewProperties.loadExtClass().loadClass(factoryClassName);
    140        }
    141        catch (Exception ex) {
    142            log.debug("getFactory(): ViewProperties.loadExtClass().loadClass({}) failure:", factoryClassName, ex);
    143
    144            try {
    145                log.trace("getFactory(): Class.forName({})", factoryClassName);
    146
    147                /* Attempt to load the class directly by the given name */
    148                theClass = Class.forName(factoryClassName);
    149            }
    150            catch (Exception ex2) {
    151                log.debug("getFactory(): Class.forName({}) failure:", factoryClassName, ex);
    152
    153                /* At this point, we have no choice but to fall back to the default modules */
    154                switch (viewType) {
    155                    case TABLE:
    156                        log.trace("getFactory(): returning default TableView factory instance");
    157                        return new DefaultTableViewFactory();
    158                    case IMAGE:
    159                        log.trace("getFactory(): returning default ImageView factory instance");
    160                        return new DefaultImageViewFactory();
    161                    case PALETTE:
    162                        log.trace("getFactory(): returning default PaletteView factory instance");
    163                        return new DefaultPaletteViewFactory();
    164                    case METADATA:
    165                        log.trace("getFactory(): returning default MetaDataView factory instance");
    166                        return new DefaultMetaDataViewFactory();
    167                    case TREEVIEW:
    168                        log.trace("getFactory(): returning default TreeView factory instance");
    169                        return new DefaultTreeViewFactory();
    170                    default:
    171                        throw new Exception("getFactory(): invalid DataViewType");
    172                }
    173            }
    174        }
    175
    176        if (theClass == null) throw new ClassNotFoundException();
    177
    178        try {
    179            theFactory = (DataViewFactory) Tools.newInstance(theClass, null);
    180
    181            log.trace("getFactory(): returning DataViewFactory instance {}", theFactory);
    182        }
    183        catch (Exception ex) {
    184            log.debug("getFactory(): Error instantiating class:", ex);
    185            theFactory = null;
    186        }
    187
    188        return theFactory;
    189    }
    190}