1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.model.processor;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.joran.action.ActionUtil;
19  import ch.qos.logback.core.joran.action.ActionUtil.Scope;
20  import ch.qos.logback.core.model.InsertFromJNDIModel;
21  import ch.qos.logback.core.model.Model;
22  import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
23  import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
24  import ch.qos.logback.core.util.JNDIUtil;
25  import ch.qos.logback.core.util.OptionHelper;
26  
27  public class InsertFromJNDIModelHandler extends ModelHandlerBase {
28  
29      public InsertFromJNDIModelHandler(Context context) {
30          super(context);
31      }
32  
33      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
34          return new InsertFromJNDIModelHandler(context);
35      }
36  
37      @Override
38      protected Class<InsertFromJNDIModel> getSupportedModelClass() {
39          return InsertFromJNDIModel.class;
40      }
41  
42      @Override
43      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
44          InsertFromJNDIModel ifjm = (InsertFromJNDIModel) model;
45          detachedHandle(mic, ifjm);
46      }
47  
48      /**
49       *
50       * @param capc
51       * @param ifjm
52       * @since 1.5.11
53       */
54      public void detachedHandle(ContextAwarePropertyContainer capc, InsertFromJNDIModel ifjm) {
55          int errorCount = 0;
56          String envEntryName = capc.subst(ifjm.getEnvEntryName());
57          String asKey = capc.subst(ifjm.getAs());
58  
59          String scopeStr = capc.subst(ifjm.getScopeStr());
60          Scope scope = ActionUtil.stringToScope(scopeStr);
61  
62          String envEntryValue;
63  
64          if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryName)) {
65              addError("[" + InsertFromJNDIModel.ENV_ENTRY_NAME_ATTR + "] missing");
66              errorCount++;
67          }
68  
69          if (OptionHelper.isNullOrEmptyOrAllSpaces(asKey)) {
70              addError("[" + InsertFromJNDIModel.AS_ATTR + "] missing");
71              errorCount++;
72          }
73  
74          if (errorCount != 0) {
75              return;
76          }
77  
78          try {
79              javax.naming.Context ctx = JNDIUtil.getInitialContext();
80              envEntryValue = JNDIUtil.lookupString(ctx, envEntryName);
81              if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryValue)) {
82                  addError("[" + envEntryName + "] has null or empty value");
83              } else {
84                  addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
85                  PropertyModelHandlerHelper.setProperty(capc, asKey, envEntryValue, scope);
86              }
87          } catch (Exception e) {
88              addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");
89          }
90  
91      }
92  
93  }