You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Harry Levinson <ak...@gmail.com> on 2008/10/24 03:20:09 UTC

read context.xml Resource attributes

What is the proper way to read attributes of a Resource in context.xml, for
example the "url" attribute?

I am building a test web page that shows whether or not we are successfully
connected to the database. Only techies inide the company will be able to
view that web page, so security is not an issue. (I will not display
username/password anyway.)

I want to retrieve and display the url attribute for a specified Resource in
context.xml.

I know I can use XML functions to read the file, but is there a way to read
it using envContext() or something like that, after I create a Connection or
Context object?

Here are two different scenarios that begin the section of code I'm looking
for:

    Connection conn = connections.get(dataSource);
    ...

    Context initContext = new InitialContext();
    Context envContext = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/MYDB");
    ...

I googled but can't find a solution.

thanks,
Harry

Re: read context.xml Resource attributes

Posted by Juha Laiho <Ju...@iki.fi>.
Harry Levinson wrote:
> What is the proper way to read attributes of a Resource in context.xml, for
> example the "url" attribute?
> 
> I am building a test web page that shows whether or not we are successfully
> connected to the database. Only techies inide the company will be able to
> view that web page, so security is not an issue. (I will not display
> username/password anyway.)

I think the JMX MBeans would be the correct way for this -- however I've not
worked with them, so cannot provide any direct information, only this vague
pointer.

Another way could be to use the capabilities provided by the underlying
DataSource implementation. F.ex. if the used DataSource implementation
is OracleDataSource, you could recast the ds object you get from JNDI
back to OracleDataSource, and call getUrl() on that. But this completely
depends on the database and DataSource you're using, while the JMX approach
would be usable for any JNDI resource.
-- 
..Juha

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: read context.xml Resource attributes

Posted by Harry Levinson <ak...@gmail.com>.
Found a solution that uses the Connection object:

String dataSource = "jdbc/whatever";
DatabaseMetaData dbMetaData;
Connection conn = connections.get(dataSource);

dbMetaData = conn.getMetaData();
String url = dbMetaData.getURL();

Thanks for your help Juha.