View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.pagination;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  import java.util.List;
33  
34  import com.mindtree.techworks.insight.InsightConstants;
35  
36  
37  
38  /**
39   * The <code>FilePageImpl</code> class is the file based caching implementation that 
40   * serailizes/de-serializes the LogEvents.  
41   * 
42   * @author antonypulicken
43   * @version 1.0, 05/02/02
44   * @see com.mindtree.techworks.insight.pagination.PageImpl
45   */
46  public class FilePageImpl extends PageImpl {
47  	
48  	/**
49  	 * Used for object serialization
50  	 */
51  	private static final long serialVersionUID = -1269915233299873471L;
52  	
53  	/**
54  	 * Deafault Constructor 
55  	 *
56  	 */
57  	public FilePageImpl(){
58  	}
59  	
60  	/**
61  	 * Method that activates the Page
62  	 * 
63  	 */	
64  	protected void load() {
65  		try{
66  			FileInputStream fil = new FileInputStream(getFile());
67  			ObjectInputStream obj = new ObjectInputStream(fil);
68  			List eventList = (List)obj.readObject();
69  			setLogEventList(eventList);			
70  			obj.close();
71  			fil.close();
72  		} catch (IOException e) {
73  			e.printStackTrace();
74  		}catch (ClassNotFoundException e1) {
75  			e1.printStackTrace();
76  		}		
77  	}
78  
79  	/**
80  	 * Method that passivates the Page
81  	 * 
82  	 */	
83  	protected void store() {
84  		try {
85  			FileOutputStream fil  = new FileOutputStream(getFile());
86  			ObjectOutputStream obj = new ObjectOutputStream(fil);
87  			obj.writeObject(getLogEventList());
88  			obj.close();
89  			fil.close();			
90  		} catch (IOException e) {
91  			e.printStackTrace();
92  		}		
93  	}
94  
95  	/**
96  	 * Returns the file. This method will create the default cache directory and the PageSet 
97  	 * directory in case its not created already
98  	 * @return file 
99  	 */	
100 	private File getFile(){
101 		String rootDir = System.getProperty("java.io.tmpdir") + InsightConstants.ROOT_CACHE_DIR;
102 		createDir(rootDir);
103 		String pageSetDir = rootDir + getPageSetNamespace();
104 		createDir(pageSetDir);
105 		String fileName = getPageNamespace() + InsightConstants.FILE_EXT;		
106 		String fullyQualFileName = pageSetDir + InsightConstants.FILE_SEPARATOR + fileName;
107 		File file = new File(fullyQualFileName);
108 		return file;
109 	}
110 
111 	/**
112 	 * Creates the directory in case its not present
113 	 * @param path The directory path
114 	 */	
115 	private void createDir(String path){
116 		File dir = new File(path);
117 		if(! dir.isDirectory()){
118 			dir.mkdir();
119 		}
120 	}
121 	
122 	/**
123 	 * Deletes the file
124 	 * 
125 	 */
126 	protected void delete(){
127 		File file = getFile();
128 		if(file.exists()){
129 			file.delete();
130 		}
131 		file = null;
132 		//setPageNamespace(null);
133 	}
134 }