Guest User

Untitled

a guest
Mar 21st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.92 KB | None | 0 0
  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.  
  15. package com.liferay.cdi.beanscanner.internal;
  16.  
  17. import com.liferay.portal.kernel.log.Log;
  18. import com.liferay.portal.kernel.log.LogFactoryUtil;
  19. import com.liferay.portal.kernel.model.CompanyConstants;
  20. import com.liferay.portal.kernel.model.Portlet;
  21. import com.liferay.portal.kernel.model.PortletInfo;
  22. import com.liferay.portal.kernel.portlet.InvokerPortlet;
  23. import com.liferay.portal.kernel.portlet.LiferayWindowState;
  24. import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
  25. import com.liferay.portal.kernel.util.ArrayUtil;
  26. import com.liferay.portal.kernel.util.ContentTypes;
  27. import com.liferay.portal.kernel.util.GetterUtil;
  28. import com.liferay.portal.kernel.util.PortletKeys;
  29. import com.liferay.portal.kernel.util.SetUtil;
  30. import com.liferay.portal.kernel.util.StringBundler;
  31. import com.liferay.portal.kernel.util.StringUtil;
  32. import com.liferay.portal.kernel.xml.QName;
  33. import com.liferay.portal.kernel.xml.SAXReaderUtil;
  34. import org.osgi.framework.BundleContext;
  35. import org.osgi.framework.ServiceRegistration;
  36.  
  37. import java.lang.annotation.Annotation;
  38. import java.lang.reflect.Method;
  39.  
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.Dictionary;
  43. import java.util.HashMap;
  44. import java.util.HashSet;
  45. import java.util.Hashtable;
  46. import java.util.List;
  47. import java.util.Locale;
  48. import java.util.Map;
  49. import java.util.Set;
  50.  
  51. import javax.enterprise.event.Observes;
  52. import javax.enterprise.inject.spi.AfterBeanDiscovery;
  53. import javax.enterprise.inject.spi.AnnotatedType;
  54. import javax.enterprise.inject.spi.BeforeBeanDiscovery;
  55. import javax.enterprise.inject.spi.Extension;
  56. import javax.enterprise.inject.spi.ProcessAnnotatedType;
  57.  
  58. import javax.portlet.PortletMode;
  59. import javax.portlet.WindowState;
  60. import javax.portlet.annotations.ActionMethod;
  61. import javax.portlet.annotations.EventMethod;
  62. import javax.portlet.annotations.InitParameter;
  63. import javax.portlet.annotations.LocaleString;
  64. import javax.portlet.annotations.PortletConfiguration;
  65. import javax.portlet.annotations.PortletQName;
  66. import javax.portlet.annotations.Preference;
  67. import javax.portlet.annotations.RenderMethod;
  68. import javax.portlet.annotations.SecurityRoleRef;
  69. import javax.portlet.annotations.ServeResourceMethod;
  70. import javax.portlet.annotations.Supports;
  71.  
  72. /**
  73. * @author Neil Griffin
  74. */
  75. public class BeanPortletExtension implements Extension {
  76.  
  77. public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {
  78.  
  79. collectActionMethods();
  80. collectEventMethods();
  81. collectRenderMethods();
  82. collectServeResourceMethods();
  83.  
  84. BundleContext bundleContext = null;
  85.  
  86. /*
  87. ServiceRegistration<Portlet> serviceRegistration =
  88. bundleContext.registerService(
  89. Portlet.class, new CDIPortlet(osgiUIProvider),
  90. properties);
  91. */
  92. }
  93.  
  94. public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
  95. _actionMethods = new ArrayList<>();
  96. _eventMethods = new ArrayList<>();
  97. _portletModels = new HashMap<>();
  98. _renderMethods = new ArrayList<>();
  99. _serveResourceMethods = new ArrayList<>();
  100. }
  101.  
  102. public <T> void processAnnotatedType(
  103. @Observes ProcessAnnotatedType<T> processAnnotatedType) {
  104.  
  105. com.liferay.portal.kernel.model.Portlet portalPortletModel =
  106. PortletLocalServiceUtil.getPortletById(
  107. CompanyConstants.SYSTEM, PortletKeys.PORTAL);
  108.  
  109. AnnotatedType<T> annotatedType =
  110. processAnnotatedType.getAnnotatedType();
  111.  
  112. Class<T> javaClass = annotatedType.getJavaClass();
  113.  
  114. PortletConfiguration portletConfiguration = javaClass.getAnnotation(
  115. PortletConfiguration.class);
  116.  
  117. if (portletConfiguration != null) {
  118. String portletName = portletConfiguration.portletName();
  119. String portletId = portletName;
  120. Dictionary<String, Object> properties = new Hashtable<>();
  121. collectJxPortletFeatures(portletConfiguration, portletModel);
  122.  
  123. _portletModels.put(portletName, portletModel);
  124. }
  125.  
  126. processMethods(javaClass, ActionMethod.class, _actionMethods);
  127. processMethods(javaClass, EventMethod.class, _eventMethods);
  128. processMethods(javaClass, RenderMethod.class, _renderMethods);
  129. processMethods(
  130. javaClass, ServeResourceMethod.class, _serveResourceMethods);
  131. }
  132.  
  133. protected com.liferay.portal.kernel.model.Portlet buildPortletModel(
  134. CDIPortletApp cdiPortletApp, String portletId) {
  135.  
  136. com.liferay.portal.kernel.model.Portlet portletModel =
  137. PortletLocalServiceUtil.createPortlet(0);
  138.  
  139. portletModel.setPortletId(portletId);
  140.  
  141. portletModel.setCompanyId(CompanyConstants.SYSTEM);
  142. portletModel.setPluginPackage(cdiPortletApp.getPluginPackage());
  143. portletModel.setPortletApp(cdiPortletApp);
  144. portletModel.setRoleMappers(cdiPortletApp.getRoleMappers());
  145. portletModel.setStrutsPath(portletId);
  146.  
  147. return portletModel;
  148. }
  149.  
  150. protected void collectActionMethods() {
  151.  
  152. for (Method method : _actionMethods) {
  153.  
  154. ActionMethod actionMethod = method.getAnnotation(
  155. ActionMethod.class);
  156.  
  157. String portletName = actionMethod.portletName();
  158.  
  159. Portlet portletModel = _portletModels.get(portletName);
  160.  
  161. if (portletModel == null) {
  162.  
  163. _log.error(
  164. "No portlet named \"" + portletName +
  165. "\" was registered via @PortletConfiguration for " +
  166. "@ActionMethod " + method.getName());
  167.  
  168. break;
  169. }
  170.  
  171. portletModel.setPublishingEvents(
  172. createQNames(actionMethod.publishingEvents()));
  173.  
  174. // TODO: portlet3 - Somehow need to associate the discovered
  175. // ActionMethod with the portletName in the _portletModels map.
  176. String actionName = actionMethod.actionName();
  177. System.err.println(
  178. "!@#$ @ActionMethod portletName=" + portletName +
  179. " actionName=" + actionName);
  180. }
  181. }
  182.  
  183. protected void collectEventMethods() {
  184.  
  185. for (Method method : _eventMethods) {
  186.  
  187. EventMethod eventMethod = method.getAnnotation(EventMethod.class);
  188.  
  189. String portletName = eventMethod.portletName();
  190.  
  191. Portlet portletModel = _portletModels.get(portletName);
  192.  
  193. if (portletModel == null) {
  194.  
  195. _log.error(
  196. "No portlet named \"" + portletName +
  197. "\" was registered via @PortletConfiguration for " +
  198. "@EventMethod " + method.getName());
  199.  
  200. break;
  201. }
  202.  
  203. portletModel.setProcessingEvents(
  204. createQNames(eventMethod.processingEvents()));
  205.  
  206. Set<QName> publishingEvents = portletModel.getPublishingEvents();
  207.  
  208. if (publishingEvents == null) {
  209. publishingEvents = createQNames(eventMethod.publishingEvents());
  210. }
  211. else {
  212. publishingEvents.addAll(
  213. createQNames(eventMethod.publishingEvents()));
  214. }
  215.  
  216. portletModel.setPublishingEvents(publishingEvents);
  217.  
  218. // TODO: portlet3 - Somehow need to associate the discovered
  219. // EventMethod with the portletName in the _portletModels map.
  220. System.err.println("!@#$ @EventMethod portletName=" + portletName);
  221. }
  222. }
  223.  
  224. protected void collectExpirationCache(
  225. PortletConfiguration portletConfiguration, Portlet portletModel) {
  226.  
  227. portletModel.setExpCache(portletConfiguration.cacheExpirationTime());
  228. }
  229.  
  230. protected void collectInitParams(
  231. PortletConfiguration portletConfiguration, Portlet portletModel) {
  232.  
  233. Map<String, String> initParams = new HashMap<>();
  234.  
  235. for (InitParameter initParameter : portletConfiguration.initParams()) {
  236. initParams.put(initParameter.name(), initParameter.value());
  237. }
  238.  
  239. initParams.put(
  240. InvokerPortlet.INIT_INVOKER_PORTLET_NAME, "portlet-servlet");
  241.  
  242. portletModel.setInitParams(initParams);
  243. }
  244.  
  245. protected void collectJxPortletFeatures(
  246. PortletConfiguration portletConfiguration,
  247. com.liferay.portal.kernel.model.Portlet portletModel) {
  248.  
  249. collectExpirationCache(portletConfiguration, portletModel);
  250. collectInitParams(portletConfiguration, portletModel);
  251. collectPortletInfo(portletConfiguration, portletModel);
  252. collectPortletModes(portletConfiguration, portletModel);
  253. collectPortletPreferences(portletConfiguration, portletModel);
  254. collectResourceBundle(portletConfiguration, portletModel);
  255. collectSecurityRoleRefs(portletConfiguration, portletModel);
  256. collectSupportedPublicRenderParameters(
  257. portletConfiguration, portletModel);
  258. collectWindowStates(portletConfiguration, portletModel);
  259. }
  260.  
  261. protected String collectLocale(LocaleString[] localeStrings) {
  262. return collectLocale(localeStrings, null);
  263. }
  264.  
  265. protected String collectLocale(
  266. LocaleString[] localeStrings, String defaultValue) {
  267.  
  268. String english = Locale.ENGLISH.getLanguage();
  269.  
  270. for (LocaleString localeString : localeStrings) {
  271.  
  272. if ((localeString.locale() == null) ||
  273. english.equals(localeString.locale())) {
  274.  
  275. return localeString.value();
  276. }
  277. }
  278.  
  279. return defaultValue;
  280. }
  281.  
  282. protected void collectPortletInfo(
  283. PortletConfiguration portletConfiguration, Portlet portletModel) {
  284.  
  285. String portletInfoTitle = collectLocale(portletConfiguration.title());
  286.  
  287. String portletDisplayName = collectLocale(
  288. portletConfiguration.displayName(), portletInfoTitle);
  289.  
  290. String portletInfoKeyWords = collectLocale(
  291. portletConfiguration.keywords());
  292.  
  293. String portletDescription = collectLocale(
  294. portletConfiguration.description());
  295.  
  296. String portletInfoShortTitle = collectLocale(
  297. portletConfiguration.shortTitle());
  298.  
  299. PortletInfo portletInfo = new PortletInfo(
  300. portletDisplayName, portletInfoShortTitle, portletInfoKeyWords,
  301. portletDescription);
  302.  
  303. portletModel.setPortletInfo(portletInfo);
  304. }
  305.  
  306. protected void collectPortletModes(
  307. PortletConfiguration portletConfiguration, Portlet portletModel) {
  308.  
  309. Map<String, Set<String>> portletModes = new HashMap<>();
  310. String[] defaultPortletModes = {toLowerCase(PortletMode.VIEW)};
  311. portletModes.put(
  312. ContentTypes.TEXT_HTML, SetUtil.fromArray(defaultPortletModes));
  313.  
  314. for (Supports supports : portletConfiguration.supports()) {
  315. portletModes.put(
  316. supports.mimeType(),
  317. SetUtil.fromArray(
  318. ArrayUtil.append(
  319. defaultPortletModes, supports.portletModes())));
  320. }
  321.  
  322. portletModel.setPortletModes(portletModes);
  323. }
  324.  
  325. protected void collectPortletPreferences(
  326. PortletConfiguration portletConfiguration, Portlet portletModel) {
  327.  
  328. StringBundler sb = new StringBundler();
  329. sb.append("<?xml version=\"1.0\"?>");
  330. sb.append("<portlet-preferences>");
  331.  
  332. for (Preference preference : portletConfiguration.prefs()) {
  333.  
  334. sb.append("<preference>");
  335. sb.append("<name>");
  336. sb.append(preference.name());
  337. sb.append("</name>");
  338.  
  339. String[] values = preference.values();
  340.  
  341. for (String value : values) {
  342. sb.append("<value>");
  343. sb.append(value);
  344. sb.append("</value>");
  345. }
  346.  
  347. sb.append("</preference>");
  348. }
  349.  
  350. sb.append("</portlet-preferences>");
  351.  
  352. portletModel.setDefaultPreferences(sb.toString());
  353. }
  354.  
  355. protected void collectRenderMethods() {
  356.  
  357. for (Method method : _renderMethods) {
  358.  
  359. RenderMethod renderMethod = method.getAnnotation(
  360. RenderMethod.class);
  361.  
  362. for (String portletName : renderMethod.portletNames()) {
  363.  
  364. String contentType = renderMethod.contentType();
  365. String include = renderMethod.include();
  366. int ordinal = renderMethod.ordinal();
  367. String portletMode = renderMethod.portletMode();
  368.  
  369. // TODO: portlet3 - Somehow need to associate the discovered
  370. // RenderMethod with the portletName in the _portletModels map.
  371. System.err.println(
  372. "!@#$ @RenderMethod portletName=" + portletName +
  373. " contentType=" + contentType + " include=" + include +
  374. " ordinal=" + ordinal + " portletMode=" + portletMode);
  375. }
  376. }
  377. }
  378.  
  379. protected void collectResourceBundle(
  380. PortletConfiguration portletConfiguration, Portlet portletModel) {
  381.  
  382. String resourceBundle = GetterUtil.getString(
  383. portletConfiguration.resourceBundle(),
  384. portletModel.getResourceBundle());
  385.  
  386. portletModel.setResourceBundle(resourceBundle);
  387. }
  388.  
  389. protected void collectSecurityRoleRefs(
  390. PortletConfiguration portletConfiguration, Portlet portletModel) {
  391.  
  392. Set<String> unlinkedRoles = new HashSet<>();
  393.  
  394. for (SecurityRoleRef roleRef : portletConfiguration.roleRefs()) {
  395. unlinkedRoles.add(roleRef.roleName());
  396. }
  397.  
  398. if (unlinkedRoles.isEmpty()) {
  399. unlinkedRoles.add("administrator");
  400. unlinkedRoles.add("guest");
  401. unlinkedRoles.add("power-user");
  402. unlinkedRoles.add("user");
  403. }
  404.  
  405. portletModel.setUnlinkedRoles(unlinkedRoles);
  406.  
  407. portletModel.linkRoles();
  408. }
  409.  
  410. protected void collectServeResourceMethods() {
  411.  
  412. for (Method method : _serveResourceMethods) {
  413.  
  414. ServeResourceMethod serveResourceMethod = method.getAnnotation(
  415. ServeResourceMethod.class);
  416.  
  417. for (String portletName : serveResourceMethod.portletNames()) {
  418.  
  419. boolean asyncSupported = serveResourceMethod.asyncSupported();
  420. String characterEncoding =
  421. serveResourceMethod.characterEncoding();
  422. String contentType = serveResourceMethod.contentType();
  423. String include = serveResourceMethod.include();
  424. int ordinal = serveResourceMethod.ordinal();
  425. String resourceID = serveResourceMethod.resourceID();
  426.  
  427. // TODO: portlet3 - Somehow need to associate the discovered
  428. // ServeResourceMethod with the portletName in the
  429. // _portletModels map.
  430. System.err.println(
  431. "!@#$ @ServeResourceMethod portletName=" + portletName +
  432. " asyncSupported=" + asyncSupported +
  433. " characterEncoding=" + characterEncoding +
  434. " contentType=" + contentType + " include=" + include +
  435. " ordinal=" + ordinal + " resourceId=" + resourceID);
  436. }
  437. }
  438. }
  439.  
  440. protected void collectSupportedPublicRenderParameters(
  441. PortletConfiguration portletConfiguration, Portlet portletModel) {
  442.  
  443. LOH -- This is the last of the "collectJx" ones to implement (you think)....
  444. }
  445.  
  446. protected void collectWindowStates(
  447. PortletConfiguration portletConfiguration, Portlet portletModel) {
  448.  
  449. Map<String, Set<String>> windowStates = new HashMap<>();
  450. windowStates.put(
  451. ContentTypes.TEXT_HTML,
  452. SetUtil.fromArray(
  453. new String[] {
  454. toLowerCase(LiferayWindowState.EXCLUSIVE),
  455. toLowerCase(LiferayWindowState.POP_UP),
  456. toLowerCase(WindowState.MAXIMIZED),
  457. toLowerCase(WindowState.MINIMIZED),
  458. toLowerCase(WindowState.NORMAL)
  459. }));
  460.  
  461. for (Supports supports : portletConfiguration.supports()) {
  462. Set<String> mimeTypeWindowStates = new HashSet<>();
  463. mimeTypeWindowStates.add(toLowerCase(WindowState.NORMAL));
  464.  
  465. String[] supportedWindowStates = supports.windowStates();
  466.  
  467. if (supportedWindowStates.length == 0) {
  468. mimeTypeWindowStates.add(
  469. toLowerCase(LiferayWindowState.EXCLUSIVE));
  470. mimeTypeWindowStates.add(
  471. toLowerCase(LiferayWindowState.POP_UP));
  472. mimeTypeWindowStates.add(toLowerCase(WindowState.MAXIMIZED));
  473. mimeTypeWindowStates.add(toLowerCase(WindowState.MINIMIZED));
  474. }
  475. else {
  476. mimeTypeWindowStates.addAll(
  477. SetUtil.fromArray(supportedWindowStates));
  478. }
  479.  
  480. windowStates.put(supports.mimeType(), mimeTypeWindowStates);
  481. }
  482.  
  483. portletModel.setWindowStates(windowStates);
  484. }
  485.  
  486. protected Set<QName> createQNames(PortletQName[] portletQNames) {
  487.  
  488. Set<QName> qNames = new HashSet<>();
  489.  
  490. for (PortletQName portletQName : portletQNames) {
  491.  
  492. qNames.add(
  493. SAXReaderUtil.createQName(
  494. portletQName.localPart(),
  495. SAXReaderUtil.createNamespace(
  496. portletQName.namespaceURI())));
  497. }
  498.  
  499. return qNames;
  500. }
  501.  
  502. protected <T> void processMethods(
  503. Class<T> javaClass, Class<? extends Annotation> annotationClass,
  504. List<Method> methodList) {
  505.  
  506. Method[] methods = javaClass.getMethods();
  507.  
  508. for (Method method : methods) {
  509.  
  510. if (method.getAnnotation(annotationClass) != null) {
  511. methodList.add(method);
  512. }
  513. }
  514. }
  515.  
  516. protected String toLowerCase(Object object) {
  517. String string = String.valueOf(object);
  518.  
  519. return StringUtil.toLowerCase(string.trim());
  520. }
  521.  
  522. private static final Log _log = LogFactoryUtil.getLog(
  523. PortletTrackerExtension.class);
  524.  
  525. private List<Method> _actionMethods;
  526. private List<Method> _eventMethods;
  527. private Map<String, Dictionary> _portletModels;
  528. private List<Method> _renderMethods;
  529. private List<Method> _serveResourceMethods;
  530. }
Add Comment
Please, Sign In to add comment