1 21 package oracle.toplink.essentials.jndi; 23 24 import java.util.*; 25 import java.sql.*; 26 import javax.naming.*; 27 import javax.sql.*; 28 import oracle.toplink.essentials.sessions.*; 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.internal.helper.*; 31 import oracle.toplink.essentials.internal.localization.*; 32 33 43 public class JNDIConnector implements Connector { 44 protected DataSource dataSource; 45 protected Context context; 46 protected String name; 47 public static final int STRING_LOOKUP = 1; 48 public static final int COMPOSITE_NAME_LOOKUP = 2; 49 public static final int COMPOUND_NAME_LOOKUP = 3; 50 protected int lookupType = COMPOSITE_NAME_LOOKUP; 52 53 58 public JNDIConnector() { 59 super(); 60 } 61 62 66 public JNDIConnector(Context context, String name) throws ValidationException { 67 this(name); 68 this.context = context; 69 } 70 71 75 public JNDIConnector(String name) { 76 this.name = name; 77 } 78 79 83 public JNDIConnector(DataSource dataSource) { 84 this.dataSource = dataSource; 85 } 86 87 91 public Object clone() { 92 try { 93 return super.clone(); 94 } catch (Exception exception) { 95 throw new InternalError ("Clone failed"); 96 } 97 } 98 99 103 public Connection connect(Properties properties) throws DatabaseException, ValidationException { 104 String user = properties.getProperty("user"); 105 String password = properties.getProperty("password"); 106 107 DataSource dataSource = getDataSource(); 108 if (dataSource == null) { 109 try { 110 if (lookupType == STRING_LOOKUP) { 112 dataSource = (DataSource)getContext().lookup(getName()); 113 } else if (lookupType == COMPOSITE_NAME_LOOKUP) { 114 dataSource = (DataSource)getContext().lookup(new CompositeName(name)); 115 } else { 116 dataSource = (DataSource)getContext().lookup(new CompoundName(name, new Properties())); 117 } 118 this.setDataSource(dataSource); 119 } catch (NamingException exception) { 120 throw ValidationException.cannotAcquireDataSource(getName(), exception); 121 } 122 } 123 124 try { 125 if ((user == null) || (user.equalsIgnoreCase(""))) { 130 return dataSource.getConnection(); 131 } else { 132 return dataSource.getConnection(user, password); 133 } 134 } catch (SQLException exception) { 135 throw DatabaseException.sqlException(exception); 136 } 137 } 138 139 143 public Context getContext() { 144 if (context == null) { 145 try { 146 context = new InitialContext(); 147 } catch (NamingException exception) { 148 } 149 } 150 return context; 151 } 152 153 157 public DataSource getDataSource() { 158 return dataSource; 159 } 160 161 166 public String getName() { 167 return name; 168 } 169 170 175 public String getConnectionDetails() { 176 return getName(); 177 } 178 179 183 public void setContext(Context context) { 184 this.context = context; 185 } 186 187 191 public void setDataSource(DataSource dataSource) { 192 this.dataSource = dataSource; 193 } 194 195 200 public void setName(String name) throws ValidationException { 201 this.name = name; 202 } 203 204 public void setLookupType(int lookupType) { 205 this.lookupType = lookupType; 206 } 207 208 public int getLookupType() { 209 return lookupType; 210 } 211 212 216 public String toString() { 217 return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("datasource_name", (Object [])null) + "=>" + getName(); 218 } 219 220 224 public void toString(java.io.PrintWriter writer) { 225 writer.print(ToStringLocalization.buildMessage("connector", (Object [])null) + "=>" + Helper.getShortClassName(getClass())); 226 writer.print(" "); 227 writer.println(ToStringLocalization.buildMessage("datasource_name", (Object [])null) + "=>" + getName()); 228 } 229 } 230 | Popular Tags |