PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/taurus-web/src/org/taurus/web/gwt/server/BaseServiceRemote.java

http://tauruss.googlecode.com/
Java | 264 lines | 118 code | 46 blank | 100 comment | 16 complexity | 3e8610f3e4cb35710a7a266c25ebebb6 MD5 | raw file
  1. /*
  2. * This file is part of Taurus
  3. *
  4. * Taurus is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * at your option) any later version.
  8. *
  9. * Taurus is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Melenti. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.taurus.web.gwt.server;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.commons.beanutils.BeanUtils;
  21. import org.apache.log4j.Logger;
  22. import org.taurus.services.ServiceAdministrator;
  23. import org.taurus.services.ServiceProject;
  24. import org.taurus.services.ServiceSecurity;
  25. import com.google.gson.Gson;
  26. import com.google.gson.GsonBuilder;
  27. import com.google.gwt.user.server.rpc.RemoteServiceServlet;
  28. import com.thoughtworks.xstream.XStream;
  29. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
  30. /**
  31. * Incluir aquí la descripcion de la clase.
  32. *
  33. * @version $Revision: 1.0 $ [17/07/2008]
  34. * @author henry molina <a href="mailto:henrymolinanoboa@gmail.com">taurus</a>
  35. */
  36. public class BaseServiceRemote extends RemoteServiceServlet {
  37. private static final long serialVersionUID = 4394294482171221329L;
  38. protected static ServiceSecurity serviceSecurity;
  39. protected static ServiceAdministrator serviceAdministrator;
  40. protected static ServiceProject serviceProject;
  41. protected static XStream xstream;
  42. protected static Gson gson = null;
  43. private static Logger log = Logger.getLogger(BaseServiceRemote.class);
  44. /**
  45. *
  46. */
  47. public BaseServiceRemote() {
  48. super();
  49. if (serviceSecurity == null) {
  50. serviceSecurity = ServiceSecurity.Util.getInstance();
  51. }
  52. if (serviceAdministrator == null) {
  53. serviceAdministrator = ServiceAdministrator.Util.getInstance();
  54. }
  55. if (serviceProject == null) {
  56. serviceProject = ServiceProject.Util.getInstance();
  57. }
  58. if (xstream == null) {
  59. xstream = new XStream(new JettisonMappedXmlDriver());
  60. }
  61. if (gson == null) {
  62. final GsonBuilder gsonBuilder = new GsonBuilder();
  63. gsonBuilder.setDateFormat("dd/MM/yyyy");
  64. gson = gsonBuilder.excludeFieldsWithoutExposeAnnotation().create();
  65. }
  66. }
  67. /**
  68. * Incluir aquí la descripcion del metodo.
  69. *
  70. * @param json
  71. * @return
  72. */
  73. @SuppressWarnings("unchecked")
  74. protected synchronized Object fromJson(final String json, Class clazz) {
  75. Object obj = gson.fromJson(json, clazz);
  76. if (log.isDebugEnabled()) {
  77. try {
  78. BeanUtils.describe(obj);
  79. } catch (Exception e) {
  80. log.error(e);
  81. }
  82. }
  83. return obj;
  84. }
  85. @SuppressWarnings("unchecked")
  86. protected Object getAttributeContextHttp(final Class clazz) {
  87. return getThreadLocalRequest().getSession(false).getServletContext().getAttribute(clazz.getName());
  88. }
  89. /**
  90. * Incluir aqui­ la descripcion del metodo.
  91. *
  92. * @param name
  93. * @return
  94. */
  95. protected Object getAttributeContextHttp(final String name) {
  96. return getThreadLocalRequest().getSession(false).getServletContext().getAttribute(name);
  97. }
  98. /**
  99. * Recupera un objeto desde el request http
  100. *
  101. * @param clazz
  102. * @return
  103. */
  104. protected Object getAttributeRequestHttp(final Class<Object> clazz) {
  105. Object obj = getThreadLocalRequest().getAttribute(clazz.getName());
  106. if (obj == null) {
  107. obj = getThreadLocalRequest().getParameter(clazz.getName());
  108. }
  109. return obj;
  110. }
  111. /**
  112. * Recupera un objeto desde el session http
  113. *
  114. * @param clazz
  115. * @return
  116. */
  117. protected Object getAttributeSessionHttp(final Class clazz) {
  118. return getThreadLocalRequest().getSession(false).getAttribute(clazz.getName());
  119. }
  120. /**
  121. * Incluir aquí la descripcion del metodo.
  122. *
  123. * @param name
  124. * @return
  125. */
  126. protected Object getAttributeSessionHttp(final String name) {
  127. return getThreadLocalRequest().getSession(false).getAttribute(name);
  128. }
  129. protected void invalidateSessionUser() {
  130. getThreadLocalRequest().getSession().invalidate();
  131. }
  132. /**
  133. *
  134. * @param name
  135. */
  136. protected void removeAttributeSessionHttp(String name) {
  137. getThreadLocalRequest().getSession(false).removeAttribute(name);
  138. }
  139. protected void resetSessionUser() {
  140. getThreadLocalRequest().getSession().invalidate();
  141. getThreadLocalRequest().getSession(true);
  142. }
  143. /**
  144. * Establece el valor del atributo usando como nombre el fcn de la clase del objeto pasado como valor
  145. *
  146. * @param value
  147. */
  148. protected void setAttributeContextHttp(Object value) {
  149. getThreadLocalRequest().getSession(false).getServletContext().setAttribute(value.getClass().getName(), value);
  150. }
  151. /**
  152. * Estblece el valor de una variable de contexto de la aplicación
  153. *
  154. * @param name
  155. * el nombre del atributo
  156. * @param value
  157. * el valor que va a tener el atributo
  158. */
  159. protected void setAttributeContextHttp(final String name, Object value) {
  160. getThreadLocalRequest().getSession(false).getServletContext().setAttribute(name, value);
  161. }
  162. /**
  163. * Guarda un objeto en el request http
  164. *
  165. * @param object
  166. */
  167. protected void setAttributeRequestHttp(final Object object) {
  168. getThreadLocalRequest().setAttribute(object.getClass().getName(), object);
  169. }
  170. /**
  171. * Guarda un objeto en el session http
  172. *
  173. * @param object
  174. */
  175. protected void setAttributeSessionHttp(final Object object) {
  176. getThreadLocalRequest().getSession(false).setAttribute(object.getClass().getName(), object);
  177. }
  178. /**
  179. * Incluir aquí la descripcion del metodo.
  180. *
  181. * @param name
  182. * @param object
  183. */
  184. protected void setAttributeSessionHttp(final String name, final Object object) {
  185. getThreadLocalRequest().getSession(false).setAttribute(name, object);
  186. }
  187. /**
  188. * transforma un objeto a formato json
  189. *
  190. * @param object
  191. * @return
  192. */
  193. protected String toJson(Object object) {
  194. xstream.alias(object.getClass().getSimpleName().substring(0, 1).toLowerCase()
  195. + object.getClass().getSimpleName().substring(1), object.getClass());
  196. return xstream.toXML(object);
  197. }
  198. /**
  199. * Incluir aquí la descripcion del metodo.
  200. *
  201. * @param object
  202. * @return
  203. */
  204. protected synchronized String toJson2(final Object object) {
  205. String json = null;
  206. if (object instanceof ArrayList) {
  207. json = gson.toJson(object);
  208. } else {
  209. List<Object> list = new ArrayList<Object>();
  210. list.add(object);
  211. json = gson.toJson(list);
  212. }
  213. if (log.isDebugEnabled()) {
  214. log.debug(json);
  215. }
  216. return json;
  217. }
  218. }