FileDocCategorySizeDatePackage
JNDIConnector.javaAPI DocGlassfish v2 API8373Tue May 22 16:54:44 BST 2007oracle.toplink.essentials.jndi

JNDIConnector

public class JNDIConnector extends Object implements Connector
Specifies the J2EE DataSource lookup options. This connector is normally used with a login in a J2EE environment to connect to a server's connection pool defined by the DataSource name. The JNDI name that the DataSource is registered under must be specified, this must include any required prefix such as "java:comp/env/", (unless a DataSource object is given). A Context is only required if not running on the server, otheriwse default to a new InitialContext().
author
Big Country
since
TOPLink/Java 2.1

Fields Summary
protected DataSource
dataSource
protected Context
context
protected String
name
public static final int
STRING_LOOKUP
public static final int
COMPOSITE_NAME_LOOKUP
public static final int
COMPOUND_NAME_LOOKUP
protected int
lookupType
Constructors Summary
public JNDIConnector()
PUBLIC: Construct a Connector with no settings. The datasource name will still need to be set.


                         
      
        super();
    
public JNDIConnector(Context context, String name)
PUBLIC: Construct a Connector with the datasource name.

        this(name);
        this.context = context;
    
public JNDIConnector(String name)
PUBLIC: Construct a Connector with the datasource name.

        this.name = name;
    
public JNDIConnector(DataSource dataSource)
PUBLIC: Construct a Connector with the datasource object.

        this.dataSource = dataSource;
    
Methods Summary
public java.lang.Objectclone()
INTERNAL: Clone the connector.

        try {
            return super.clone();
        } catch (Exception exception) {
            throw new InternalError("Clone failed");
        }
    
public java.sql.Connectionconnect(java.util.Properties properties)
INTERNAL: Connect with the specified properties and return the Connection.

        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        DataSource dataSource = getDataSource();
        if (dataSource == null) {
            try {
                //bug#2761428 and 4405389 JBoss needs to look up datasources based on a string not a composite or compound name
                if (lookupType == STRING_LOOKUP) {
                    dataSource = (DataSource)getContext().lookup(getName());
                } else if (lookupType == COMPOSITE_NAME_LOOKUP) {
                    dataSource = (DataSource)getContext().lookup(new CompositeName(name));
                } else {
                    dataSource = (DataSource)getContext().lookup(new CompoundName(name, new Properties()));
                }
                this.setDataSource(dataSource);
            } catch (NamingException exception) {
                throw ValidationException.cannotAcquireDataSource(getName(), exception);
            }
        }

        try {
            // WebLogic connection pools do not require a user name and password.
            // JDBCLogin usually initializes these values with an empty string.
            // WebLogic data source does not support the getConnection() call with arguments
            // it only supports the zero argument call. DM 26/07/2000
            if ((user == null) || (user.equalsIgnoreCase(""))) {
                return dataSource.getConnection();
            } else {
                return dataSource.getConnection(user, password);
            }
        } catch (SQLException exception) {
            throw DatabaseException.sqlException(exception);
        }
    
public java.lang.StringgetConnectionDetails()
PUBLIC: Provide the details of my connection information. This is primarily for JMX runtime services.

return
java.lang.String

        return getName();
    
public javax.naming.ContextgetContext()
PUBLIC: Return the JNDI Context that can supplied the named DataSource.

        if (context == null) {
            try {
                context = new InitialContext();
            } catch (NamingException exception) {
            }
        }
        return context;
    
public javax.sql.DataSourcegetDataSource()
PUBLIC: Return the javax.sql.DataSource.

        return dataSource;
    
public intgetLookupType()

        return lookupType;
    
public java.lang.StringgetName()
PUBLIC: Return the name of the DataSource within the JNDI Context.

        return name;
    
public voidsetContext(javax.naming.Context context)
PUBLIC: Set the JNDI Context that can supply the named DataSource.

        this.context = context;
    
public voidsetDataSource(javax.sql.DataSource dataSource)
PUBLIC: Set the javax.sql.DataSource.

        this.dataSource = dataSource;
    
public voidsetLookupType(int lookupType)

        this.lookupType = lookupType;
    
public voidsetName(java.lang.String name)
PUBLIC: Set the name of the DataSource within the JNDI Context.

        this.name = name;
    
public java.lang.StringtoString()
PUBLIC: Print data source info.

        return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("datasource_name", (Object[])null) + "=>" + getName();
    
public voidtoString(java.io.PrintWriter writer)
INTERNAL: Print something useful on the log.

        writer.print(ToStringLocalization.buildMessage("connector", (Object[])null) + "=>" + Helper.getShortClassName(getClass()));
        writer.print(" ");
        writer.println(ToStringLocalization.buildMessage("datasource_name", (Object[])null) + "=>" + getName());