001 package net.sf.logdistiller; 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.util.*; 018 019 import net.sf.logdistiller.publishers.BasePublishers; 020 import net.sf.logdistiller.util.ExtensionHelper; 021 import net.sf.logdistiller.util.FormatUtil; 022 023 /** 024 * Definition of available report publishers. 025 */ 026 public abstract class Publishers 027 { 028 /** 029 * get the <code>Publishers</code> instances defined by this extension. 030 * 031 * @return the list of publishers 032 * @see LogType 033 */ 034 public abstract List definePublishers(); 035 036 public static final List ALL; 037 038 public static final Map MAP; 039 static 040 { 041 List all = new ArrayList( new BasePublishers().definePublishers() ); 042 all.addAll( loadExtensionPublishers() ); 043 ALL = Collections.unmodifiableList( all ); 044 045 Map map = new HashMap(); 046 Iterator iter = ALL.iterator(); 047 while ( iter.hasNext() ) 048 { 049 Publisher type = (Publisher) iter.next(); 050 map.put( type.getId(), type ); 051 } 052 MAP = Collections.unmodifiableMap( map ); 053 } 054 055 /** 056 * get all the report publishers defined (predefined and custom). 057 */ 058 public static List getAllPublishers() 059 { 060 return ALL; 061 } 062 063 public static String listAllPublishersIds() 064 { 065 return FormatUtil.join( ", ", MAP.keySet().iterator() ); 066 } 067 068 public static Publisher getPublisher( String id ) 069 { 070 return (Publisher) MAP.get( id ); 071 } 072 073 /** 074 * Loads report publishers defined by extension mechanism: if defined, <code>publishers</code> property in 075 * <code>logdistiller.properties</code> is the full class name of a concrete implementation of 076 * <code>Publishers</code> class. 077 * 078 * @return List the list of every <code>Publisher</code> defined by loaded <code>Publishers</code> 079 */ 080 private static List loadExtensionPublishers() 081 { 082 Iterator iter = ExtensionHelper.findExtensions( "publishers" ).iterator(); 083 List publishers = new ArrayList(); 084 while ( iter.hasNext() ) 085 { 086 String publishersClass = (String) iter.next(); 087 if ( publishersClass != null ) 088 { 089 publishers.addAll( loadPublishers( publishersClass ) ); 090 } 091 } 092 return publishers; 093 } 094 095 private static List loadPublishers( String publishersClass ) 096 { 097 try 098 { 099 Publishers publishers = (Publishers) Class.forName( publishersClass ).newInstance(); 100 return publishers.definePublishers(); 101 } 102 catch ( ClassNotFoundException cnfe ) 103 { 104 throw new RuntimeException( "unable to load plugins class " + publishersClass, cnfe ); 105 } 106 catch ( IllegalAccessException iae ) 107 { 108 throw new RuntimeException( "unable to access plugins constructor for class " + publishersClass, iae ); 109 } 110 catch ( InstantiationException ie ) 111 { 112 throw new RuntimeException( "unable to instanciate plugins class " + publishersClass, ie ); 113 } 114 } 115 }