001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.nanocontainer.persistence.jdbc;
011
012 import javax.naming.Context;
013 import javax.naming.InitialContext;
014 import javax.sql.DataSource;
015
016 import org.nanocontainer.persistence.ExceptionHandler;
017 import org.picocontainer.Startable;
018
019 /**
020 * @author Juze Peleteiro <juze -a-t- intelli -dot- biz>
021 */
022 public class JNDIDataSource extends AbstractDataSource implements Startable {
023
024 private final String name;
025
026 private final Context context;
027
028 private DataSource dataSource;
029
030 /**
031 * @param name JNDI name where the original DataSource is.
032 * @param jdbcExceptionHandler The ExceptionHandler component instance.
033 */
034 public JNDIDataSource(final String name) {
035 this.name = name;
036 this.context = null;
037 }
038
039 /**
040 * @param name JNDI name where the original DataSource is.
041 * @param jdbcExceptionHandler The ExceptionHandler component instance.
042 */
043 public JNDIDataSource(final String name, final ExceptionHandler jdbcExceptionHandler) {
044 super(jdbcExceptionHandler);
045 this.name = name;
046 this.context = null;
047 }
048
049 /**
050 * @param name JNDI name where the original DataSource is.
051 * @param context JNDI context.
052 */
053 public JNDIDataSource(final String name, final Context context) {
054 this.name = name;
055 this.context = context;
056 }
057
058 /**
059 * @param name JNDI name where the original DataSource is.
060 * @param context JNDI context.
061 * @param jdbcExceptionHandler The ExceptionHandler component instance.
062 */
063 public JNDIDataSource(final String name, final Context context, final ExceptionHandler jdbcExceptionHandler) {
064 super(jdbcExceptionHandler);
065 this.name = name;
066 this.context = context;
067 }
068
069 /**
070 * @see org.nanocontainer.persistence.jdbc.AbstractDataSource#getDelegatedDataSource()
071 */
072 protected DataSource getDelegatedDataSource() throws Exception {
073 if (dataSource == null) {
074 Context jndiContext;
075 if (context == null) {
076 jndiContext = new InitialContext();
077 } else {
078 jndiContext = context;
079 }
080
081 dataSource = (DataSource) jndiContext.lookup(name);
082 }
083
084 return dataSource;
085 }
086
087 /**
088 * @see org.nanocontainer.persistence.jdbc.AbstractDataSource#invalidateDelegatedDataSource()
089 */
090 protected void invalidateDelegatedDataSource() throws Exception {
091 dataSource = null;
092 }
093
094 /**
095 * @see org.picocontainer.Startable#start()
096 */
097 public void start() {
098 // Do nothing
099 }
100
101 /**
102 * @see org.picocontainer.Startable#stop()
103 */
104 public void stop() {
105 dataSource = null;
106 }
107
108 }