PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/johnnyhowey/liferay-plugins
Java | 444 lines | 314 code | 114 blank | 16 comment | 42 complexity | 0b3a409b6be80378b8c6e7a4460b80d0 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.opensocial.service;
  15. import aQute.bnd.annotation.ProviderType;
  16. import com.liferay.opensocial.model.GadgetClp;
  17. import com.liferay.opensocial.model.OAuthConsumerClp;
  18. import com.liferay.opensocial.model.OAuthTokenClp;
  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.model.BaseModel;
  24. import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
  25. import com.liferay.portal.kernel.util.PropsUtil;
  26. import com.liferay.portal.kernel.util.Validator;
  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 Brian Wing Shun Chan
  34. */
  35. @ProviderType
  36. public class ClpSerializer {
  37. public static String getServletContextName() {
  38. if (Validator.isNotNull(_servletContextName)) {
  39. return _servletContextName;
  40. }
  41. synchronized (ClpSerializer.class) {
  42. if (Validator.isNotNull(_servletContextName)) {
  43. return _servletContextName;
  44. }
  45. try {
  46. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  47. Class<?> portletPropsClass = classLoader.loadClass(
  48. "com.liferay.util.portlet.PortletProps");
  49. Method getMethod = portletPropsClass.getMethod("get",
  50. new Class<?>[] { String.class });
  51. String portletPropsServletContextName = (String)getMethod.invoke(null,
  52. "opensocial-portlet-deployment-context");
  53. if (Validator.isNotNull(portletPropsServletContextName)) {
  54. _servletContextName = portletPropsServletContextName;
  55. }
  56. }
  57. catch (Throwable t) {
  58. if (_log.isInfoEnabled()) {
  59. _log.info(
  60. "Unable to locate deployment context from portlet properties");
  61. }
  62. }
  63. if (Validator.isNull(_servletContextName)) {
  64. try {
  65. String propsUtilServletContextName = PropsUtil.get(
  66. "opensocial-portlet-deployment-context");
  67. if (Validator.isNotNull(propsUtilServletContextName)) {
  68. _servletContextName = propsUtilServletContextName;
  69. }
  70. }
  71. catch (Throwable t) {
  72. if (_log.isInfoEnabled()) {
  73. _log.info(
  74. "Unable to locate deployment context from portal properties");
  75. }
  76. }
  77. }
  78. if (Validator.isNull(_servletContextName)) {
  79. _servletContextName = "opensocial-portlet";
  80. }
  81. return _servletContextName;
  82. }
  83. }
  84. public static Object translateInput(BaseModel<?> oldModel) {
  85. Class<?> oldModelClass = oldModel.getClass();
  86. String oldModelClassName = oldModelClass.getName();
  87. if (oldModelClassName.equals(GadgetClp.class.getName())) {
  88. return translateInputGadget(oldModel);
  89. }
  90. if (oldModelClassName.equals(OAuthConsumerClp.class.getName())) {
  91. return translateInputOAuthConsumer(oldModel);
  92. }
  93. if (oldModelClassName.equals(OAuthTokenClp.class.getName())) {
  94. return translateInputOAuthToken(oldModel);
  95. }
  96. return oldModel;
  97. }
  98. public static Object translateInput(List<Object> oldList) {
  99. List<Object> newList = new ArrayList<Object>(oldList.size());
  100. for (int i = 0; i < oldList.size(); i++) {
  101. Object curObj = oldList.get(i);
  102. newList.add(translateInput(curObj));
  103. }
  104. return newList;
  105. }
  106. public static Object translateInputGadget(BaseModel<?> oldModel) {
  107. GadgetClp oldClpModel = (GadgetClp)oldModel;
  108. BaseModel<?> newModel = oldClpModel.getGadgetRemoteModel();
  109. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  110. return newModel;
  111. }
  112. public static Object translateInputOAuthConsumer(BaseModel<?> oldModel) {
  113. OAuthConsumerClp oldClpModel = (OAuthConsumerClp)oldModel;
  114. BaseModel<?> newModel = oldClpModel.getOAuthConsumerRemoteModel();
  115. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  116. return newModel;
  117. }
  118. public static Object translateInputOAuthToken(BaseModel<?> oldModel) {
  119. OAuthTokenClp oldClpModel = (OAuthTokenClp)oldModel;
  120. BaseModel<?> newModel = oldClpModel.getOAuthTokenRemoteModel();
  121. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  122. return newModel;
  123. }
  124. public static Object translateInput(Object obj) {
  125. if (obj instanceof BaseModel<?>) {
  126. return translateInput((BaseModel<?>)obj);
  127. }
  128. else if (obj instanceof List<?>) {
  129. return translateInput((List<Object>)obj);
  130. }
  131. else {
  132. return obj;
  133. }
  134. }
  135. public static Object translateOutput(BaseModel<?> oldModel) {
  136. Class<?> oldModelClass = oldModel.getClass();
  137. String oldModelClassName = oldModelClass.getName();
  138. if (oldModelClassName.equals(
  139. "com.liferay.opensocial.model.impl.GadgetImpl")) {
  140. return translateOutputGadget(oldModel);
  141. }
  142. else if (oldModelClassName.endsWith("Clp")) {
  143. try {
  144. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  145. Method getClpSerializerClassMethod = oldModelClass.getMethod(
  146. "getClpSerializerClass");
  147. Class<?> oldClpSerializerClass = (Class<?>)getClpSerializerClassMethod.invoke(oldModel);
  148. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  149. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput",
  150. BaseModel.class);
  151. Class<?> oldModelModelClass = oldModel.getModelClass();
  152. Method getRemoteModelMethod = oldModelClass.getMethod("get" +
  153. oldModelModelClass.getSimpleName() + "RemoteModel");
  154. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  155. BaseModel<?> newModel = (BaseModel<?>)translateOutputMethod.invoke(null,
  156. oldRemoteModel);
  157. return newModel;
  158. }
  159. catch (Throwable t) {
  160. if (_log.isInfoEnabled()) {
  161. _log.info("Unable to translate " + oldModelClassName, t);
  162. }
  163. }
  164. }
  165. if (oldModelClassName.equals(
  166. "com.liferay.opensocial.model.impl.OAuthConsumerImpl")) {
  167. return translateOutputOAuthConsumer(oldModel);
  168. }
  169. else if (oldModelClassName.endsWith("Clp")) {
  170. try {
  171. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  172. Method getClpSerializerClassMethod = oldModelClass.getMethod(
  173. "getClpSerializerClass");
  174. Class<?> oldClpSerializerClass = (Class<?>)getClpSerializerClassMethod.invoke(oldModel);
  175. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  176. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput",
  177. BaseModel.class);
  178. Class<?> oldModelModelClass = oldModel.getModelClass();
  179. Method getRemoteModelMethod = oldModelClass.getMethod("get" +
  180. oldModelModelClass.getSimpleName() + "RemoteModel");
  181. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  182. BaseModel<?> newModel = (BaseModel<?>)translateOutputMethod.invoke(null,
  183. oldRemoteModel);
  184. return newModel;
  185. }
  186. catch (Throwable t) {
  187. if (_log.isInfoEnabled()) {
  188. _log.info("Unable to translate " + oldModelClassName, t);
  189. }
  190. }
  191. }
  192. if (oldModelClassName.equals(
  193. "com.liferay.opensocial.model.impl.OAuthTokenImpl")) {
  194. return translateOutputOAuthToken(oldModel);
  195. }
  196. else if (oldModelClassName.endsWith("Clp")) {
  197. try {
  198. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  199. Method getClpSerializerClassMethod = oldModelClass.getMethod(
  200. "getClpSerializerClass");
  201. Class<?> oldClpSerializerClass = (Class<?>)getClpSerializerClassMethod.invoke(oldModel);
  202. Class<?> newClpSerializerClass = classLoader.loadClass(oldClpSerializerClass.getName());
  203. Method translateOutputMethod = newClpSerializerClass.getMethod("translateOutput",
  204. BaseModel.class);
  205. Class<?> oldModelModelClass = oldModel.getModelClass();
  206. Method getRemoteModelMethod = oldModelClass.getMethod("get" +
  207. oldModelModelClass.getSimpleName() + "RemoteModel");
  208. Object oldRemoteModel = getRemoteModelMethod.invoke(oldModel);
  209. BaseModel<?> newModel = (BaseModel<?>)translateOutputMethod.invoke(null,
  210. oldRemoteModel);
  211. return newModel;
  212. }
  213. catch (Throwable t) {
  214. if (_log.isInfoEnabled()) {
  215. _log.info("Unable to translate " + oldModelClassName, t);
  216. }
  217. }
  218. }
  219. return oldModel;
  220. }
  221. public static Object translateOutput(List<Object> oldList) {
  222. List<Object> newList = new ArrayList<Object>(oldList.size());
  223. for (int i = 0; i < oldList.size(); i++) {
  224. Object curObj = oldList.get(i);
  225. newList.add(translateOutput(curObj));
  226. }
  227. return newList;
  228. }
  229. public static Object translateOutput(Object obj) {
  230. if (obj instanceof BaseModel<?>) {
  231. return translateOutput((BaseModel<?>)obj);
  232. }
  233. else if (obj instanceof List<?>) {
  234. return translateOutput((List<Object>)obj);
  235. }
  236. else {
  237. return obj;
  238. }
  239. }
  240. public static Throwable translateThrowable(Throwable throwable) {
  241. if (_useReflectionToTranslateThrowable) {
  242. try {
  243. UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
  244. ObjectOutputStream objectOutputStream = new ObjectOutputStream(unsyncByteArrayOutputStream);
  245. objectOutputStream.writeObject(throwable);
  246. objectOutputStream.flush();
  247. objectOutputStream.close();
  248. UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(unsyncByteArrayOutputStream.unsafeGetByteArray(),
  249. 0, unsyncByteArrayOutputStream.size());
  250. Thread currentThread = Thread.currentThread();
  251. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  252. ObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(unsyncByteArrayInputStream,
  253. contextClassLoader);
  254. throwable = (Throwable)objectInputStream.readObject();
  255. objectInputStream.close();
  256. return throwable;
  257. }
  258. catch (ClassNotFoundException cnfe) {
  259. if (_log.isInfoEnabled()) {
  260. _log.info("Do not use reflection to translate throwable");
  261. }
  262. _useReflectionToTranslateThrowable = false;
  263. }
  264. catch (SecurityException se) {
  265. if (_log.isInfoEnabled()) {
  266. _log.info("Do not use reflection to translate throwable");
  267. }
  268. _useReflectionToTranslateThrowable = false;
  269. }
  270. catch (Throwable throwable2) {
  271. _log.error(throwable2, throwable2);
  272. return throwable2;
  273. }
  274. }
  275. Class<?> clazz = throwable.getClass();
  276. String className = clazz.getName();
  277. if (className.equals(
  278. "com.liferay.opensocial.exception.DuplicateGadgetURLException")) {
  279. return new com.liferay.opensocial.exception.DuplicateGadgetURLException(throwable.getMessage(),
  280. throwable.getCause());
  281. }
  282. if (className.equals(
  283. "com.liferay.opensocial.exception.GadgetPortletCategoryNamesException")) {
  284. return new com.liferay.opensocial.exception.GadgetPortletCategoryNamesException(throwable.getMessage(),
  285. throwable.getCause());
  286. }
  287. if (className.equals(
  288. "com.liferay.opensocial.exception.GadgetURLException")) {
  289. return new com.liferay.opensocial.exception.GadgetURLException(throwable.getMessage(),
  290. throwable.getCause());
  291. }
  292. if (className.equals(
  293. "com.liferay.opensocial.exception.NoSuchGadgetException")) {
  294. return new com.liferay.opensocial.exception.NoSuchGadgetException(throwable.getMessage(),
  295. throwable.getCause());
  296. }
  297. if (className.equals(
  298. "com.liferay.opensocial.exception.NoSuchOAuthConsumerException")) {
  299. return new com.liferay.opensocial.exception.NoSuchOAuthConsumerException(throwable.getMessage(),
  300. throwable.getCause());
  301. }
  302. if (className.equals(
  303. "com.liferay.opensocial.exception.NoSuchOAuthTokenException")) {
  304. return new com.liferay.opensocial.exception.NoSuchOAuthTokenException(throwable.getMessage(),
  305. throwable.getCause());
  306. }
  307. return throwable;
  308. }
  309. public static Object translateOutputGadget(BaseModel<?> oldModel) {
  310. GadgetClp newModel = new GadgetClp();
  311. newModel.setModelAttributes(oldModel.getModelAttributes());
  312. newModel.setGadgetRemoteModel(oldModel);
  313. return newModel;
  314. }
  315. public static Object translateOutputOAuthConsumer(BaseModel<?> oldModel) {
  316. OAuthConsumerClp newModel = new OAuthConsumerClp();
  317. newModel.setModelAttributes(oldModel.getModelAttributes());
  318. newModel.setOAuthConsumerRemoteModel(oldModel);
  319. return newModel;
  320. }
  321. public static Object translateOutputOAuthToken(BaseModel<?> oldModel) {
  322. OAuthTokenClp newModel = new OAuthTokenClp();
  323. newModel.setModelAttributes(oldModel.getModelAttributes());
  324. newModel.setOAuthTokenRemoteModel(oldModel);
  325. return newModel;
  326. }
  327. private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
  328. private static String _servletContextName;
  329. private static boolean _useReflectionToTranslateThrowable = true;
  330. }