001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.configuration.configurator.impl;
016    
017    import com.liferay.portal.kernel.cache.PortalCacheManagerNames;
018    import com.liferay.portal.kernel.cache.configurator.PortalCacheConfiguratorSettings;
019    import com.liferay.portal.kernel.configuration.Configuration;
020    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
030    import com.liferay.portal.service.ServiceComponentLocalService;
031    import com.liferay.portal.service.configuration.ServiceComponentConfiguration;
032    import com.liferay.portal.service.configuration.configurator.ServiceConfigurator;
033    import com.liferay.registry.Registry;
034    import com.liferay.registry.RegistryUtil;
035    import com.liferay.registry.ServiceRegistrar;
036    
037    import java.net.URL;
038    import java.util.HashMap;
039    
040    import java.util.List;
041    import java.util.Map;
042    import java.util.Properties;
043    
044    /**
045     * @author Miguel Pastor
046     */
047    public class ServiceConfiguratorImpl implements ServiceConfigurator {
048            
049            public void destory() {
050                    if (_serviceRegistrar != null) {
051                            _serviceRegistrar.destroy();
052                    }
053            }
054    
055            @Override
056            public void destroyServices(
057                            ServiceComponentConfiguration serviceComponentConfiguration,
058                            ClassLoader classLoader)
059                    throws Exception {
060    
061                    destroyServiceComponent(serviceComponentConfiguration, classLoader);
062            }
063    
064            @Override
065            public void initServices(
066                            ServiceComponentConfiguration serviceComponentConfiguration,
067                            ClassLoader classLoader)
068                    throws Exception {
069    
070                    initServiceComponent(serviceComponentConfiguration, classLoader);
071    
072                    reconfigureCaches(classLoader);
073    
074                    readResourceActions(classLoader);
075            }
076    
077            public void setServiceComponentLocalService(
078                    ServiceComponentLocalService serviceComponentLocalService) {
079    
080                    _serviceComponentLocalService = serviceComponentLocalService;
081            }
082    
083            protected void destroyServiceComponent(
084                            ServiceComponentConfiguration serviceComponentConfiguration,
085                            ClassLoader classLoader)
086                    throws Exception {
087    
088                    _serviceComponentLocalService.destroyServiceComponent(
089                            serviceComponentConfiguration, classLoader);
090            }
091    
092            protected URL getPortalCacheConfigurationURL(
093                    Configuration configuration, ClassLoader classLoader,
094                    String configLocation) {
095    
096                    String cacheConfigurationLocation = configuration.get(configLocation);
097    
098                    if (Validator.isNull(cacheConfigurationLocation)) {
099                            return null;
100                    }
101    
102                    return classLoader.getResource(cacheConfigurationLocation);
103            }
104    
105            protected void initServiceComponent(
106                    ServiceComponentConfiguration serviceComponentConfiguration,
107                    ClassLoader classLoader) {
108    
109                    Configuration configuration = null;
110    
111                    try {
112                            configuration = ConfigurationFactoryUtil.getConfiguration(
113                                    classLoader, "service");
114                    }
115                    catch (Exception e) {
116                            if (_log.isDebugEnabled()) {
117                                    _log.debug("Unable to read service.properties");
118                            }
119    
120                            return;
121                    }
122    
123                    Properties properties = configuration.getProperties();
124    
125                    if (properties.isEmpty()) {
126                            return;
127                    }
128    
129                    String buildNamespace = GetterUtil.getString(
130                            properties.getProperty("build.namespace"));
131                    long buildNumber = GetterUtil.getLong(
132                            properties.getProperty("build.number"));
133                    long buildDate = GetterUtil.getLong(
134                            properties.getProperty("build.date"));
135                    boolean buildAutoUpgrade = GetterUtil.getBoolean(
136                            properties.getProperty("build.auto.upgrade"), true);
137    
138                    if (_log.isDebugEnabled()) {
139                            _log.debug("Build namespace " + buildNamespace);
140                            _log.debug("Build number " + buildNumber);
141                            _log.debug("Build date " + buildDate);
142                            _log.debug("Build auto upgrade " + buildAutoUpgrade);
143                    }
144    
145                    if (Validator.isNull(buildNamespace)) {
146                            return;
147                    }
148    
149                    try {
150                            _serviceComponentLocalService.initServiceComponent(
151                                    serviceComponentConfiguration,
152                                    classLoader, buildNamespace, buildNumber, buildDate,
153                                    buildAutoUpgrade);
154                    }
155                    catch (PortalException pe) {
156                            _log.error("Unable to initialize service component", pe);
157                    }
158            }
159    
160            protected void readResourceActions(ClassLoader classLoader) {
161                    Configuration configuration = null;
162    
163                    try {
164                            configuration = ConfigurationFactoryUtil.getConfiguration(
165                                    classLoader, "portlet");
166                    }
167                    catch (Exception e) {
168                            if (_log.isDebugEnabled()) {
169                                    _log.debug("Unable to read portlet.properties");
170                            }
171    
172                            return;
173                    }
174    
175                    String[] resourceActionsConfigs = StringUtil.split(
176                            configuration.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
177    
178                    for (String resourceActionsConfig : resourceActionsConfigs) {
179                            try {
180                                    ResourceActionsUtil.read(
181                                            null, classLoader, resourceActionsConfig);
182                            }
183                            catch (Exception e) {
184                                    _log.error(
185                                            "Unable to read resource actions config in " +
186                                                    resourceActionsConfig,
187                                            e);
188                            }
189                    }
190    
191                    String[] portletIds = StringUtil.split(
192                            configuration.get("service.configurator.portlet.ids"));
193    
194                    for (String portletId : portletIds) {
195                            List<String> modelNames =
196                                    ResourceActionsUtil.getPortletModelResources(portletId);
197    
198                            for (String modelName : modelNames) {
199                                    List<String> modelActions =
200                                            ResourceActionsUtil.getModelResourceActions(modelName);
201    
202                                    ResourceActionLocalServiceUtil.checkResourceActions(
203                                            modelName, modelActions);
204                            }
205                    }
206            }
207    
208            protected void reconfigureCaches(ClassLoader classLoader) throws Exception {
209                    Configuration configuration = null;
210    
211                    try {
212                            configuration = ConfigurationFactoryUtil.getConfiguration(
213                                    classLoader, "portlet");
214                    }
215                    catch (Exception e) {
216                            if (_log.isDebugEnabled()) {
217                                    _log.debug("Unable to read portlet.properties");
218                            }
219    
220                            return;
221                    }
222                    
223                    String singleVMConfigurationLocation = configuration.get(
224                            PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
225                    String multiVMConfigurationLocation = configuration.get(
226                            PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION);
227    
228                    if (Validator.isNull(singleVMConfigurationLocation) &&
229                            Validator.isNull(multiVMConfigurationLocation)) {
230                            
231                            return;
232                    }
233                    
234                    if (_serviceRegistrar == null) {
235                            Registry registry = RegistryUtil.getRegistry();
236    
237                            _serviceRegistrar = registry.getServiceRegistrar(
238                                    PortalCacheConfiguratorSettings.class);
239                    }
240    
241                    if (Validator.isNotNull(singleVMConfigurationLocation)) {
242                            Map<String, Object> properties = new HashMap<>();
243    
244                            properties.put(
245                                    "portal.cache.manager.name", PortalCacheManagerNames.SINGLE_VM);
246    
247                            _serviceRegistrar.registerService(
248                                    PortalCacheConfiguratorSettings.class,
249                                    new PortalCacheConfiguratorSettings(
250                                            classLoader, singleVMConfigurationLocation),
251                                    properties);
252                    }
253    
254                    if (Validator.isNotNull(multiVMConfigurationLocation)) {
255                            Map<String, Object> properties = new HashMap<>();
256    
257                            properties.put(
258                                    "portal.cache.manager.name", PortalCacheManagerNames.MULTI_VM);
259    
260                            _serviceRegistrar.registerService(
261                                    PortalCacheConfiguratorSettings.class,
262                                    new PortalCacheConfiguratorSettings(
263                                            classLoader, multiVMConfigurationLocation),
264                                    properties);
265                    }
266            }
267    
268            private static Log _log = LogFactoryUtil.getLog(
269                    ServiceConfiguratorImpl.class);
270    
271            private ServiceComponentLocalService _serviceComponentLocalService;
272            private volatile ServiceRegistrar<PortalCacheConfiguratorSettings>
273                    _serviceRegistrar;
274    
275    }