001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2;
020
021import javax.management.*;
022import java.io.File;
023import java.io.FileFilter;
024import java.net.MalformedURLException;
025import java.net.URL;
026import java.net.URLClassLoader;
027import java.security.PrivilegedAction;
028
029/**
030 * Q2 Class Loader (scans deploy/lib directory for new jars)
031 *
032 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
033 * @author <a href="mailto:taherkordy@dpi2.dpi.net.ir">Alireza Taherkordi</a>
034 * @version $Revision$ $Date$
035 */
036@SuppressWarnings("deprecation")
037public class QClassLoader
038    extends URLClassLoader 
039    implements QClassLoaderMBean, FileFilter {
040    File libDir;
041    ObjectName loaderName;
042    MBeanServer server;
043    long lastModified;
044
045    public QClassLoader 
046        (MBeanServer server, File libDir, ObjectName loaderName, 
047         ClassLoader mainClassLoader) 
048    {
049        super(new URL[] { }, mainClassLoader);
050        this.loaderName = loaderName;
051        this.libDir     = libDir;
052        this.server     = server;
053    }
054    
055    public void addURL (String url) throws MalformedURLException {
056        addURL (new URL (url));
057    }
058    
059    public boolean accept (File f) {
060        return f.getName().endsWith (".jar");
061    }
062
063    public boolean isModified () {
064        return libDir.canRead () && lastModified != libDir.lastModified();
065    }
066    public QClassLoader scan (boolean forceNewClassLoader) 
067        throws InstanceAlreadyExistsException,
068               InstanceNotFoundException,
069               NotCompliantMBeanException,
070               MBeanRegistrationException
071    
072    {
073        if (!isModified () && !forceNewClassLoader || !libDir.canRead())
074            return this;
075        QClassLoader loader;
076        if (server.isRegistered (loaderName)) {
077            server.unregisterMBean (loaderName);
078            loader = new QClassLoader(server, libDir, loaderName, getParent());
079        } else
080            loader = this;
081
082        File file[] = libDir.listFiles (this);
083        for (File aFile : file) {
084            try {
085                loader.addURL(aFile.toURL());
086            } catch (MalformedURLException e) {
087                e.printStackTrace();
088            }
089        }
090        loader.lastModified = libDir.lastModified ();
091        server.registerMBean (loader, loaderName);
092        return loader;
093    }
094    public void forceNewClassLoaderOnNextScan() {
095        this.lastModified = 0L;
096    }
097}
098