View Javadoc
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.core.db;
15  
16  import javax.naming.Context;
17  import javax.naming.InitialContext;
18  import javax.sql.DataSource;
19  
20  import org.xml.sax.Attributes;
21  
22  import ch.qos.logback.core.joran.action.Action;
23  import ch.qos.logback.core.joran.spi.InterpretationContext;
24  import ch.qos.logback.core.joran.util.PropertySetter;
25  import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
26  import ch.qos.logback.core.util.OptionHelper;
27  
28  /**
29   *
30   * @author Ceki Gulcu
31   *
32   */
33  public class BindDataSourceToJNDIAction extends Action {
34  
35      static final String DATA_SOURCE_CLASS = "dataSourceClass";
36      static final String URL = "url";
37      static final String USER = "user";
38      static final String PASSWORD = "password";
39  	private final BeanDescriptionCache beanDescriptionCache;
40  
41      public BindDataSourceToJNDIAction(BeanDescriptionCache beanDescriptionCache) {
42  		this.beanDescriptionCache = beanDescriptionCache;
43  	}
44  
45  	/**
46       * Instantiates an a data source and bind it to JNDI
47       * Most of the required parameters are placed in the ec.substitutionProperties
48       */
49      public void begin(InterpretationContext ec, String localName, Attributes attributes) {
50          String dsClassName = ec.getProperty(DATA_SOURCE_CLASS);
51  
52          if (OptionHelper.isEmpty(dsClassName)) {
53              addWarn("dsClassName is a required parameter");
54              ec.addError("dsClassName is a required parameter");
55  
56              return;
57          }
58  
59          String urlStr = ec.getProperty(URL);
60          String userStr = ec.getProperty(USER);
61          String passwordStr = ec.getProperty(PASSWORD);
62  
63          try {
64              DataSource ds = (DataSource) OptionHelper.instantiateByClassName(dsClassName, DataSource.class, context);
65  
66              PropertySetter setter = new PropertySetter(beanDescriptionCache,ds);
67              setter.setContext(context);
68  
69              if (!OptionHelper.isEmpty(urlStr)) {
70                  setter.setProperty("url", urlStr);
71              }
72  
73              if (!OptionHelper.isEmpty(userStr)) {
74                  setter.setProperty("user", userStr);
75              }
76  
77              if (!OptionHelper.isEmpty(passwordStr)) {
78                  setter.setProperty("password", passwordStr);
79              }
80  
81              Context ctx = new InitialContext();
82              ctx.rebind("dataSource", ds);
83          } catch (Exception oops) {
84              addError("Could not bind  datasource. Reported error follows.", oops);
85              ec.addError("Could not not bind  datasource of type [" + dsClassName + "].");
86          }
87      }
88  
89      public void end(InterpretationContext ec, String name) {
90      }
91  }