1 /**
2 *
3 * Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.store.nntp.newsrc;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 /**
28 * Base class implementation of a standard news reader news rc file. This is
29 * used to track newsgroup subscriptions and SEEN flags for articles. This is an
30 * abstract class designed for subclasses to bridge to the physical store type
31 * used for the newsgroup information.
32 */
33 public abstract class NNTPNewsrc {
34
35
36 Map groups = new HashMap();
37
38
39 boolean dirty = false;
40
41 /**
42 * Base class constructor for NNTPNewsrc items. Subclasses provide their own
43 * domain-specific intialization.
44 */
45 protected NNTPNewsrc() {
46 }
47
48 /**
49 * Load the data from the newsrc file and parse into an instore group
50 * database.
51 */
52 public void load() {
53 BufferedReader in = null;
54
55 try {
56 in = getInputReader();
57
58 String line = in.readLine();
59
60 while (line != null) {
61
62
63 NNTPNewsrcGroup group = NNTPNewsrcGroup.parse(this, line);
64
65
66 if (group != null) {
67 groups.put(group.getName(), group);
68 }
69
70 line = in.readLine();
71 }
72
73 in.close();
74 } catch (IOException e) {
75
76
77
78 } finally {
79 if (in != null) {
80 try {
81 in.close();
82 } catch (IOException e) {
83
84 }
85 }
86 }
87 }
88
89 /**
90 * Save the newsrc file data back to the original source file.
91 *
92 * @exception IOException
93 */
94 public void save() throws IOException {
95 Writer out = getOutputWriter();
96
97 Iterator i = groups.values().iterator();
98
99 while (i.hasNext()) {
100 NNTPNewsrcGroup group = (NNTPNewsrcGroup) i.next();
101 group.save(out);
102 }
103
104 out.close();
105 }
106
107 /**
108 * Abstract open method intended for sub class initialization. The subclass
109 * is responsible for creating the BufferedReaded used to read the .newsrc
110 * file.
111 *
112 * @return A BufferedReader for reading the .newsrc file.
113 * @exception IOException
114 */
115 abstract public BufferedReader getInputReader() throws IOException;
116
117 /**
118 * Abstract open for output method intended for subclass implementation. The
119 * subclasses are reponsible for opening the output stream and creating an
120 * appropriate Writer for saving the .newsrc file.
121 *
122 * @return A Writer target at the .newsrc file save location.
123 * @exception IOException
124 */
125 abstract public Writer getOutputWriter() throws IOException;
126
127 /**
128 * Retrieve the newsrc group information for a named group. If the file does
129 * not currently include this group, an unsubscribed group will be added to
130 * the file.
131 *
132 * @param name
133 * The name of the target group.
134 *
135 * @return The NNTPNewsrcGroup item corresponding to this name.
136 */
137 public NNTPNewsrcGroup getGroup(String name) {
138 NNTPNewsrcGroup group = (NNTPNewsrcGroup) groups.get(name);
139
140
141
142 if (group == null) {
143 group = new NNTPNewsrcGroup(this, name, null, false);
144 groups.put(name, group);
145
146 dirty = true;
147 }
148 return group;
149 }
150
151 /**
152 * Mark this newsrc database as dirty.
153 */
154 public void setDirty() {
155 dirty = true;
156 }
157
158 /**
159 * Close the newsrc file, persisting it back to disk if the file has
160 * changed.
161 */
162 public void close() {
163 if (dirty) {
164 try {
165 save();
166 } catch (IOException e) {
167
168 }
169 }
170 }
171
172 /**
173 * Retrieve the current set of loaded groups.
174 *
175 * @return An iterator for traversing the group set.
176 */
177 public Iterator getGroups() {
178 return groups.values().iterator();
179 }
180 }