PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/liferay/liferay-plugins
Java | 348 lines | 243 code | 89 blank | 16 comment | 36 complexity | f92f49cae2f660ec3d807d9cf82fb73c MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2012 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.wsrp.service;
  15. import com.liferay.portal.kernel.exception.PortalException;
  16. import com.liferay.portal.kernel.exception.SystemException;
  17. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
  18. import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
  19. import com.liferay.portal.kernel.log.Log;
  20. import com.liferay.portal.kernel.log.LogFactoryUtil;
  21. import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
  22. import com.liferay.portal.kernel.util.PropsUtil;
  23. import com.liferay.portal.kernel.util.Validator;
  24. import com.liferay.portal.model.BaseModel;
  25. import com.liferay.wsrp.model.WSRPConsumerClp;
  26. import com.liferay.wsrp.model.WSRPConsumerPortletClp;
  27. import com.liferay.wsrp.model.WSRPProducerClp;
  28. import java.io.ObjectInputStream;
  29. import java.io.ObjectOutputStream;
  30. import java.lang.reflect.Method;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * @author Brian Wing Shun Chan
  35. */
  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. "wsrp-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. "wsrp-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 = "wsrp-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(WSRPConsumerClp.class.getName())) {
  88. return translateInputWSRPConsumer(oldModel);
  89. }
  90. if (oldModelClassName.equals(WSRPConsumerPortletClp.class.getName())) {
  91. return translateInputWSRPConsumerPortlet(oldModel);
  92. }
  93. if (oldModelClassName.equals(WSRPProducerClp.class.getName())) {
  94. return translateInputWSRPProducer(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 translateInputWSRPConsumer(BaseModel<?> oldModel) {
  107. WSRPConsumerClp oldClpModel = (WSRPConsumerClp)oldModel;
  108. BaseModel<?> newModel = oldClpModel.getWSRPConsumerRemoteModel();
  109. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  110. return newModel;
  111. }
  112. public static Object translateInputWSRPConsumerPortlet(
  113. BaseModel<?> oldModel) {
  114. WSRPConsumerPortletClp oldClpModel = (WSRPConsumerPortletClp)oldModel;
  115. BaseModel<?> newModel = oldClpModel.getWSRPConsumerPortletRemoteModel();
  116. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  117. return newModel;
  118. }
  119. public static Object translateInputWSRPProducer(BaseModel<?> oldModel) {
  120. WSRPProducerClp oldClpModel = (WSRPProducerClp)oldModel;
  121. BaseModel<?> newModel = oldClpModel.getWSRPProducerRemoteModel();
  122. newModel.setModelAttributes(oldClpModel.getModelAttributes());
  123. return newModel;
  124. }
  125. public static Object translateInput(Object obj) {
  126. if (obj instanceof BaseModel<?>) {
  127. return translateInput((BaseModel<?>)obj);
  128. }
  129. else if (obj instanceof List<?>) {
  130. return translateInput((List<Object>)obj);
  131. }
  132. else {
  133. return obj;
  134. }
  135. }
  136. public static Object translateOutput(BaseModel<?> oldModel) {
  137. Class<?> oldModelClass = oldModel.getClass();
  138. String oldModelClassName = oldModelClass.getName();
  139. if (oldModelClassName.equals(
  140. "com.liferay.wsrp.model.impl.WSRPConsumerImpl")) {
  141. return translateOutputWSRPConsumer(oldModel);
  142. }
  143. if (oldModelClassName.equals(
  144. "com.liferay.wsrp.model.impl.WSRPConsumerPortletImpl")) {
  145. return translateOutputWSRPConsumerPortlet(oldModel);
  146. }
  147. if (oldModelClassName.equals(
  148. "com.liferay.wsrp.model.impl.WSRPProducerImpl")) {
  149. return translateOutputWSRPProducer(oldModel);
  150. }
  151. return oldModel;
  152. }
  153. public static Object translateOutput(List<Object> oldList) {
  154. List<Object> newList = new ArrayList<Object>(oldList.size());
  155. for (int i = 0; i < oldList.size(); i++) {
  156. Object curObj = oldList.get(i);
  157. newList.add(translateOutput(curObj));
  158. }
  159. return newList;
  160. }
  161. public static Object translateOutput(Object obj) {
  162. if (obj instanceof BaseModel<?>) {
  163. return translateOutput((BaseModel<?>)obj);
  164. }
  165. else if (obj instanceof List<?>) {
  166. return translateOutput((List<Object>)obj);
  167. }
  168. else {
  169. return obj;
  170. }
  171. }
  172. public static Throwable translateThrowable(Throwable throwable) {
  173. if (_useReflectionToTranslateThrowable) {
  174. try {
  175. UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
  176. ObjectOutputStream objectOutputStream = new ObjectOutputStream(unsyncByteArrayOutputStream);
  177. objectOutputStream.writeObject(throwable);
  178. objectOutputStream.flush();
  179. objectOutputStream.close();
  180. UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(unsyncByteArrayOutputStream.unsafeGetByteArray(),
  181. 0, unsyncByteArrayOutputStream.size());
  182. Thread currentThread = Thread.currentThread();
  183. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  184. ObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(unsyncByteArrayInputStream,
  185. contextClassLoader);
  186. throwable = (Throwable)objectInputStream.readObject();
  187. objectInputStream.close();
  188. return throwable;
  189. }
  190. catch (SecurityException se) {
  191. if (_log.isInfoEnabled()) {
  192. _log.info("Do not use reflection to translate throwable");
  193. }
  194. _useReflectionToTranslateThrowable = false;
  195. }
  196. catch (Throwable throwable2) {
  197. _log.error(throwable2, throwable2);
  198. return throwable2;
  199. }
  200. }
  201. Class<?> clazz = throwable.getClass();
  202. String className = clazz.getName();
  203. if (className.equals(PortalException.class.getName())) {
  204. return new PortalException();
  205. }
  206. if (className.equals(SystemException.class.getName())) {
  207. return new SystemException();
  208. }
  209. if (className.equals("com.liferay.wsrp.WSRPConsumerNameException")) {
  210. return new com.liferay.wsrp.WSRPConsumerNameException();
  211. }
  212. if (className.equals(
  213. "com.liferay.wsrp.WSRPConsumerPortletHandleException")) {
  214. return new com.liferay.wsrp.WSRPConsumerPortletHandleException();
  215. }
  216. if (className.equals(
  217. "com.liferay.wsrp.WSRPConsumerPortletNameException")) {
  218. return new com.liferay.wsrp.WSRPConsumerPortletNameException();
  219. }
  220. if (className.equals("com.liferay.wsrp.WSRPConsumerWSDLException")) {
  221. return new com.liferay.wsrp.WSRPConsumerWSDLException();
  222. }
  223. if (className.equals("com.liferay.wsrp.WSRPProducerNameException")) {
  224. return new com.liferay.wsrp.WSRPProducerNameException();
  225. }
  226. if (className.equals("com.liferay.wsrp.NoSuchConsumerException")) {
  227. return new com.liferay.wsrp.NoSuchConsumerException();
  228. }
  229. if (className.equals("com.liferay.wsrp.NoSuchConsumerPortletException")) {
  230. return new com.liferay.wsrp.NoSuchConsumerPortletException();
  231. }
  232. if (className.equals("com.liferay.wsrp.NoSuchProducerException")) {
  233. return new com.liferay.wsrp.NoSuchProducerException();
  234. }
  235. return throwable;
  236. }
  237. public static Object translateOutputWSRPConsumer(BaseModel<?> oldModel) {
  238. WSRPConsumerClp newModel = new WSRPConsumerClp();
  239. newModel.setModelAttributes(oldModel.getModelAttributes());
  240. newModel.setWSRPConsumerRemoteModel(oldModel);
  241. return newModel;
  242. }
  243. public static Object translateOutputWSRPConsumerPortlet(
  244. BaseModel<?> oldModel) {
  245. WSRPConsumerPortletClp newModel = new WSRPConsumerPortletClp();
  246. newModel.setModelAttributes(oldModel.getModelAttributes());
  247. newModel.setWSRPConsumerPortletRemoteModel(oldModel);
  248. return newModel;
  249. }
  250. public static Object translateOutputWSRPProducer(BaseModel<?> oldModel) {
  251. WSRPProducerClp newModel = new WSRPProducerClp();
  252. newModel.setModelAttributes(oldModel.getModelAttributes());
  253. newModel.setWSRPProducerRemoteModel(oldModel);
  254. return newModel;
  255. }
  256. private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
  257. private static String _servletContextName;
  258. private static boolean _useReflectionToTranslateThrowable = true;
  259. }