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