View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2008 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: FileStorage.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
24   */
25  package net.sf.bluecove.se;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.util.Properties;
33  
34  import net.sf.bluecove.util.Storage;
35  
36  /**
37   * 
38   */
39  public class FileStorage implements Storage {
40  
41  	private Properties properties;
42  
43  	private long propertiesFileLoadedLastModified = 0;
44  
45  	private File propertyFile;
46  
47  	public FileStorage() {
48  
49  	}
50  
51  	/*
52  	 * (non-Javadoc)
53  	 * 
54  	 * @see net.sf.bluecove.util.Storage#retriveData(java.lang.String)
55  	 */
56  	public synchronized String retriveData(String name) {
57  		return getProperties().getProperty(name);
58  	}
59  
60  	/*
61  	 * (non-Javadoc)
62  	 * 
63  	 * @see net.sf.bluecove.util.Storage#storeData(java.lang.String,
64  	 * java.lang.String)
65  	 */
66  	public synchronized void storeData(String name, String value) {
67  		Properties p = getProperties();
68  		if (name != null) {
69  			if (value == null) {
70  				if (p.remove(name) == null) {
71  					// Not updated
72  					return;
73  				}
74  			} else {
75  				if (value.equals(p.put(name, value))) {
76  					// Not updated
77  					return;
78  				}
79  			}
80  		}
81  		File f = getPropertyFile();
82  		if (f == null) {
83  			return;
84  		}
85  		FileOutputStream out = null;
86  		try {
87  			out = new FileOutputStream(f);
88  			// we run on Java 1.1
89  			p.save(out, "");
90  		} catch (FileNotFoundException ignore) {
91  		}
92  		try {
93  			out.close();
94  		} catch (Throwable ignore) {
95  		}
96  		propertiesFileLoadedLastModified = f.lastModified();
97  	}
98  
99  	private static File homePath() {
100 		String path = ".bluecove";
101 		boolean isWindows = false;
102 		String sysName = System.getProperty("os.name");
103 		if (sysName != null) {
104 			sysName = sysName.toLowerCase();
105 			if (sysName.indexOf("windows") != -1) {
106 				isWindows = true;
107 				path = "Application Data";
108 			}
109 		}
110 		File dir;
111 		try {
112 			dir = new File(System.getProperty("user.home"), path);
113 			if (!dir.exists()) {
114 				if (!dir.mkdirs()) {
115 					throw new SecurityException();
116 				}
117 			}
118 		} catch (SecurityException e) {
119 			dir = new File(new File(System.getProperty("java.io.tmpdir"), System.getProperty("user.name")), path);
120 		}
121 		if (isWindows) {
122 			dir = new File(dir, "BlueCove");
123 		}
124 		if (!dir.exists()) {
125 			if (!dir.mkdirs()) {
126 				return null;
127 			}
128 		} else if (!dir.isDirectory()) {
129 			dir.delete();
130 			if (!dir.mkdirs()) {
131 				return null;
132 			}
133 		}
134 		return dir;
135 	}
136 
137 	private File getPropertyFile() {
138 		if (propertyFile != null) {
139 			return propertyFile;
140 		}
141 		File homeDir = homePath();
142 		if (homeDir == null) {
143 			try {
144 				propertyFile = File.createTempFile("bluecove-tester", ".properties");
145 				return propertyFile;
146 			} catch (IOException e) {
147 				return null;
148 			}
149 		}
150 		// Position and history for different stacks and device IDs different
151 		// for testing convenience
152 		String id = "";
153 		try {
154 			String stack = System.getProperty("bluecove.stack");
155 			if (stack != null) {
156 				id = stack;
157 			}
158 		} catch (SecurityException ignore) {
159 
160 		}
161 		try {
162 			String deviceID = System.getProperty("bluecove.deviceID");
163 			if (deviceID != null) {
164 				id += deviceID;
165 			}
166 		} catch (SecurityException ignore) {
167 
168 		}
169 		propertyFile = new File(homeDir, "bluecove-tester" + id + ".properties");
170 		return propertyFile;
171 	}
172 
173 	private Properties getProperties() {
174 		File f = getPropertyFile();
175 		long lastModified = 0;
176 
177 		if (f != null && f.exists()) {
178 			lastModified = f.lastModified();
179 		} else {
180 			lastModified = propertiesFileLoadedLastModified;
181 		}
182 
183 		if ((properties != null) && (propertiesFileLoadedLastModified == lastModified)) {
184 			return properties;
185 		}
186 		Properties p = new Properties();
187 		if (f != null && f.exists()) {
188 			FileInputStream in = null;
189 			try {
190 				in = new FileInputStream(f);
191 				p.load(in);
192 			} catch (IOException ignore) {
193 			} finally {
194 				try {
195 					in.close();
196 				} catch (Throwable ignore) {
197 				}
198 			}
199 		}
200 		propertiesFileLoadedLastModified = lastModified;
201 		properties = p;
202 		return properties;
203 	}
204 
205 }