⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q2.java

📁 POS is a Java&#174 platform-based, mission-critical, ISO-8583 based financial transaction library/fr
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2008 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. */package org.jpos.q2;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileFilter;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.security.GeneralSecurityException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.TreeMap;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import javax.management.InstanceAlreadyExistsException;import javax.management.InstanceNotFoundException;import javax.management.MBeanRegistrationException;import javax.management.MBeanServer;import javax.management.MBeanServerFactory;import javax.management.MalformedObjectNameException;import javax.management.NotCompliantMBeanException;import javax.management.ObjectInstance;import javax.management.ObjectName;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.HelpFormatter;import org.apache.commons.cli.Options;import org.apache.commons.cli.PosixParser;import org.apache.commons.cli.MissingArgumentException;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import org.jpos.iso.ISOException;import org.jpos.iso.ISOUtil;import org.jpos.util.Log;import org.jpos.util.Logger;import org.jpos.util.SimpleLogListener;/** * @author <a href="mailto:taherkordy@dpi2.dpi.net.ir">Alireza Taherkordi</a> * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a> * @author <a href="mailto:alwynschoeman@yahoo.com">Alwyn Schoeman</a> * @version $Revision: 2621 $ $Date: 2008-05-01 19:05:53 -0300 (Thu, 01 May 2008) $ */public class Q2 implements FileFilter, Runnable {    public static final String Q2_VERSION          = "@version@";    public static final String Q2_REVISION         = "@revision@";    public static final String Q2_DATE             = "@date@";    public static final String DEFAULT_DEPLOY_DIR  = "deploy";    public static final String JMX_NAME            = "Q2";    public static final String LOGGER_NAME         = "Q2";    public static final String REALM               = "Q2.system";     public static final String LOGGER_CONFIG       = "00_logger.xml";    public static final String QBEAN_NAME          = "Q2:type=qbean,service=";    public static final String Q2_CLASS_LOADER     = "Q2:type=system,service=loader";    public static final String DUPLICATE_EXTENSION = "DUP";    public static final String ERROR_EXTENSION     = "BAD";    public static final String ENV_EXTENSION       = "ENV";    public static final String PROTECTED_QBEAN        = "protected-qbean";    public static final int SCAN_INTERVAL             = 2500;    public static final long SHUTDOWN_TIMEOUT         = 60000;    private MBeanServer server;    private File deployDir, libDir;    private Map dirMap;    private QFactory factory;    private QClassLoader loader;    private ClassLoader mainClassLoader;    private Log log;    private boolean shutdown;    private boolean shuttingDown;    private Thread q2Thread;    private String[] args;    private boolean hasSystemLogger;    private boolean exit;    private long startTime;    private CLI cli;    private boolean recursive;    public Q2 (String[] args) {        super();        this.args = args;        parseCmdLine (args);        this.libDir     = new File (deployDir, "lib");        this.dirMap     = new TreeMap ();        deployDir.mkdirs ();        startTime = System.currentTimeMillis();        mainClassLoader = Thread.currentThread().getContextClassLoader();    }    public Q2 () {        this (new String[] {} );    }    public void run() {        try {            start();        } catch (Exception e) {            e.printStackTrace();        }    }    public void start ()         throws MalformedObjectNameException,               InstanceAlreadyExistsException,               NotCompliantMBeanException,               MBeanRegistrationException    {        /*         * The following code determines whether a MBeanServer exists already.         * If so then the first one in the list is used.  I have not yet find a way to         * interrogate the server for information other than MBeans so to pick a         * specific one would be difficult.         */        ArrayList mbeanServerList = MBeanServerFactory.findMBeanServer(null);        if (mbeanServerList.size() == 0) {            server  = MBeanServerFactory.createMBeanServer (JMX_NAME);        } else {            server = (MBeanServer) mbeanServerList.get(0);        }        final ObjectName loaderName = new ObjectName (Q2_CLASS_LOADER);        try {            loader = (QClassLoader) java.security.AccessController.doPrivileged(                new java.security.PrivilegedAction() {                    public Object run() {                        return new QClassLoader (server, libDir, loaderName, mainClassLoader);                    }                }            );            server.registerMBean (loader, loaderName);            loader = loader.scan(false);        } catch (Throwable t) {            if (log != null)                log.error ("initial-scan", t);            else                t.printStackTrace();        }        factory = new QFactory (loaderName, this);        initSystemLogger ();        addShutdownHook ();        q2Thread = Thread.currentThread ();        q2Thread.setContextClassLoader (loader);        if (cli != null)            cli.start();        while (!shutdown) {            try {                boolean forceNewClassLoader = scan ();                QClassLoader oldClassLoader = loader;                loader = loader.scan (forceNewClassLoader);                if (loader != oldClassLoader) {                    oldClassLoader = null;                    System.gc();  // force a GC                    log.info (                      "new classloader ["                      + Integer.toString(loader.hashCode(),16)                      + "] has been created"                    );                }                deploy ();                checkModified ();                relax (SCAN_INTERVAL);            } catch (Throwable t) {                log.error ("start", t);                relax ();            }        }        undeploy ();        try {            server.unregisterMBean (loaderName);        } catch (InstanceNotFoundException e) {            log.error (e);        }        if (exit && !shuttingDown)            System.exit (0);    }    public void shutdown () {        shutdown(false);    }    public boolean running() {        return !shutdown;    }    public void shutdown (boolean join) {        shutdown = true;        if (q2Thread != null) {            log.info ("shutting down");            q2Thread.interrupt ();            if (join) {                try {                    q2Thread.join();                    log.info ("shutdown done");                } catch (InterruptedException e) {                    log.warn (e);                }            }        }        q2Thread = null;    }    public QClassLoader getLoader () {        return loader;    }    public QFactory getFactory () {        return factory;    }    public String[] getCommandLineArgs() {        return args;    }    public boolean accept (File f) {        return f.canRead() &&             (f.getName().endsWith (".xml") ||              (recursive && f.isDirectory() && !"lib".equalsIgnoreCase (f.getName())));    }    public File getDeployDir () {        return deployDir;    }    private boolean scan () {            boolean rc = false;            File file[] = deployDir.listFiles (this);            // Arrays.sort (file); --apr not required - we use TreeMap            if (file == null) {                // Shutting down might be best, how to trigger from within?                throw new Error("Deploy directory \""+deployDir.getAbsolutePath()+"\" is not available");            } else {                for (int i=0; i<file.length; i++) {                    if (register (file[i]))                        rc = true;                }            }            return rc;    }    private void deploy () {        List startList = new ArrayList ();        Iterator iter = dirMap.entrySet().iterator();        try {            while (iter.hasNext() && !shutdown) {                Map.Entry entry = (Map.Entry) iter.next();                File   f        = (File)   entry.getKey ();                QEntry qentry   = (QEntry) entry.getValue ();                long deployed   = qentry.getDeployed ();                if (deployed == 0) {                    if (deploy (f)) {                        if (qentry.isQBean ())                            startList.add (qentry.getInstance());                        qentry.setDeployed (f.lastModified ());                    } else {                        // deploy failed, clean up.                        iter.remove();                    }                } else if (deployed != f.lastModified ()) {                    undeploy (f);                    iter.remove ();                    loader.forceNewClassLoaderOnNextScan();                }            }            iter = startList.iterator();            while (iter.hasNext ()) {                start ((ObjectInstance) iter.next ());            }        }        catch (Exception e){            log.error ("deploy", e);        }    }    private void undeploy () {        Object[] set = dirMap.entrySet().toArray ();        int l = set.length;        while (l-- > 0) {            Map.Entry entry = (Map.Entry) set[l];            File   f  = (File) entry.getKey ();            undeploy (f);        }    }    private void addShutdownHook () {        Runtime.getRuntime().addShutdownHook (            new Thread ("Q2-ShutdownHook") {                public void run () {                    shuttingDown = true;                    shutdown = true;                    if (q2Thread != null) {                        log.info ("shutting down (hook)");                        try {                            q2Thread.join (SHUTDOWN_TIMEOUT);                        } catch (InterruptedException e) {                         } catch (NullPointerException e) {                            // on thin Q2 systems where shutdown is very fast,                             // q2Thread can become null between the upper if and                            // the actual join. Not a big deal so we ignore the                            // exception.                        }                    }                }            }        );    }    private void checkModified () {        Iterator iter = dirMap.entrySet().iterator();        while (iter.hasNext()) {            Map.Entry entry = (Map.Entry) iter.next();            File   f        = (File)   entry.getKey ();            QEntry qentry   = (QEntry) entry.getValue ();            if (qentry.isQBean() && qentry.isQPersist()) {                ObjectName name = qentry.getObjectName ();                if (getState (name) == QBean.STARTED && isModified (name)) {                    qentry.setDeployed (persist (f, name));                }            }        }    }    private int getState (ObjectName name) {        int status = -1;        if (name != null) {            try {                status = (                    (Integer) server.getAttribute (name, "State")                ).intValue();            } catch (Exception e) {                log.warn ("getState", e);            }        }        return status;    }    private boolean isModified (ObjectName name) {        boolean modified = false;        if (name != null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -