View Javadoc

1   /*
2    * Trip Tracker, a real-time position tracking system for the Internet.
3    * Copyright (C) 2006  Team Trip Tracker
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  
20  package triptracker.server.persistence;
21  
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.util.Date;
27  import java.util.List;
28  
29  import triptracker.core.Coordinate;
30  import triptracker.core.Route;
31  import triptracker.core.User;
32  import triptracker.core.Utils;
33  
34  /***
35   * Works as an adapter for Persistence classes with built-in logging and all
36   * classes throwing an UnsupportedOperationException if not overridden.
37   */
38  public abstract class PersistenceAdapter implements Persistence {
39  	private String logFile = "database.log";
40  	private int userId = -1;
41  	
42  	/***
43  	 * Set the filename where log messages should be written.
44  	 * 
45  	 * @param file
46  	 */
47  	protected void setLogFile(String file) {
48  		logFile = file;
49  	}
50  	
51  	/***
52  	 * Returns the file where log messages are written.
53  	 * 
54  	 * @return filename of output log messages
55  	 */
56  	protected String getLogFile() {
57  		return logFile;
58  	}
59  
60  	/***
61  	 * {@inheritDoc}
62  	 */
63  	public int getUserId(String username) {
64  		throw new UnsupportedOperationException();
65  	}
66  
67  	/***
68  	 * {@inheritDoc}
69  	 */
70  	public int authUser(String username, String password) {
71  		throw new UnsupportedOperationException();
72  	}
73  
74  	/***
75  	 * {@inheritDoc}
76  	 */
77  	public void storeCoordinate(int routeID, Coordinate coord) {
78  		throw new UnsupportedOperationException();
79  	}
80  
81  	/***
82  	 * {@inheritDoc}
83  	 */
84  	public void storeCoordinate(int routeID, List<Coordinate> coord) {
85  		throw new UnsupportedOperationException();
86  	}
87  
88  	
89  	/***
90  	 * {@inheritDoc}
91  	 */
92  	public Route makeRoute(int userID, String description, boolean publicRoute) {
93  		throw new UnsupportedOperationException();
94  	}
95  
96  	/***
97  	 * {@inheritDoc}
98  	 */
99  	public List<Route> getRoutes(String username) {
100 		throw new UnsupportedOperationException();
101 	}
102 
103 	/***
104 	 * {@inheritDoc}
105 	 */
106 	public Route getRoute(int routeId) {
107 		throw new UnsupportedOperationException();
108 	}
109 	
110 	/***
111 	 * {@inheritDoc}
112 	 */
113 	public List<Coordinate> getCoordinates(int routeID) {
114 		throw new UnsupportedOperationException();
115 	}
116 
117 	/***
118 	 * {@inheritDoc}
119 	 */
120 	public List<User> getUserList() {
121 		throw new UnsupportedOperationException();
122 	}
123 	
124 	/***
125 	 * {@inheritDoc}
126 	 */
127 	public User getUser(int userId) {
128 		throw new UnsupportedOperationException();
129 	}
130 
131 	/***
132 	 * {@inheritDoc}
133 	 */
134 	public void setVisible(int routeID, boolean visible) {
135 		throw new UnsupportedOperationException();
136 	}
137 
138 	/***
139 	 * {@inheritDoc}
140 	 */
141 	public void lockRoute(int routeID, boolean lock) {
142 		throw new UnsupportedOperationException();
143 	}
144 	
145 	public void setUserId(int userId) {
146 		this.userId = userId;
147 	}
148 	
149 	public int getUserId() {
150 		return userId;
151 	}
152 
153 	protected void log(String message) {
154 		File file = new File(logFile);
155 
156 		try {
157 			if (!(file.exists())) {
158 				file.createNewFile();
159 			}
160 			
161 			PrintWriter logOut = new PrintWriter(new FileWriter(logFile, true));
162 			logOut.println("[" + Utils.dateToString(new Date()) + "]["
163 					+ Thread.currentThread().getId() + "] " + message);
164 			logOut.close();
165 		} catch (IOException e) {
166 			System.err.println("Exception while logging: " + e);
167 			e.printStackTrace();
168 		}
169 	}
170 
171 }