1 package net.sourceforge.blogentis.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import net.sourceforge.blogentis.om.Blog;
29 import net.sourceforge.blogentis.om.StoredBlog;
30 import net.sourceforge.blogentis.om.StoredBlogPeer;
31 import net.sourceforge.blogentis.utils.impl.BlogImpl;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.torque.TorqueException;
36 import org.apache.torque.util.Criteria;
37 import org.apache.turbine.services.InitializationException;
38 import org.apache.turbine.services.TurbineBaseService;
39 import org.apache.turbine.services.TurbineServices;
40
41 /***
42 * Keep a list of run-time blog instances.
43 *
44 * @author abas
45 */
46 public class BlogManagerService
47 extends TurbineBaseService {
48
49 private static Log log = LogFactory.getLog(LinkFactoryService.class);
50
51 public static final String SERVICE_NAME = "BlogManagerService";
52
53 static protected Map byName = new HashMap();
54 static protected Map byId = new HashMap();
55
56 public void init(Object ignored)
57 throws InitializationException {
58 TurbineServices.getInstance().getService("TorqueService");
59 setInit(true);
60 }
61
62 public void shutdown() {
63 super.shutdown();
64 }
65
66 public static synchronized Blog getBlog(String name)
67 throws TorqueException {
68 Blog b = (Blog)byName.get(name);
69 if (b == null) {
70 Criteria c = new Criteria();
71 StoredBlog sb = StoredBlogPeer.retrieveByName(name);
72 b = new BlogImpl(sb);
73 byName.put(b.getName(), b);
74 byId.put(new Integer(b.getBlogId()), b);
75 }
76 return b;
77 }
78
79 public static synchronized Blog getBlog(int id)
80 throws TorqueException {
81 Integer r = new Integer(id);
82 Blog b = (Blog)byId.get(r);
83 if (b == null) {
84 Criteria c = new Criteria();
85 StoredBlog sb = StoredBlogPeer.retrieveByPK(id);
86 b = new BlogImpl(sb);
87 byName.put(b.getName(), b);
88 byId.put(r, b);
89 }
90 return b;
91 }
92
93 public static synchronized void clear() {
94 byName.clear();
95 byId.clear();
96 }
97 }