001: package vqwiki.file;
002:
003: import org.apache.log4j.Logger;
004: import vqwiki.AbstractNotify;
005: import vqwiki.Environment;
006: import vqwiki.WikiException;
007:
008: import java.io.*;
009: import java.util.*;
010:
011: /**
012: * Implementation of Notify which stores notification records in text files.
013: * Notification files use the same filename as the associated topic and use
014: * the extension ".ntf".
015: *
016: * @author Robert E Brewer
017: * @version 0.1
018: */
019: public class FileNotify extends AbstractNotify {
020:
021: private static final Logger logger = Logger
022: .getLogger(FileNotify.class);
023: protected Set membersToNotify = new HashSet();
024: protected File notifyFile;
025:
026: /**
027: * No-arg constructor for compatibility only, always use FileNotify(newTopicName) instead.
028: */
029: public FileNotify() {
030: }
031:
032: /**
033: * Instantiates and reads in a Notify object.
034: *
035: * @param newTopicName the topic name with which this notification is associated
036: * @exception vqwiki.WikiException if the file could not be opened or read
037: */
038: public FileNotify(String virtualWiki, String newTopicName)
039: throws WikiException {
040: this .topicName = newTopicName;
041: this .virtualWiki = virtualWiki;
042: this .notifyFile = makeNotifyFile();
043: if (this .notifyFile.exists())
044: readNotifyFile();
045: }
046:
047: /**
048: *
049: */
050: private File makeNotifyFile() {
051: return FileHandler.getPathFor(virtualWiki, topicName + ".ntf");
052: }
053:
054: /**
055: *
056: */
057: private synchronized boolean createNotifyFile()
058: throws WikiException {
059: try {
060: notifyFile.createNewFile();
061: if (notifyFile.exists())
062: return true;
063: } catch (IOException e) {
064: }
065: throw new WikiException("Notify File could not be created.");
066: }
067:
068: /**
069: *
070: */
071: private synchronized boolean readNotifyFile() throws WikiException {
072: try {
073: String aMember;
074: BufferedReader reader = new BufferedReader(
075: new InputStreamReader(new FileInputStream(
076: notifyFile)));
077: do {
078: aMember = reader.readLine();
079: if (aMember != null)
080: membersToNotify.add(aMember);
081: } while (aMember != null);
082: reader.close();
083: } catch (IOException e) {
084: throw new WikiException("Notify File could not be read.");
085: }
086: return true;
087: }
088:
089: /**
090: *
091: */
092: public Collection getMembers() throws Exception {
093: return this .membersToNotify;
094: }
095:
096: /**
097: *
098: */
099: private synchronized boolean writeNotifyFile() throws WikiException {
100: try {
101: if (!notifyFile.exists())
102: createNotifyFile();
103: BufferedWriter writer = new BufferedWriter(
104: new OutputStreamWriter(new FileOutputStream(
105: notifyFile), Environment.getInstance()
106: .getFileEncoding()));
107: Iterator anIterator = membersToNotify.iterator();
108: while (anIterator.hasNext()) {
109: String aMember = (String) anIterator.next();
110: writer.write(aMember);
111: writer.newLine();
112: }
113: writer.close();
114: } catch (IOException e) {
115: throw new WikiException("Notify File could not be written");
116: }
117: return true;
118: }
119:
120: /**
121: * Adds a user to the list of members to be notified when the associated topic changes.
122: *
123: * @param userName the name of the user to add
124: * @exception vqwiki.WikiException if the file could not be written
125: */
126: public void addMember(String userName) throws WikiException {
127: membersToNotify.add(userName);
128: writeNotifyFile();
129: }
130:
131: /**
132: * Removes a user from the list of members to be notified when the associated topic changes.
133: *
134: * @param userName the name of the user to remove
135: * @exception vqwiki.WikiException if the file could not be written
136: */
137: public synchronized void removeMember(String userName)
138: throws WikiException {
139: membersToNotify.remove(userName);
140: if (membersToNotify.isEmpty()) {
141: notifyFile.delete();
142: } else {
143: writeNotifyFile();
144: }
145: }
146:
147: /**
148: * Checks whether the user is in the list of members to be notified when the associated topic changes.
149: *
150: * @param userName the name of the user to check
151: * @return boolean True if the user is in the list.
152: */
153: public boolean isMember(String userName) {
154: return membersToNotify.contains(userName);
155: }
156:
157: /**
158: * Retrieves the home directory of the VQWiki installation.
159: *
160: * @return String the home directory
161: */
162: protected String fileBase() {
163: return Environment.getInstance().getHomeDir();
164: }
165:
166: /**
167: *
168: */
169: public static Collection getAll(String virtualWiki)
170: throws Exception {
171: Collection all = new ArrayList();
172: File path = FileHandler.getPathFor(virtualWiki, "");
173: File[] list = path.listFiles(new FileExtensionFilter("ntf"));
174: for (int i = 0; i < list.length; i++) {
175: File file = list[i];
176: String fileName = file.getName();
177: all.add(new FileNotify(virtualWiki, fileName.substring(0,
178: fileName.length() - 4)));
179: }
180: return all;
181: }
182: }
|