|
| 1 | +/* |
| 2 | + * See the NOTICE file distributed with this work for additional |
| 3 | + * information regarding copyright ownership. |
| 4 | + * |
| 5 | + * This is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU Lesser General Public License as |
| 7 | + * published by the Free Software Foundation; either version 2.1 of |
| 8 | + * the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This software is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this software; if not, write to the Free |
| 17 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 19 | + */ |
| 20 | +package com.xwiki.analytics.internal; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import javax.inject.Inject; |
| 27 | +import javax.inject.Named; |
| 28 | +import javax.inject.Provider; |
| 29 | +import javax.inject.Singleton; |
| 30 | + |
| 31 | +import org.quartz.SchedulerException; |
| 32 | +import org.quartz.Trigger; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.xwiki.component.annotation.Component; |
| 35 | +import org.xwiki.component.phase.Initializable; |
| 36 | +import org.xwiki.component.phase.InitializationException; |
| 37 | +import org.xwiki.extension.event.ExtensionEvent; |
| 38 | +import org.xwiki.extension.event.ExtensionInstalledEvent; |
| 39 | +import org.xwiki.extension.event.ExtensionUpgradedEvent; |
| 40 | +import org.xwiki.model.reference.LocalDocumentReference; |
| 41 | +import org.xwiki.observation.AbstractEventListener; |
| 42 | +import org.xwiki.observation.event.Event; |
| 43 | + |
| 44 | +import com.xpn.xwiki.XWikiContext; |
| 45 | +import com.xpn.xwiki.XWikiException; |
| 46 | +import com.xpn.xwiki.doc.XWikiDocument; |
| 47 | +import com.xpn.xwiki.objects.BaseObject; |
| 48 | +import com.xpn.xwiki.plugin.scheduler.JobState; |
| 49 | +import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin; |
| 50 | + |
| 51 | +/** |
| 52 | + * Schedules the jobs for the aggregators macros on install and reschedules them on upgrade to make sure that everything |
| 53 | + * works properly. |
| 54 | + * |
| 55 | + * @version $Id$ |
| 56 | + * @since 1.2 |
| 57 | + */ |
| 58 | +@Component |
| 59 | +@Named(AnalyticsApplicationListener.ROLE_HINT) |
| 60 | +@Singleton |
| 61 | +public class AnalyticsApplicationListener extends AbstractEventListener implements Initializable |
| 62 | +{ |
| 63 | + protected static final String ROLE_HINT = "AnalyticsApplicationListener"; |
| 64 | + |
| 65 | + protected static final List<String> CODE_SPACE = Arrays.asList("Analytics", "Code", "Jobs"); |
| 66 | + |
| 67 | + protected static final List<String> JOB_PAGES = List.of("JobLastSeenUser"); |
| 68 | + |
| 69 | + private static final String ANALYTICS_APPLICATION_ID = "com.xwiki.analytics:application-analytics-ui"; |
| 70 | + |
| 71 | + @Inject |
| 72 | + private Logger logger; |
| 73 | + |
| 74 | + @Inject |
| 75 | + private Provider<XWikiContext> contextProvider; |
| 76 | + |
| 77 | + /** |
| 78 | + * Default constructor. |
| 79 | + */ |
| 80 | + public AnalyticsApplicationListener() |
| 81 | + { |
| 82 | + //TODO: Filter by ExtensionInstallEvent with the help of the extension id when XCOMMONS-2526 is added to |
| 83 | + // the platform and the analytics application depends on that parent. |
| 84 | + |
| 85 | + super(ROLE_HINT, |
| 86 | + Arrays.<Event>asList(new ExtensionUpgradedEvent(ANALYTICS_APPLICATION_ID), new ExtensionInstalledEvent())); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The migration should be done at ExtensionUpgradedEvent, but for avoiding XCOMMONS-751: Getting wrong component |
| 91 | + * instance during JAR extension upgrade, it is done also at initialization step, since when an extension is |
| 92 | + * upgraded its listeners are initialized too. After the issue is fixed and the application starts depending on a |
| 93 | + * version of XWiki >= the version where is fixed, then only the migration from inside the event should be executed. |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public void initialize() throws InitializationException |
| 97 | + { |
| 98 | + |
| 99 | + // Don't schedule jobs at xwiki start up time. |
| 100 | + if (this.contextProvider.get() != null) { |
| 101 | + prepareJobs(true); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onEvent(Event event, Object source, Object data) |
| 107 | + { |
| 108 | + |
| 109 | + if (event instanceof ExtensionUpgradedEvent) { |
| 110 | + // Unscheduled and reschedule the jobs |
| 111 | + prepareJobs(true); |
| 112 | + } else if (isAnalyticsInstallEvent(event)) { |
| 113 | + prepareJobs(false); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Will gather all the jobs pages that are bundled in the analytics app. |
| 119 | + * @return list with document references of all the jobs that are brought by the analytics app. |
| 120 | + */ |
| 121 | + private List<LocalDocumentReference> getJobPages() |
| 122 | + { |
| 123 | + List<LocalDocumentReference> jobPages = new ArrayList<>(); |
| 124 | + for (String jobPage : JOB_PAGES) { |
| 125 | + jobPages.add(new LocalDocumentReference(CODE_SPACE, jobPage)); |
| 126 | + } |
| 127 | + return jobPages; |
| 128 | + } |
| 129 | + |
| 130 | + private void prepareJobs(boolean reschedule) |
| 131 | + { |
| 132 | + List<LocalDocumentReference> jobPages = getJobPages(); |
| 133 | + try { |
| 134 | + |
| 135 | + for (LocalDocumentReference jobPage : jobPages) { |
| 136 | + scheduleJob(reschedule, jobPage); |
| 137 | + } |
| 138 | + } catch (SchedulerException | XWikiException e) { |
| 139 | + logger.error("Failed to schedule jobs", e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static boolean isAnalyticsInstallEvent(Event event) |
| 144 | + { |
| 145 | + return event instanceof ExtensionInstalledEvent && ANALYTICS_APPLICATION_ID.equals( |
| 146 | + ((ExtensionEvent) event).getExtensionId().getId()); |
| 147 | + } |
| 148 | + |
| 149 | + private void scheduleJob(boolean doReschedule, LocalDocumentReference jobDocReference) |
| 150 | + throws XWikiException, SchedulerException |
| 151 | + { |
| 152 | + XWikiContext xcontext = contextProvider.get(); |
| 153 | + |
| 154 | + SchedulerPlugin schedulerPlugin = |
| 155 | + (SchedulerPlugin) xcontext.getWiki().getPluginManager().getPlugin("scheduler"); |
| 156 | + XWikiDocument jobDoc = xcontext.getWiki().getDocument(jobDocReference, xcontext); |
| 157 | + BaseObject job = jobDoc.getXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE); |
| 158 | + JobState jobState = schedulerPlugin.getJobStatus(job, xcontext); |
| 159 | + |
| 160 | + if (doReschedule && jobState.getQuartzState().equals(Trigger.TriggerState.NORMAL)) { |
| 161 | + schedulerPlugin.unscheduleJob(job, xcontext); |
| 162 | + schedulerPlugin.scheduleJob(job, xcontext); |
| 163 | + logger.info("Job, [{}], was rescheduled successfully", jobDocReference); |
| 164 | + } else if (jobState.getQuartzState().equals(Trigger.TriggerState.NONE)) { |
| 165 | + schedulerPlugin.scheduleJob(job, xcontext); |
| 166 | + logger.info("Job, [{}], was scheduled successfully", jobDocReference); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments