View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.selector;
18  
19  import java.net.URI;
20  import java.util.List;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.logging.log4j.core.LoggerContext;
24  
25  /**
26   * Interface used to locate a LoggerContext.
27   */
28  public interface ContextSelector {
29  
30      long DEFAULT_STOP_TIMEOUT = 50;
31  
32      /**
33       * Shuts down the LoggerContext.
34       * @param fqcn The fully qualified class name of the caller.
35       * @param loader The ClassLoader to use or null.
36       * @param currentContext If true returns the current Context, if false returns the Context appropriate
37       * @param allContexts if true all LoggerContexts that can be located will be shutdown.
38       * @since 2.13.0
39       */
40      default void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext,
41                            final boolean allContexts) {
42          if (hasContext(fqcn, loader, currentContext)) {
43              getContext(fqcn, loader, currentContext).stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
44          }
45      }
46  
47      /**
48       * Checks to see if a LoggerContext is installed. The default implementation returns false.
49       * @param fqcn The fully qualified class name of the caller.
50       * @param loader The ClassLoader to use or null.
51       * @param currentContext If true returns the current Context, if false returns the Context appropriate
52       * for the caller if a more appropriate Context can be determined.
53       * @return true if a LoggerContext has been installed, false otherwise.
54       * @since 2.13.0
55       */
56      default boolean hasContext(String fqcn, ClassLoader loader, boolean currentContext) {
57          return false;
58      }
59  
60      /**
61       * Returns the LoggerContext.
62       * @param fqcn The fully qualified class name of the caller.
63       * @param loader ClassLoader to use or null.
64       * @param currentContext If true returns the current Context, if false returns the Context appropriate
65       * for the caller if a more appropriate Context can be determined.
66       * @return The LoggerContext.
67       */
68      LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext);
69  
70      /**
71       * Returns the LoggerContext.
72       * @param fqcn The fully qualified class name of the caller.
73       * @param loader ClassLoader to use or null.
74       * @param currentContext If true returns the current Context, if false returns the Context appropriate
75       * for the caller if a more appropriate Context can be determined.
76       * @param configLocation The location of the configuration for the LoggerContext.
77       * @return The LoggerContext.
78       */
79      LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext, URI configLocation);
80  
81      /**
82       * Returns a List of all the available LoggerContexts.
83       * @return The List of LoggerContexts.
84       */
85      List<LoggerContext> getLoggerContexts();
86  
87      /**
88       * Remove any references to the LoggerContext.
89       * @param context The context to remove.
90       */
91      void removeContext(LoggerContext context);
92  }