View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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 or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.remote.file;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  import org.jdiagnose.concurrent.SynchronizedRef;
25  import org.jdiagnose.remote.ExceptionListener;
26  import org.jdiagnose.remote.StdErrExceptionListener;
27  
28  /***
29   * @author jamie
30   */
31  public class DailyRollingFileEmitter implements Emitter {
32  
33      private ExceptionListener exceptionListener = StdErrExceptionListener.DEFAULT_STD_ERR_EXCEPTION_LISTENER;
34      private BasicFileEmitter basicFileEmitter = null;
35      private DateFormat dateFormat;
36      private SynchronizedRef currentDate = new SynchronizedRef("");
37      private File folder = null;
38      private String fileName = null;
39      private Object basicFileEmitterLock = new Object();
40      private boolean destroyed = false;
41  
42      public DailyRollingFileEmitter() {
43  
44      }
45  
46      public DailyRollingFileEmitter(File file) {
47          this.basicFileEmitter = new BasicFileEmitter(file);
48          folder = file.getParentFile();
49          fileName = file.getName();
50      }
51  
52      public void setFile(File file) {
53          basicFileEmitter.setFile(file);
54          folder = file.getParentFile();
55          fileName = file.getName();
56      }
57  
58      public DailyRollingFileEmitter(String pattern, File file) {
59          this(file);
60          this.dateFormat = new SimpleDateFormat(pattern);
61      }
62      
63      public void init() throws IOException {
64          synchronized(basicFileEmitterLock) {
65              basicFileEmitter.init();
66              destroyed = false;
67          }
68      }
69      
70      public void destroy() throws IOException {
71          synchronized(basicFileEmitterLock) {
72              basicFileEmitter.destroy();
73              destroyed = true;
74          }
75      }
76  
77      /* (non-Javadoc)
78       * @see org.jdiagnose.remote.file.Emitter#emit(java.lang.StringBuffer)
79       */
80      public void emit(StringBuffer value) {
81          synchronized (basicFileEmitterLock) {
82              if(!destroyed) {
83  	            long currentTimeMillis = System.currentTimeMillis();
84                  String date = dateFormat.format(new Date(currentTimeMillis));
85  	            Object currentDateValue = currentDate.get();
86  	            if (currentDateValue.equals("")) {
87  	                currentDate.set(date);
88  	            } else if (!currentDateValue.equals(date)) {
89  	                try {
90  	                    basicFileEmitter.destroy();
91  	                    File file = basicFileEmitter.getFile();
92  	                    File renamedFile = new File(folder, fileName + currentDateValue);
93  	                    if(renamedFile.exists()) {
94  	                        renamedFile.delete();
95  	                    }
96                          file.renameTo(renamedFile);
97  	                    currentDate.set(date);
98  	                    basicFileEmitter = new BasicFileEmitter(new File(folder,
99  	                            fileName));
100 	                } catch (IOException e) {
101 	                    exceptionListener.onException(e);
102 	                }
103 	            }
104 	            basicFileEmitter.emit(value);
105             }
106         }
107     }
108 
109     public void setExceptionListener(ExceptionListener exceptionListener) {
110         this.exceptionListener = exceptionListener;
111     }
112     
113     public void setPattern(String pattern) {
114         this.dateFormat = new SimpleDateFormat(pattern);
115     }
116 }