PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/WEB-INF/service/com/liferay/docs/guestbook/service/ClpSerializer.java

https://gitlab.com/bruce.ng/guestbook-portlet
Java | 341 lines | 230 code | 95 blank | 16 comment | 38 complexity | 89f41424a18a58b4df5e067ed8d16c30 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.docs.guestbook.service;
  15. import com.liferay.docs.guestbook.model.EntryClp;
  16. import com.liferay.docs.guestbook.model.GuestbookClp;
  17. import com.liferay.portal.kernel.exception.PortalException;
  18. import com.liferay.portal.kernel.exception.SystemException;
  19. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
  20. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
  21. import com.liferay.portal.kernel.log.Log;
  22. import com.liferay.portal.kernel.log.LogFactoryUtil;
  23. import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
  24. import com.liferay.portal.kernel.util.PropsUtil;
  25. import com.liferay.portal.kernel.util.Validator;
  26. import com.liferay.portal.model.BaseModel;
  27. import java.io.ObjectInputStream;
  28. import java.io.ObjectOutputStream;
  29. import java.lang.reflect.Method;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. /**
  33. * @author Rich Sezov
  34. */
  35. public class ClpSerializer {
  36. public static String getServletContextName() {
  37. if (Validator.isNotNull(_servletContextName)) {
  38. return _servletContextName;
  39. }
  40. synchronized (ClpSerializer.class) {
  41. if (Validator.isNotNull(_servletContextName)) {
  42. return _servletContextName;
  43. }
  44. try {
  45. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  46. Class<?> portletPropsClass = classLoader.loadClass("com.liferay.util.portlet.PortletProps");
  47. Method getMethod = portletPropsClass.getMethod("get", new Class<?>[] { String.class });
  48. String portletPropsServletContextName = (String) getMethod.invoke(null, "guestbook-portlet-deployment-context");
  49. if (Validator.isNotNull(portletPropsServletContextName)) {
  50. _servletContextName = portletPropsServletContextName;
  51. }
  52. } catch (Throwable t) {
  53. if (_log.isInfoEnabled()) {
  54. _log.info("Unable to locate deployment context from portlet properties");
  55. }
  56. }
  57. if (Validator.isNull(_servletContextName)) {
  58. try {
  59. String propsUtilServletContextName = PropsUtil.get("guestbook-portlet-deployment-context");
  60. if (Validator.isNotNull(propsUtilServletContextName)) {
  61. _servletContextName = propsUtilServletContextName;
  62. }
  63. } catch (Throwable t) {
  64. if (_log.isInfoEnabled()) {
  65. _log.info("Unable to locate deployment context from portal properties");
  66. }
  67. }
  68. }
  69. if (Validator.isNull(_servletContextName)) {
  70. _servletContextName = "guestbook-portlet";
  71. }
  72. return _servletContextName;
  73. }
  74. }
  75. public static Object translateInput(BaseModel<?> oldModel) {
  76. Class<?> oldModelClass = oldModel.getClass();
  77. String oldModelClassName = oldModelClass.getName();
  78. if (oldModelClassName.equals(EntryClp.class.getName())) {
  79. return translateInputEntry(oldModel);
  80. }
  81. if (oldModelClassName.equals(GuestbookClp.class.getName())) {
  82. return translateInputGuestbook(oldModel);
  83. }
  84. return oldModel;
  85. }
  86. public static Object translateInput(List<Object> oldList) {
  87. List<Object> newList = new ArrayList<Object>(oldList.size());
  88. for (int i = 0; i < oldList.size(); i++) {
  89. Object curObj = oldList.get(i);
  90. newList.add(translateInput(curObj));
  91. }
  92. return newList;
  93. }
  94. public static Object translateInputEntry(BaseModel<?> oldModel) {
  95. EntryClp oldClpModel = (EntryClp) oldModel;
  96. BaseModel<?> newModel = oldClpModel.getEntryRemoteModel();
  97. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  98. return newModel;
  99. }
  100. public static Object translateInputGuestbook(BaseModel<?> oldModel) {
  101. GuestbookClp oldClpModel = (GuestbookClp) oldModel;
  102. BaseModel<?> newModel = oldClpModel.getGuestbookRemoteModel();
  103. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  104. return newModel;
  105. }
  106. public static Object translateInput(Object obj) {
  107. if (obj instanceof BaseModel<?>) {
  108. return translateInput((BaseModel<?>) obj);
  109. } else if (obj instanceof List<?>) {
  110. return translateInput((List<Object>) obj);
  111. } else {
  112. return obj;
  113. }
  114. }
  115. public static Object translateOutput(BaseModel<?> oldModel) {
  116. Class<?> oldModelClass = oldModel.getClass();
  117. String oldModelClassName = oldModelClass.getName();
  118. if (oldModelClassName.equals("com.liferay.docs.guestbook.model.impl.EntryImpl")) {
  119. return translateOutputEntry(oldModel);
  120. } else if (oldModelClassName.endsWith("Clp")) {
  121. try {
  122. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  123. Method getClpSerializerClassMethod = oldModelClass.getMethod("getClpSerializerClass");
  124. Class<?> oldClpSerializerClass = (Class<?>) getClpSerializerClassMethod.invoke(oldModel);
  125. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  126. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput", BaseModel.class);
  127. Class<?> oldModelModelClass = oldModel.getModelClass();
  128. Method getRemoteModelMethod = oldModelClass.getMethod("get" + oldModelModelClass.getSimpleName() + "RemoteModel");
  129. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  130. BaseModel<?> newModel = (BaseModel<?>) translateOutputMethod.invoke(null, oldRemoteModel);
  131. return newModel;
  132. } catch (Throwable t) {
  133. if (_log.isInfoEnabled()) {
  134. _log.info("Unable to translate " + oldModelClassName, t);
  135. }
  136. }
  137. }
  138. if (oldModelClassName.equals("com.liferay.docs.guestbook.model.impl.GuestbookImpl")) {
  139. return translateOutputGuestbook(oldModel);
  140. } else if (oldModelClassName.endsWith("Clp")) {
  141. try {
  142. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  143. Method getClpSerializerClassMethod = oldModelClass.getMethod("getClpSerializerClass");
  144. Class<?> oldClpSerializerClass = (Class<?>) getClpSerializerClassMethod.invoke(oldModel);
  145. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  146. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput", BaseModel.class);
  147. Class<?> oldModelModelClass = oldModel.getModelClass();
  148. Method getRemoteModelMethod = oldModelClass.getMethod("get" + oldModelModelClass.getSimpleName() + "RemoteModel");
  149. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  150. BaseModel<?> newModel = (BaseModel<?>) translateOutputMethod.invoke(null, oldRemoteModel);
  151. return newModel;
  152. } catch (Throwable t) {
  153. if (_log.isInfoEnabled()) {
  154. _log.info("Unable to translate " + oldModelClassName, t);
  155. }
  156. }
  157. }
  158. return oldModel;
  159. }
  160. public static Object translateOutput(List<Object> oldList) {
  161. List<Object> newList = new ArrayList<Object>(oldList.size());
  162. for (int i = 0; i < oldList.size(); i++) {
  163. Object curObj = oldList.get(i);
  164. newList.add(translateOutput(curObj));
  165. }
  166. return newList;
  167. }
  168. public static Object translateOutput(Object obj) {
  169. if (obj instanceof BaseModel<?>) {
  170. return translateOutput((BaseModel<?>) obj);
  171. } else if (obj instanceof List<?>) {
  172. return translateOutput((List<Object>) obj);
  173. } else {
  174. return obj;
  175. }
  176. }
  177. public static Throwable translateThrowable(Throwable throwable) {
  178. if (_useReflectionToTranslateThrowable) {
  179. try {
  180. UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
  181. ObjectOutputStream objectOutputStream = new ObjectOutputStream(unsyncByteArrayOutputStream);
  182. objectOutputStream.writeObject(throwable);
  183. objectOutputStream.flush();
  184. objectOutputStream.close();
  185. UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(
  186. unsyncByteArrayOutputStream.unsafeGetByteArray(), 0, unsyncByteArrayOutputStream.size());
  187. Thread currentThread = Thread.currentThread();
  188. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  189. ObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(unsyncByteArrayInputStream, contextClassLoader);
  190. throwable = (Throwable) objectInputStream.readObject();
  191. objectInputStream.close();
  192. return throwable;
  193. } catch (SecurityException se) {
  194. if (_log.isInfoEnabled()) {
  195. _log.info("Do not use reflection to translate throwable");
  196. }
  197. _useReflectionToTranslateThrowable = false;
  198. } catch (Throwable throwable2) {
  199. _log.error(throwable2, throwable2);
  200. return throwable2;
  201. }
  202. }
  203. Class<?> clazz = throwable.getClass();
  204. String className = clazz.getName();
  205. if (className.equals(PortalException.class.getName())) {
  206. return new PortalException();
  207. }
  208. if (className.equals(SystemException.class.getName())) {
  209. return new SystemException();
  210. }
  211. if (className.equals("com.liferay.docs.guestbook.EntryEmailException")) {
  212. return new com.liferay.docs.guestbook.EntryEmailException();
  213. }
  214. if (className.equals("com.liferay.docs.guestbook.EntryMessageException")) {
  215. return new com.liferay.docs.guestbook.EntryMessageException();
  216. }
  217. if (className.equals("com.liferay.docs.guestbook.EntryNameException")) {
  218. return new com.liferay.docs.guestbook.EntryNameException();
  219. }
  220. if (className.equals("com.liferay.docs.guestbook.GuestbookNameException")) {
  221. return new com.liferay.docs.guestbook.GuestbookNameException();
  222. }
  223. if (className.equals("com.liferay.docs.guestbook.NoSuchEntryException")) {
  224. return new com.liferay.docs.guestbook.NoSuchEntryException();
  225. }
  226. if (className.equals("com.liferay.docs.guestbook.NoSuchGuestbookException")) {
  227. return new com.liferay.docs.guestbook.NoSuchGuestbookException();
  228. }
  229. return throwable;
  230. }
  231. public static Object translateOutputEntry(BaseModel<?> oldModel) {
  232. EntryClp newModel = new EntryClp();
  233. newModel.setModelAttributes(oldModel.getModelAttributes());
  234. newModel.setEntryRemoteModel(oldModel);
  235. return newModel;
  236. }
  237. public static Object translateOutputGuestbook(BaseModel<?> oldModel) {
  238. GuestbookClp newModel = new GuestbookClp();
  239. newModel.setModelAttributes(oldModel.getModelAttributes());
  240. newModel.setGuestbookRemoteModel(oldModel);
  241. return newModel;
  242. }
  243. private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
  244. private static String _servletContextName;
  245. private static boolean _useReflectionToTranslateThrowable = true;
  246. }