001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.plugins;
020
021/**
022 * Dummie Class Loader, only to get the Class Name in a simple way
023 * @author Carlos Ferreira
024 */
025public class PluginPanelLoader extends ClassLoader
026{
027
028    private static PluginPanelLoader instance = null;
029
030    private PluginPanelLoader(ClassLoader parent)
031    {
032        super(parent);
033    }
034
035    private PluginPanelLoader()
036    {
037        super(getSystemClassLoader());
038    }
039
040    private static synchronized PluginPanelLoader getInstance()
041    {
042        if (instance == null)
043        {
044            instance = new PluginPanelLoader();
045        }
046        return instance;
047    }
048
049    private static synchronized PluginPanelLoader getInstance(ClassLoader cl)
050    {
051        if (instance == null)
052        {
053            instance = new PluginPanelLoader(cl);
054        }
055        return instance;
056    }
057
058   /* @Override
059    public Class<?> loadClass(String name)
060    {
061        try
062        {
063            IPluginControllerAdmin plugins = AdminRefs.getInstance().getPluginController();
064            HashMap<String, byte[]> panelClasses = plugins.getPanelClasses();
065            Set<String> keys = panelClasses.keySet();
066            for (String key : keys)
067            {
068                byte[] pp = panelClasses.get(key);
069
070                ClassLoader cl = PluginPanel.class.getClassLoader();
071                Class c = defineClass(pp);
072
073                return c;
074            }
075        } catch (RemoteException ex)
076        {
077            LoggerFactory.getLogger(PluginPanelLoader.class).error(ex.getMessage(), ex);
078        }
079        return null;
080    }
081
082    @Override
083    public Class<?> loadClass(String name, boolean resolve)
084    {
085        try
086        {
087            IPluginControllerAdmin plugins = AdminRefs.getInstance().getPluginController();
088            HashMap<String, byte[]> panelClasses = plugins.getPanelClasses();
089            Set<String> keys = panelClasses.keySet();
090            for (String key : keys)
091            {
092                byte[] pp = panelClasses.get(key);
093
094                //ClassLoader cl = PluginPanel.class.getClassLoader();
095                Class c = defineClass(pp);
096
097                return c;
098            }
099        } catch (RemoteException ex)
100        {
101            LoggerFactory.getLogger(PluginPanelLoader.class).error(ex.getMessage(), ex);
102        }
103        return null;
104    }*/
105
106    public Class defineClass(byte[] b)
107    {
108        Class c = defineClass(null, b, 0, b.length);
109        //System.out.println(c.getName().substring(0, c.getName().lastIndexOf('.')));
110        //super.definePackage(c.getPackage().getName(), c.getPackage().getSpecificationTitle(), c.getPackage().getSpecificationVersion(), c.getPackage().getSpecificationVendor(),
111        //        c.getPackage().getImplementationTitle(), c.getPackage().getImplementationVersion(), c.getPackage().getImplementationVendor(), null);
112        super.resolveClass(c);
113        //System.out.println(c.getClassLoader().toString());
114        return c;
115        /*     try
116        {
117        this.loadClass(c.getName());
118        System.out.println(c.getName());
119        } catch (ClassNotFoundException ex)
120        {
121        LoggerFactory.getLogger(ServerOptions.class).error(ex.getMessage(), ex);
122        }*/
123    }
124}