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

📄 interceptormanager.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile$
 * $Revision: 28502 $
 * $Date: 2006-03-13 13:38:47 -0800 (Mon, 13 Mar 2006) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
 */

package org.jivesoftware.xmpp.workgroup.interceptor;

import org.jivesoftware.xmpp.workgroup.Workgroup;
import org.jivesoftware.xmpp.workgroup.WorkgroupManager;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.BeanUtils;
import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Base class for fastpath packet interceptors.
 *
 * @author Gaston Dombiak
 */
public abstract class InterceptorManager {

    private List<PacketInterceptor> availableInterceptors =
            new CopyOnWriteArrayList<PacketInterceptor>();

    private CopyOnWriteArrayList<PacketInterceptor> globalInterceptors =
            new CopyOnWriteArrayList<PacketInterceptor>();
    private ConcurrentHashMap<String, CopyOnWriteArrayList<PacketInterceptor>> localInterceptors =
            new ConcurrentHashMap<String, CopyOnWriteArrayList<PacketInterceptor>>();

    /**
     * Constructs a new InterceptorManager.
     */
    protected InterceptorManager() {
        loadAvailableInterceptors();
        loadGlobalInterceptors();
    }

    /**
     * Returns an unmodifiable list of global packet interceptors. Global
     * interceptors are applied to all packets read and sent by the server.
     *
     * @return an unmodifiable list of the global packet interceptors.
     */
    public List<PacketInterceptor> getInterceptors() {
        return Collections.unmodifiableList(globalInterceptors);
    }

    /**
     * Returns an unmodifiable list of packet interceptors specific for the specified workgroup.
     *
     * @param workgroup the bare JID address of the workgroup.
     * @return an unmodifiable list of the packet interceptors specific for the specified workgroup.
     */
    public List<PacketInterceptor> getInterceptors(String workgroup) {
        List<PacketInterceptor> interceptors = getLocalInterceptors(workgroup);
        if (interceptors == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(interceptors);
    }

    /**
     * Return the global interceptor at the specified index in the list of currently configured
     * interceptors.
     *
     * @param index the index in the list of interceptors.
     * @return the interceptor at the specified index.
     */
    public PacketInterceptor getInterceptor(int index) {
        if (index < 0 || index > globalInterceptors.size()-1) {
            throw new IllegalArgumentException("Index " + index + " is not valid.");
        }
        return globalInterceptors.get(index);
    }

    /**
     * Return the interceptor at the specified index in the list of currently configured
     * interceptors for the specified workgroup.
     *
     * @param workgroup the bare JID address of the workgroup.
     * @param index the index in the list of interceptors.
     * @return the interceptor at the specified index.
     */
    public PacketInterceptor getInterceptor(String workgroup, int index) {
        List<PacketInterceptor> interceptors = getLocalInterceptors(workgroup);
        if (interceptors == null) {
            return null;
        }
        if (index < 0 || (index > interceptors.size())) {
            throw new IndexOutOfBoundsException("Index " + index + " invalid.");
        }
        return interceptors.get(index);
    }

    /**
     * Inserts a new interceptor at the specified index in the list of global interceptors.
     *
     * @param index the index in the list to insert the new global interceptor at.
     * @param interceptor the interceptor to add.
     */
    public void addInterceptor(int index, PacketInterceptor interceptor) {
        if (interceptor == null) {
            throw new NullPointerException("Parameter interceptor was null.");
        }
        if (index < 0 || (index > globalInterceptors.size())) {
            throw new IndexOutOfBoundsException("Index " + index + " invalid.");
        }
        // Remove the interceptor from the list since the position might have changed
        if (globalInterceptors.contains(interceptor)) {
            int oldIndex = globalInterceptors.indexOf(interceptor);
            if (oldIndex < index) {
                index -= 1;
            }
            globalInterceptors.remove(interceptor);
        }

        globalInterceptors.add(index, interceptor);
        saveInterceptors();
    }

    /**
     * Inserts a new interceptor at specified index in the list of currently configured
     * interceptors for the specified workgroup.
     *
     * @param workgroup the bare JID address of the workgroup.
     * @param index the index in the list to insert the new interceptor at.
     * @param interceptor the interceptor to add.
     */
    public void addInterceptor(String workgroup, int index, PacketInterceptor interceptor) {
        if (interceptor == null) {
            throw new NullPointerException("Parameter interceptor was null.");
        }
        List<PacketInterceptor> interceptors = getLocalInterceptors(workgroup);
        interceptors =
                localInterceptors.putIfAbsent(workgroup,
                        new CopyOnWriteArrayList<PacketInterceptor>());
        if (interceptors == null) {
            interceptors = localInterceptors.get(workgroup);
        }
        if (index < 0 || (index > interceptors.size())) {
            throw new IndexOutOfBoundsException("Index " + index + " invalid.");
        }
        // Remove the interceptor from the list since the position might have changed
        if (interceptors.contains(interceptor)) {
            int oldIndex = interceptors.indexOf(interceptor);
            if (oldIndex < index) {
                index -= 1;
            }
            interceptors.remove(interceptor);
        }

        interceptors.add(index, interceptor);
        saveInterceptors();
    }

    /**
     * Removes the global interceptor from the list.
     *
     * @param interceptor the global interceptor to remove.
     * @return true if the item was present in the list
     */
    public boolean removeInterceptor(PacketInterceptor interceptor) {
        boolean answer = globalInterceptors.remove(interceptor);
        saveInterceptors();
        return answer;
    }

    /**
     * Removes the interceptor for the specified workgroup from the list.
     *
     * @param workgroup the bare JID address of the workgroup.
     * @param interceptor the interceptor to remove.
     * @return true if the item was present in the list
     */
    public boolean removeInterceptor(String workgroup, PacketInterceptor interceptor) {
        List interceptors = getLocalInterceptors(workgroup);
        if (interceptors == null) {
            return false;
        }
        boolean answer = interceptors.remove(interceptor);
        saveInterceptors();
        return answer;
    }

    /**
     * Invokes all currently-installed interceptors on the specified packet.
     * All global interceptors will be invoked as well as interceptors that
     * are related to the specified workgroup.<p>
     *
     * Interceptors are executed before and after processing an incoming packet
     * and sending a packet to a user. This means that interceptors are able to alter or
     * reject packets before they are processed further. If possible, interceptors
     * should perform their work in a short time so that overall performance is not
     * compromised.
     *
     * @param workgroup the bare JID address of the workgroup.
     * @param packet the packet that has been read or is about to be sent.
     * @param read true indicates that the packet was read. When false, the packet
     *      is being sent to a user.
     * @param processed true if the packet has already processed (incoming or outgoing).
     *      If the packet hasn't already been processed, this flag will be false.
     * @throws PacketRejectedException if the packet should be prevented from being processed.
     */
    public void invokeInterceptors(String workgroup, Packet packet, boolean read,
            boolean processed) throws PacketRejectedException
    {
        // Invoke the global interceptors for this packet
        for (PacketInterceptor interceptor : globalInterceptors) {
            try {
                interceptor.interceptPacket(workgroup, packet, read, processed);
            }
            catch (PacketRejectedException e) {
                if (processed) {
                    Log.error("Post interceptor cannot reject packet.", e);
                }
                else {
                    // Throw this exception since we don't really want to catch it
                    throw e;
                }
            }
            catch (Exception e) {
                Log.error("Error in interceptor", e);
            }
        }
        // Invoke the interceptors that are related to the specified workgroup
        List<PacketInterceptor> interceptors = getLocalInterceptors(workgroup);
        if (interceptors != null) {
            for (PacketInterceptor interceptor : interceptors) {
                try {
                    interceptor.interceptPacket(workgroup, packet, read, processed);
                }
                catch (PacketRejectedException e) {
                    if (processed) {
                        Log.error("Post interceptor cannot reject packet.", e);
                    }
                    else {
                        // Throw this exception since we don't really want to catch it
                        throw e;
                    }
                }
                catch (Exception e) {
                    Log.error("Error in interceptor", e);
                }
            }
        }
    }

    public synchronized void saveInterceptors() {
        // Delete all global interceptors stored as properties.
        JiveGlobals.deleteProperty("interceptor.global." + getPropertySuffix());

        // Delete all the workgroup interceptors stored as properties.
        for (String jid : localInterceptors.keySet()) {
            try {
                Workgroup workgroup = WorkgroupManager.getInstance().getWorkgroup(new JID(jid));

                Collection<String> propertyNames = workgroup.getProperties().getPropertyNames();
                for (String propertyName : propertyNames) {
                   if (propertyName.startsWith("jive.interceptor." + getPropertySuffix())) {
                       workgroup.getProperties().deleteProperty(propertyName);
                   }
                }
            }
            catch (Exception e) {
               Log.error(e);

⌨️ 快捷键说明

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