001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.modules;
020
021import java.util.Collection;
022import java.util.Iterator;
023import java.util.Set;
024import java.util.TreeSet;
025
026import org.apache.wiki.Release;
027import org.apache.wiki.WikiEngine;
028
029
030/**
031 *  Superclass for all JSPWiki managers for modules (plugins, etc).
032 */
033public abstract class ModuleManager
034{
035
036    /**
037     * Location of the property-files of plugins.
038     *  (Each plugin should include this property-file in its jar-file)
039     */
040    public static final String PLUGIN_RESOURCE_LOCATION = "ini/jspwiki_module.xml";
041
042    protected WikiEngine m_engine;
043
044    private boolean m_loadIncompatibleModules = false;
045
046    /**
047     *  Constructs the ModuleManager.
048     *
049     *  @param engine The WikiEngine which owns this manager.
050     */
051    public ModuleManager( WikiEngine engine )
052    {
053        m_engine = engine;
054    }
055
056    /**
057     *  Returns true, if the given module is compatible with this version of JSPWiki.
058     *
059     *  @param info The module to check
060     *  @return True, if the module is compatible.
061     */
062    public boolean checkCompatibility( WikiModuleInfo info )
063    {
064        if( !m_loadIncompatibleModules )
065        {
066            String minVersion = info.getMinVersion();
067            String maxVersion = info.getMaxVersion();
068
069            return Release.isNewerOrEqual( minVersion ) && Release.isOlderOrEqual( maxVersion );
070        }
071
072        return true;
073    }
074
075    /**
076     * Returns a collection of modules currently managed by this ModuleManager.  Each
077     * entry is an instance of the WikiModuleInfo class.  This method should return something
078     * which is safe to iterate over, even if the underlying collection changes.
079     *
080     * @return A Collection of WikiModuleInfo instances.
081     */
082    public abstract Collection< WikiModuleInfo > modules();
083
084    protected < T extends WikiModuleInfo > Collection< WikiModuleInfo > modules( Iterator< T > iterator ) {
085        Set< WikiModuleInfo > ls = new TreeSet<>();
086
087        for( Iterator< T > i = iterator; i.hasNext(); ) {
088            WikiModuleInfo wmi = i.next();
089            if( !ls.contains( wmi ) ) ls.add( wmi );
090        }
091
092        return ls;
093    }
094
095    /**
096     * Returns the {@link WikiModuleInfo} information about the provided moduleName.
097     * @param moduleName
098     * @return The wikiModuleInfo
099     */
100    public abstract WikiModuleInfo getModuleInfo(String moduleName);
101}