View Javadoc

1   package net.sourceforge.blogentis.utils.tools;
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: FragmentTool.java,v 1.1 2004/10/22 17:34:15 tassos Exp $
23  //
24  
25  import java.util.HashMap;
26  
27  import net.sourceforge.blogentis.om.Blog;
28  import net.sourceforge.blogentis.storage.VelocityFragment;
29  import net.sourceforge.blogentis.turbine.BlogRunDataService;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.turbine.services.pull.ApplicationTool;
34  
35  /***
36   * @author abas
37   */
38  public class FragmentTool implements ApplicationTool {
39      private static final char PACKAGE_SEPARATOR = '.';
40  
41      private static final char PATH_SEPARATOR = '/';
42  
43      public static final String DEFAULT_CLASS = VelocityFragment.class.getName();
44  
45      public static final String SEARCH_PACKAGE = "net.sourceforge.blogentis.modules";
46  
47      public static final String DEFAULT_PACKAGE = "fragments";
48  
49      public static final String DEFAUT_PACKAGE_CLASS = "Default";
50  
51      private static final Log log = LogFactory.getLog(FragmentTool.class);
52  
53      protected HashMap blogs = new HashMap();
54  
55      protectedVelocityFragment getCachedFragment(Blog blog, String packageName,/package-summary.html">ong> VelocityFragment getCachedFragment(Blog blog, String packageName,
56                                                   String fragmentName) {
57          synchronized (blogs) {
58              HashMap fragments = (HashMap)blogs.get(blog == null ? "NULL"
59                      : blog.getName());
60              if (fragments == null)
61                  return null;
62              return (VelocityFragment)fragments.get(packageName + PATH_SEPARATOR
63                                                     + fragmentName);
64          }
65      }
66  
67      protected void setCacheFragment(Blog blog, VelocityFragment fragment) {
68          String blogName = (blog == null) ? "NULL" : blog.getName();
69          synchronized (blogs) {
70              if (!blogs.containsKey(blogName))
71                  blogs.put(blogName, new HashMap());
72              HashMap fragments = (HashMap)blogs.get(blogName);
73              fragments.put(fragment.getName(), fragment);
74          }
75      }
76  
77      protectedVelocityFragment loadFragment(Blog blog, String packageName,/package-summary.html">ong> VelocityFragment loadFragment(Blog blog, String packageName,
78                                              String name) {
79          VelocityFragment f = null;
80  
81          String path = packageName + PATH_SEPARATOR + name;
82  
83          try {
84              Class c = null;
85              try {
86                  c = Class.forName(SEARCH_PACKAGE
87                                    + PACKAGE_SEPARATOR
88                                    + path.replace(PATH_SEPARATOR,
89                                            PACKAGE_SEPARATOR));
90              } catch (ClassNotFoundException ignored) {
91                  try {
92                      c = Class.forName(SEARCH_PACKAGE + PACKAGE_SEPARATOR
93                                        + packageName + PACKAGE_SEPARATOR
94                                        + DEFAUT_PACKAGE_CLASS);
95                  } catch (ClassNotFoundException ignored2) {
96                      c = Class.forName(DEFAULT_CLASS);
97                  }
98              }
99              f = (VelocityFragment)c.newInstance();
100             f.init(blog, path);
101             setCacheFragment(blog, f);
102         } catch (Exception e) {
103             log.error(e);
104         }
105         return f;
106     }
107 
108     public VelocityFragment getFragment(String pack, String name) {
109         Blog b = BlogRunDataService.getCurrentRunData().getBlog();
110         VelocityFragment f = getCachedFragment(b, pack, name);
111         if (f != null)
112             return f;
113         return loadFragment(b, pack, name);
114     }
115 
116     public VelocityFragment getFragment(String name) {
117         return getFragment(DEFAULT_PACKAGE, name);
118     }
119 
120     public void init(Object data) {}
121 
122     public void refresh() {}
123 }