1
2
3
4
5
6
7
8
9
10 package com.atlassian.core.user.preferences;
11
12 import com.atlassian.core.AtlassianCoreException;
13 import com.atlassian.core.util.ClassLoaderUtils;
14 import com.atlassian.core.util.XMLUtils;
15 import com.opensymphony.module.propertyset.PropertySet;
16 import com.opensymphony.module.propertyset.PropertySetManager;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.NodeList;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 public class DefaultPreferences implements Preferences
27 {
28 static Preferences _instance;
29 private PropertySet backingPS;
30
31 public DefaultPreferences()
32 {
33 backingPS = PropertySetManager.getInstance("memory", null);
34
35 InputStream defaults = ClassLoaderUtils.getResourceAsStream("preferences-default.xml", this.getClass());
36 try
37 {
38 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
39 Document xmlDoc = db.parse(defaults);
40 Element root = xmlDoc.getDocumentElement();
41 NodeList preferences = root.getElementsByTagName("preference");
42 for (int i = 0; i < preferences.getLength(); i++)
43 {
44 Element preference = (Element) preferences.item(i);
45 String operation = preference.getAttribute("type");
46 if (operation == null)
47 operation = "String";
48 String name = XMLUtils.getContainedText(preference, "name");
49 String value = XMLUtils.getContainedText(preference, "value");
50 if ("String".equals(operation))
51 backingPS.setString(name, value);
52 else if ("Long".equals(operation))
53 backingPS.setLong(name, new Long(value).longValue());
54 else if ("Boolean".equals(operation))
55 backingPS.setBoolean(name, new Boolean(value).booleanValue());
56 }
57 }
58 catch (Exception e)
59 {
60 e.printStackTrace();
61 }
62 try
63 {
64 defaults.close();
65 }
66 catch (IOException e)
67 {
68 e.printStackTrace();
69 }
70 }
71
72 public static Preferences getPreferences()
73 {
74 if (_instance == null)
75 {
76 _instance = new DefaultPreferences();
77 }
78
79 return _instance;
80 }
81
82 public long getLong(String key)
83 {
84 return backingPS.getLong(key);
85 }
86
87 public void setLong(String key, long value) throws AtlassianCoreException
88 {
89 throw new AtlassianCoreException("Trying to set a Default preference this is not allowed");
90 }
91
92 public String getString(String key)
93 {
94 return backingPS.getString(key);
95 }
96
97 public void setString(String key, String value) throws AtlassianCoreException
98 {
99 throw new AtlassianCoreException("Trying to set a Default preference this is not allowed");
100 }
101
102 public boolean getBoolean(String key)
103 {
104 return backingPS.getBoolean(key);
105 }
106
107 public void setBoolean(String key, boolean b) throws AtlassianCoreException
108 {
109 throw new AtlassianCoreException("Trying to set a Default preference this is not allowed");
110 }
111
112 public void remove(String key) throws AtlassianCoreException
113 {
114 throw new AtlassianCoreException("Trying to set a Default preference this is not allowed");
115 }
116 }