PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/portlets/chat-portlet/docroot/WEB-INF/service/com/liferay/chat/service/ClpSerializer.java

https://github.com/sez11a/liferay-plugins
Java | 354 lines | 247 code | 91 blank | 16 comment | 33 complexity | 5db082b7d263701faa6af4e03335fbb4 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-present 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.chat.service;
  15. import aQute.bnd.annotation.ProviderType;
  16. import com.liferay.chat.model.EntryClp;
  17. import com.liferay.chat.model.StatusClp;
  18. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
  19. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
  20. import com.liferay.portal.kernel.log.Log;
  21. import com.liferay.portal.kernel.log.LogFactoryUtil;
  22. import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
  23. import com.liferay.portal.kernel.util.PropsUtil;
  24. import com.liferay.portal.kernel.util.Validator;
  25. import com.liferay.portal.model.BaseModel;
  26. import java.io.ObjectInputStream;
  27. import java.io.ObjectOutputStream;
  28. import java.lang.reflect.Method;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * @author Brian Wing Shun Chan
  33. */
  34. @ProviderType
  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(
  47. "com.liferay.util.portlet.PortletProps");
  48. Method getMethod = portletPropsClass.getMethod("get",
  49. new Class<?>[] { String.class });
  50. String portletPropsServletContextName = (String)getMethod.invoke(null,
  51. "chat-portlet-deployment-context");
  52. if (Validator.isNotNull(portletPropsServletContextName)) {
  53. _servletContextName = portletPropsServletContextName;
  54. }
  55. }
  56. catch (Throwable t) {
  57. if (_log.isInfoEnabled()) {
  58. _log.info(
  59. "Unable to locate deployment context from portlet properties");
  60. }
  61. }
  62. if (Validator.isNull(_servletContextName)) {
  63. try {
  64. String propsUtilServletContextName = PropsUtil.get(
  65. "chat-portlet-deployment-context");
  66. if (Validator.isNotNull(propsUtilServletContextName)) {
  67. _servletContextName = propsUtilServletContextName;
  68. }
  69. }
  70. catch (Throwable t) {
  71. if (_log.isInfoEnabled()) {
  72. _log.info(
  73. "Unable to locate deployment context from portal properties");
  74. }
  75. }
  76. }
  77. if (Validator.isNull(_servletContextName)) {
  78. _servletContextName = "chat-portlet";
  79. }
  80. return _servletContextName;
  81. }
  82. }
  83. public static Object translateInput(BaseModel<?> oldModel) {
  84. Class<?> oldModelClass = oldModel.getClass();
  85. String oldModelClassName = oldModelClass.getName();
  86. if (oldModelClassName.equals(EntryClp.class.getName())) {
  87. return translateInputEntry(oldModel);
  88. }
  89. if (oldModelClassName.equals(StatusClp.class.getName())) {
  90. return translateInputStatus(oldModel);
  91. }
  92. return oldModel;
  93. }
  94. public static Object translateInput(List<Object> oldList) {
  95. List<Object> newList = new ArrayList<Object>(oldList.size());
  96. for (int i = 0; i < oldList.size(); i++) {
  97. Object curObj = oldList.get(i);
  98. newList.add(translateInput(curObj));
  99. }
  100. return newList;
  101. }
  102. public static Object translateInputEntry(BaseModel<?> oldModel) {
  103. EntryClp oldClpModel = (EntryClp)oldModel;
  104. BaseModel<?> newModel = oldClpModel.getEntryRemoteModel();
  105. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  106. return newModel;
  107. }
  108. public static Object translateInputStatus(BaseModel<?> oldModel) {
  109. StatusClp oldClpModel = (StatusClp)oldModel;
  110. BaseModel<?> newModel = oldClpModel.getStatusRemoteModel();
  111. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  112. return newModel;
  113. }
  114. public static Object translateInput(Object obj) {
  115. if (obj instanceof BaseModel<?>) {
  116. return translateInput((BaseModel<?>)obj);
  117. }
  118. else if (obj instanceof List<?>) {
  119. return translateInput((List<Object>)obj);
  120. }
  121. else {
  122. return obj;
  123. }
  124. }
  125. public static Object translateOutput(BaseModel<?> oldModel) {
  126. Class<?> oldModelClass = oldModel.getClass();
  127. String oldModelClassName = oldModelClass.getName();
  128. if (oldModelClassName.equals("com.liferay.chat.model.impl.EntryImpl")) {
  129. return translateOutputEntry(oldModel);
  130. }
  131. else if (oldModelClassName.endsWith("Clp")) {
  132. try {
  133. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  134. Method getClpSerializerClassMethod = oldModelClass.getMethod(
  135. "getClpSerializerClass");
  136. Class<?> oldClpSerializerClass = (Class<?>)getClpSerializerClassMethod.invoke(oldModel);
  137. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  138. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput",
  139. BaseModel.class);
  140. Class<?> oldModelModelClass = oldModel.getModelClass();
  141. Method getRemoteModelMethod = oldModelClass.getMethod("get" +
  142. oldModelModelClass.getSimpleName() + "RemoteModel");
  143. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  144. BaseModel<?> newModel = (BaseModel<?>)translateOutputMethod.invoke(null,
  145. oldRemoteModel);
  146. return newModel;
  147. }
  148. catch (Throwable t) {
  149. if (_log.isInfoEnabled()) {
  150. _log.info("Unable to translate " + oldModelClassName, t);
  151. }
  152. }
  153. }
  154. if (oldModelClassName.equals("com.liferay.chat.model.impl.StatusImpl")) {
  155. return translateOutputStatus(oldModel);
  156. }
  157. else if (oldModelClassName.endsWith("Clp")) {
  158. try {
  159. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  160. Method getClpSerializerClassMethod = oldModelClass.getMethod(
  161. "getClpSerializerClass");
  162. Class<?> oldClpSerializerClass = (Class<?>)getClpSerializerClassMethod.invoke(oldModel);
  163. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  164. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput",
  165. BaseModel.class);
  166. Class<?> oldModelModelClass = oldModel.getModelClass();
  167. Method getRemoteModelMethod = oldModelClass.getMethod("get" +
  168. oldModelModelClass.getSimpleName() + "RemoteModel");
  169. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  170. BaseModel<?> newModel = (BaseModel<?>)translateOutputMethod.invoke(null,
  171. oldRemoteModel);
  172. return newModel;
  173. }
  174. catch (Throwable t) {
  175. if (_log.isInfoEnabled()) {
  176. _log.info("Unable to translate " + oldModelClassName, t);
  177. }
  178. }
  179. }
  180. return oldModel;
  181. }
  182. public static Object translateOutput(List<Object> oldList) {
  183. List<Object> newList = new ArrayList<Object>(oldList.size());
  184. for (int i = 0; i < oldList.size(); i++) {
  185. Object curObj = oldList.get(i);
  186. newList.add(translateOutput(curObj));
  187. }
  188. return newList;
  189. }
  190. public static Object translateOutput(Object obj) {
  191. if (obj instanceof BaseModel<?>) {
  192. return translateOutput((BaseModel<?>)obj);
  193. }
  194. else if (obj instanceof List<?>) {
  195. return translateOutput((List<Object>)obj);
  196. }
  197. else {
  198. return obj;
  199. }
  200. }
  201. public static Throwable translateThrowable(Throwable throwable) {
  202. if (_useReflectionToTranslateThrowable) {
  203. try {
  204. UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
  205. ObjectOutputStream objectOutputStream = new ObjectOutputStream(unsyncByteArrayOutputStream);
  206. objectOutputStream.writeObject(throwable);
  207. objectOutputStream.flush();
  208. objectOutputStream.close();
  209. UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(unsyncByteArrayOutputStream.unsafeGetByteArray(),
  210. 0, unsyncByteArrayOutputStream.size());
  211. Thread currentThread = Thread.currentThread();
  212. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  213. ObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(unsyncByteArrayInputStream,
  214. contextClassLoader);
  215. throwable = (Throwable)objectInputStream.readObject();
  216. objectInputStream.close();
  217. return throwable;
  218. }
  219. catch (ClassNotFoundException cnfe) {
  220. if (_log.isInfoEnabled()) {
  221. _log.info("Do not use reflection to translate throwable");
  222. }
  223. _useReflectionToTranslateThrowable = false;
  224. }
  225. catch (SecurityException se) {
  226. if (_log.isInfoEnabled()) {
  227. _log.info("Do not use reflection to translate throwable");
  228. }
  229. _useReflectionToTranslateThrowable = false;
  230. }
  231. catch (Throwable throwable2) {
  232. _log.error(throwable2, throwable2);
  233. return throwable2;
  234. }
  235. }
  236. Class<?> clazz = throwable.getClass();
  237. String className = clazz.getName();
  238. if (className.equals("com.liferay.chat.NoSuchEntryException")) {
  239. return new com.liferay.chat.NoSuchEntryException(throwable.getMessage(),
  240. throwable.getCause());
  241. }
  242. if (className.equals("com.liferay.chat.NoSuchStatusException")) {
  243. return new com.liferay.chat.NoSuchStatusException(throwable.getMessage(),
  244. throwable.getCause());
  245. }
  246. return throwable;
  247. }
  248. public static Object translateOutputEntry(BaseModel<?> oldModel) {
  249. EntryClp newModel = new EntryClp();
  250. newModel.setModelAttributes(oldModel.getModelAttributes());
  251. newModel.setEntryRemoteModel(oldModel);
  252. return newModel;
  253. }
  254. public static Object translateOutputStatus(BaseModel<?> oldModel) {
  255. StatusClp newModel = new StatusClp();
  256. newModel.setModelAttributes(oldModel.getModelAttributes());
  257. newModel.setStatusRemoteModel(oldModel);
  258. return newModel;
  259. }
  260. private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
  261. private static String _servletContextName;
  262. private static boolean _useReflectionToTranslateThrowable = true;
  263. }