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: Persistence.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
24   */
25  package net.sf.bluecove.obex;
26  
27  import java.io.File;
28  import java.io.FileReader;
29  import java.io.FileWriter;
30  import java.io.IOException;
31  import java.io.LineNumberReader;
32  import java.io.Reader;
33  import java.util.Enumeration;
34  import java.util.Hashtable;
35  import java.util.Properties;
36  
37  public class Persistence {
38  
39  	private static final String configFileName = "obex-install.cfg";
40  
41  	private static final String devicePrefix = "device:";
42  
43  	private static final String selectedPrefix = "selected:";
44  
45  	private static final Properties properties = new Properties();
46  
47  	private static File homePath() {
48  		String path = ".bluecove";
49  		boolean isWindows = false;
50  		String sysName = System.getProperty("os.name");
51  		if (sysName != null) {
52  			sysName = sysName.toLowerCase();
53  			if (sysName.indexOf("windows") != -1) {
54  				isWindows = true;
55  				path = "Application Data";
56  			}
57  		}
58  		File dir;
59  		try {
60  			dir = new File(System.getProperty("user.home"), path);
61  			if (!dir.exists()) {
62  				if (!dir.mkdirs()) {
63  					throw new SecurityException();
64  				}
65  			}
66  		} catch (SecurityException e) {
67  			dir = new File(new File(System.getProperty("java.io.tmpdir"), System.getProperty("user.name")), path);
68  		}
69  		if (isWindows) {
70  			dir = new File(dir, "BlueCove");
71  		}
72  		if (!dir.exists()) {
73  			if (!dir.mkdirs()) {
74  				return null;
75  			}
76  		} else if (!dir.isDirectory()) {
77  			dir.delete();
78  			if (!dir.mkdirs()) {
79  				return null;
80  			}
81  		}
82  		return dir;
83  	}
84  
85  	private static File getConfigFile() {
86  		File home = homePath();
87  		if (home == null) {
88  			return null;
89  		}
90  		return new File(home, configFileName);
91  	}
92  
93  	public static String loadDevices(Hashtable devices) {
94  		File cf = getConfigFile();
95  		if (cf == null || !cf.exists()) {
96  			return null;
97  		}
98  		Reader fr = null;
99  		LineNumberReader lnr = null;
100 		try {
101 			lnr = new LineNumberReader(fr = new FileReader(cf));
102 			devices.clear();
103 			String line = lnr.readLine();
104 			String selected = null;
105 			while (line != null) {
106 				if (line.startsWith(devicePrefix)) {
107 					DeviceInfo di = new DeviceInfo();
108 					di.loadFromLine(line.substring(devicePrefix.length()));
109 					if (di.isValid()) {
110 						devices.put(di.btAddress.toLowerCase(), di);
111 					}
112 				} else if (line.startsWith(selectedPrefix)) {
113 					selected = line.substring(selectedPrefix.length());
114 				} else {
115 					int p = line.indexOf('=');
116 					if (p != -1) {
117 						properties.put(line.substring(0, p), line.substring(p + 1));
118 					}
119 				}
120 				line = lnr.readLine();
121 			}
122 			return selected;
123 		} catch (Throwable e) {
124 			Logger.debug(e);
125 			return null;
126 		} finally {
127 			if (lnr != null) {
128 				try {
129 					lnr.close();
130 				} catch (IOException e) {
131 				}
132 			}
133 			if (fr != null) {
134 				try {
135 					fr.close();
136 				} catch (IOException e) {
137 				}
138 			}
139 		}
140 	}
141 
142 	public static void storeDevices(Hashtable devices, String selected) {
143 		File cf = getConfigFile();
144 		if (cf == null) {
145 			return;
146 		}
147 		FileWriter fw = null;
148 		try {
149 			fw = new FileWriter(cf, false);
150 			for (Enumeration i = devices.keys(); i.hasMoreElements();) {
151 				String addr = (String) i.nextElement();
152 				DeviceInfo di = (DeviceInfo) devices.get(addr);
153 				fw.write(devicePrefix + di.saveAsLine() + "\n");
154 			}
155 			if (selected != null) {
156 				fw.write(selectedPrefix + selected + "\n");
157 			}
158 			for (Enumeration en = properties.propertyNames(); en.hasMoreElements();) {
159 				String name = (String) en.nextElement();
160 				fw.write(name + "=" + properties.getProperty(name) + "\n");
161 			}
162 			fw.flush();
163 		} catch (Throwable e) {
164 			Logger.debug(e);
165 			return;
166 		} finally {
167 			if (fw != null) {
168 				try {
169 					fw.close();
170 				} catch (IOException e) {
171 				}
172 			}
173 		}
174 	}
175 
176 	public static String getProperty(String key, String defaultValue) {
177 		return properties.getProperty(key, defaultValue);
178 	}
179 
180 	public static void setProperty(String key, String value) {
181 		properties.setProperty(key, value);
182 	}
183 }