KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > JdbcConnectionDescriptor


1 package org.apache.ojb.broker.metadata;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19
20 import javax.sql.DataSource JavaDoc;
21
22 import org.apache.ojb.broker.util.logging.Logger;
23 import org.apache.ojb.broker.util.logging.LoggerFactory;
24 import org.apache.ojb.broker.PBKey;
25 import org.apache.commons.lang.builder.ToStringBuilder;
26 import org.apache.commons.lang.builder.ToStringStyle;
27 import org.apache.commons.lang.SystemUtils;
28
29 /**
30  * JdbcConnectionDescriptor describes all relevant parameters of
31  * JDBC Connections used by the PersistenceBroker.
32  *
33  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
34  * @version $Id: JdbcConnectionDescriptor.java,v 1.31.2.4 2005/07/24 23:32:43 arminw Exp $
35  */

36 public class JdbcConnectionDescriptor extends DescriptorBase implements Serializable JavaDoc, XmlCapable
37 {
38     private static final long serialVersionUID = -600900924512028960L;
39     private Logger logger = LoggerFactory.getLogger(JdbcConnectionDescriptor.class);
40
41     public static final int AUTO_COMMIT_IGNORE_STATE = 0;
42     public static final int AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE = 1;
43     public static final int AUTO_COMMIT_SET_FALSE = 2;
44
45     private String JavaDoc m_jcdAlias;
46     private String JavaDoc m_Dbms;
47     private String JavaDoc m_Driver;
48     private String JavaDoc m_Protocol;
49     private String JavaDoc m_SubProtocol;
50     private String JavaDoc m_DbAlias;
51     private String JavaDoc m_DatasourceName;
52     private String JavaDoc m_UserName;
53     private String JavaDoc m_Password;
54     private double m_JdbcLevel = 2.0;
55     private boolean m_eagerRelease = false;
56     private boolean m_batchMode = false;
57     private boolean defaultConnection = false;
58     private int useAutoCommit = AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE;
59     private boolean ignoreAutoCommitExceptions = false;
60     private PBKey pbKey;
61     private ConnectionPoolDescriptor cpd;
62     private SequenceDescriptor sequenceDescriptor;
63     private ObjectCacheDescriptor objectCacheDescriptor;
64     private transient DataSource JavaDoc dataSource;
65
66     /**
67      * Constructor declaration
68      */

69     public JdbcConnectionDescriptor()
70     {
71         cpd = new ConnectionPoolDescriptor();
72         objectCacheDescriptor = new ObjectCacheDescriptor();
73     }
74
75     /**
76      * Returns the appropriate {@link ObjectCacheDescriptor}
77      * or <code>null</code> if not specified.
78      */

79     public ObjectCacheDescriptor getObjectCacheDescriptor()
80     {
81         return objectCacheDescriptor;
82     }
83
84     /**
85      * Sets the {@link ObjectCacheDescriptor} for representing connection/database.
86      */

87     public void setObjectCacheDescriptor(ObjectCacheDescriptor objectCacheDescriptor)
88     {
89         this.objectCacheDescriptor = objectCacheDescriptor;
90     }
91
92     /**
93      * Returns the data source that this connection descriptor represents if any.
94      *
95      * @return The data source or <code>null</code>
96      */

97     public DataSource JavaDoc getDataSource()
98     {
99         return dataSource;
100     }
101
102     /**
103      * Sets the data source that this connection descriptor represents.
104      *
105      * @param dataSource The data source
106      */

107     public void setDataSource(DataSource JavaDoc dataSource)
108     {
109         this.dataSource = dataSource;
110     }
111
112     /**
113      * Get the alias name for this descriptor.
114      */

115     public String JavaDoc getJcdAlias()
116     {
117         return m_jcdAlias;
118     }
119
120     /**
121      * Set an alias name for this descriptor.
122      */

123     public void setJcdAlias(String JavaDoc jcdAlias)
124     {
125         this.clearPBKey();
126         this.m_jcdAlias = jcdAlias;
127     }
128
129     /**
130      *
131      */

132     public boolean isDefaultConnection()
133     {
134         return defaultConnection;
135     }
136
137     public boolean isDataSource()
138     {
139         return (getDataSource() != null) || (getDatasourceName() != null);
140     }
141
142     /**
143      *
144      */

145     public void setDefaultConnection(boolean defaultConnection)
146     {
147         this.defaultConnection = defaultConnection;
148     }
149
150     /**
151      * Return the associated <code>SequenceDescriptor</code>
152      * or <code>null</code> if not set.
153      */

154     public SequenceDescriptor getSequenceDescriptor()
155     {
156         return sequenceDescriptor;
157     }
158
159     /**
160      * Set the <code>SequenceDescriptor</code> for this
161      * connection descriptor.
162      */

163     public void setSequenceDescriptor(SequenceDescriptor sequenceDescriptor)
164     {
165         this.sequenceDescriptor = sequenceDescriptor;
166     }
167
168     /**
169      * Returns the connection pool descriptor.
170      */

171     public ConnectionPoolDescriptor getConnectionPoolDescriptor()
172     {
173         return cpd;
174     }
175
176     /**
177      * Sets the connection pool descriptor.
178      */

179     public void setConnectionPoolDescriptor(ConnectionPoolDescriptor cpd)
180     {
181         this.cpd = cpd;
182     }
183
184     /**
185      * Return a key to identify the connection descriptor.
186      */

187     public PBKey getPBKey()
188     {
189         if (pbKey == null)
190         {
191             this.pbKey = new PBKey(this.getJcdAlias(), this.getUserName(), this.getPassWord());
192         }
193         return pbKey;
194     }
195
196     private void clearPBKey()
197     {
198         this.pbKey = null;
199     }
200
201     public int getUseAutoCommit()
202     {
203         return useAutoCommit;
204     }
205
206     public void setUseAutoCommit(int useAutoCommit)
207     {
208         this.useAutoCommit = useAutoCommit;
209     }
210
211     public boolean isIgnoreAutoCommitExceptions()
212     {
213         return ignoreAutoCommitExceptions;
214     }
215
216     public void setIgnoreAutoCommitExceptions(boolean ignoreAutoCommitExceptions)
217     {
218         this.ignoreAutoCommitExceptions = ignoreAutoCommitExceptions;
219     }
220
221     /**
222      * Returns the database platform name.
223      */

224     public String JavaDoc getDbms()
225     {
226         return m_Dbms;
227     }
228
229     /**
230      * Sets the database platform name.
231      */

232     public void setDbms(String JavaDoc str)
233     {
234         m_Dbms = str;
235     }
236
237     /**
238      * Returns the driver name.
239      */

240     public String JavaDoc getDriver()
241     {
242         return m_Driver;
243     }
244
245     /**
246      * Set the database driver.
247      */

248     public void setDriver(String JavaDoc str)
249     {
250         m_Driver = str;
251     }
252
253     /**
254      * Returns the database protocol.
255      */

256     public String JavaDoc getProtocol()
257     {
258         return m_Protocol;
259     }
260
261     /**
262      * Sets the database protocol.
263      */

264     public void setProtocol(String JavaDoc str)
265     {
266         m_Protocol = str;
267     }
268
269     /**
270      * Returns the database sub-protocol.
271      */

272     public String JavaDoc getSubProtocol()
273     {
274         return m_SubProtocol;
275     }
276
277     /**
278      * Sets the database sub-protocol.
279      */

280     public void setSubProtocol(String JavaDoc str)
281     {
282         m_SubProtocol = str;
283     }
284
285     /**
286      * Returns the database alias name
287      * used by OJB.
288      */

289     public String JavaDoc getDbAlias()
290     {
291         return m_DbAlias;
292     }
293
294     /**
295      * Sets the database alias name. These
296      * names you could find in the repository.dtd.
297      */

298     public void setDbAlias(String JavaDoc str)
299     {
300         m_DbAlias = str;
301     }
302
303     /**
304      * Returns the database user name.
305      */

306     public String JavaDoc getUserName()
307     {
308         return m_UserName;
309     }
310
311     /**
312      * Sets the database user name.
313      */

314     public void setUserName(String JavaDoc str)
315     {
316         this.clearPBKey();
317         m_UserName = str;
318     }
319
320     /**
321      * Returns the database password.
322      */

323     public String JavaDoc getPassWord()
324     {
325         return m_Password;
326     }
327
328     /**
329      * Sets the database password.
330      */

331     public void setPassWord(String JavaDoc str)
332     {
333         this.clearPBKey();
334         m_Password = str;
335     }
336
337     /**
338      * Gets the datasourceName.
339      * @return Returns a String
340      */

341     public String JavaDoc getDatasourceName()
342     {
343         return m_DatasourceName;
344     }
345
346     /**
347      * Sets the datasourceName.
348      * @param datasourceName The datasourceName to set
349      */

350     public void setDatasourceName(String JavaDoc datasourceName)
351     {
352         m_DatasourceName = datasourceName;
353     }
354
355     /**
356      * Gets the jdbcLevel.
357      * @return Returns a String
358      */

359     public double getJdbcLevel()
360     {
361         return m_JdbcLevel;
362     }
363
364     /**
365      * Sets the jdbcLevel. parse the string setting and check that it is indeed an integer.
366      * @param jdbcLevel The jdbcLevel to set
367      */

368     public void setJdbcLevel(String JavaDoc jdbcLevel)
369     {
370         if (jdbcLevel != null)
371         {
372             try
373             {
374                 double intLevel = Double.parseDouble(jdbcLevel);
375                 setJdbcLevel(intLevel);
376             }
377             catch(NumberFormatException JavaDoc nfe)
378             {
379                 setJdbcLevel(2.0);
380                 logger.info("Specified JDBC level was not numeric (Value=" + jdbcLevel + "), used default jdbc level of 2.0 ");
381             }
382         }
383         else
384         {
385             setJdbcLevel(2.0);
386             logger.info("Specified JDBC level was null, used default jdbc level of 2.0 ");
387         }
388     }
389
390     public void setJdbcLevel(double jdbcLevel)
391     {
392         m_JdbcLevel = jdbcLevel;
393     }
394
395     public boolean getEagerRelease()
396     {
397         return m_eagerRelease;
398     }
399
400     public void setEagerRelease(boolean flag)
401     {
402         m_eagerRelease = flag;
403     }
404
405     public boolean getBatchMode()
406     {
407         return m_batchMode;
408     }
409
410     public void setBatchMode(boolean flag)
411     {
412         m_batchMode = flag;
413     }
414
415     /**
416      * Returns a String representation of this class.
417      */

418     public String JavaDoc toString()
419     {
420         ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
421         buf.
422         append("jcd-alias", m_jcdAlias).
423         append("default-connection", defaultConnection).
424         append("dbms", m_Dbms).
425         append("jdbc-level", m_JdbcLevel).
426         append("driver", m_Driver).
427         append("protocol", m_Protocol).
428         append("sub-protocol", m_SubProtocol).
429         append("db-alias", m_DbAlias).
430         append("user", m_UserName).
431         append("password", "*****").
432         append("eager-release", m_eagerRelease).
433         append("ConnectionPoolDescriptor", cpd).
434         append("batchMode", m_batchMode).
435         append("useAutoCommit", getUseAutoCommitAsString(useAutoCommit)).
436         append("ignoreAutoCommitExceptions", ignoreAutoCommitExceptions).
437         append("sequenceDescriptor", sequenceDescriptor);
438         return buf.toString();
439     }
440
441     /*
442      * @see XmlCapable#toXML()
443      */

444     public String JavaDoc toXML()
445     {
446         RepositoryTags tags = RepositoryTags.getInstance();
447         String JavaDoc eol = SystemUtils.LINE_SEPARATOR;
448
449         StringBuffer JavaDoc strReturn = new StringBuffer JavaDoc( 1024 );
450         strReturn.append( eol );
451         strReturn.append( " <!-- Descriptor for Connection " );
452         strReturn.append( getProtocol() );
453         strReturn.append( ":" );
454         strReturn.append( getSubProtocol() );
455         strReturn.append( ":" );
456         strReturn.append( getDbAlias() );
457         strReturn.append( " -->" );
458         strReturn.append( eol );
459
460         strReturn.append( " " );
461         strReturn.append( tags.getOpeningTagNonClosingById( JDBC_CONNECTION_DESCRIPTOR ) );
462         strReturn.append( eol );
463         strReturn.append( " " );
464         strReturn.append( tags.getAttribute( JCD_ALIAS, this.getJcdAlias() ) );
465         strReturn.append( eol );
466         strReturn.append( " " );
467         strReturn.append( tags.getAttribute( DEFAULT_CONNECTION, "" + this.isDefaultConnection() ) );
468         strReturn.append( eol );
469         strReturn.append( " " );
470         strReturn.append( tags.getAttribute( DBMS_NAME, this.getDbms() ) );
471         strReturn.append( eol );
472         strReturn.append( " " );
473         strReturn.append( tags.getAttribute( JDBC_LEVEL, "" + this.getJdbcLevel() ) );
474         strReturn.append( eol );
475
476         //username is optional
477
String JavaDoc user = getUserName();
478         if( user != null )
479         {
480             strReturn.append( " " );
481             strReturn.append( tags.getAttribute( USER_NAME, user ) );
482             strReturn.append( eol );
483         }
484         // password is optional
485
String JavaDoc passwd = getPassWord();
486         if( passwd != null )
487         {
488             strReturn.append( " " );
489             strReturn.append( tags.getAttribute( USER_PASSWD, passwd ) );
490             strReturn.append( eol );
491         }
492
493         // JDBC Datasource or DriverManager information are alternatives:
494
String JavaDoc dsn = getDatasourceName();
495         if( dsn != null )
496         {
497             strReturn.append( " " );
498             strReturn.append( tags.getAttribute( DATASOURCE_NAME, this.getDatasourceName() ) );
499             strReturn.append( eol );
500         }
501         else
502         {
503             strReturn.append( " " );
504             strReturn.append( tags.getAttribute( DRIVER_NAME, this.getDriver() ) );
505             strReturn.append( eol );
506             strReturn.append( " " );
507             strReturn.append( tags.getAttribute( URL_PROTOCOL, this.getProtocol() ) );
508             strReturn.append( eol );
509             strReturn.append( " " );
510             strReturn.append( tags.getAttribute( URL_SUBPROTOCOL, this.getSubProtocol() ) );
511             strReturn.append( eol );
512             strReturn.append( " " );
513             strReturn.append( encode( tags.getAttribute( URL_DBALIAS, this.getDbAlias() ) ) );
514             strReturn.append( eol );
515         }
516         strReturn.append( " " );
517         strReturn.append( tags.getAttribute( EAGER_RELEASE, "" + this.getEagerRelease() ) );
518         strReturn.append( eol );
519         strReturn.append( " " );
520         strReturn.append( tags.getAttribute( BATCH_MODE, "" + this.getBatchMode() ) );
521         strReturn.append( eol );
522         strReturn.append( " " );
523         strReturn.append( tags.getAttribute( USE_AUTOCOMMIT, "" + this.getUseAutoCommit() ) );
524         strReturn.append( eol );
525         strReturn.append( " " );
526         strReturn.append( tags.getAttribute( IGNORE_AUTOCOMMIT_EXCEPTION, "" + this.isIgnoreAutoCommitExceptions() ) );
527         strReturn.append( eol );
528
529         strReturn.append( " >" );
530         strReturn.append( eol );
531         strReturn.append( eol );
532
533         strReturn.append( this.getConnectionPoolDescriptor().toXML() );
534         strReturn.append( eol );
535         if( this.getSequenceDescriptor() != null )
536         {
537             strReturn.append( this.getSequenceDescriptor().toXML() );
538         }
539         strReturn.append( eol );
540         strReturn.append( " " );
541         strReturn.append( tags.getClosingTagById( JDBC_CONNECTION_DESCRIPTOR ) );
542         strReturn.append( eol );
543         return strReturn.toString();
544     }
545
546     private static String JavaDoc encode(String JavaDoc toBeEncoded)
547     {
548         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
549         char c;
550         for (int i=0; i < toBeEncoded.length(); i++)
551         {
552             c = toBeEncoded.charAt(i);
553             if (c == '<')
554             {
555                 retval.append("&lt;");
556             }
557             else if (c == '>')
558             {
559                 retval.append("&gt;");
560             }
561             //else if (c == '"')
562
//{
563
// retval.append("&quot;");
564
//}
565
else if (c == '&')
566             {
567                 retval.append("&amp;");
568             }
569             else if (c == ' ')
570             {
571                 retval.append("&nbsp;");
572             }
573             else
574             {
575                 retval.append(c);
576             }
577         }
578         return retval.toString();
579     }
580
581     private static String JavaDoc getUseAutoCommitAsString(int state)
582     {
583         switch (state)
584         {
585             case AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE:
586                 return "AUTO_COMMIT_SET_TRUE_AND_TEMPORARY_FALSE";
587             case AUTO_COMMIT_SET_FALSE:
588                 return "AUTO_COMMIT_SET_FALSE";
589             case AUTO_COMMIT_IGNORE_STATE:
590                 return "AUTO_COMMIT_IGNORE_STATE";
591             default: return "UNKOWN_STATE";
592         }
593     }
594 }
595
Popular Tags