| 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.domain; |
| 33 | |
|
| 34 | |
import org.apache.commons.logging.Log; |
| 35 | |
import org.apache.commons.logging.LogFactory; |
| 36 | |
|
| 37 | |
import java.io.File; |
| 38 | |
import java.io.FileInputStream; |
| 39 | |
import java.io.FileOutputStream; |
| 40 | |
import java.io.IOException; |
| 41 | |
import java.util.*; |
| 42 | |
|
| 43 | |
import net.sourceforge.pebble.PebbleContext; |
| 44 | |
|
| 45 | |
import javax.servlet.http.HttpServletRequest; |
| 46 | |
|
| 47 | |
public abstract class AbstractBlog extends TimePeriod { |
| 48 | |
|
| 49 | |
|
| 50 | |
public static final String BLOG_PROPERTIES_FILE = "blog.properties"; |
| 51 | |
|
| 52 | |
private static final int MAXIMUM_MESSAGES = 20; |
| 53 | |
|
| 54 | |
|
| 55 | 4 | private static Log log = LogFactory.getLog(AbstractBlog.class); |
| 56 | |
|
| 57 | |
|
| 58 | |
private String root; |
| 59 | |
|
| 60 | |
protected static final String FALSE = "false"; |
| 61 | |
protected static final String TRUE = "true"; |
| 62 | |
|
| 63 | |
public static final String NAME_KEY = "name"; |
| 64 | |
public static final String AUTHOR_KEY = "author"; |
| 65 | |
public static final String DESCRIPTION_KEY = "description"; |
| 66 | |
public static final String IMAGE_KEY = "image"; |
| 67 | |
public static final String TIMEZONE_KEY = "timeZone"; |
| 68 | |
public static final String RECENT_BLOG_ENTRIES_ON_HOME_PAGE_KEY = "recentBlogEntriesOnHomePage"; |
| 69 | |
public static final String RECENT_RESPONSES_ON_HOME_PAGE_KEY = "recentResponsesOnHomePage"; |
| 70 | |
public static final String LANGUAGE_KEY = "language"; |
| 71 | |
public static final String COUNTRY_KEY = "country"; |
| 72 | |
public static final String CHARACTER_ENCODING_KEY = "characterEncoding"; |
| 73 | |
public static final String THEME_KEY = "theme"; |
| 74 | |
|
| 75 | 2740 | private List<Message> messages = new LinkedList<Message>(); |
| 76 | |
|
| 77 | |
|
| 78 | |
protected Properties properties; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public AbstractBlog(String root) { |
| 87 | 2740 | super(null); |
| 88 | 2740 | this.root = root; |
| 89 | |
|
| 90 | |
|
| 91 | 2740 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
protected void init() { |
| 99 | 2740 | loadProperties(); |
| 100 | 2740 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
protected void loadProperties() { |
| 107 | |
try { |
| 108 | 2740 | this.properties = new Properties(getDefaultProperties()); |
| 109 | |
|
| 110 | 2740 | File blogPropertiesFile = new File(getRoot(), BLOG_PROPERTIES_FILE); |
| 111 | 2740 | if (!blogPropertiesFile.exists()) { |
| 112 | 2740 | return; |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | FileInputStream fin = new FileInputStream(blogPropertiesFile); |
| 116 | 0 | properties.load(fin); |
| 117 | 0 | fin.close(); |
| 118 | 0 | } catch (IOException ioe) { |
| 119 | 0 | log.error("A blog.properties file at " + getRoot() + " cannot be loaded", ioe); |
| 120 | 0 | } |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
public boolean isConfigured() { |
| 124 | 0 | File blogPropertiesFile = new File(getRoot(), BLOG_PROPERTIES_FILE); |
| 125 | 0 | return blogPropertiesFile.exists(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
protected abstract Properties getDefaultProperties(); |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public abstract String getId(); |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public String getRoot() { |
| 148 | 73444 | return root; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
protected void setRoot(String root) { |
| 157 | 0 | this.root = root; |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public Properties getProperties() { |
| 166 | 4 | return (Properties)properties.clone(); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public String getProperty(String key) { |
| 176 | 29992 | return properties.getProperty(key); |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public void setProperty(String key, String value) { |
| 186 | 2876 | if (key != null) { |
| 187 | 2876 | if (value != null) { |
| 188 | 2876 | properties.setProperty(key, value); |
| 189 | |
} else { |
| 190 | 0 | removeProperty(key); |
| 191 | |
} |
| 192 | |
} |
| 193 | 2876 | } |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public void removeProperty(String key) { |
| 201 | 20 | properties.remove(key); |
| 202 | 20 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
public void storeProperties() throws BlogServiceException { |
| 210 | |
try { |
| 211 | 0 | FileOutputStream fout = new FileOutputStream(new File(getRoot(), BLOG_PROPERTIES_FILE)); |
| 212 | 0 | properties.store(fout, "Properties for " + getName()); |
| 213 | 0 | fout.flush(); |
| 214 | 0 | fout.close(); |
| 215 | 0 | } catch (IOException ioe) { |
| 216 | 0 | log.error(ioe); |
| 217 | 0 | } |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public String getName() { |
| 226 | 44 | return properties.getProperty(NAME_KEY); |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public String getAuthor() { |
| 235 | 12 | return properties.getProperty(AUTHOR_KEY); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
public String getDescription() { |
| 244 | 12 | return properties.getProperty(DESCRIPTION_KEY); |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public String getImage() { |
| 253 | 16 | return properties.getProperty(IMAGE_KEY); |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
public abstract String getUrl(); |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public abstract String getRelativeUrl(); |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public String getDomainName() { |
| 276 | 24 | return PebbleContext.getInstance().getConfiguration().getDomainName(); |
| 277 | |
} |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
public String getProtocol() { |
| 285 | 4 | String url = PebbleContext.getInstance().getConfiguration().getUrl(); |
| 286 | 4 | return url.substring(0, url.indexOf("://")+3); |
| 287 | |
} |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public String getContext() { |
| 295 | 12 | String url = PebbleContext.getInstance().getConfiguration().getUrl(); |
| 296 | 12 | int index = url.indexOf("/", url.indexOf("://")+3); |
| 297 | 12 | if (index == -1) { |
| 298 | 0 | return "/"; |
| 299 | |
} else { |
| 300 | 12 | return url.substring(index); |
| 301 | |
} |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public String getTimeZoneId() { |
| 310 | 307404 | return properties.getProperty(TIMEZONE_KEY); |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
public TimeZone getTimeZone() { |
| 319 | 307272 | return TimeZone.getTimeZone(getTimeZoneId()); |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
public Calendar getCalendar() { |
| 330 | 299868 | return Calendar.getInstance(getTimeZone(), getLocale()); |
| 331 | |
} |
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
public int getRecentBlogEntriesOnHomePage() { |
| 339 | 52 | return Integer.parseInt(properties.getProperty(RECENT_BLOG_ENTRIES_ON_HOME_PAGE_KEY)); |
| 340 | |
} |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
public int getRecentResponsesOnHomePage() { |
| 348 | 4 | return Integer.parseInt(properties.getProperty(RECENT_RESPONSES_ON_HOME_PAGE_KEY)); |
| 349 | |
} |
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
public String getCharacterEncoding() { |
| 357 | 36 | return properties.getProperty(CHARACTER_ENCODING_KEY); |
| 358 | |
} |
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
public String getLanguage() { |
| 366 | 302984 | return properties.getProperty(LANGUAGE_KEY); |
| 367 | |
} |
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
public String getCountry() { |
| 375 | 302976 | return properties.getProperty(COUNTRY_KEY); |
| 376 | |
} |
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
public Locale getLocale() { |
| 384 | 302972 | return new Locale(getLanguage(), getCountry()); |
| 385 | |
} |
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
public String getTheme() { |
| 393 | 0 | return properties.getProperty(THEME_KEY); |
| 394 | |
} |
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
public String getImagesDirectory() { |
| 402 | 2732 | return getRoot() + File.separator + "images"; |
| 403 | |
} |
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
public String getIndexesDirectory() { |
| 411 | 35944 | return getRoot() + File.separator + "indexes"; |
| 412 | |
} |
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
public String getSearchIndexDirectory() { |
| 420 | 688 | return getIndexesDirectory() + File.separator + "search"; |
| 421 | |
} |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
public String getLogsDirectory() { |
| 429 | 4152 | return getRoot() + File.separator + "logs"; |
| 430 | |
} |
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
public abstract List<BlogEntry> getRecentBlogEntries(int numberOfEntries); |
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public List<BlogEntry> getRecentBlogEntries() { |
| 448 | 12 | return getRecentBlogEntries(getRecentBlogEntriesOnHomePage()); |
| 449 | |
} |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
public void setRecentBlogEntries(List<BlogEntry> entries) { |
| 458 | |
|
| 459 | 0 | } |
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
public abstract Date getLastModified(); |
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
public String toString() { |
| 474 | 0 | return getName(); |
| 475 | |
} |
| 476 | |
|
| 477 | |
public synchronized void info(String text) { |
| 478 | 108 | Message message = new Message(MessageType.INFO, text); |
| 479 | 108 | messages.add(0, message); |
| 480 | 108 | log.info(message.getText()); |
| 481 | 108 | truncateMessages(); |
| 482 | 108 | } |
| 483 | |
|
| 484 | |
public synchronized void warn(String text) { |
| 485 | 0 | Message message = new Message(MessageType.WARN, text); |
| 486 | 0 | messages.add(0, message); |
| 487 | 0 | log.warn(message.getText()); |
| 488 | 0 | truncateMessages(); |
| 489 | 0 | } |
| 490 | |
|
| 491 | |
public synchronized void error(String text) { |
| 492 | 4 | Message message = new Message(MessageType.ERROR, text); |
| 493 | 4 | messages.add(0, message); |
| 494 | 4 | log.error(message.getText()); |
| 495 | 4 | truncateMessages(); |
| 496 | 4 | } |
| 497 | |
|
| 498 | |
private void truncateMessages() { |
| 499 | 112 | if (messages.size() > MAXIMUM_MESSAGES) { |
| 500 | 0 | messages = messages.subList(0, MAXIMUM_MESSAGES); |
| 501 | |
} |
| 502 | 112 | } |
| 503 | |
|
| 504 | |
public synchronized void clearMessages() { |
| 505 | 0 | messages.clear(); |
| 506 | 0 | } |
| 507 | |
|
| 508 | |
public List<Message> getMessages() { |
| 509 | 8 | return new ArrayList<Message>(messages); |
| 510 | |
} |
| 511 | |
|
| 512 | |
public int getNumberOfMessages() { |
| 513 | 0 | return messages.size(); |
| 514 | |
} |
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
public abstract void log(HttpServletRequest request, int status); |
| 522 | |
|
| 523 | |
} |