001package net.sf.logdistiller.util;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.io.IOException;
018import java.net.URL;
019import java.util.*;
020
021/**
022 * Helper to load extensions for <b>LogDistiller</b>. Extensions are declared in <code>logdistiller.properties</code>
023 * resource file, as properties. Each extensible part can be declared as a property which key value specifies the part
024 * extended (<code>logtypes</code>, <code>plugins</code>, <code>publishers</code> or <code>reportformats</code>), the
025 * value is the full class name (with package) of the extension.
026 *
027 * @since 0.7
028 */
029public class ExtensionHelper
030{
031    private static List<Properties> extensionProperties;
032
033    public static List<String> findExtensions( String type )
034    {
035        if ( extensionProperties == null )
036        {
037            extensionProperties = findExtensions();
038        }
039        List<String> extensions = new ArrayList<String>();
040        for ( Properties prop : extensionProperties )
041        {
042            String extension = prop.getProperty( type );
043            if ( extension != null )
044            {
045                extensions.add( extension );
046            }
047        }
048        return extensions;
049    }
050
051    private static List<Properties> findExtensions()
052    {
053        URL url = null;
054        try
055        {
056            List<Properties> extensions = new ArrayList<Properties>();
057            Enumeration<URL> res = ExtensionHelper.class.getClassLoader().getResources( "logdistiller.properties" );
058            while ( res.hasMoreElements() )
059            {
060                url = res.nextElement();
061                Properties prop = new Properties();
062                prop.load( url.openStream() );
063                extensions.add( prop );
064            }
065            return extensions;
066        }
067        catch ( IOException ioe )
068        {
069            String msg =
070                ( url == null ) ? "unable to list resources logdistiller.properties" : "unable to load resource "
071                    + url.toExternalForm();
072            throw new RuntimeException( msg, ioe );
073        }
074    }
075}