View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: BlogManagerService.java,v 1.1 2004/10/28 10:45:51 tassos Exp $
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  }