Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PluginProperties |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright (c) 2003-2011, Simon Brown | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * - Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * - Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * - Neither the name of Pebble nor the names of its contributors may | |
17 | * be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | * POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | package net.sourceforge.pebble; | |
33 | ||
34 | import net.sourceforge.pebble.domain.Blog; | |
35 | import net.sourceforge.pebble.event.response.ContentSpamListener; | |
36 | import net.sourceforge.pebble.event.response.SpamScoreListener; | |
37 | import net.sourceforge.pebble.event.response.LinkSpamListener; | |
38 | import org.apache.commons.logging.Log; | |
39 | import org.apache.commons.logging.LogFactory; | |
40 | ||
41 | import java.io.*; | |
42 | import java.util.*; | |
43 | ||
44 | /** | |
45 | * Contains properties that can be used by Pebble plugins. | |
46 | * | |
47 | * @author Simon Brown | |
48 | */ | |
49 | public class PluginProperties { | |
50 | ||
51 | /** | |
52 | * the log used by this class | |
53 | */ | |
54 | 4 | private static final Log log = LogFactory.getLog(PluginProperties.class); |
55 | ||
56 | /** | |
57 | * the Properties object that backs this instance | |
58 | */ | |
59 | private Properties properties; | |
60 | ||
61 | /** | |
62 | * the owning blog | |
63 | */ | |
64 | private Blog blog; | |
65 | ||
66 | /** | |
67 | * Creates a new instance with the specified owning blog. | |
68 | * | |
69 | * @param blog the owning Blog instance | |
70 | */ | |
71 | 2720 | public PluginProperties(Blog blog) { |
72 | 2720 | this.blog = blog; |
73 | 2720 | loadProperties(); |
74 | 2720 | } |
75 | ||
76 | /** | |
77 | * Helper method to load the properties from disk. | |
78 | */ | |
79 | private void loadProperties() { | |
80 | try { | |
81 | 2720 | properties = new Properties(); |
82 | 2720 | properties.setProperty(ContentSpamListener.REGEX_LIST_KEY, ContentSpamListener.DEFAULT_REGEX_LIST); |
83 | 2720 | properties.setProperty(ContentSpamListener.THRESHOLD_KEY, "" + ContentSpamListener.DEFAULT_THRESHOLD); |
84 | 2720 | properties.setProperty(LinkSpamListener.COMMENT_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD); |
85 | 2720 | properties.setProperty(LinkSpamListener.TRACKBACK_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD); |
86 | 2720 | properties.setProperty(SpamScoreListener.COMMENT_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD); |
87 | 2720 | properties.setProperty(SpamScoreListener.TRACKBACK_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD); |
88 | ||
89 | 2720 | File propertiesFile = new File(blog.getPluginPropertiesFile()); |
90 | 2720 | if (!propertiesFile.exists()) { |
91 | 2720 | return; |
92 | } | |
93 | ||
94 | 0 | FileInputStream fin = new FileInputStream(propertiesFile); |
95 | 0 | properties.load(fin); |
96 | 0 | fin.close(); |
97 | 0 | } catch (FileNotFoundException fnfe) { |
98 | // do nothing - there are no plugin properties to load | |
99 | 0 | } catch (IOException e) { |
100 | 0 | log.error(e.getMessage()); |
101 | 0 | } |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * Gets properties file as a String in the format it is saved on disk. | |
106 | * | |
107 | * @return a String | |
108 | */ | |
109 | public String getPropertiesAsString() { | |
110 | 0 | StringBuffer buf = new StringBuffer(); |
111 | 0 | List keys = new ArrayList(properties.keySet()); |
112 | 0 | Collections.sort(keys); |
113 | 0 | Iterator it = keys.iterator(); |
114 | 0 | while (it.hasNext()) { |
115 | 0 | String key = (String) it.next(); |
116 | 0 | buf.append(key); |
117 | 0 | buf.append("="); |
118 | 0 | buf.append(properties.getProperty(key)); |
119 | 0 | buf.append(System.getProperty("line.separator")); |
120 | 0 | } |
121 | ||
122 | 0 | return buf.toString(); |
123 | } | |
124 | ||
125 | /** | |
126 | * Determines whether a property with the specified name exists. | |
127 | * | |
128 | * @param name the name of th eproperty to test for | |
129 | * @return true if the property exists, false otherwise | |
130 | */ | |
131 | public boolean hasProperty(String name) { | |
132 | 772 | return properties.containsKey(name); |
133 | } | |
134 | ||
135 | /** | |
136 | * Gets the named property. | |
137 | * | |
138 | * @param name the name of the property | |
139 | * @return the value of the property, or null if it doesn't exist | |
140 | */ | |
141 | public String getProperty(String name) { | |
142 | 2048 | return properties.getProperty(name); |
143 | } | |
144 | ||
145 | public Properties getProperties() { | |
146 | 0 | return properties; |
147 | } | |
148 | ||
149 | /** | |
150 | * Sets the named property. | |
151 | * | |
152 | * @param name the name of the property | |
153 | * @param value the value of the property | |
154 | */ | |
155 | public void setProperty(String name, String value) { | |
156 | 556 | properties.setProperty(name, value); |
157 | 556 | } |
158 | ||
159 | /** | |
160 | * Helper method to store the properties to disk. | |
161 | */ | |
162 | public void store() { | |
163 | try { | |
164 | 140 | FileOutputStream fout = new FileOutputStream(blog.getPluginPropertiesFile()); |
165 | 140 | if (fout != null) { |
166 | 140 | properties.store(fout, "Plugin properties"); |
167 | 140 | fout.flush(); |
168 | 140 | fout.close(); |
169 | } | |
170 | 0 | } catch (FileNotFoundException fnfe) { |
171 | 0 | } catch (IOException e) { |
172 | 0 | log.error(e.getMessage()); |
173 | 140 | } |
174 | 140 | } |
175 | ||
176 | } |