001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.joran.action.ActionUtil;
019import ch.qos.logback.core.joran.action.ActionUtil.Scope;
020import ch.qos.logback.core.model.InsertFromJNDIModel;
021import ch.qos.logback.core.model.Model;
022import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
023import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
024import ch.qos.logback.core.util.JNDIUtil;
025import ch.qos.logback.core.util.OptionHelper;
026
027public class InsertFromJNDIModelHandler extends ModelHandlerBase {
028
029    public InsertFromJNDIModelHandler(Context context) {
030        super(context);
031    }
032
033    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
034        return new InsertFromJNDIModelHandler(context);
035    }
036
037    @Override
038    protected Class<InsertFromJNDIModel> getSupportedModelClass() {
039        return InsertFromJNDIModel.class;
040    }
041
042    @Override
043    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
044        InsertFromJNDIModel ifjm = (InsertFromJNDIModel) model;
045        detachedHandle(mic, ifjm);
046    }
047
048    /**
049     *
050     * @param capc
051     * @param ifjm
052     * @since 1.5.11
053     */
054    public void detachedHandle(ContextAwarePropertyContainer capc, InsertFromJNDIModel ifjm) {
055        int errorCount = 0;
056        String envEntryName = capc.subst(ifjm.getEnvEntryName());
057        String asKey = capc.subst(ifjm.getAs());
058
059        String scopeStr = capc.subst(ifjm.getScopeStr());
060        Scope scope = ActionUtil.stringToScope(scopeStr);
061
062        String envEntryValue;
063
064        if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryName)) {
065            addError("[" + InsertFromJNDIModel.ENV_ENTRY_NAME_ATTR + "] missing");
066            errorCount++;
067        }
068
069        if (OptionHelper.isNullOrEmptyOrAllSpaces(asKey)) {
070            addError("[" + InsertFromJNDIModel.AS_ATTR + "] missing");
071            errorCount++;
072        }
073
074        if (errorCount != 0) {
075            return;
076        }
077
078        try {
079            javax.naming.Context ctx = JNDIUtil.getInitialContext();
080            envEntryValue = JNDIUtil.lookupString(ctx, envEntryName);
081            if (OptionHelper.isNullOrEmptyOrAllSpaces(envEntryValue)) {
082                addError("[" + envEntryName + "] has null or empty value");
083            } else {
084                addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope");
085                PropertyModelHandlerHelper.setProperty(capc, asKey, envEntryValue, scope);
086            }
087        } catch (Exception e) {
088            addError("Failed to lookup JNDI env-entry [" + envEntryName + "]");
089        }
090
091    }
092
093}