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

📄 interceptormanager.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
           }
        }

        // Save the global interceptors as system properties
        JiveGlobals.setProperties(getPropertiesMap(globalInterceptors,
                "interceptor.global." + getPropertySuffix() + "."));

        // Save all the workgroup interceptors as properties of the workgroups.
        for (String jid : localInterceptors.keySet()) {
            try {
                Workgroup workgroup = WorkgroupManager.getInstance().getWorkgroup(new JID(jid));
                Map propertyMap = getPropertiesMap(localInterceptors.get(jid),
                        "jive.interceptor." + getPropertySuffix() + ".");
                for (Iterator i=propertyMap.keySet().iterator(); i.hasNext(); ) {
                    String name = (String)i.next();
                    String value = (String)propertyMap.get(name);
                    workgroup.getProperties().setProperty(name, value);
                }
            }
            catch (Exception e) {
               Log.error(e);
           }
        }
    }

    private Map getPropertiesMap(List<PacketInterceptor> interceptors, String context) {
        // Build the properties map that will be saved later
        Map propertyMap = new HashMap();

        if (!interceptors.isEmpty()) {
            propertyMap.put(context + "interceptorCount", String.valueOf(interceptors.size()));
        }

        // Now write them out again
        for (int i = 0; i < interceptors.size(); i++) {
            PacketInterceptor interceptor = interceptors.get(i);
            String interceptorContext = context + "interceptor" + i + ".";

            // Write out class name
            propertyMap.put(interceptorContext + "className", interceptor.getClass().getName());

            // Write out all properties
            Map interceptorProps = BeanUtils.getProperties(interceptor);
            for (Iterator iter=interceptorProps.keySet().iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                String value = (String) interceptorProps.get(name);
                if (value != null && !"".equals(value)) {
                    propertyMap.put(interceptorContext + "properties." + name, value);
                }
            }
        }
        return propertyMap;
    }

    /**
     * Installs a new class into the list of available interceptors for the system. Exceptions
     * are thrown if the class can't be loaded from the classpath, or the class isn't an instance
     * of PacketInterceptor.
     *
     * @param newClass the class to add to the list of available filters in the system.
     * @throws IllegalArgumentException if the class is not a filter or could not be instantiated.
     */
    public synchronized void addInterceptorClass(Class newClass) throws IllegalArgumentException
    {
        try {
            PacketInterceptor newInterceptor = (PacketInterceptor) newClass.newInstance();
            // Make sure the interceptor isn't already in the list.
            for (PacketInterceptor interceptor : availableInterceptors) {
                if (newInterceptor.getClass().equals(interceptor.getClass())) {
                    return;
                }
            }
            // Add in the new interceptor
            availableInterceptors.add(newInterceptor);
            // Write out new class names.
            JiveGlobals.deleteProperty("interceptor.interceptorClasses." + getPropertySuffix());
            for (int i=0; i<availableInterceptors.size(); i++) {
                String cName = availableInterceptors.get(i).getClass().getName();
                JiveGlobals.setProperty("interceptor.interceptorClasses."+ getPropertySuffix() + ".interceptor" + i, cName);
            }
        }
        catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
        catch (InstantiationException e2) {
            throw new IllegalArgumentException(e2.getMessage());
        }
        catch (ClassCastException e5) {
            throw new IllegalArgumentException("Class is not a PacketInterceptor");
        }
    }

    /**
     * Returns an array of PacketInterceptor objects that are all of the currently available
     * incerceptors in the system.
     *
     * @return an array of all available interceptors in the current context.
     */
    public List<PacketInterceptor> getAvailableInterceptors() {
        return Collections.unmodifiableList(availableInterceptors);
    }

    /**
     * Returns the suffix to append at the end of global properties to ensure that each subclass
     * is not overwritting the properties of another subclass.
     *
     * @return the suffix to append at the end of global properties to ensure that each subclass
     *         is not overwritting the properties of another subclass.
     */
    protected abstract String getPropertySuffix();

    /**
     * Returns the collection of built-in packet interceptor classes.
     *
     * @return the collection of built-in packet interceptor classes.
     */
    protected abstract Collection<Class> getBuiltInInterceptorClasses();

    private void loadAvailableInterceptors() {
        // Load interceptor classes
        List<PacketInterceptor> interceptorList = new ArrayList<PacketInterceptor>();
        // First, add in built-in list of interceptors.
        for (Class newClass : getBuiltInInterceptorClasses()) {
            try {
                PacketInterceptor interceptor = (PacketInterceptor) newClass.newInstance();
                interceptorList.add(interceptor);
            }
            catch (Exception e) { }
        }

        // Now get custom interceptors.
        List classNames = JiveGlobals.getProperties("interceptor.interceptorClasses." +
                getPropertySuffix());
        for (int i=0; i<classNames.size(); i++) {
            install_interceptor: try {
                Class interceptorClass = loadClass((String)classNames.get(i));
                // Make sure that the interceptor isn't already installed.
                for (int j=0; j<interceptorList.size(); j++) {
                    if (interceptorClass.equals(interceptorList.get(j).getClass())) {
                        break install_interceptor;
                    }
                }
                PacketInterceptor interceptor = (PacketInterceptor) interceptorClass.newInstance();
                interceptorList.add(interceptor);
            }
            catch (Exception e) {
                Log.error(e);
            }
        }

        availableInterceptors.addAll(interceptorList);
    }

    private void loadGlobalInterceptors() {
        // See if a record for this context exists yet. If not, create one.
        int interceptorCount = JiveGlobals.getIntProperty("interceptor.global." +
                getPropertySuffix() +
                ".interceptorCount", 0);

        // Load up all filters and their filter types
        List<PacketInterceptor> interceptorList = new ArrayList<PacketInterceptor>(interceptorCount);
        for (int i=0; i<interceptorCount; i++) {
            try {
                String interceptorContext = "interceptor.global." + getPropertySuffix() + ".interceptor" + i + ".";
                String className = JiveGlobals.getProperty(interceptorContext + "className");
                Class interceptorClass = loadClass(className);
                interceptorList.add((PacketInterceptor) interceptorClass.newInstance());

                // Load properties.
                List props = JiveGlobals.getPropertyNames(interceptorContext + "properties");
                Map interceptorProps = new HashMap();

                for (int k = 0; k < props.size(); k++) {
                    String key = (String)props.get(k);
                    String value = JiveGlobals.getProperty((String)props.get(k));
                    // Get the bean property name, which is everything after the last '.' in the
                    // xml property name.
                    interceptorProps.put(key.substring(key.lastIndexOf(".")+1), value);
                }

                // Set properties on the bean
                BeanUtils.setProperties(interceptorList.get(i), interceptorProps);
            }
            catch (Exception e) {
                Log.error("Error loading global interceptor " + i, e);
            }
        }
        globalInterceptors.addAll(interceptorList);
    }

    private void loadLocalInterceptors(String workgroupJID) throws UserNotFoundException {
        Workgroup workgroup = WorkgroupManager.getInstance().getWorkgroup(new JID(workgroupJID));

        int interceptorCount = 0;
        String iCount = workgroup.getProperties().getProperty("jive.interceptor." +
                getPropertySuffix() + ".interceptorCount");
        if (iCount != null) {
            try {
                interceptorCount = Integer.parseInt(iCount);
            }
            catch (NumberFormatException nfe) { /* ignore */ }
        }

        // Load up all intercpetors.
        List<PacketInterceptor> interceptorList = new ArrayList<PacketInterceptor>(interceptorCount);
        for (int i=0; i<interceptorCount; i++) {
            try {
                String interceptorContext = "jive.interceptor." + getPropertySuffix() + ".interceptor" + i + ".";
                String className = workgroup.getProperties().getProperty(interceptorContext +
                        "className");
                Class interceptorClass = loadClass(className);
                interceptorList.add((PacketInterceptor) interceptorClass.newInstance());

                // Load properties.
                Map interceptorProps = new HashMap();
                for (String key : getChildrenPropertyNames(interceptorContext + "properties",
                        workgroup.getProperties().getPropertyNames()))
                {
                    String value = workgroup.getProperties().getProperty(key);
                    // Get the bean property name, which is everything after the last '.' in the
                    // xml property name.
                    interceptorProps.put(key.substring(key.lastIndexOf(".")+1), value);
                }

                // Set properties on the bean
                BeanUtils.setProperties(interceptorList.get(i), interceptorProps);
            }
            catch (Exception e) {
                Log.error("Error loading local interceptor " + i, e);
            }
        }
        localInterceptors.put(workgroupJID,
                new CopyOnWriteArrayList<PacketInterceptor>(interceptorList));
    }

    /**
     * Returns a child property names given a parent and an Iterator of property names.
     *
     * @param parent parent property name.
     * @param properties all property names to search.
     * @return an Iterator of child property names.
     */
    private static Collection<String> getChildrenPropertyNames(String parent, Collection<String> properties) {
        List<String> results = new ArrayList<String>();
        for (String name : properties) {
            if (name.startsWith(parent) && !name.equals(parent)) {
                results.add(name);
            }
        }
        return results;
    }

    private Class loadClass(String className) throws ClassNotFoundException {
        try {
            return ClassUtils.forName(className);
        }
        catch (ClassNotFoundException e) {
            return this.getClass().getClassLoader().loadClass(className);
        }
    }

    private List<PacketInterceptor> getLocalInterceptors(String workgroup) {
        List<PacketInterceptor> interceptors = localInterceptors.get(workgroup);
        if (interceptors == null) {
            if (interceptors == null) {
                synchronized (workgroup.intern()) {
                    try {
                        loadLocalInterceptors(workgroup);
                        interceptors = localInterceptors.get(workgroup);
                    }
                    catch (UserNotFoundException e) {
                        Log.warn(e);
                    }
                }
            }
        }
        return interceptors;
    }
}

⌨️ 快捷键说明

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