File Source: pluginproperties.java
1 /*
2 * Copyright (c) 2003-2006, 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 */
/*
P/P * Method: net.sourceforge.pebble.PluginProperties__static_init
*
* Postconditions:
* init'ed(log)
*/
54 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 */
/*
P/P * Method: void net.sourceforge.pebble.PluginProperties(Blog)
*
* Preconditions:
* (soft) blog != null
*
* Postconditions:
* this.blog == blog
* (soft) this.blog != null
* this.properties == &new Properties(loadProperties#1)
* new Properties(loadProperties#1) num objects == 1
*/
71 public PluginProperties(Blog blog) {
72 this.blog = blog;
73 loadProperties();
74 }
75
76 /**
77 * Helper method to load the properties from disk.
78 */
79 private void loadProperties() {
80 try {
/*
P/P * Method: void loadProperties()
*
* Preconditions:
* (soft) this.blog != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@54 != null
*
* Postconditions:
* this.properties == One-of{&new Properties(loadProperties#1), old this.properties}
* new Properties(loadProperties#1) num objects <= 1
*
* Test Vectors:
* java.io.File:exists(...)@90: {1}, {0}
*/
81 properties = new Properties();
82 properties.setProperty(ContentSpamListener.REGEX_LIST_KEY, ContentSpamListener.DEFAULT_REGEX_LIST);
83 properties.setProperty(ContentSpamListener.THRESHOLD_KEY, "" + ContentSpamListener.DEFAULT_THRESHOLD);
84 properties.setProperty(LinkSpamListener.COMMENT_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD);
85 properties.setProperty(LinkSpamListener.TRACKBACK_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD);
86 properties.setProperty(SpamScoreListener.COMMENT_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD);
87 properties.setProperty(SpamScoreListener.TRACKBACK_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD);
88
89 File propertiesFile = new File(blog.getPluginPropertiesFile());
90 if (!propertiesFile.exists()) {
91 return;
92 }
93
94 FileInputStream fin = new FileInputStream(propertiesFile);
95 properties.load(fin);
96 fin.close();
97 } catch (FileNotFoundException fnfe) {
98 // do nothing - there are no plugin properties to load
99 } catch (IOException e) {
100 log.error(e.getMessage());
101 }
102 }
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() {
/*
P/P * Method: String getPropertiesAsString()
*
* Preconditions:
* this.properties != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@114: {1}, {0}
*/
110 StringBuffer buf = new StringBuffer();
111 List keys = new ArrayList(properties.keySet());
112 Collections.sort(keys);
113 Iterator it = keys.iterator();
114 while (it.hasNext()) {
115 String key = (String) it.next();
116 buf.append(key);
117 buf.append("=");
118 buf.append(properties.getProperty(key));
119 buf.append(System.getProperty("line.separator"));
120 }
121
122 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) {
/*
P/P * Method: bool hasProperty(String)
*
* Preconditions:
* this.properties != null
*
* Postconditions:
* init'ed(return_value)
*/
132 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) {
/*
P/P * Method: String getProperty(String)
*
* Preconditions:
* this.properties != null
*
* Postconditions:
* init'ed(return_value)
*/
142 return properties.getProperty(name);
143 }
144
145 public Properties getProperties() {
/*
P/P * Method: Properties getProperties()
*
* Preconditions:
* init'ed(this.properties)
*
* Postconditions:
* return_value == this.properties
* init'ed(return_value)
*/
146 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) {
/*
P/P * Method: void setProperty(String, String)
*
* Preconditions:
* this.properties != null
*/
156 properties.setProperty(name, value);
157 }
158
159 /**
160 * Helper method to store the properties to disk.
161 */
162 public void store() {
163 try {
/*
P/P * Method: void store()
*
* Preconditions:
* (soft) this.blog != null
* (soft) this.properties != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@54 != null
*/
164 FileOutputStream fout = new FileOutputStream(blog.getPluginPropertiesFile());
165 if (fout != null) {
166 properties.store(fout, "Plugin properties");
167 fout.flush();
168 fout.close();
169 }
170 } catch (FileNotFoundException fnfe) {
171 } catch (IOException e) {
172 log.error(e.getMessage());
173 }
174 }
175
176 }
SofCheck Inspector Build Version : 2.22510
| pluginproperties.java |
2010-Jun-25 19:40:34 |
| pluginproperties.class |
2010-Jul-19 20:23:40 |