/plugins/LucenePlugin/tags/20090224/main/src/java/com/townsfolkdesigns/lucene/jedit/manager/OptionsManager.java

# · Java · 202 lines · 154 code · 22 blank · 26 comment · 14 complexity · 3ab9b5cc7739ab3b3b43d89fcc275bc5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008 Eric Berry <elberry@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package com.townsfolkdesigns.lucene.jedit.manager;
  23. import java.beans.XMLDecoder;
  24. import java.beans.XMLEncoder;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileOutputStream;
  28. import java.io.ObjectOutputStream;
  29. import java.lang.reflect.Field;
  30. import java.lang.reflect.Modifier;
  31. import java.util.HashMap;
  32. import java.util.LinkedList;
  33. import java.util.List;
  34. import java.util.Map;
  35. import org.gjt.sp.util.Log;
  36. import com.townsfolkdesigns.lucene.jedit.LucenePlugin;
  37. public class OptionsManager {
  38. private transient static OptionsManager instance = new OptionsManager();
  39. private boolean automaticallyIndexProjects;
  40. private List<String> directories;
  41. private String fileNameRegex;
  42. private long indexInterval;
  43. private transient File settingsFile;
  44. private OptionsManager() {
  45. setDefaults();
  46. // try to load from storage.
  47. load();
  48. }
  49. public static OptionsManager getInstance() {
  50. return instance;
  51. }
  52. public synchronized void clear() {
  53. setDefaults();
  54. save();
  55. }
  56. public synchronized List<String> getDirectories() {
  57. return directories;
  58. }
  59. public synchronized String getFileNameRegex() {
  60. return fileNameRegex;
  61. }
  62. public synchronized long getIndexInterval() {
  63. return indexInterval;
  64. }
  65. /**
  66. * @return the settingsFile
  67. */
  68. public synchronized File getSettingsFile() {
  69. return settingsFile;
  70. }
  71. public synchronized boolean isAutomaticallyIndexProjects() {
  72. return automaticallyIndexProjects;
  73. }
  74. public synchronized void load() {
  75. File settingsFile = getSettingsFile();
  76. Map<String, Object> settingsMap = new HashMap<String, Object>();
  77. FileInputStream fis = null;
  78. XMLDecoder decoder = null;
  79. try {
  80. fis = new FileInputStream(settingsFile);
  81. decoder = new XMLDecoder(fis);
  82. settingsMap = (Map<String, Object>) decoder.readObject();
  83. } catch (Exception e) {
  84. Log.log(Log.ERROR, this, "Error closing ObjectOutputStream", e);
  85. } finally {
  86. if (decoder != null) {
  87. try {
  88. decoder.close();
  89. } catch (Exception e) {
  90. Log.log(Log.ERROR, this, "Error closing XMLEncoder", e);
  91. }
  92. }
  93. if (fis != null) {
  94. try {
  95. fis.close();
  96. } catch (Exception e) {
  97. Log.log(Log.ERROR, this, "Error closing ObjectOutputStream", e);
  98. }
  99. }
  100. }
  101. Field field = null;
  102. for (String fieldName : settingsMap.keySet()) {
  103. try {
  104. field = getClass().getDeclaredField(fieldName);
  105. if (field != null) {
  106. field.set(this, settingsMap.get(fieldName));
  107. } else {
  108. Log.log(Log.WARNING, this, "The given field \"" + fieldName
  109. + "\" was not found, are you loading settings from a new version?");
  110. }
  111. } catch (Exception e) {
  112. Log.log(Log.ERROR, this, "Error loading value for field: " + field.getName(), e);
  113. }
  114. }
  115. }
  116. public synchronized void save() {
  117. File settingsFile = getSettingsFile();
  118. Map<String, Object> settingsMap = new HashMap<String, Object>();
  119. Field[] fields = getClass().getDeclaredFields();
  120. for (Field field : fields) {
  121. if (!Modifier.isTransient(field.getModifiers())) {
  122. try {
  123. settingsMap.put(field.getName(), field.get(this));
  124. } catch (Exception e) {
  125. Log.log(Log.ERROR, this, "Error getting value for field: " + field.getName(), e);
  126. }
  127. }
  128. }
  129. FileOutputStream fos = null;
  130. XMLEncoder encoder = null;
  131. try {
  132. fos = new FileOutputStream(settingsFile, false);
  133. encoder = new XMLEncoder(fos);
  134. encoder.writeObject(settingsMap);
  135. encoder.flush();
  136. } catch (Exception e) {
  137. Log.log(Log.ERROR, this, "Error storing serialized settings to file: " + settingsFile.getPath(), e);
  138. } finally {
  139. if (encoder != null) {
  140. try {
  141. encoder.close();
  142. } catch (Exception e) {
  143. Log.log(Log.ERROR, this, "Error closing XMLEncoder", e);
  144. }
  145. }
  146. if (fos != null) {
  147. try {
  148. fos.close();
  149. } catch (Exception e) {
  150. Log.log(Log.ERROR, this, "Error closing ObjectOutputStream", e);
  151. }
  152. }
  153. }
  154. }
  155. public synchronized void setAutomaticallyIndexProjects(boolean automaticallyIndexProjects) {
  156. this.automaticallyIndexProjects = automaticallyIndexProjects;
  157. }
  158. public synchronized void setDirectories(List<String> directories) {
  159. this.directories = directories;
  160. }
  161. public synchronized void setFileNameRegex(String fileNameRegex) {
  162. this.fileNameRegex = fileNameRegex;
  163. }
  164. public synchronized void setIndexInterval(long indexInterval) {
  165. this.indexInterval = indexInterval;
  166. }
  167. private void setDefaults() {
  168. // default values.
  169. setAutomaticallyIndexProjects(true);
  170. setDirectories(new LinkedList<String>());
  171. setFileNameRegex(".*");
  172. setIndexInterval(5 * 60 * 1000); // 5 minutes.
  173. File pluginHome = new LucenePlugin().getPluginHome();
  174. File settingsFile = new File(pluginHome, "settings.xml");
  175. setSettingsFile(settingsFile);
  176. }
  177. void setSettingsFile(File settingsFile) {
  178. this.settingsFile = settingsFile;
  179. }
  180. }