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.cache.configurator.PortalCacheConfigurator;
018    import com.liferay.portal.kernel.configuration.Configuration;
019    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.security.permission.ResourceActionsUtil;
028    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
029    import com.liferay.portal.service.ServiceComponentLocalService;
030    import com.liferay.portal.service.configuration.ServiceComponentConfiguration;
031    import com.liferay.portal.service.configuration.configurator.ServiceConfigurator;
032    import com.liferay.util.log4j.Log4JUtil;
033    
034    import java.net.URL;
035    
036    import java.util.List;
037    import java.util.Properties;
038    
039    /**
040     * @author Miguel Pastor
041     */
042    public class ServiceConfiguratorImpl implements ServiceConfigurator {
043    
044            @Override
045            public void destroyServices(
046                            ServiceComponentConfiguration serviceComponentConfiguration,
047                            ClassLoader classLoader)
048                    throws Exception {
049    
050                    destroyServiceComponent(serviceComponentConfiguration, classLoader);
051            }
052    
053            @Override
054            public void initServices(
055                            ServiceComponentConfiguration serviceComponentConfiguration,
056                            ClassLoader classLoader)
057                    throws Exception {
058    
059                    initLog4J(classLoader);
060    
061                    initServiceComponent(serviceComponentConfiguration, classLoader);
062    
063                    reconfigureCaches(classLoader);
064    
065                    readResourceActions(classLoader);
066            }
067    
068            public void setPortalCacheConfigurator(
069                    PortalCacheConfigurator portalCacheConfigurator) {
070    
071                    _portalCacheConfigurator = portalCacheConfigurator;
072            }
073    
074            public void setServiceComponentLocalService(
075                    ServiceComponentLocalService serviceComponentLocalService) {
076    
077                    _serviceComponentLocalService = serviceComponentLocalService;
078            }
079    
080            protected void destroyServiceComponent(
081                            ServiceComponentConfiguration serviceComponentConfiguration,
082                            ClassLoader classLoader)
083                    throws Exception {
084    
085                    _serviceComponentLocalService.destroyServiceComponent(
086                            serviceComponentConfiguration, classLoader);
087            }
088    
089            protected URL getPortalCacheConfigurationURL(
090                    Configuration configuration, ClassLoader classLoader,
091                    String configLocation) {
092    
093                    String cacheConfigurationLocation = configuration.get(configLocation);
094    
095                    if (Validator.isNull(cacheConfigurationLocation)) {
096                            return null;
097                    }
098    
099                    return classLoader.getResource(cacheConfigurationLocation);
100            }
101    
102            protected void initLog4J(ClassLoader classLoader) {
103                    Log4JUtil.configureLog4J(
104                            classLoader.getResource("META-INF/portal-log4j.xml"));
105            }
106    
107            protected void initServiceComponent(
108                    ServiceComponentConfiguration serviceComponentConfiguration,
109                    ClassLoader classLoader) {
110    
111                    Configuration configuration = null;
112    
113                    try {
114                            configuration = ConfigurationFactoryUtil.getConfiguration(
115                                    classLoader, "service");
116                    }
117                    catch (Exception e) {
118                            if (_log.isDebugEnabled()) {
119                                    _log.debug("Unable to read service.properties");
120                            }
121    
122                            return;
123                    }
124    
125                    Properties properties = configuration.getProperties();
126    
127                    if (properties.isEmpty()) {
128                            return;
129                    }
130    
131                    String buildNamespace = GetterUtil.getString(
132                            properties.getProperty("build.namespace"));
133                    long buildNumber = GetterUtil.getLong(
134                            properties.getProperty("build.number"));
135                    long buildDate = GetterUtil.getLong(
136                            properties.getProperty("build.date"));
137                    boolean buildAutoUpgrade = GetterUtil.getBoolean(
138                            properties.getProperty("build.auto.upgrade"), true);
139    
140                    if (_log.isDebugEnabled()) {
141                            _log.debug("Build namespace " + buildNamespace);
142                            _log.debug("Build number " + buildNumber);
143                            _log.debug("Build date " + buildDate);
144                            _log.debug("Build auto upgrade " + buildAutoUpgrade);
145                    }
146    
147                    if (Validator.isNull(buildNamespace)) {
148                            return;
149                    }
150    
151                    try {
152                            _serviceComponentLocalService.initServiceComponent(
153                                    serviceComponentConfiguration,
154                                    classLoader, buildNamespace, buildNumber, buildDate,
155                                    buildAutoUpgrade);
156                    }
157                    catch (PortalException pe) {
158                            _log.error("Unable to initialize service component", pe);
159                    }
160            }
161    
162            protected void readResourceActions(ClassLoader classLoader) {
163                    Configuration configuration = ConfigurationFactoryUtil.getConfiguration(
164                            classLoader, "portlet");
165    
166                    String[] resourceActionsConfigs = StringUtil.split(
167                            configuration.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
168    
169                    for (String resourceActionsConfig : resourceActionsConfigs) {
170                            try {
171                                    ResourceActionsUtil.read(
172                                            null, classLoader, resourceActionsConfig);
173                            }
174                            catch (Exception e) {
175                                    _log.error(
176                                            "Unable to read resource actions config in " +
177                                                    resourceActionsConfig,
178                                            e);
179                            }
180                    }
181    
182                    String[] portletIds = StringUtil.split(
183                            configuration.get("service.configurator.portlet.ids"));
184    
185                    for (String portletId : portletIds) {
186                            List<String> modelNames =
187                                    ResourceActionsUtil.getPortletModelResources(portletId);
188    
189                            for (String modelName : modelNames) {
190                                    List<String> modelActions =
191                                            ResourceActionsUtil.getModelResourceActions(modelName);
192    
193                                    ResourceActionLocalServiceUtil.checkResourceActions(
194                                            modelName, modelActions);
195                            }
196                    }
197            }
198    
199            protected void reconfigureCaches(ClassLoader classLoader) throws Exception {
200                    Configuration configuration = null;
201    
202                    try {
203                            configuration = ConfigurationFactoryUtil.getConfiguration(
204                                    classLoader, "portlet");
205                    }
206                    catch (Exception e) {
207                            if (_log.isDebugEnabled()) {
208                                    _log.debug("Unable to read portlet.properties");
209                            }
210    
211                            return;
212                    }
213    
214                    _portalCacheConfigurator.reconfigureCaches(
215                            classLoader,
216                            getPortalCacheConfigurationURL(
217                                    configuration, classLoader,
218                                    PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION));
219    
220                    _portalCacheConfigurator.reconfigureCaches(
221                            classLoader,
222                            getPortalCacheConfigurationURL(
223                                    configuration, classLoader,
224                                    PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION));
225    
226                    _portalCacheConfigurator.reconfigureHibernateCache(
227                            getPortalCacheConfigurationURL(
228                                    configuration, classLoader,
229                                    PropsKeys.NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME));
230            }
231    
232            private static Log _log = LogFactoryUtil.getLog(
233                    ServiceConfiguratorImpl.class);
234    
235            private PortalCacheConfigurator _portalCacheConfigurator;
236            private ServiceComponentLocalService _serviceComponentLocalService;
237    
238    }