View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging;
2   
3   import java.util.Arrays;
4   import java.util.Collections;
5   import java.util.Comparator;
6   import java.util.Map;
7   import java.util.logging.Level;
8   
9   /**
10   * Class that wraps the map with log categories. It allows you
11   * to simply create new categories or query it's mapping.
12   * <p><p>
13   * The only constructor LogCategories(Logger) is protected and is instantiated
14   * during the construction of AgentLogger.
15   *
16   * @author Jimmy
17   */
18  public abstract class AbstractLogCategories implements ILogCategories {
19  
20  	private Map<String, LogCategory> immutableCategories = null;
21  
22  	private Comparator<String> stringComparator = new Comparator<String>() {
23  
24  		@Override
25  		public int compare(String o1, String o2) {
26  			if (o1 == null) {
27  				if (o1 == o2) return 0;
28  				return -1;
29  			}
30  			if (o2 == null) return 1;
31  			return o1.compareTo(o2);
32  		}
33  
34  	};
35  
36      /**
37       * @return Muttable map with categories.
38       */
39      protected abstract Map<String, LogCategory> getCategoriesInternal();
40  
41  
42  	/**
43  	 * Whether some category with specified name exists.
44  	 * @param name
45  	 * @return
46  	 */
47      @Override
48  	public boolean hasCategory(String name) {
49  		return Arrays.binarySearch(getCategoryNamesSorted(), name, stringComparator) >= 0;
50  	}
51  
52  	/**
53  	 * Returns IMMUTABLE mapping of cathegories names to instances of those log categories.
54  	 * <p><p>
55  	 * You have to synchronize on it before iterating through it!
56  	 * @return
57  	 */
58      @Override
59  	public Map<String, LogCategory> getCategories() {
60          if(immutableCategories == null) {
61              immutableCategories = Collections.unmodifiableMap(getCategoriesInternal());
62          }
63          return immutableCategories;
64  	}
65  
66      /**
67       * Used by {@link AbstractAgentLogger} to slip itself into the map.
68       * @param name
69       * @param category
70       */
71      @Override
72      public void addLogCategory(String name, LogCategory category) {
73      	getCategoriesInternal().put(name, category);
74      }
75  
76  	/**
77  	 * Returns names of all existing log categories.
78  	 * @return
79  	 */
80      @Override
81  	public String[] getCategoryNames() {
82  		return getCategoriesInternal().keySet().toArray(new String[0]);
83  	}
84  
85  	/**
86  	 * Returns names of all existing log categories sorted alphabetically.
87  	 * @return
88  	 */
89      @Override
90  	public String[] getCategoryNamesSorted() {
91  		String[] names = getCategoryNames();
92  		Arrays.sort(names, stringComparator);
93  		return names;
94  	}
95  
96  	
97  	/**
98  	 * Set level for all handlers of all categories.
99  	 *
100 	 * @param newLevel
101 	 */
102     @Override
103 	public void setLevel(Level newLevel) {
104     	synchronized(getCategories()) {
105     		for (LogCategory category : getCategories().values()) {
106     			category.setLevel(newLevel);    		
107     		}
108     	}
109 	}
110 
111 }