| 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 net.sourceforge.pebble.PebbleContext; |
| 35 | |
import net.sourceforge.pebble.util.UpgradeUtilities; |
| 36 | |
import net.sourceforge.pebble.comparator.BlogByLastModifiedDateComparator; |
| 37 | |
import org.apache.commons.logging.Log; |
| 38 | |
import org.apache.commons.logging.LogFactory; |
| 39 | |
|
| 40 | |
import java.io.*; |
| 41 | |
import java.util.*; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class BlogManager { |
| 49 | |
|
| 50 | |
|
| 51 | 4 | private static Log log = LogFactory.getLog(BlogManager.class); |
| 52 | |
|
| 53 | |
|
| 54 | 4 | private static BlogManager instance = new BlogManager(); |
| 55 | |
|
| 56 | |
private static final String THEMES_PATH = "themes"; |
| 57 | |
private static final String DEFAULT_BLOG = "default"; |
| 58 | |
|
| 59 | |
|
| 60 | 4 | private Map<String,Blog> blogs = new HashMap<String,Blog>(); |
| 61 | |
|
| 62 | 4 | private boolean multiBlog = false; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 4 | private BlogManager() { |
| 68 | 4 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public static BlogManager getInstance() { |
| 76 | 9524 | return instance; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Blog getBlog() { |
| 85 | 12 | return getBlog(DEFAULT_BLOG); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public Blog getBlog(String id) { |
| 98 | 336 | return blogs.get(id); |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void startBlogs() { |
| 105 | 0 | File blogsDirectory = getBlogsDirectory(); |
| 106 | 0 | File defaultBlog = new File(blogsDirectory, DEFAULT_BLOG); |
| 107 | |
|
| 108 | 0 | if (!blogsDirectory.exists()) { |
| 109 | 0 | log.info("Pebble blog directory does not exist - creating"); |
| 110 | 0 | defaultBlog.mkdirs(); |
| 111 | |
} |
| 112 | |
|
| 113 | 0 | if (isMultiBlog()) { |
| 114 | |
|
| 115 | 0 | File files[] = getBlogsDirectory().listFiles(); |
| 116 | 0 | if (files != null) { |
| 117 | 0 | for (File file : files) { |
| 118 | 0 | if (file.isDirectory()) { |
| 119 | 0 | startBlog(file.getAbsolutePath(), file.getName()); |
| 120 | |
} |
| 121 | |
} |
| 122 | |
} |
| 123 | 0 | } else { |
| 124 | |
|
| 125 | 0 | startBlog(defaultBlog.getAbsolutePath(), DEFAULT_BLOG); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
public void stopBlogs() { |
| 130 | 0 | for (Blog blog : blogs.values()) { |
| 131 | 0 | stopBlog(blog); |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
private void stopBlog(Blog blog) { |
| 136 | 0 | blog.stop(); |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
public void reloadBlog(Blog blog) { |
| 140 | 0 | stopBlog(blog); |
| 141 | |
|
| 142 | 0 | File f = new File(getBlogsDirectory(), blog.getId()); |
| 143 | 0 | startBlog(f.getAbsolutePath(), blog.getId()); |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
private void startBlog(String blogDir, String blogId) { |
| 153 | 0 | Blog blog = new Blog(blogDir); |
| 154 | 0 | blog.setId(blogId); |
| 155 | |
|
| 156 | 0 | File pathToLiveThemes = new File(PebbleContext.getInstance().getWebApplicationRoot(), THEMES_PATH); |
| 157 | 0 | Theme theme = new Theme(blog, "user-" + blogId, pathToLiveThemes.getAbsolutePath()); |
| 158 | 0 | blog.setEditableTheme(theme); |
| 159 | |
|
| 160 | 0 | blog.start(); |
| 161 | 0 | blogs.put(blog.getId(), blog); |
| 162 | |
|
| 163 | |
|
| 164 | 0 | File versionFile = new File(blogDir, "pebble.version"); |
| 165 | 0 | String blogVersion = null; |
| 166 | 0 | String currentVersion = PebbleContext.getInstance().getBuildVersion(); |
| 167 | |
try { |
| 168 | 0 | if (versionFile.exists()) { |
| 169 | 0 | BufferedReader reader = new BufferedReader(new FileReader(versionFile)); |
| 170 | 0 | blogVersion = reader.readLine(); |
| 171 | 0 | reader.close(); |
| 172 | |
} |
| 173 | 0 | } catch (Exception e) { |
| 174 | 0 | log.error("Exception encountered", e); |
| 175 | 0 | } |
| 176 | |
|
| 177 | |
try { |
| 178 | 0 | if (blogVersion == null || !blogVersion.equals(currentVersion)) { |
| 179 | 0 | UpgradeUtilities.upgradeBlog(blog, blogVersion, currentVersion); |
| 180 | |
|
| 181 | 0 | if (currentVersion != null) { |
| 182 | 0 | BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile)); |
| 183 | 0 | writer.write(currentVersion); |
| 184 | 0 | writer.close(); |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | 0 | reloadBlog(blog); |
| 189 | |
} |
| 190 | 0 | } catch (Exception e) { |
| 191 | 0 | log.error("Exception encountered", e); |
| 192 | 0 | } |
| 193 | 0 | } |
| 194 | |
|
| 195 | |
public void addBlog(String blogId) { |
| 196 | 0 | File file = new File(getBlogsDirectory(), blogId); |
| 197 | 0 | file.mkdirs(); |
| 198 | 0 | startBlog(file.getAbsolutePath(), blogId); |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public boolean isMultiBlog() { |
| 207 | 1272 | return this.multiBlog; |
| 208 | |
} |
| 209 | |
|
| 210 | |
public void setMultiBlog(boolean multiBlog) { |
| 211 | 2552 | this.multiBlog = multiBlog; |
| 212 | 2552 | } |
| 213 | |
|
| 214 | |
public void addBlog(Blog blog) { |
| 215 | 2688 | blogs.put(blog.getId(), blog); |
| 216 | 2688 | } |
| 217 | |
|
| 218 | |
public void removeAllBlogs() { |
| 219 | 2552 | blogs = new HashMap<String,Blog>(); |
| 220 | 2552 | } |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public Collection<Blog> getBlogs() { |
| 228 | 12 | List<Blog> sortedBlogs = new ArrayList<Blog>(blogs.values()); |
| 229 | 12 | Collections.sort(sortedBlogs, new BlogByLastModifiedDateComparator()); |
| 230 | 12 | return sortedBlogs; |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public int getNumberOfBlogs() { |
| 239 | 4 | return blogs.size(); |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
public List<Blog> getPublicBlogs() { |
| 250 | 20 | List<Blog> list = new ArrayList<Blog>(); |
| 251 | 20 | for (Blog blog : blogs.values()) { |
| 252 | 40 | if (blog.isPublic()) { |
| 253 | 40 | list.add(blog); |
| 254 | |
} |
| 255 | |
} |
| 256 | |
|
| 257 | 20 | return list; |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
public boolean hasBlog(String id) { |
| 267 | 32 | return blogs.containsKey(id); |
| 268 | |
} |
| 269 | |
|
| 270 | |
public MultiBlog getMultiBlog() { |
| 271 | 20 | return new MultiBlog(PebbleContext.getInstance().getConfiguration().getDataDirectory()); |
| 272 | |
} |
| 273 | |
|
| 274 | |
private File getBlogsDirectory() { |
| 275 | 0 | return new File(PebbleContext.getInstance().getConfiguration().getDataDirectory(), "blogs"); |
| 276 | |
} |
| 277 | |
|
| 278 | |
} |