|
| 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.xpn.xwiki.plugin.scheduler.internal; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import javax.inject.Inject; |
| 28 | +import javax.inject.Provider; |
| 29 | +import javax.inject.Singleton; |
| 30 | + |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.xwiki.classloader.ClassLoaderManager; |
| 33 | +import org.xwiki.classloader.NamespaceURLClassLoader; |
| 34 | +import org.xwiki.component.annotation.Component; |
| 35 | +import org.xwiki.model.EntityType; |
| 36 | +import org.xwiki.model.reference.EntityReference; |
| 37 | + |
| 38 | +import com.xpn.xwiki.XWikiContext; |
| 39 | +import com.xpn.xwiki.XWikiException; |
| 40 | +import com.xpn.xwiki.doc.XWikiDocument; |
| 41 | +import com.xpn.xwiki.objects.BaseObject; |
| 42 | +import com.xpn.xwiki.objects.BaseObjectReference; |
| 43 | +import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin; |
| 44 | + |
| 45 | +/** |
| 46 | + * Component dedicated to handle operations related to loading classes for Scheduler. |
| 47 | + * |
| 48 | + * @version $Id$ |
| 49 | + * @since 17.10.1 |
| 50 | + * @since 18.0.0RC1 |
| 51 | + */ |
| 52 | +@Component(roles = SchedulersClassLoaderManager.class) |
| 53 | +@Singleton |
| 54 | +public class SchedulersClassLoaderManager |
| 55 | +{ |
| 56 | + private SchedulerPlugin schedulerPlugin; |
| 57 | + |
| 58 | + private final Map<String, Set<BaseObjectReference>> schedulersMapPerNamespace = new HashMap<>(); |
| 59 | + |
| 60 | + @Inject |
| 61 | + private Provider<XWikiContext> contextProvider; |
| 62 | + |
| 63 | + @Inject |
| 64 | + private Logger logger; |
| 65 | + |
| 66 | + @Inject |
| 67 | + private ClassLoaderManager classLoaderManager; |
| 68 | + |
| 69 | + /** |
| 70 | + * Define the instance of the scheduler plugin to use. |
| 71 | + * @param schedulerPlugin the scheduler plugin instance this instance should use. |
| 72 | + */ |
| 73 | + public void setSchedulerPlugin(SchedulerPlugin schedulerPlugin) |
| 74 | + { |
| 75 | + this.schedulerPlugin = schedulerPlugin; |
| 76 | + } |
| 77 | + |
| 78 | + private void registerScheduler(String namespace, BaseObjectReference objectReference) |
| 79 | + { |
| 80 | + if (!this.schedulersMapPerNamespace.containsKey(namespace)) { |
| 81 | + this.schedulersMapPerNamespace.put(namespace, new HashSet<>()); |
| 82 | + } |
| 83 | + this.schedulersMapPerNamespace.get(namespace).add(objectReference); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Remove scheduler information related to given object reference. |
| 88 | + * @param objectReference the reference of a scheduler object. |
| 89 | + */ |
| 90 | + public void removeScheduler(BaseObjectReference objectReference) |
| 91 | + { |
| 92 | + for (Set<BaseObjectReference> objectReferenceSet : this.schedulersMapPerNamespace.values()) { |
| 93 | + objectReferenceSet.remove(objectReference); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Remove all schedulers information associated to a namespace. |
| 99 | + * @param namespace the namespace for which to remove information. |
| 100 | + */ |
| 101 | + public void removeSchedulers(String namespace) |
| 102 | + { |
| 103 | + this.schedulersMapPerNamespace.remove(namespace); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Perform operations when a classloader of a specific namespace is reset. |
| 108 | + * @param namespace the namespace for which an event has been triggered. |
| 109 | + */ |
| 110 | + public void onClassLoaderReset(String namespace) |
| 111 | + { |
| 112 | + schedulersMapPerNamespace |
| 113 | + .getOrDefault(namespace, Set.of()) |
| 114 | + .parallelStream() |
| 115 | + .forEach(this::reloadScheduler); |
| 116 | + } |
| 117 | + |
| 118 | + private void reloadScheduler(BaseObjectReference objectReference) |
| 119 | + { |
| 120 | + XWikiContext context = contextProvider.get(); |
| 121 | + EntityReference documentReference = objectReference.extractReference(EntityType.DOCUMENT); |
| 122 | + try { |
| 123 | + XWikiDocument document = context.getWiki().getDocument(documentReference, context); |
| 124 | + BaseObject jobObject = document.getXObject(SchedulerJobClassDocumentInitializer.XWIKI_JOB_CLASSREFERENCE); |
| 125 | + this.schedulerPlugin.unscheduleJob(jobObject, context); |
| 126 | + this.schedulerPlugin.scheduleJob(jobObject, context); |
| 127 | + } catch (XWikiException e) { |
| 128 | + this.logger.error("Error while trying to reload scheduler for object [{}]: ", objectReference, e); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Load a class for a scheduler and register it at the same time. |
| 134 | + * @param className the name of the class to load. |
| 135 | + * @param baseObjectReference the reference of the object of the scheduler. |
| 136 | + * @return the instance of the given class name. |
| 137 | + * @throws ClassNotFoundException if the class cannot be found. |
| 138 | + */ |
| 139 | + public Class<?> loadClassAndRegister(String className, BaseObjectReference baseObjectReference) |
| 140 | + throws ClassNotFoundException |
| 141 | + { |
| 142 | + String namespace = null; |
| 143 | + |
| 144 | + // Reload the root classloader if needed: it's important if it's been dropped. |
| 145 | + NamespaceURLClassLoader classLoader = this.classLoaderManager.getURLClassLoader(null, true); |
| 146 | + Class<?> result = Class.forName(className, true, classLoader); |
| 147 | + |
| 148 | + // find the actual namespace of the classloader from where the class has been found. |
| 149 | + if (result.getClassLoader() instanceof NamespaceURLClassLoader namespaceURLClassLoader) { |
| 150 | + namespace = namespaceURLClassLoader.getNamespace(); |
| 151 | + } |
| 152 | + |
| 153 | + this.registerScheduler(namespace, baseObjectReference); |
| 154 | + return result; |
| 155 | + } |
| 156 | +} |
0 commit comments