1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.util.MethodInvoker;
26  import com.liferay.portal.kernel.util.MethodWrapper;
27  import com.liferay.portal.kernel.util.NullWrapper;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.taglib.util.IncludeTag;
31  
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.PageContext;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * <a href="SocialBookmarkTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author David Truong
42   * @author Jorge Ferrer
43   *
44   */
45  public class SocialBookmarkTag extends IncludeTag {
46  
47      public static String doTag(
48              String type, String url, String title, String target,
49              PageContext pageContext)
50          throws Exception {
51  
52          return doTag(_PAGE, type, url, title, target, pageContext);
53      }
54  
55      public static String doTag(
56              String page, String type, String url, String title, String target,
57              PageContext pageContext)
58          throws Exception {
59  
60          Object returnObj = null;
61  
62          Thread currentThread = Thread.currentThread();
63  
64          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
65  
66          try {
67              currentThread.setContextClassLoader(
68                  PortalClassLoaderUtil.getClassLoader());
69  
70              Object targetWrapper = target;
71  
72              if (targetWrapper == null) {
73                  targetWrapper = new NullWrapper(String.class.getName());
74              }
75  
76              MethodWrapper methodWrapper = new MethodWrapper(
77                  _TAG_CLASS, _TAG_DO_END_METHOD,
78                  new Object[] {
79                      page, type, url, title, targetWrapper, pageContext
80                  });
81  
82              returnObj = MethodInvoker.invoke(methodWrapper);
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86          }
87          finally {
88              currentThread.setContextClassLoader(contextClassLoader);
89          }
90  
91          if (returnObj != null) {
92              return returnObj.toString();
93          }
94          else {
95              return StringPool.BLANK;
96          }
97      }
98  
99      public int doEndTag() throws JspException {
100         try {
101             doTag(getPage(), _type, _url, _title, _target, pageContext);
102         }
103         catch (Exception e) {
104             if (e instanceof JspException) {
105                 throw (JspException)e;
106             }
107             else {
108                 throw new JspException(e);
109             }
110         }
111 
112         return EVAL_PAGE;
113     }
114 
115     public void setType(String type) {
116         _type = type;
117     }
118 
119     public void setUrl(String url) {
120         _url = url;
121     }
122 
123     public void setTitle(String title) {
124         _title = title;
125     }
126 
127     public void setTarget(String target) {
128         _target = target;
129     }
130 
131     protected String getDefaultPage() {
132         return _PAGE;
133     }
134 
135     private static final String _TAG_CLASS =
136         "com.liferay.portal.servlet.taglib.ui.SocialBookmarkTagUtil";
137 
138     private static final String _TAG_DO_END_METHOD = "doEndTag";
139 
140     private static final String _PAGE =
141         "/html/taglib/ui/social_bookmark/page.jsp";
142 
143     private static Log _log = LogFactory.getLog(SocialBookmarkTag.class);
144 
145     private String _type;
146     private String _url;
147     private String _title;
148     private String _target;
149 
150 }