Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
- * any later version.
- *
- * This library is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
- * details.
- */
- package com.liferay.cdi.beanscanner.internal;
- import com.liferay.portal.kernel.log.Log;
- import com.liferay.portal.kernel.log.LogFactoryUtil;
- import com.liferay.portal.kernel.model.CompanyConstants;
- import com.liferay.portal.kernel.model.Portlet;
- import com.liferay.portal.kernel.model.PortletInfo;
- import com.liferay.portal.kernel.portlet.InvokerPortlet;
- import com.liferay.portal.kernel.portlet.LiferayWindowState;
- import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
- import com.liferay.portal.kernel.util.ArrayUtil;
- import com.liferay.portal.kernel.util.ContentTypes;
- import com.liferay.portal.kernel.util.GetterUtil;
- import com.liferay.portal.kernel.util.PortletKeys;
- import com.liferay.portal.kernel.util.SetUtil;
- import com.liferay.portal.kernel.util.StringBundler;
- import com.liferay.portal.kernel.util.StringUtil;
- import com.liferay.portal.kernel.xml.QName;
- import com.liferay.portal.kernel.xml.SAXReaderUtil;
- import org.osgi.framework.BundleContext;
- import org.osgi.framework.ServiceRegistration;
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Dictionary;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Set;
- import javax.enterprise.event.Observes;
- import javax.enterprise.inject.spi.AfterBeanDiscovery;
- import javax.enterprise.inject.spi.AnnotatedType;
- import javax.enterprise.inject.spi.BeforeBeanDiscovery;
- import javax.enterprise.inject.spi.Extension;
- import javax.enterprise.inject.spi.ProcessAnnotatedType;
- import javax.portlet.PortletMode;
- import javax.portlet.WindowState;
- import javax.portlet.annotations.ActionMethod;
- import javax.portlet.annotations.EventMethod;
- import javax.portlet.annotations.InitParameter;
- import javax.portlet.annotations.LocaleString;
- import javax.portlet.annotations.PortletConfiguration;
- import javax.portlet.annotations.PortletQName;
- import javax.portlet.annotations.Preference;
- import javax.portlet.annotations.RenderMethod;
- import javax.portlet.annotations.SecurityRoleRef;
- import javax.portlet.annotations.ServeResourceMethod;
- import javax.portlet.annotations.Supports;
- /**
- * @author Neil Griffin
- */
- public class BeanPortletExtension implements Extension {
- public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {
- collectActionMethods();
- collectEventMethods();
- collectRenderMethods();
- collectServeResourceMethods();
- BundleContext bundleContext = null;
- /*
- ServiceRegistration<Portlet> serviceRegistration =
- bundleContext.registerService(
- Portlet.class, new CDIPortlet(osgiUIProvider),
- properties);
- */
- }
- public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
- _actionMethods = new ArrayList<>();
- _eventMethods = new ArrayList<>();
- _portletModels = new HashMap<>();
- _renderMethods = new ArrayList<>();
- _serveResourceMethods = new ArrayList<>();
- }
- public <T> void processAnnotatedType(
- @Observes ProcessAnnotatedType<T> processAnnotatedType) {
- com.liferay.portal.kernel.model.Portlet portalPortletModel =
- PortletLocalServiceUtil.getPortletById(
- CompanyConstants.SYSTEM, PortletKeys.PORTAL);
- AnnotatedType<T> annotatedType =
- processAnnotatedType.getAnnotatedType();
- Class<T> javaClass = annotatedType.getJavaClass();
- PortletConfiguration portletConfiguration = javaClass.getAnnotation(
- PortletConfiguration.class);
- if (portletConfiguration != null) {
- String portletName = portletConfiguration.portletName();
- String portletId = portletName;
- Dictionary<String, Object> properties = new Hashtable<>();
- collectJxPortletFeatures(portletConfiguration, portletModel);
- _portletModels.put(portletName, portletModel);
- }
- processMethods(javaClass, ActionMethod.class, _actionMethods);
- processMethods(javaClass, EventMethod.class, _eventMethods);
- processMethods(javaClass, RenderMethod.class, _renderMethods);
- processMethods(
- javaClass, ServeResourceMethod.class, _serveResourceMethods);
- }
- protected com.liferay.portal.kernel.model.Portlet buildPortletModel(
- CDIPortletApp cdiPortletApp, String portletId) {
- com.liferay.portal.kernel.model.Portlet portletModel =
- PortletLocalServiceUtil.createPortlet(0);
- portletModel.setPortletId(portletId);
- portletModel.setCompanyId(CompanyConstants.SYSTEM);
- portletModel.setPluginPackage(cdiPortletApp.getPluginPackage());
- portletModel.setPortletApp(cdiPortletApp);
- portletModel.setRoleMappers(cdiPortletApp.getRoleMappers());
- portletModel.setStrutsPath(portletId);
- return portletModel;
- }
- protected void collectActionMethods() {
- for (Method method : _actionMethods) {
- ActionMethod actionMethod = method.getAnnotation(
- ActionMethod.class);
- String portletName = actionMethod.portletName();
- Portlet portletModel = _portletModels.get(portletName);
- if (portletModel == null) {
- _log.error(
- "No portlet named \"" + portletName +
- "\" was registered via @PortletConfiguration for " +
- "@ActionMethod " + method.getName());
- break;
- }
- portletModel.setPublishingEvents(
- createQNames(actionMethod.publishingEvents()));
- // TODO: portlet3 - Somehow need to associate the discovered
- // ActionMethod with the portletName in the _portletModels map.
- String actionName = actionMethod.actionName();
- System.err.println(
- "!@#$ @ActionMethod portletName=" + portletName +
- " actionName=" + actionName);
- }
- }
- protected void collectEventMethods() {
- for (Method method : _eventMethods) {
- EventMethod eventMethod = method.getAnnotation(EventMethod.class);
- String portletName = eventMethod.portletName();
- Portlet portletModel = _portletModels.get(portletName);
- if (portletModel == null) {
- _log.error(
- "No portlet named \"" + portletName +
- "\" was registered via @PortletConfiguration for " +
- "@EventMethod " + method.getName());
- break;
- }
- portletModel.setProcessingEvents(
- createQNames(eventMethod.processingEvents()));
- Set<QName> publishingEvents = portletModel.getPublishingEvents();
- if (publishingEvents == null) {
- publishingEvents = createQNames(eventMethod.publishingEvents());
- }
- else {
- publishingEvents.addAll(
- createQNames(eventMethod.publishingEvents()));
- }
- portletModel.setPublishingEvents(publishingEvents);
- // TODO: portlet3 - Somehow need to associate the discovered
- // EventMethod with the portletName in the _portletModels map.
- System.err.println("!@#$ @EventMethod portletName=" + portletName);
- }
- }
- protected void collectExpirationCache(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- portletModel.setExpCache(portletConfiguration.cacheExpirationTime());
- }
- protected void collectInitParams(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- Map<String, String> initParams = new HashMap<>();
- for (InitParameter initParameter : portletConfiguration.initParams()) {
- initParams.put(initParameter.name(), initParameter.value());
- }
- initParams.put(
- InvokerPortlet.INIT_INVOKER_PORTLET_NAME, "portlet-servlet");
- portletModel.setInitParams(initParams);
- }
- protected void collectJxPortletFeatures(
- PortletConfiguration portletConfiguration,
- com.liferay.portal.kernel.model.Portlet portletModel) {
- collectExpirationCache(portletConfiguration, portletModel);
- collectInitParams(portletConfiguration, portletModel);
- collectPortletInfo(portletConfiguration, portletModel);
- collectPortletModes(portletConfiguration, portletModel);
- collectPortletPreferences(portletConfiguration, portletModel);
- collectResourceBundle(portletConfiguration, portletModel);
- collectSecurityRoleRefs(portletConfiguration, portletModel);
- collectSupportedPublicRenderParameters(
- portletConfiguration, portletModel);
- collectWindowStates(portletConfiguration, portletModel);
- }
- protected String collectLocale(LocaleString[] localeStrings) {
- return collectLocale(localeStrings, null);
- }
- protected String collectLocale(
- LocaleString[] localeStrings, String defaultValue) {
- String english = Locale.ENGLISH.getLanguage();
- for (LocaleString localeString : localeStrings) {
- if ((localeString.locale() == null) ||
- english.equals(localeString.locale())) {
- return localeString.value();
- }
- }
- return defaultValue;
- }
- protected void collectPortletInfo(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- String portletInfoTitle = collectLocale(portletConfiguration.title());
- String portletDisplayName = collectLocale(
- portletConfiguration.displayName(), portletInfoTitle);
- String portletInfoKeyWords = collectLocale(
- portletConfiguration.keywords());
- String portletDescription = collectLocale(
- portletConfiguration.description());
- String portletInfoShortTitle = collectLocale(
- portletConfiguration.shortTitle());
- PortletInfo portletInfo = new PortletInfo(
- portletDisplayName, portletInfoShortTitle, portletInfoKeyWords,
- portletDescription);
- portletModel.setPortletInfo(portletInfo);
- }
- protected void collectPortletModes(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- Map<String, Set<String>> portletModes = new HashMap<>();
- String[] defaultPortletModes = {toLowerCase(PortletMode.VIEW)};
- portletModes.put(
- ContentTypes.TEXT_HTML, SetUtil.fromArray(defaultPortletModes));
- for (Supports supports : portletConfiguration.supports()) {
- portletModes.put(
- supports.mimeType(),
- SetUtil.fromArray(
- ArrayUtil.append(
- defaultPortletModes, supports.portletModes())));
- }
- portletModel.setPortletModes(portletModes);
- }
- protected void collectPortletPreferences(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- StringBundler sb = new StringBundler();
- sb.append("<?xml version=\"1.0\"?>");
- sb.append("<portlet-preferences>");
- for (Preference preference : portletConfiguration.prefs()) {
- sb.append("<preference>");
- sb.append("<name>");
- sb.append(preference.name());
- sb.append("</name>");
- String[] values = preference.values();
- for (String value : values) {
- sb.append("<value>");
- sb.append(value);
- sb.append("</value>");
- }
- sb.append("</preference>");
- }
- sb.append("</portlet-preferences>");
- portletModel.setDefaultPreferences(sb.toString());
- }
- protected void collectRenderMethods() {
- for (Method method : _renderMethods) {
- RenderMethod renderMethod = method.getAnnotation(
- RenderMethod.class);
- for (String portletName : renderMethod.portletNames()) {
- String contentType = renderMethod.contentType();
- String include = renderMethod.include();
- int ordinal = renderMethod.ordinal();
- String portletMode = renderMethod.portletMode();
- // TODO: portlet3 - Somehow need to associate the discovered
- // RenderMethod with the portletName in the _portletModels map.
- System.err.println(
- "!@#$ @RenderMethod portletName=" + portletName +
- " contentType=" + contentType + " include=" + include +
- " ordinal=" + ordinal + " portletMode=" + portletMode);
- }
- }
- }
- protected void collectResourceBundle(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- String resourceBundle = GetterUtil.getString(
- portletConfiguration.resourceBundle(),
- portletModel.getResourceBundle());
- portletModel.setResourceBundle(resourceBundle);
- }
- protected void collectSecurityRoleRefs(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- Set<String> unlinkedRoles = new HashSet<>();
- for (SecurityRoleRef roleRef : portletConfiguration.roleRefs()) {
- unlinkedRoles.add(roleRef.roleName());
- }
- if (unlinkedRoles.isEmpty()) {
- unlinkedRoles.add("administrator");
- unlinkedRoles.add("guest");
- unlinkedRoles.add("power-user");
- unlinkedRoles.add("user");
- }
- portletModel.setUnlinkedRoles(unlinkedRoles);
- portletModel.linkRoles();
- }
- protected void collectServeResourceMethods() {
- for (Method method : _serveResourceMethods) {
- ServeResourceMethod serveResourceMethod = method.getAnnotation(
- ServeResourceMethod.class);
- for (String portletName : serveResourceMethod.portletNames()) {
- boolean asyncSupported = serveResourceMethod.asyncSupported();
- String characterEncoding =
- serveResourceMethod.characterEncoding();
- String contentType = serveResourceMethod.contentType();
- String include = serveResourceMethod.include();
- int ordinal = serveResourceMethod.ordinal();
- String resourceID = serveResourceMethod.resourceID();
- // TODO: portlet3 - Somehow need to associate the discovered
- // ServeResourceMethod with the portletName in the
- // _portletModels map.
- System.err.println(
- "!@#$ @ServeResourceMethod portletName=" + portletName +
- " asyncSupported=" + asyncSupported +
- " characterEncoding=" + characterEncoding +
- " contentType=" + contentType + " include=" + include +
- " ordinal=" + ordinal + " resourceId=" + resourceID);
- }
- }
- }
- protected void collectSupportedPublicRenderParameters(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- LOH -- This is the last of the "collectJx" ones to implement (you think)....
- }
- protected void collectWindowStates(
- PortletConfiguration portletConfiguration, Portlet portletModel) {
- Map<String, Set<String>> windowStates = new HashMap<>();
- windowStates.put(
- ContentTypes.TEXT_HTML,
- SetUtil.fromArray(
- new String[] {
- toLowerCase(LiferayWindowState.EXCLUSIVE),
- toLowerCase(LiferayWindowState.POP_UP),
- toLowerCase(WindowState.MAXIMIZED),
- toLowerCase(WindowState.MINIMIZED),
- toLowerCase(WindowState.NORMAL)
- }));
- for (Supports supports : portletConfiguration.supports()) {
- Set<String> mimeTypeWindowStates = new HashSet<>();
- mimeTypeWindowStates.add(toLowerCase(WindowState.NORMAL));
- String[] supportedWindowStates = supports.windowStates();
- if (supportedWindowStates.length == 0) {
- mimeTypeWindowStates.add(
- toLowerCase(LiferayWindowState.EXCLUSIVE));
- mimeTypeWindowStates.add(
- toLowerCase(LiferayWindowState.POP_UP));
- mimeTypeWindowStates.add(toLowerCase(WindowState.MAXIMIZED));
- mimeTypeWindowStates.add(toLowerCase(WindowState.MINIMIZED));
- }
- else {
- mimeTypeWindowStates.addAll(
- SetUtil.fromArray(supportedWindowStates));
- }
- windowStates.put(supports.mimeType(), mimeTypeWindowStates);
- }
- portletModel.setWindowStates(windowStates);
- }
- protected Set<QName> createQNames(PortletQName[] portletQNames) {
- Set<QName> qNames = new HashSet<>();
- for (PortletQName portletQName : portletQNames) {
- qNames.add(
- SAXReaderUtil.createQName(
- portletQName.localPart(),
- SAXReaderUtil.createNamespace(
- portletQName.namespaceURI())));
- }
- return qNames;
- }
- protected <T> void processMethods(
- Class<T> javaClass, Class<? extends Annotation> annotationClass,
- List<Method> methodList) {
- Method[] methods = javaClass.getMethods();
- for (Method method : methods) {
- if (method.getAnnotation(annotationClass) != null) {
- methodList.add(method);
- }
- }
- }
- protected String toLowerCase(Object object) {
- String string = String.valueOf(object);
- return StringUtil.toLowerCase(string.trim());
- }
- private static final Log _log = LogFactoryUtil.getLog(
- PortletTrackerExtension.class);
- private List<Method> _actionMethods;
- private List<Method> _eventMethods;
- private Map<String, Dictionary> _portletModels;
- private List<Method> _renderMethods;
- private List<Method> _serveResourceMethods;
- }
Add Comment
Please, Sign In to add comment