View Javadoc

1   package net.sf.navigator.taglib;
2   
3   import net.sf.navigator.displayer.MenuDisplayer;
4   import net.sf.navigator.displayer.MenuDisplayerMapping;
5   import net.sf.navigator.displayer.MessageResourcesMenuDisplayer;
6   import net.sf.navigator.menu.MenuRepository;
7   import net.sf.navigator.menu.PermissionsAdapter;
8   import net.sf.navigator.menu.RolesPermissionsAdapter;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.jsp.JspException;
14  import javax.servlet.jsp.tagext.TagSupport;
15  import java.util.Locale;
16  import java.util.MissingResourceException;
17  import java.util.ResourceBundle;
18  
19  /**
20   * This is the main tag of Struts Menu and can be used in a JSP as follows:</p>
21   * <pre>
22   *  &lt;menu:useMenuDisplayer name="ListMenu"&gt;
23   *     &lt;menu:displayMenu name="MyMenu"/&gt;
24   *  &lt;/menu:useMenuDisplayer&gt;
25   * </pre>
26   * 
27   * @author  ssayles, mraible
28   * @version $Revision: 1.16 $ $Date: 2006/10/04 22:26:02 $
29   */
30  public class UseMenuDisplayerTag extends TagSupport {
31      //~ Static fields/initializers =============================================
32  
33      private static Log log = LogFactory.getLog(UseMenuDisplayerTag.class);
34      public static final String PRIVATE_REPOSITORY = "net.sf.navigator.repositoryKey";
35      public static final String DISPLAYER_KEY = "net.sf.navigator.taglib.DISPLAYER";
36      public static final String ROLES_ADAPTER = "rolesAdapter";
37      public static final String MENU_ID = "net.sf.navigator.MENU_ID";
38      
39      protected static ResourceBundle messages =
40              ResourceBundle.getBundle("net.sf.navigator.taglib.LocalStrings");
41  
42      //~ Instance fields ========================================================
43  
44      protected MenuDisplayer menuDisplayer;
45      protected String localeKey;
46      protected String name;
47      protected String bundleKey;
48      protected String id;
49      private String config = MenuDisplayer.DEFAULT_CONFIG;
50      private String permissions;
51      private String repository;
52      protected ResourceBundle rb; // used to allow setting of ResourceBundle 
53                                   // from JSTL in EL tag
54  
55      //~ Methods ================================================================
56  
57      public String getBundle() {
58          return bundleKey;
59      }
60  
61      public void setBundle(String bundle) {
62          this.bundleKey = bundle;
63      }
64  
65      public String getConfig() {
66          return config;
67      }
68  
69      public void setConfig(String config) {
70          if (log.isDebugEnabled()) {
71              log.debug("setting config to: " + config);
72          }
73  
74          this.config = config;
75      }
76  
77      public String getLocale() {
78          return localeKey;
79      }
80  
81      public void setLocale(String locale) {
82          this.localeKey = locale;
83      }
84  
85      public String getName() {
86          return name;
87      }
88  
89      public void setName(String name) {
90          this.name = name;
91      }
92  
93      public void setId(String id) {
94          this.id = id;
95      }
96  
97      public String getRepository() {
98          return repository;
99      }
100 
101     /**
102      * This method allows users to override the key used to lookup the
103      * repository.  If not specified - the default repository is used, which is
104      * "net.sf.navigator.MENU_REPOSITORY" or
105      * UseMenuDisplayerTag.MENU_REPOSITORY_KEY.
106      * @param repository
107      */
108     public void setRepository(String repository) {
109         this.repository = repository;
110     }
111 
112     /** Getter for property permissions.
113      * @return Value of property permissions.
114      */
115     public String getPermissions() {
116         return this.permissions;
117     }
118 
119     /** Setter for property permissions.
120      * @param permissions New value of property permissions.
121      */
122     public void setPermissions(String permissions) {
123         this.permissions = permissions;
124     }
125 
126     public int doStartTag() throws JspException {
127         if (repository == null) {
128             repository = MenuRepository.MENU_REPOSITORY_KEY;
129         }
130 
131         if (log.isDebugEnabled()) {
132             log.debug("Looking for repository named '" + repository + "'");
133         }
134 
135         // get the menu repository
136         MenuRepository rep =
137             (MenuRepository) pageContext.findAttribute(this.repository);
138 
139         if (rep == null) {
140             throw new JspException(messages.getString("menurepository.not.found"));
141         } else {
142             // set repository as a pageContext variable so that DisplayMenuTag
143             // can grab it.
144             if (log.isDebugEnabled()) {
145                 log.debug("stuffing repository into pageContext...");
146             }
147             pageContext.setAttribute(PRIVATE_REPOSITORY, rep);
148         }
149 
150         // get the displayer mapping
151         MenuDisplayerMapping displayerMapping =
152                 rep.getMenuDisplayerMapping(this.name);
153 
154         if (displayerMapping == null) {
155             throw new JspException(messages.getString("displayer.mapping.not.found"));
156         }
157 
158         PermissionsAdapter permissions = getPermissionsAdapter();
159 
160         //get an instance of the menu displayer
161         MenuDisplayer displayerInstance;
162 
163         try {
164             displayerInstance =
165                     (MenuDisplayer) Class.forName(displayerMapping.getType())
166                     .newInstance();
167             menuDisplayer = displayerInstance;
168             // default to use the config on the mapping
169             if (displayerMapping.getConfig() != null) {
170                 // this value (config) is set on the displayer below
171                 setConfig(displayerMapping.getConfig());
172             }
173         } catch (Exception e) {
174             throw new JspException(e.getMessage());
175         }
176         
177         if (bundleKey == null) {
178             this.bundleKey = "org.apache.struts.action.MESSAGE";
179         }
180         
181         // setup the displayerInstance
182         // if the displayer is a MessageResourceMenuDisplayer
183         // and a bundle is specified, then pass it the bundle (message resources) and
184         // the locale
185         if ((bundleKey != null && !"".equals(bundleKey)) &&
186                 (displayerInstance instanceof MessageResourcesMenuDisplayer)) {
187             MessageResourcesMenuDisplayer mrDisplayerInstance =
188                     (MessageResourcesMenuDisplayer) displayerInstance;
189             Locale locale;
190 
191             if (localeKey == null) {
192                 // default to Struts locale
193                 locale = 
194                     (Locale) pageContext.findAttribute("org.apache.struts.action.LOCALE");
195                 if (locale == null) {
196                     locale = pageContext.getRequest().getLocale();
197                 }
198             } else {
199                 locale = (Locale) pageContext.findAttribute(localeKey);
200             }
201             mrDisplayerInstance.setLocale(locale);
202             
203             if (rb != null) {
204                 mrDisplayerInstance.setMessageResources(rb);
205             } else {
206                 Object resources = pageContext.findAttribute(bundleKey);
207                 
208                 if (resources == null) {
209                     // try a simple ResourceBundle
210                     try {
211                         rb = ResourceBundle.getBundle(bundleKey, locale);
212                         mrDisplayerInstance.setMessageResources(rb);
213                     } catch (MissingResourceException mre) {
214                         log.error(mre.getMessage());
215                     }
216                 } else {
217                      mrDisplayerInstance.setMessageResources(resources);   
218                 }
219             }
220         }
221 
222         displayerInstance.setConfig(config);
223         if (id != null) {
224             pageContext.setAttribute("menuId", id);
225         }
226 
227         displayerInstance.init(pageContext, displayerMapping);
228         displayerInstance.setPermissionsAdapter(permissions);
229 
230         pageContext.setAttribute(DISPLAYER_KEY, displayerInstance);
231 
232         return (EVAL_BODY_INCLUDE);
233     }
234 
235     protected PermissionsAdapter getPermissionsAdapter()
236             throws JspException {
237         PermissionsAdapter adapter = null;
238 
239         if (permissions != null) {
240             // If set to "rolesAdapter", then create automatically
241             if (permissions.equalsIgnoreCase(ROLES_ADAPTER)) {
242                 adapter =
243                     new RolesPermissionsAdapter((HttpServletRequest) pageContext.getRequest());
244             } else {
245                 adapter =
246                     (PermissionsAdapter) pageContext.findAttribute(permissions);
247 
248                 if (adapter == null) {
249                     throw new JspException(messages.getString("permissions.not.found"));
250                 }
251             }
252         }
253 
254         return adapter;
255     }
256 
257     public int doEndTag() throws JspException {
258         menuDisplayer.end(pageContext);
259         pageContext.removeAttribute(DISPLAYER_KEY);
260         pageContext.removeAttribute(PRIVATE_REPOSITORY);
261         return (EVAL_PAGE);
262     }
263 
264     public void release() {
265         if (log.isDebugEnabled()) {
266             log.debug("release() called");
267         }
268 
269         this.menuDisplayer = null;
270         this.bundleKey = null;
271         this.config = MenuDisplayer.DEFAULT_CONFIG;
272         this.localeKey = null;
273         this.name = null;
274         this.menuDisplayer = null;
275         this.repository = null;
276         this.permissions = null;
277         this.rb = null;
278     }
279 }