1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
package net.sourceforge.pebble.index; |
33 |
|
|
34 |
|
import net.sourceforge.pebble.domain.Blog; |
35 |
|
import net.sourceforge.pebble.domain.StaticPage; |
36 |
|
import org.apache.commons.logging.Log; |
37 |
|
import org.apache.commons.logging.LogFactory; |
38 |
|
|
39 |
|
import java.io.*; |
40 |
|
import java.util.*; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@author |
46 |
|
|
|
|
| 38,2% |
Uncovered Elements: 68 (110) |
Complexity: 29 |
Complexity Density: 0,39 |
|
47 |
|
public class StaticPageIndex { |
48 |
|
|
49 |
|
private static final Log log = LogFactory.getLog(StaticPageIndex.class); |
50 |
|
|
51 |
|
private static final String PAGES_INDEX_DIRECTORY_NAME = "pages"; |
52 |
|
private static final String NAME_TO_ID_INDEX_FILE_NAME = "name.index"; |
53 |
|
private static final String LOCK_FILE_NAME = "pages.lock"; |
54 |
|
private static final int MAXIMUM_LOCK_ATTEMPTS = 3; |
55 |
|
|
56 |
|
|
57 |
|
private Blog blog; |
58 |
|
|
59 |
|
|
60 |
|
private Map<String,String> index = new HashMap<String,String>(); |
61 |
|
private int lockAttempts = 0; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
63 |
1334
|
public StaticPageIndex(Blog blog) {... |
64 |
1334
|
this.blog = blog; |
65 |
|
|
66 |
|
|
67 |
1334
|
File indexDirectory = new File(blog.getIndexesDirectory(), PAGES_INDEX_DIRECTORY_NAME); |
68 |
1334
|
if (!indexDirectory.exists()) { |
69 |
1318
|
indexDirectory.mkdirs(); |
70 |
|
} |
71 |
|
|
72 |
1334
|
readIndex(); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@param |
79 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
80 |
18
|
public synchronized void reindex(Collection<StaticPage> staticPages) {... |
81 |
18
|
if (lock()) { |
82 |
|
|
83 |
18
|
index = new HashMap<String,String>(); |
84 |
18
|
for (StaticPage staticPage : staticPages) { |
85 |
0
|
index.put(staticPage.getName(), staticPage.getId()); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
18
|
writeIndex(); |
90 |
18
|
unlock(); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 6 |
Complexity Density: 0,38 |
|
99 |
0
|
public synchronized void index(StaticPage staticPage) {... |
100 |
0
|
if (lock()) { |
101 |
0
|
readIndex(); |
102 |
|
|
103 |
|
|
104 |
0
|
Iterator it = index.keySet().iterator(); |
105 |
0
|
while (it.hasNext()) { |
106 |
0
|
String key = (String)it.next(); |
107 |
0
|
String value = index.get(key); |
108 |
0
|
if (value.equals(staticPage.getId())) { |
109 |
0
|
it.remove(); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
0
|
index.put(staticPage.getName(), staticPage.getId()); |
115 |
0
|
writeIndex(); |
116 |
0
|
unlock(); |
117 |
|
} else { |
118 |
0
|
if (lockAttempts <= MAXIMUM_LOCK_ATTEMPTS) { |
119 |
0
|
try { |
120 |
0
|
Thread.sleep(1000); |
121 |
|
} catch (InterruptedException ie) { |
122 |
|
|
123 |
|
} |
124 |
0
|
index(staticPage); |
125 |
|
} else { |
126 |
0
|
blog.error("Could not index static page - try <a href=\"utilities.secureaction?action=buildIndexes\">rebuilding the indexes</a>."); |
127 |
|
} |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
@param |
135 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0,4 |
|
136 |
0
|
public synchronized void unindex(StaticPage staticPage) {... |
137 |
0
|
if (lock()) { |
138 |
0
|
readIndex(); |
139 |
0
|
index.remove(staticPage.getName()); |
140 |
0
|
writeIndex(); |
141 |
0
|
unlock(); |
142 |
|
} else { |
143 |
0
|
if (lockAttempts <= MAXIMUM_LOCK_ATTEMPTS) { |
144 |
0
|
try { |
145 |
0
|
Thread.sleep(1000); |
146 |
|
} catch (InterruptedException ie) { |
147 |
|
|
148 |
|
} |
149 |
0
|
unindex(staticPage); |
150 |
|
} else { |
151 |
0
|
blog.reindexStaticPages(); |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
|
|
| 25% |
Uncovered Elements: 12 (16) |
Complexity: 4 |
Complexity Density: 0,33 |
|
159 |
1334
|
private void readIndex() {... |
160 |
1334
|
log.info("Reading index from disk"); |
161 |
1334
|
File indexFile = getIndexFile(); |
162 |
1334
|
if (indexFile.exists()) { |
163 |
0
|
try { |
164 |
0
|
BufferedReader reader = new BufferedReader(new FileReader(indexFile)); |
165 |
0
|
String indexEntry = reader.readLine(); |
166 |
0
|
while (indexEntry != null) { |
167 |
0
|
String[] parts = indexEntry.split("="); |
168 |
0
|
index.put(parts[0], parts[1]); |
169 |
|
|
170 |
0
|
indexEntry = reader.readLine(); |
171 |
|
} |
172 |
|
|
173 |
0
|
reader.close(); |
174 |
|
} catch (Exception e) { |
175 |
0
|
log.error("Error while reading index", e); |
176 |
|
} |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
|
|
| 66,7% |
Uncovered Elements: 3 (9) |
Complexity: 2 |
Complexity Density: 0,22 |
|
183 |
18
|
private void writeIndex() {... |
184 |
18
|
try { |
185 |
18
|
File indexFile = getIndexFile(); |
186 |
18
|
BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile)); |
187 |
|
|
188 |
18
|
for (String name : index.keySet()) { |
189 |
0
|
writer.write(name + "=" + index.get(name)); |
190 |
0
|
writer.newLine(); |
191 |
|
} |
192 |
|
|
193 |
18
|
writer.flush(); |
194 |
18
|
writer.close(); |
195 |
|
} catch (Exception e) { |
196 |
0
|
log.error("Error while writing index", e); |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@return |
205 |
|
|
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0
|
public String getStaticPage(String name) {... |
208 |
0
|
return index.get(name); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@return |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
0
|
public List<String> getStaticPages() {... |
217 |
0
|
return new LinkedList<String>(index.values()); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
@return |
225 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
0
|
public boolean contains(String name) {... |
227 |
0
|
return index.containsKey(name); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
@return |
234 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
0
|
public int getNumberOfStaticPages() {... |
236 |
0
|
return index.size(); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
239 |
1352
|
private File getIndexFile() {... |
240 |
1352
|
File indexDirectory = new File(blog.getIndexesDirectory(), PAGES_INDEX_DIRECTORY_NAME); |
241 |
1352
|
return new File(indexDirectory, NAME_TO_ID_INDEX_FILE_NAME); |
242 |
|
} |
243 |
|
|
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
|
244 |
18
|
private boolean lock() {... |
245 |
18
|
File lockFile = new File(blog.getIndexesDirectory(), LOCK_FILE_NAME); |
246 |
18
|
boolean success = false; |
247 |
18
|
try { |
248 |
18
|
success = lockFile.createNewFile(); |
249 |
18
|
if (!success) { |
250 |
0
|
lockAttempts++; |
251 |
|
} |
252 |
|
} catch (IOException ioe) { |
253 |
0
|
log.warn("Error while creating lock file", ioe); |
254 |
|
} |
255 |
|
|
256 |
18
|
return success; |
257 |
|
} |
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
259 |
18
|
private void unlock() {... |
260 |
18
|
File lockFile = new File(blog.getIndexesDirectory(), LOCK_FILE_NAME); |
261 |
18
|
lockFile.delete(); |
262 |
18
|
lockAttempts = 0; |
263 |
|
} |
264 |
|
|
265 |
|
} |