001
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 import com.liferay.util.log4j.Log4JUtil;
037
038 import java.net.URL;
039 import java.util.HashMap;
040
041 import java.util.List;
042 import java.util.Map;
043 import java.util.Properties;
044
045
048 public class ServiceConfiguratorImpl implements ServiceConfigurator {
049
050 public void destory() {
051 if (_serviceRegistrar != null) {
052 _serviceRegistrar.destroy();
053 }
054 }
055
056 @Override
057 public void destroyServices(
058 ServiceComponentConfiguration serviceComponentConfiguration,
059 ClassLoader classLoader)
060 throws Exception {
061
062 destroyServiceComponent(serviceComponentConfiguration, classLoader);
063 }
064
065 @Override
066 public void initServices(
067 ServiceComponentConfiguration serviceComponentConfiguration,
068 ClassLoader classLoader)
069 throws Exception {
070
071 initLog4J(classLoader);
072
073 initServiceComponent(serviceComponentConfiguration, classLoader);
074
075 reconfigureCaches(classLoader);
076
077 readResourceActions(classLoader);
078 }
079
080 public void setServiceComponentLocalService(
081 ServiceComponentLocalService serviceComponentLocalService) {
082
083 _serviceComponentLocalService = serviceComponentLocalService;
084 }
085
086 protected void destroyServiceComponent(
087 ServiceComponentConfiguration serviceComponentConfiguration,
088 ClassLoader classLoader)
089 throws Exception {
090
091 _serviceComponentLocalService.destroyServiceComponent(
092 serviceComponentConfiguration, classLoader);
093 }
094
095 protected URL getPortalCacheConfigurationURL(
096 Configuration configuration, ClassLoader classLoader,
097 String configLocation) {
098
099 String cacheConfigurationLocation = configuration.get(configLocation);
100
101 if (Validator.isNull(cacheConfigurationLocation)) {
102 return null;
103 }
104
105 return classLoader.getResource(cacheConfigurationLocation);
106 }
107
108 protected void initLog4J(ClassLoader classLoader) {
109 Log4JUtil.configureLog4J(
110 classLoader.getResource("META-INF/portal-log4j.xml"));
111 }
112
113 protected void initServiceComponent(
114 ServiceComponentConfiguration serviceComponentConfiguration,
115 ClassLoader classLoader) {
116
117 Configuration configuration = null;
118
119 try {
120 configuration = ConfigurationFactoryUtil.getConfiguration(
121 classLoader, "service");
122 }
123 catch (Exception e) {
124 if (_log.isDebugEnabled()) {
125 _log.debug("Unable to read service.properties");
126 }
127
128 return;
129 }
130
131 Properties properties = configuration.getProperties();
132
133 if (properties.isEmpty()) {
134 return;
135 }
136
137 String buildNamespace = GetterUtil.getString(
138 properties.getProperty("build.namespace"));
139 long buildNumber = GetterUtil.getLong(
140 properties.getProperty("build.number"));
141 long buildDate = GetterUtil.getLong(
142 properties.getProperty("build.date"));
143 boolean buildAutoUpgrade = GetterUtil.getBoolean(
144 properties.getProperty("build.auto.upgrade"), true);
145
146 if (_log.isDebugEnabled()) {
147 _log.debug("Build namespace " + buildNamespace);
148 _log.debug("Build number " + buildNumber);
149 _log.debug("Build date " + buildDate);
150 _log.debug("Build auto upgrade " + buildAutoUpgrade);
151 }
152
153 if (Validator.isNull(buildNamespace)) {
154 return;
155 }
156
157 try {
158 _serviceComponentLocalService.initServiceComponent(
159 serviceComponentConfiguration,
160 classLoader, buildNamespace, buildNumber, buildDate,
161 buildAutoUpgrade);
162 }
163 catch (PortalException pe) {
164 _log.error("Unable to initialize service component", pe);
165 }
166 }
167
168 protected void readResourceActions(ClassLoader classLoader) {
169 Configuration configuration = ConfigurationFactoryUtil.getConfiguration(
170 classLoader, "portlet");
171
172 String[] resourceActionsConfigs = StringUtil.split(
173 configuration.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
174
175 for (String resourceActionsConfig : resourceActionsConfigs) {
176 try {
177 ResourceActionsUtil.read(
178 null, classLoader, resourceActionsConfig);
179 }
180 catch (Exception e) {
181 _log.error(
182 "Unable to read resource actions config in " +
183 resourceActionsConfig,
184 e);
185 }
186 }
187
188 String[] portletIds = StringUtil.split(
189 configuration.get("service.configurator.portlet.ids"));
190
191 for (String portletId : portletIds) {
192 List<String> modelNames =
193 ResourceActionsUtil.getPortletModelResources(portletId);
194
195 for (String modelName : modelNames) {
196 List<String> modelActions =
197 ResourceActionsUtil.getModelResourceActions(modelName);
198
199 ResourceActionLocalServiceUtil.checkResourceActions(
200 modelName, modelActions);
201 }
202 }
203 }
204
205 protected void reconfigureCaches(ClassLoader classLoader) throws Exception {
206 Configuration configuration = null;
207
208 try {
209 configuration = ConfigurationFactoryUtil.getConfiguration(
210 classLoader, "portlet");
211 }
212 catch (Exception e) {
213 if (_log.isDebugEnabled()) {
214 _log.debug("Unable to read portlet.properties");
215 }
216
217 return;
218 }
219
220 String singleVMConfigurationLocation = configuration.get(
221 PropsKeys.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
222 String multiVMConfigurationLocation = configuration.get(
223 PropsKeys.EHCACHE_MULTI_VM_CONFIG_LOCATION);
224
225 if (Validator.isNull(singleVMConfigurationLocation) &&
226 Validator.isNull(multiVMConfigurationLocation)) {
227
228 return;
229 }
230
231 if (_serviceRegistrar == null) {
232 Registry registry = RegistryUtil.getRegistry();
233
234 _serviceRegistrar = registry.getServiceRegistrar(
235 PortalCacheConfiguratorSettings.class);
236 }
237
238 if (Validator.isNotNull(singleVMConfigurationLocation)) {
239 Map<String, Object> properties = new HashMap<>();
240
241 properties.put(
242 "portal.cache.manager.name", PortalCacheManagerNames.SINGLE_VM);
243
244 _serviceRegistrar.registerService(
245 PortalCacheConfiguratorSettings.class,
246 new PortalCacheConfiguratorSettings(
247 classLoader, singleVMConfigurationLocation),
248 properties);
249 }
250
251 if (Validator.isNotNull(multiVMConfigurationLocation)) {
252 Map<String, Object> properties = new HashMap<>();
253
254 properties.put(
255 "portal.cache.manager.name", PortalCacheManagerNames.MULTI_VM);
256
257 _serviceRegistrar.registerService(
258 PortalCacheConfiguratorSettings.class,
259 new PortalCacheConfiguratorSettings(
260 classLoader, multiVMConfigurationLocation),
261 properties);
262 }
263 }
264
265 private static Log _log = LogFactoryUtil.getLog(
266 ServiceConfiguratorImpl.class);
267
268 private ServiceComponentLocalService _serviceComponentLocalService;
269 private volatile ServiceRegistrar<PortalCacheConfiguratorSettings>
270 _serviceRegistrar;
271
272 }