001    package 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    
017    import java.io.IOException;
018    import java.net.URL;
019    import 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     */
029    public class ExtensionHelper
030    {
031        private static List extensionProperties;
032    
033        public static List findExtensions( String type )
034        {
035            if ( extensionProperties == null )
036            {
037                extensionProperties = findExtensions();
038            }
039            List extensions = new ArrayList();
040            Iterator iter = extensionProperties.iterator();
041            while ( iter.hasNext() )
042            {
043                Properties prop = (Properties) iter.next();
044                String extension = prop.getProperty( type );
045                if ( extension != null )
046                {
047                    extensions.add( extension );
048                }
049            }
050            return extensions;
051        }
052    
053        private static List findExtensions()
054        {
055            URL url = null;
056            try
057            {
058                List extensions = new ArrayList();
059                Enumeration res = ExtensionHelper.class.getClassLoader().getResources( "logdistiller.properties" );
060                while ( res.hasMoreElements() )
061                {
062                    url = (URL) res.nextElement();
063                    Properties prop = new Properties();
064                    prop.load( url.openStream() );
065                    extensions.add( prop );
066                }
067                return extensions;
068            }
069            catch ( IOException ioe )
070            {
071                String msg =
072                    ( url == null ) ? "unable to list resources logdistiller.properties" : "unable to load resource "
073                        + url.toExternalForm();
074                throw new RuntimeException( msg, ioe );
075            }
076        }
077    }