View Javadoc

1   /*
2    * Copyright 2006 Thomas Peuss
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13   * or implied. See the License for the specific language governing
14   * permissions and limitations under the License.
15   */
16  package logtail.beans;
17  
18  import java.io.File;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.TimeZone;
26  
27  import javax.faces.application.Application;
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  import javax.faces.model.DataModel;
31  import javax.faces.model.ListDataModel;
32  import javax.servlet.http.HttpSession;
33  import javax.servlet.http.HttpSessionBindingEvent;
34  import javax.servlet.http.HttpSessionBindingListener;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import logtail.logfile.LogfileDescriptor;
40  import logtail.logfile.LogfileManager;
41  
42  public class LogfilesBean implements HttpSessionBindingListener {
43  	private static final Log log=LogFactory.getLog(LogfilesBean.class);
44  	private DataModel logfilesModel;
45  	private Map tailLogDescriptors;
46  	private int maxLogLines;
47  	
48  	public LogfilesBean() {
49  		tailLogDescriptors=new HashMap();
50  		loadLogfiles();
51  		log.debug("Initialized. "+this.getLogfiles().size()+" logfiles found");
52  	}
53  
54  	public String rescan() {
55  		log.debug("Rescan of logs triggered");
56  		loadLogfiles();
57  		return "reload";
58  	}
59  	
60  	public String logout() {
61  		FacesContext context = FacesContext.getCurrentInstance();
62  		
63  		HttpSession session=(HttpSession)context.getExternalContext().getSession(true);
64  		
65  		session.invalidate();
66  		
67  		return "loggedout";
68  	}
69  
70  	public String sortAsc() {
71  		List logfiles=(List)logfilesModel.getWrappedData();
72  		Collections.sort(logfiles,new Comparator() {
73  			public int compare(Object arg0, Object arg1) {
74  				File f1=(File)arg0;
75  				File f2=(File)arg1;
76  				
77  				if (f1==f2) {
78  					return 0;
79  				}
80  				
81  				if (f1.lastModified()<f2.lastModified()) {
82  					return -1;
83  				} else {
84  					return 1;
85  				}
86  			}
87  		});
88  		
89  		setLogfiles(logfiles);
90  
91  		return "reload";
92  	}
93  	
94  	public String sortDesc() {
95  		List logfiles=(List)logfilesModel.getWrappedData();
96  		Collections.sort(logfiles,new Comparator() {
97  			public int compare(Object arg0, Object arg1) {
98  				File f1=(File)arg0;
99  				File f2=(File)arg1;
100 				
101 				if (f1==f2) {
102 					return 0;
103 				}
104 				
105 				if (f1.lastModified()<f2.lastModified()) {
106 					return 1;
107 				} else {
108 					return -1;
109 				}
110 			}
111 		});
112 		
113 		setLogfiles(logfiles);
114 
115 		return "reload";
116 	}
117 	
118 	protected void loadLogfiles() {
119 		FacesContext context = FacesContext.getCurrentInstance();
120 		String logs=context.getExternalContext().getInitParameter("logs");
121 		
122 		closeTailLogDescriptors();
123 		this.tailLogDescriptors=new HashMap();
124 		this.setLogfiles(LogfileManager.getLogfiles());		
125 	}
126 
127 	public static LogfilesBean getLogfilesBean() {
128 		FacesContext context = FacesContext.getCurrentInstance();
129         Application application=context.getApplication();
130         return (LogfilesBean)application.createValueBinding("#{LogfilesBean}").getValue(context);		
131 	}
132 	
133 	public static ExternalContext getExternalContext() {
134 		return FacesContext.getCurrentInstance().getExternalContext();
135 	}
136 	
137 	public List getLogfiles() {
138 		return (List)logfilesModel.getWrappedData();
139 	}
140 
141 	public void setLogfiles(List logfiles) {
142 		this.logfilesModel=new ListDataModel(logfiles);
143 	}
144 
145 	public Map getTailLogDescriptors() {
146 		return tailLogDescriptors;
147 	}
148 
149 	public void setTailLogDescriptors(Map tailLogDescriptors) {
150 		this.tailLogDescriptors = tailLogDescriptors;
151 	}
152 	
153 	protected void closeTailLogDescriptors() {
154 		Iterator iter=tailLogDescriptors.keySet().iterator();
155 		
156 		while (iter.hasNext()) {
157 			String id=(String)iter.next();
158 			LogfileDescriptor desc=(LogfileDescriptor)tailLogDescriptors.get(id);
159 			desc.closeRAFile();
160 		}
161 	}
162 
163 	public DataModel getLogfilesModel() {
164 		return logfilesModel;
165 	}
166 
167 	public void setLogfilesModel(DataModel logfilesModel) {
168 		this.logfilesModel = logfilesModel;
169 	}
170 
171 	public boolean isLoggedIn() {
172 		FacesContext context = FacesContext.getCurrentInstance();
173 		if(context.getExternalContext().getRemoteUser()==null) {
174 			return false;
175 		} else {
176 			return true;
177 		}
178 	}
179 	
180 	public void valueBound(HttpSessionBindingEvent event) {
181 		// Do nothing
182 	}
183 
184 	public void valueUnbound(HttpSessionBindingEvent event) {
185 		// Remove all open LogfileDescriptors
186 		closeTailLogDescriptors();
187 	}
188 
189 	public int getMaxLogLines() {
190 		return maxLogLines;
191 	}
192 
193 	public void setMaxLogLines(int maxLogLines) {
194 		this.maxLogLines = maxLogLines;
195 	}
196 	
197 	public TimeZone getTimeZone() {
198 		return TimeZone.getDefault();
199 	}
200 }