001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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 v1.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 */
014package ch.qos.logback.core.db;
015
016import javax.naming.Context;
017import javax.naming.InitialContext;
018import javax.sql.DataSource;
019
020import org.xml.sax.Attributes;
021
022import ch.qos.logback.core.joran.action.Action;
023import ch.qos.logback.core.joran.spi.InterpretationContext;
024import ch.qos.logback.core.joran.util.PropertySetter;
025import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
026import ch.qos.logback.core.util.OptionHelper;
027
028/**
029 *
030 * @author Ceki Gulcu
031 *
032 */
033public class BindDataSourceToJNDIAction extends Action {
034
035    static final String DATA_SOURCE_CLASS = "dataSourceClass";
036    static final String URL = "url";
037    static final String USER = "user";
038    static final String PASSWORD = "password";
039        private final BeanDescriptionCache beanDescriptionCache;
040
041    public BindDataSourceToJNDIAction(BeanDescriptionCache beanDescriptionCache) {
042                this.beanDescriptionCache = beanDescriptionCache;
043        }
044
045        /**
046     * Instantiates an a data source and bind it to JNDI
047     * Most of the required parameters are placed in the ec.substitutionProperties
048     */
049    public void begin(InterpretationContext ec, String localName, Attributes attributes) {
050        String dsClassName = ec.getProperty(DATA_SOURCE_CLASS);
051
052        if (OptionHelper.isEmpty(dsClassName)) {
053            addWarn("dsClassName is a required parameter");
054            ec.addError("dsClassName is a required parameter");
055
056            return;
057        }
058
059        String urlStr = ec.getProperty(URL);
060        String userStr = ec.getProperty(USER);
061        String passwordStr = ec.getProperty(PASSWORD);
062
063        try {
064            DataSource ds = (DataSource) OptionHelper.instantiateByClassName(dsClassName, DataSource.class, context);
065
066            PropertySetter setter = new PropertySetter(beanDescriptionCache,ds);
067            setter.setContext(context);
068
069            if (!OptionHelper.isEmpty(urlStr)) {
070                setter.setProperty("url", urlStr);
071            }
072
073            if (!OptionHelper.isEmpty(userStr)) {
074                setter.setProperty("user", userStr);
075            }
076
077            if (!OptionHelper.isEmpty(passwordStr)) {
078                setter.setProperty("password", passwordStr);
079            }
080
081            Context ctx = new InitialContext();
082            ctx.rebind("dataSource", ds);
083        } catch (Exception oops) {
084            addError("Could not bind  datasource. Reported error follows.", oops);
085            ec.addError("Could not not bind  datasource of type [" + dsClassName + "].");
086        }
087    }
088
089    public void end(InterpretationContext ec, String name) {
090    }
091}