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

📄 jahialistenersregistry.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//////  JahiaListenersRegistry//  EV      25.11.2000//  MJ      23.03.2001  fixed addListener, removeListener, getListener////  init//  getAllListeners//  getListenerByName( listenerName )//  getListener( listenerClassName )//  addListener( lName, lClassName )//  removeListener( lName )//  wakeupListeners( methodName,2 event )//package org.jahia.registries;import java.io.*;                               // Streamsimport java.util.*;					            // Enumeration, Vector, Hashtable, Propertiesimport java.lang.reflect.*;                     // Method objectimport javax.servlet.*;					        // ServletContextimport org.jahia.utils.*;                   // JahiaConsoleimport org.jahia.data.events.*;             // JahiaEventListenerInterfaceimport org.jahia.services.*;                // ServicesRegistryimport org.jahia.exceptions.JahiaException; // JahiaExceptionpublic class JahiaListenersRegistry {    private static          JahiaListenersRegistry  theObject           = null;    /**     * @associates JahiaEventListenerInterface     */    private                 Hashtable               theRegistry         = null;    /**     * @associates String     */    private                 Hashtable               myTable             = null;    private                 boolean                 initialized         = false;    private static  final   String                  REGISTRY_FILENAME   = "listeners.registry";    private                 String                  registryFile        = null;    private                 Properties              registryProperties  = null;    /***        * constructor        *        */    private JahiaListenersRegistry()    {        JahiaConsole.println( "JahiaListenersRegistry", "Starting up..." );    } // end constructor    /***        * returns a single instance of the registry        *        */    public static synchronized JahiaListenersRegistry getInstance()    {        if (theObject == null) {            theObject = new JahiaListenersRegistry();        }        return theObject;    } // end getInstance    /***        * init        *        * @param        config              a ServletConfig object, with "homedir" property        *        */    public void init( ServletConfig config )    {        if (!initialized) {            theRegistry = new Hashtable();            myTable     = new Hashtable();            JahiaConsole.println( "JahiaListernersRegistry.init", "Start Loading" );            initialized = loadAllListenersFromFile( config );            JahiaConsole.println( "JahiaListernersRegistry.init", "Ended Loading" );        }    } // end init    /***        * get all listeners in registry        *        * @return       a vector of JahiaEventListenerInterface objects        *        */    public Vector getAllListeners()    {        Vector out = new Vector();        if (theRegistry.size() > 0) {            Enumeration keys = theRegistry.keys();            while (keys.hasMoreElements()) {                String theKey = (String) keys.nextElement();                out.add( (JahiaEventListenerInterface) theRegistry.get( theKey ) );            }        }        return out;    } // end getAllListeners    /***        * gets a listener by its name        * @author       MJ        *        * @param        listenerName        the listener name        * @return       a JahiaEventListenerInterface object        *        */    public JahiaEventListenerInterface getListenerByName ( String listenerName )    {        String listenerClassName = (String) myTable.get(listenerName);        return (JahiaEventListenerInterface ) this.getListener(listenerClassName);    } // end getListenerByName    /***        * gets a listener by its class name        *        * @param        listenerClassName        the listener name        * @return       a JahiaEventListenerInterface object        *        */    public JahiaEventListenerInterface getListener( String listenerClassName )    {        return (JahiaEventListenerInterface) theRegistry.get( listenerClassName );    } // end getListener    /***        * hot-plugs a listener into the registry        *        * @param        lName               the listener name        * @param        lClassName          the listener class name (with full package path)        * @return       true if everything went okay, false if not        *        */    public synchronized boolean addListener( String lName, String lClassName )    {        boolean out = false;        JahiaEventListenerInterface theListener = invokeListener( lClassName );        if (theListener != null) {            theRegistry.put( lClassName, theListener );            registryProperties.setProperty( lName, lClassName );            if (saveRegistryInFile()) {                out = true;            }        }        return out;    } // end addListener    /***        * hot-removes a listener from the registry        *        * @param        lName          the name of listener to remove        * @return       true if everything went okay, false if not        *        */    public synchronized boolean removeListener( String lName )    {        boolean out = false;        String lClassName = (String)myTable.get( lName );        theRegistry.remove( lClassName );        registryProperties.remove( lName );        if (saveRegistryInFile()) {            out = true;        }        return out;    } // end removeListener    /***        * wakes up a specific method of all listeners        *        * @param        methodName          method name to ring        * @param        theEvent            the event to send        *        */    public void wakeupListeners( String methodName, JahiaEvent theEvent )    throws JahiaException    {        try {            Vector listeners = getAllListeners();            if (listeners.size() > 0)  {                for (int i=0; i < listeners.size(); i++) {                    JahiaEventListenerInterface theListener = (JahiaEventListenerInterface) listeners.elementAt(i);                    Class theClass = theListener.getClass();                    Class eventClass = theEvent.getClass();                    Method theMethod = theClass.getMethod( methodName, new Class[] { eventClass } );                    if (theMethod != null) {                         theMethod.invoke( theListener, new Object[] { (org.jahia.data.events.JahiaEvent)theEvent } );                    }                }            }        } catch (NoSuchMethodException nsme)  {            String errorMsg = "NoSuchMethodException when trying to execute method " + methodName + "(" + nsme.getMessage() + ")";            JahiaConsole.println( "JahiaListenersRegistry", errorMsg );            throw new JahiaException( "NoSuchMethodException",                                     errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, nsme );        } catch (InvocationTargetException ite)  {            String errorMsg = "InvocationTargetException when trying to execute method " + methodName + "(" + ite.getTargetException().getMessage() + ")";            JahiaConsole.println( "JahiaListenersRegistry", errorMsg );            throw new JahiaException( "InvocationTargetException",                                     errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, ite.getTargetException() );        } catch (IllegalAccessException iae)  {            String errorMsg = "IllegalAccessException when trying to execute method " + methodName + "(" + iae.getMessage() + ")";            JahiaConsole.println( "JahiaListenersRegistry", errorMsg );            throw new JahiaException( "IllegalAccessException",                                     errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, iae );        }    } // end wakeupListener    /***        * loads all the listeners from a properties file        *        * @param        config              a ServletConfig object, with "homedir" property        * @return       true if load was ok, false if problems encountered        *        */    private boolean loadAllListenersFromFile( ServletConfig config )    {        this.registryFile = config.getServletContext().getRealPath(                                config.getInitParameter( "webinf_path" ) + "etc/config/" + REGISTRY_FILENAME );        this.registryProperties = new Properties();        try {            FileInputStream in = new FileInputStream( registryFile );            registryProperties.load(in);            Enumeration keys = registryProperties.propertyNames();            while (keys.hasMoreElements()) {                String theKey = (String) keys.nextElement();                String listenerName = registryProperties.getProperty( theKey );                if (listenerName != null) {                    listenerName = listenerName.trim();                    // add the key/classname pair to our internal table                    myTable.put(theKey, listenerName);                    // invoke the listener itself...                    JahiaEventListenerInterface theListener = invokeListener( listenerName );                    if (theListener != null) {                        theRegistry.put( listenerName, theListener );                        JahiaConsole.println( "JahiaListenersRegistry", "Loading " + theKey );                    }                }            }            return true;        } catch (NullPointerException npe) {            return createRegistryFile();        } catch (java.io.IOException ioe) {            return createRegistryFile();        }    } // end loadAllListenersFromFile    /***        * invokes a listener with its class name        *        * @param        className           the listener's class name        * @return       a JahiaEventListenerInterface instance        *        */    private JahiaEventListenerInterface invokeListener( String className )    {        JahiaEventListenerInterface theListener = null;        Class listenerClass = null;        try {            listenerClass = Class.forName( className );            theListener = (JahiaEventListenerInterface) listenerClass.newInstance();        } catch ( ClassNotFoundException cnfe ) {            String errorMsg = "ClassNotFound when trying to load " + className + "(" + cnfe.getMessage() + ")";            JahiaException je = new JahiaException( "ClassNotFoundException",                                     errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, cnfe );        } catch ( InstantiationException cie ) {            String errorMsg = "InstantiationException when trying to load " + className + "(" + cie.getMessage() + ")";            JahiaException je = new JahiaException( "InstanciationException",                                     errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, cie );        } catch ( IllegalAccessException iae){            String errorMsg = "IllegalAccessException when trying to load " + className + "(" + iae.getMessage() + ")";            JahiaException je = new JahiaException( "IllegalAccessException",                            errorMsg, JahiaException.LISTENER_ERROR, JahiaException.WARNING, iae );        }        return theListener;    } // end invokeListener    /***        * creates the registry file        *        * @return       true if file created, false if problems encountered        *        */    private synchronized boolean createRegistryFile()    {        try {            JahiaConsole.println("JahiaListenersRegistry.createRegistryFile","Creating registry file" );            FileOutputStream out = new FileOutputStream( registryFile );            registryProperties.store(out, "// Jahia Listeners Registry");            out.close();            return true;        } catch (java.io.IOException e) {            String errorMsg = "Cannot create registry file " + registryFile + " !";            JahiaException je = new JahiaException(   "Cannot create registry file !",                                    errorMsg, JahiaException.FILE_ERROR, JahiaException.WARNING, e );            return false;        }    } // end createRegistryFile    /***        * saves all the listeners into a properties file        *        * @return       true if save was ok, false if problems encountered        *        */    private synchronized boolean saveRegistryInFile()    {        try {            FileOutputStream out = new FileOutputStream( registryFile );            registryProperties.store( out, "// Jahia Listeners Registry" );            //registryProperties.list( new PrintStream(out) );            out.close();            return true;        } catch (java.io.IOException e) {            String errorMsg = "Cannot save registry file " + registryFile + " !";            JahiaException je = new JahiaException(   "Cannot save registry file !",                                    errorMsg, JahiaException.FILE_ERROR, JahiaException.WARNING, e );            return false;        }    } // end saveRegistryInFile} // end JahiaListenersRegistry

⌨️ 快捷键说明

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