View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.view;
22  
23  import java.io.File;
24  import java.net.URL;
25  import java.util.List;
26  
27  import net.sf.jour.filter.MatchListFilter;
28  import net.sf.jour.rt.agent.Event;
29  import net.sf.jour.rt.view.config.ClassnamesType;
30  import net.sf.jour.rt.view.config.FilterType;
31  import net.sf.jour.rt.view.config.JourView;
32  import net.sf.jour.rt.view.config.MethodsignatureType;
33  import net.sf.jour.util.ConfigFileUtil;
34  import net.sf.jour.util.FileChageListener;
35  import net.sf.jour.util.FileUtil;
36  import net.sf.jour.util.FileWatchdog;
37  
38  import org.apache.log4j.Logger;
39  
40  /***
41   * A collection of filter patterns to apply on collected data.
42   * 
43   * Created on 04.12.2004
44   *
45   * Contributing Author(s):
46   *
47   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
48   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
49   *
50   * @author vlads
51   * @version $Revision: 1.12 $ ($Author: vlads $) $Date: 2004/12/16 17:40:19 $
52   */
53  public class ViewFilter extends MatchListFilter implements FileChageListener {
54  	
55  	private boolean filterOn;
56  	private boolean isInitialized;
57  	
58  	private ReplacementList classnameView = new ReplacementList();
59  	
60  	private ReplacementList methodsignatureView = new ReplacementList();
61  	
62  	public static final String CONFING_FILE = "jour_view.xml";
63  	
64  	static final Logger log = Logger.getLogger(ViewFilter.class);
65  	
66  	public ViewFilter() {
67  	    // Do Initialization on first request.
68  	}
69  	
70  	public void isInitialized() {
71  	    if (isInitialized) {
72  	        return;
73  	    }
74  	    synchronized (this) {
75  	        if (isInitialized) {
76  		        return;
77  		    }
78              isInitialized = true;
79              try {
80                  initialize();
81              } catch (NoClassDefFoundError e) {
82                  log.error("Filter disabled", e);
83                  filterOn = false;
84              } catch (RuntimeException e) {
85                  log.error("Filter disabled", e);
86                  filterOn = false;
87              }
88          }
89  	}
90  	
91  	public boolean filer(Event event) {
92  	    isInitialized();
93  	    
94  		if (!filterOn) {
95  			return false;
96  		}
97  		return !(super.match(event));
98  	}
99  	
100     /***
101      * @return Returns the classnameView.
102      */
103     public ReplacementList getClassnameView() {
104         isInitialized();
105         return classnameView;
106     }
107     
108     /***
109      * @return Returns the methodsignatureView.
110      */
111     public ReplacementList getMethodsignatureView() {
112         isInitialized();
113         return methodsignatureView;
114     }
115 	
116 	public void addFilter(FilterType definition) {
117 		ViewFilterItem f = new ViewFilterItem();
118 		f.readConfig(definition);
119 		addInclude(f);
120 	}
121 	
122 	public boolean readContext(Object o) {
123 		if (o instanceof FilterType) {
124 			addFilter((FilterType)o);
125 			return true;
126 		} else if (o instanceof ClassnamesType) {
127 		    this.classnameView.readConfig(((ClassnamesType)o).getTrim());
128 		    return true;
129 		} else if (o instanceof MethodsignatureType) {
130 		    this.methodsignatureView.readConfig(((MethodsignatureType)o).getTrim());
131 		    return true;
132 		} else {
133 			return super.readContext(o);
134 		}
135 	}
136 	
137 	public void doOnFileChange(String fileName) {
138 	    reload();
139 	}
140 	
141 	protected void reload() {
142 		// I belive that there is no need for synchronization.
143 		filterOn = false;
144 		reset();
145 		initialize();
146 	}
147 	
148     protected void initialize() {
149         JourView view = (JourView) ConfigFileUtil.unmarshalConfigFile(CONFING_FILE, "net.sf.jour.rt.view.config");
150         boolean forceAutoReload = false;
151         if (view == null) {
152             log.info("ViewFilter Config file not loaded " + CONFING_FILE);
153             forceAutoReload = true;
154         } else {
155             List list = view.getFilter();
156             readConfig(view.getClassnames());
157             readConfig(view.getMethodsignature());
158             readConfig(view.getFilter());
159             debug();
160             this.classnameView.debug();
161             this.methodsignatureView.debug();
162             filterOn = true;
163         }
164 		
165 		
166 		URL url = FileUtil.getFileLocation(CONFING_FILE);
167 		File file = new File(url.getFile());
168 
169 	    FileWatchdog watchdog = FileWatchdog.getFileWatchdog(file.getAbsolutePath(), true);
170 	    
171 		if (forceAutoReload || ((view.getAutoreload() != null) && view.getAutoreload().isEnabled())) {
172             if (!file.canRead()) {
173                 log.warn("config is not auto reloadable" + file.getAbsolutePath());
174             }
175             long dellay = 0;
176             if (view != null) {
177                 dellay = view.getAutoreload().getDelay();
178             }
179             if (dellay < 1000) {
180                 dellay = FileWatchdog.DEFAULT_DELAY;
181             }
182             watchdog.setDelay(dellay);
183             watchdog.addListener(this);
184         } else {
185             if (watchdog != null) {
186                 watchdog.finish();
187                 watchdog.setDelay(Long.MAX_VALUE);
188             }
189         }
190     }
191 }