1   package ch.qos.logback.core.model.processor;
2   
3   import javax.naming.NamingException;
4   
5   import ch.qos.logback.core.Context;
6   import ch.qos.logback.core.joran.action.ActionUtil;
7   import ch.qos.logback.core.joran.action.ActionUtil.Scope;
8   import ch.qos.logback.core.model.InsertFromJNDIModel;
9   import ch.qos.logback.core.model.Model;
10  import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
11  import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
12  import ch.qos.logback.core.util.JNDIUtil;
13  import ch.qos.logback.core.util.OptionHelper;
14  
15  public class InsertFromJNDIModelHandler extends ModelHandlerBase {
16  
17      public InsertFromJNDIModelHandler(Context context) {
18          super(context);
19      }
20  
21      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
22          return new InsertFromJNDIModelHandler(context);
23      }
24  
25      @Override
26      protected Class<InsertFromJNDIModel> getSupportedModelClass() {
27          return InsertFromJNDIModel.class;
28      }
29  
30      @Override
31      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
32          InsertFromJNDIModel ifjm = (InsertFromJNDIModel) model;
33          detachedHandle(mic, ifjm);
34      }
35  
36      /**
37       *
38       * @param capc
39       * @param ifjm
40       * @since 1.5.11
41       */
42      public void detachedHandle(ContextAwarePropertyContainer capc, InsertFromJNDIModel ifjm) {
43          int errorCount = 0;
44          String envEntryName = capc.subst(ifjm.getEnvEntryName());
45          String asKey = capc.subst(ifjm.getAs());
46  
47          String scopeStr = capc.subst(ifjm.getScopeStr());
48          Scope scope = ActionUtil.stringToScope(scopeStr);
49  
50          String envEntryValue;
51  
52          if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryName)) {
53              addError("[" + InsertFromJNDIModel.ENV_ENTRY_NAME_ATTR + "] missing");
54              errorCount++;
55          }
56  
57          if (OptionHelper.isNullOrEmptyOrAllSpaces(asKey)) {
58              addError("[" + InsertFromJNDIModel.AS_ATTR + "] missing");
59              errorCount++;
60          }
61  
62          if (errorCount != 0) {
63              return;
64          }
65  
66          try {
67              javax.naming.Context ctx = JNDIUtil.getInitialContext();
68              envEntryValue = JNDIUtil.lookupString(ctx, envEntryName);
69              if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryValue)) {
70                  addError("[" + envEntryName + "] has null or empty value");
71              } else {
72                  addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
73                  PropertyModelHandlerHelper.setProperty(capc, asKey, envEntryValue, scope);
74              }
75          } catch (NamingException e) {
76              addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");
77          }
78  
79      }
80  
81  }