All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pekinsoft.lookup.DefaultLookup Maven / Gradle / Ivy

Go to download

A simple platform on which Java/Swing desktop applications may be built. This updated version has packaged the entire library into a single JAR file. We have also made the following changes: ToolBarGenerator should now create ButtonGroups properly for state actions. ApplicationContext has accessors for the WindowManager, DockingManager, StatusDisplayer, and ProgressHandler implementations. It defaults to testing the Application's MainView and the MainView's StatusBar, then uses the Lookup, if the MainView and its StatusBar do not implement the desired interfaces. StatusMessage now uses the com.pekinsoft.desktop.error.ErrorLevel instead of the java.util.logging.Level, so that the levels will no longer need to be cast in order to be used.

The newest version!
/*
 * Copyright (C) 2024 PekinSOFT Systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 * 
 * *****************************************************************************
 *  Project    :   application-framework-api
 *  Class      :   DefaultLookup.java
 *  Author     :   Sean Carrick
 *  Created    :   Oct 25, 2024
 *  Modified   :   Oct 25, 2024
 *  
 *  Purpose: See class JavaDoc for explanation
 *  
 *  Revision History:
 *  
 *  WHEN          BY                   REASON
 *  ------------  -------------------  -----------------------------------------
 *  Oct 25, 2024  Sean Carrick         Initial creation.
 * *****************************************************************************
 */

package com.pekinsoft.lookup;

import com.pekinsoft.framework.Application;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sean Carrick <sean at pekinsoft dot com>
 * 
 * @version 1.0
 * @since 1.0
 */
public class DefaultLookup extends Lookup {
    
    private static DefaultLookup instance = null;
    private static Logger logger = Application.getInstance()
            .getContext().getLogger(DefaultLookup.class);
    
    public static Lookup getInstance() {
        if (instance == null) {
            instance = new DefaultLookup();
        }
        
        return instance;
    }
    
    public DefaultLookup () {
        
    }

    @Override
    public  T lookup(Class type, boolean refreshCache) {
        List providers = (List) lookupAll(type, refreshCache);
        return providers.isEmpty() ? null : providers.get(0);
    }

    @Override
    public  Collection lookupAll(Class type, boolean refreshCache) {
        if (refreshCache || !cache.containsKey(type)) {
            loadProviders(type);
        }
        return (Collection) cache.getOrDefault(type, Collections.emptyList());
    }
    
    private  void loadProviders(Class type) {
        List providers = new ArrayList<>();
        List> services = registry.get(type);
        
        if (services != null) {
            for (Class service : services) {
                try {
                    T instantiated = null;
                    try {
                        Method getInstance = service.getDeclaredMethod("getInstance");
                        instantiated = type.cast(getInstance.invoke(null));
                    } catch (NoSuchMethodException nsm) {
                        instantiated = type.cast(service.getDeclaredConstructor()
                                .newInstance());
                    }
                    if (instantiated != null) {
                        providers.add(instantiated);
                    }
                } catch (IllegalAccessException 
                        | IllegalArgumentException 
                        | InstantiationException 
                        | NoSuchMethodException 
                        | SecurityException 
                        | InvocationTargetException e) {
                    logger.log(Level.WARNING, "Could not initialize provider: {0}",
                            service.getName());
                }
            }
            cache.put(type, providers);
            logger.log(Level.FINER, "Providers registered in the cache: {0}", cache);
        }
    }

}