| 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 | |
|
| 46 | |
|
| 47 | |
public class StaticPageIndex { |
| 48 | |
|
| 49 | 4 | 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 | 2720 | private Map<String,String> index = new HashMap<String,String>(); |
| 61 | 2720 | private int lockAttempts = 0; |
| 62 | |
|
| 63 | 2720 | public StaticPageIndex(Blog blog) { |
| 64 | 2720 | this.blog = blog; |
| 65 | |
|
| 66 | |
|
| 67 | 2720 | File indexDirectory = new File(blog.getIndexesDirectory(), PAGES_INDEX_DIRECTORY_NAME); |
| 68 | 2720 | if (!indexDirectory.exists()) { |
| 69 | 2688 | indexDirectory.mkdirs(); |
| 70 | |
} |
| 71 | |
|
| 72 | 2720 | readIndex(); |
| 73 | 2720 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public synchronized void reindex(Collection<StaticPage> staticPages) { |
| 81 | 36 | if (lock()) { |
| 82 | |
|
| 83 | 36 | index = new HashMap<String,String>(); |
| 84 | 36 | for (StaticPage staticPage : staticPages) { |
| 85 | 0 | index.put(staticPage.getName(), staticPage.getId()); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | 36 | writeIndex(); |
| 90 | 36 | unlock(); |
| 91 | |
} |
| 92 | 36 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
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 | 0 | } |
| 112 | |
|
| 113 | |
|
| 114 | 0 | index.put(staticPage.getName(), staticPage.getId()); |
| 115 | 0 | writeIndex(); |
| 116 | 0 | unlock(); |
| 117 | 0 | } else { |
| 118 | 0 | if (lockAttempts <= MAXIMUM_LOCK_ATTEMPTS) { |
| 119 | |
try { |
| 120 | 0 | Thread.sleep(1000); |
| 121 | 0 | } catch (InterruptedException ie) { |
| 122 | |
|
| 123 | 0 | } |
| 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 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
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 | |
try { |
| 145 | 0 | Thread.sleep(1000); |
| 146 | 0 | } catch (InterruptedException ie) { |
| 147 | |
|
| 148 | 0 | } |
| 149 | 0 | unindex(staticPage); |
| 150 | |
} else { |
| 151 | 0 | blog.reindexStaticPages(); |
| 152 | |
} |
| 153 | |
} |
| 154 | 0 | } |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
private void readIndex() { |
| 160 | 2720 | log.info("Reading index from disk"); |
| 161 | 2720 | File indexFile = getIndexFile(); |
| 162 | 2720 | if (indexFile.exists()) { |
| 163 | |
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 | 0 | } |
| 172 | |
|
| 173 | 0 | reader.close(); |
| 174 | 0 | } catch (Exception e) { |
| 175 | 0 | log.error("Error while reading index", e); |
| 176 | 0 | } |
| 177 | |
} |
| 178 | 2720 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
private void writeIndex() { |
| 184 | |
try { |
| 185 | 36 | File indexFile = getIndexFile(); |
| 186 | 36 | BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile)); |
| 187 | |
|
| 188 | 36 | for (String name : index.keySet()) { |
| 189 | 0 | writer.write(name + "=" + index.get(name)); |
| 190 | 0 | writer.newLine(); |
| 191 | |
} |
| 192 | |
|
| 193 | 36 | writer.flush(); |
| 194 | 36 | writer.close(); |
| 195 | 0 | } catch (Exception e) { |
| 196 | 0 | log.error("Error while writing index", e); |
| 197 | 36 | } |
| 198 | 36 | } |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
public String getStaticPage(String name) { |
| 208 | 0 | return index.get(name); |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public List<String> getStaticPages() { |
| 217 | 0 | return new LinkedList<String>(index.values()); |
| 218 | |
} |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
public boolean contains(String name) { |
| 227 | 0 | return index.containsKey(name); |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public int getNumberOfStaticPages() { |
| 236 | 0 | return index.size(); |
| 237 | |
} |
| 238 | |
|
| 239 | |
private File getIndexFile() { |
| 240 | 2756 | File indexDirectory = new File(blog.getIndexesDirectory(), PAGES_INDEX_DIRECTORY_NAME); |
| 241 | 2756 | return new File(indexDirectory, NAME_TO_ID_INDEX_FILE_NAME); |
| 242 | |
} |
| 243 | |
|
| 244 | |
private boolean lock() { |
| 245 | 36 | File lockFile = new File(blog.getIndexesDirectory(), LOCK_FILE_NAME); |
| 246 | 36 | boolean success = false; |
| 247 | |
try { |
| 248 | 36 | success = lockFile.createNewFile(); |
| 249 | 36 | if (!success) { |
| 250 | 0 | lockAttempts++; |
| 251 | |
} |
| 252 | 0 | } catch (IOException ioe) { |
| 253 | 0 | log.warn("Error while creating lock file", ioe); |
| 254 | 36 | } |
| 255 | |
|
| 256 | 36 | return success; |
| 257 | |
} |
| 258 | |
|
| 259 | |
private void unlock() { |
| 260 | 36 | File lockFile = new File(blog.getIndexesDirectory(), LOCK_FILE_NAME); |
| 261 | 36 | lockFile.delete(); |
| 262 | 36 | lockAttempts = 0; |
| 263 | 36 | } |
| 264 | |
|
| 265 | |
} |