1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, 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 v1.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 package ch.qos.logback.classic.joran.action;
15
16 import javax.naming.Context;
17 import javax.naming.NamingException;
18
19 import org.xml.sax.Attributes;
20
21 import ch.qos.logback.classic.util.JNDIUtil;
22 import ch.qos.logback.core.joran.action.Action;
23 import ch.qos.logback.core.joran.action.ActionUtil;
24 import ch.qos.logback.core.joran.action.ActionUtil.Scope;
25 import ch.qos.logback.core.joran.spi.InterpretationContext;
26 import ch.qos.logback.core.util.OptionHelper;
27
28 /**
29  * Insert an env-entry found in JNDI as a new context variable  
30
31  * @author Ceki Gulcu
32  *
33  */

34 public class InsertFromJNDIAction extends Action {
35
36     public static final String ENV_ENTRY_NAME_ATTR = "env-entry-name";
37     public static final String AS_ATTR = "as";
38
39     public void begin(InterpretationContext ec, String name, Attributes attributes) {
40
41         int errorCount = 0;
42         String envEntryName = ec.subst(attributes.getValue(ENV_ENTRY_NAME_ATTR));
43         String asKey = ec.subst(attributes.getValue(AS_ATTR));
44
45         String scopeStr = attributes.getValue(SCOPE_ATTRIBUTE);
46         Scope scope = ActionUtil.stringToScope(scopeStr);
47
48         String envEntryValue;
49
50         if (OptionHelper.isEmpty(envEntryName)) {
51             String lineColStr = getLineColStr(ec);
52             addError("[" + ENV_ENTRY_NAME_ATTR + "] missing, around " + lineColStr);
53             errorCount++;
54         }
55
56         if (OptionHelper.isEmpty(asKey)) {
57             String lineColStr = getLineColStr(ec);
58             addError("[" + AS_ATTR + "] missing, around " + lineColStr);
59             errorCount++;
60         }
61
62         if (errorCount != 0) {
63             return;
64         }
65
66         try {
67             Context ctx = JNDIUtil.getInitialContext();
68             envEntryValue = JNDIUtil.lookup(ctx, envEntryName);
69             if (OptionHelper.isEmpty(envEntryValue)) {
70                 addError("[" + envEntryName + "] has null or empty value");
71             } else {
72                 addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
73                 ActionUtil.setProperty(ec, asKey, envEntryValue, scope);
74             }
75         } catch (NamingException e) {
76             addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");
77         }
78
79     }
80
81     public void end(InterpretationContext ec, String name) {
82     }
83 }
84