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.deploy.hot;
016    
017    import com.liferay.portal.kernel.configuration.Configuration;
018    import com.liferay.portal.kernel.configuration.ConfigurationFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
022    import com.liferay.portal.kernel.scheduler.SchedulerEntry;
023    import com.liferay.portal.kernel.scheduler.SchedulerException;
024    import com.liferay.portal.kernel.scheduler.StorageType;
025    import com.liferay.portal.kernel.scheduler.StorageTypeAware;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.PrefsPropsUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.registry.Filter;
031    import com.liferay.registry.Registry;
032    import com.liferay.registry.RegistryUtil;
033    import com.liferay.registry.ServiceReference;
034    import com.liferay.registry.ServiceTracker;
035    import com.liferay.registry.ServiceTrackerCustomizer;
036    
037    /**
038     * @author Raymond Aug??
039     */
040    public class SchedulerEntryRegistry {
041    
042            public SchedulerEntryRegistry() {
043                    if (!PropsValues.SCHEDULER_ENABLED) {
044                            _serviceTracker = null;
045    
046                            return;
047                    }
048    
049                    Registry registry = RegistryUtil.getRegistry();
050    
051                    Filter filter = registry.getFilter(
052                            "(&(javax.portlet.name=*)(objectClass=" +
053                                    SchedulerEntry.class.getName() + "))");
054    
055                    _serviceTracker = registry.trackServices(
056                            filter, new SchedulerEntryServiceTrackerCustomizer());
057    
058                    _serviceTracker.open();
059            }
060    
061            public void close() {
062                    if (!PropsValues.SCHEDULER_ENABLED) {
063                            return;
064                    }
065    
066                    _serviceTracker.close();
067            }
068    
069            private static final Log _log = LogFactoryUtil.getLog(
070                    SchedulerEntryRegistry.class);
071    
072            private final ServiceTracker<SchedulerEntry, SchedulerEntry>
073                    _serviceTracker;
074    
075            private class SchedulerEntryServiceTrackerCustomizer
076                    implements ServiceTrackerCustomizer<SchedulerEntry, SchedulerEntry> {
077    
078                    @Override
079                    public SchedulerEntry addingService(
080                            ServiceReference<SchedulerEntry> serviceReference) {
081    
082                            Registry registry = RegistryUtil.getRegistry();
083    
084                            SchedulerEntry schedulerEntry = registry.getService(
085                                    serviceReference);
086    
087                            StorageType storageType = StorageType.MEMORY_CLUSTERED;
088    
089                            if (schedulerEntry instanceof StorageTypeAware) {
090                                    StorageTypeAware storageTypeAware =
091                                            (StorageTypeAware)schedulerEntry;
092    
093                                    storageType = storageTypeAware.getStorageType();
094                            }
095    
096                            addTrigger(schedulerEntry, serviceReference);
097    
098                            String portletId = (String)serviceReference.getProperty(
099                                    "javax.portlet.name");
100    
101                            try {
102                                    SchedulerEngineHelperUtil.schedule(
103                                            schedulerEntry, storageType, portletId, 0);
104    
105                                    return schedulerEntry;
106                            }
107                            catch (SchedulerException e) {
108                                    _log.error(e, e);
109                            }
110    
111                            return null;
112                    }
113    
114                    @Override
115                    public void modifiedService(
116                            ServiceReference<SchedulerEntry> serviceReference,
117                            SchedulerEntry schedulerEntry) {
118                    }
119    
120                    @Override
121                    public void removedService(
122                            ServiceReference<SchedulerEntry> serviceReference,
123                            SchedulerEntry schedulerEntry) {
124    
125                            Registry registry = RegistryUtil.getRegistry();
126    
127                            registry.ungetService(serviceReference);
128    
129                            StorageType storageType = StorageType.MEMORY_CLUSTERED;
130    
131                            if (schedulerEntry instanceof StorageTypeAware) {
132                                    StorageTypeAware storageTypeAware =
133                                            (StorageTypeAware)schedulerEntry;
134    
135                                    storageType = storageTypeAware.getStorageType();
136                            }
137    
138                            try {
139                                    SchedulerEngineHelperUtil.unschedule(
140                                            schedulerEntry, storageType);
141                            }
142                            catch (SchedulerException e) {
143                                    _log.error(e, e);
144                            }
145                    }
146    
147                    protected void addTrigger(
148                            SchedulerEntry schedulerEntry,
149                            ServiceReference<SchedulerEntry> serviceReference) {
150    
151                            String propertyKey = schedulerEntry.getPropertyKey();
152    
153                            if (Validator.isNull(propertyKey)) {
154                                    return;
155                            }
156    
157                            long bundleId = GetterUtil.getLong(
158                                    serviceReference.getProperty("bundle.id"), -1);
159    
160                            String triggerValue = null;
161    
162                            if (bundleId != 0) {
163                                    Class<?> clazz = schedulerEntry.getClass();
164    
165                                    ClassLoader classloader = clazz.getClassLoader();
166    
167                                    triggerValue = getPluginPropertyValue(classloader, propertyKey);
168                            }
169                            else {
170                                    triggerValue = PrefsPropsUtil.getString(propertyKey);
171                            }
172    
173                            if (_log.isDebugEnabled()) {
174                                    _log.debug(
175                                            "Scheduler property key " + propertyKey +
176                                                    " has trigger value " + triggerValue);
177                            }
178    
179                            if (Validator.isNotNull(triggerValue)) {
180                                    schedulerEntry.setTriggerValue(triggerValue);
181                            }
182                    }
183    
184                    protected String getPluginPropertyValue(
185                            ClassLoader classLoader, String propertyKey) {
186    
187                            Configuration configuration =
188                                    ConfigurationFactoryUtil.getConfiguration(
189                                            classLoader, "portlet");
190    
191                            return configuration.get(propertyKey);
192                    }
193    
194            }
195    
196    }