📄 q2.java
字号:
/* * Copyright (c) 2005 jPOS.org. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the jPOS project * (http://www.jpos.org/)". Alternately, this acknowledgment may * appear in the software itself, if and wherever such third-party * acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse * or promote products derived from this software without prior * written permission. For written permission, please contact * license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", * nor may "jPOS" appear in their name, without prior written * permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project. For more * information please see <http://www.jpos.org/>. */package org.jpos.q2;import java.io.File;import java.io.FileFilter;import java.io.FileWriter;import java.io.IOException;import java.io.ByteArrayOutputStream;import java.io.ByteArrayInputStream;import java.io.OutputStreamWriter;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.TreeMap;import java.util.Iterator;import javax.management.ObjectName;import javax.management.ObjectInstance;import javax.management.NotCompliantMBeanException;import javax.management.MBeanRegistrationException;import javax.management.MalformedObjectNameException;import javax.management.InstanceAlreadyExistsException;import javax.management.InstanceNotFoundException;import javax.management.MBeanServer;import javax.management.MBeanServerFactory;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.security.GeneralSecurityException;import org.jdom.Element;import org.jdom.Document;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import org.apache.commons.cli.Options;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.HelpFormatter;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.PosixParser;import org.jpos.util.Log;import org.jpos.util.Logger;import org.jpos.util.SimpleLogListener;import org.jpos.iso.ISOUtil;import org.jpos.iso.ISOException;/** * @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: 1.17 $ $Date: 2005/08/16 17:30:04 $ */public class Q2 implements FileFilter { public static final String Q2_VERSION = "XX_Q2_VERSION_XX"; 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 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 Log log; private boolean shutdown; private boolean shuttingDown; private Thread q2Thread; private String[] args; private boolean hasSystemLogger; public Q2 (String[] args) { super(); this.args = args; parseCmdLine (args); this.libDir = new File (deployDir, "lib"); this.dirMap = new TreeMap (); deployDir.mkdirs (); } 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); } ObjectName loaderName = new ObjectName (Q2_CLASS_LOADER); try { loader = new QClassLoader (server, libDir, loaderName); server.registerMBean (loader, loaderName); loader = loader.scan(); } catch (Throwable t) { log.error ("initial-scan", t); } factory = new QFactory (loaderName, this); initSystemLogger (); addShutdownHook (); q2Thread = Thread.currentThread (); q2Thread.setContextClassLoader (loader); while (!shutdown) { try { loader = loader.scan (); scan (); deploy (); checkModified (); relax (SCAN_INTERVAL); } catch (Throwable t) { log.error ("start", t); relax (); } } q2Thread = null; undeploy (); try { server.unregisterMBean (loaderName); } catch (InstanceNotFoundException e) { log.error (e); } if (!shuttingDown) System.exit (0); } public void shutdown () { shutdown = true; q2Thread.interrupt (); } public QClassLoader getLoader () { return loader; } public QFactory getFactory () { return factory; } public String[] getCommandLineArgs() { return args; } public boolean accept (File f) { return f.getName().endsWith (".xml"); } public File getDeployDir () { return deployDir; } private void scan () { File file[] = deployDir.listFiles (this); // Arrays.sort (file); --apr not required - we use TreeMap for (int i=0; i<file.length; i++) { register (file[i]); } } private void deploy () { List startList = new ArrayList (); Iterator iter = dirMap.entrySet().iterator(); try { while (iter.hasNext()) { 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 (); } } iter = startList.iterator(); while (iter.hasNext ()) { start ((ObjectInstance) iter.next ()); } } catch (Exception e){ log.warn ("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 () { public void run () { shuttingDown = true; shutdown = true; log.info ("shutting down"); if (q2Thread != null) { try { q2Thread.join (SHUTDOWN_TIMEOUT); } catch (InterruptedException e) { } } } } ); } 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -