1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.kernel.util.NullWrapper;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.taglib.util.IncludeTag;
33  
34  import javax.servlet.jsp.JspException;
35  import javax.servlet.jsp.PageContext;
36  
37  /**
38   * <a href="SocialBookmarkTag.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author David Truong
41   * @author Jorge Ferrer
42   *
43   */
44  public class SocialBookmarkTag extends IncludeTag {
45  
46      public static String doTag(
47              String type, String url, String title, String target,
48              PageContext pageContext)
49          throws Exception {
50  
51          return doTag(_PAGE, type, url, title, target, pageContext);
52      }
53  
54      public static String doTag(
55              String page, String type, String url, String title, String target,
56              PageContext pageContext)
57          throws Exception {
58  
59          Object returnObj = null;
60  
61          Thread currentThread = Thread.currentThread();
62  
63          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
64  
65          try {
66              currentThread.setContextClassLoader(
67                  PortalClassLoaderUtil.getClassLoader());
68  
69              Object targetWrapper = target;
70  
71              if (targetWrapper == null) {
72                  targetWrapper = new NullWrapper(String.class.getName());
73              }
74  
75              MethodWrapper methodWrapper = new MethodWrapper(
76                  _TAG_CLASS, _TAG_DO_END_METHOD,
77                  new Object[] {
78                      page, type, url, title, targetWrapper, pageContext
79                  });
80  
81              returnObj = MethodInvoker.invoke(methodWrapper);
82          }
83          catch (Exception e) {
84              _log.error(e, e);
85          }
86          finally {
87              currentThread.setContextClassLoader(contextClassLoader);
88          }
89  
90          if (returnObj != null) {
91              return returnObj.toString();
92          }
93          else {
94              return StringPool.BLANK;
95          }
96      }
97  
98      public int doEndTag() throws JspException {
99          try {
100             doTag(getPage(), _type, _url, _title, _target, pageContext);
101         }
102         catch (Exception e) {
103             if (e instanceof JspException) {
104                 throw (JspException)e;
105             }
106             else {
107                 throw new JspException(e);
108             }
109         }
110 
111         return EVAL_PAGE;
112     }
113 
114     public void setType(String type) {
115         _type = type;
116     }
117 
118     public void setUrl(String url) {
119         _url = url;
120     }
121 
122     public void setTitle(String title) {
123         _title = title;
124     }
125 
126     public void setTarget(String target) {
127         _target = target;
128     }
129 
130     protected String getDefaultPage() {
131         return _PAGE;
132     }
133 
134     private static final String _TAG_CLASS =
135         "com.liferay.portal.servlet.taglib.ui.SocialBookmarkTagUtil";
136 
137     private static final String _TAG_DO_END_METHOD = "doEndTag";
138 
139     private static final String _PAGE =
140         "/html/taglib/ui/social_bookmark/page.jsp";
141 
142     private static Log _log = LogFactoryUtil.getLog(SocialBookmarkTag.class);
143 
144     private String _type;
145     private String _url;
146     private String _title;
147     private String _target;
148 
149 }