On 29/06/2016 05:12, Terence M. Bandoian wrote: > On 6/28/2016 3:56 AM, Mark Thomas wrote: >> On 27/06/2016 22:35, Jerry Malcolm wrote: >>> Mark, >>> >>> On 6/27/2016 1:07 PM, Mark Thomas wrote: >>>> On 27/06/2016 17:44, Jerry Malcolm wrote: >>>> >>>>> I'm assuming that context.lookup(...) simply locates the "jdbc/myDB" >>>>> <Resource> tag in the context.xml file, pulls all of the parms out of >>>>> that tag, creates a DataSource object utilizing the parms, and returns >>>>> it. If that's the case, couldn't I create a variation/subclass >>>>> of the >>>>> Context object that modifies the url parm that it found in the >>>>> resource >>>>> tag and puts the desired db name into the url before constructing the >>>>> DataSource? >>>> Sure. >>>> >>>> You need to implement the appropriate factory and then specify your >>>> factory class explicitly in the Resource element using the factory >>>> attribute. >>>> >>>> You probably want to start here for ideas on how to code up your >>>> factory: >>>> http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/ >>>> >>>> >>>> or for a more specific example: >>>> http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/BasicDataSourceFactory.java?view=annotate >>>> >>>> >>>> >>>> >>>> Mark >>>> >>> Thanks so much. This looks doable. Just to make sure I have the big >>> picture correct.... >>> >>> 1) I will define a new custom resource that returns a custom context >>> object that I write. >>> 2) On the <Resource> tag for my custom context resource I can put all of >>> the standard DataSource parms >>> 3) I then call lookup() on this custom context passing the dbName in >>> 4) This custom context will build an appropriate Reference object with >>> all the parms, instantiate a BasicDataSourceFactory, and call >>> getInstance(.....) on the factory. >>> >>> When I need an instance of the datasource: >>> Context initContext = new InitialContext(); >>> Context envContext = >>> (Context)initContext.lookup("java:/comp/env"); >>> MyDataSourceContext dsContext = (MyDataSourceContext) >>> envContext.lookup( "dsContext/myDSContext" ); >>> DataSource myDS = (DataSource) dsContext.getInstance( >>> "dbName" ); >>> >>> Am I getting close? >> Yes, but I don't think you want to use custom NamingContexts. I think >> there is a simpler way. >> >> 1. Write a custom DataSource factory that provides a getDataSource(...) >> method that allows you to pass in whatever per instance config you need. >> This DataSource factory will need to use bean style setters (like the >> example I linked to) to pick up the other config from the <Resource .../> >> >> 2. Write a custom resource factory that returns an instance of your >> DataSource factory. >> >> 3. Add a context Resource element for your DataSource factory, >> remembering to use the factory element and specify the custom resource >> factory from 2. >> >> HTH, >> >> Mark > > > If external configuration were not required, would there be a problem > instantiating a DataSource directly and setting any required properties > programmatically? Would any functionality be lost?
The option of a global resource shared between multiple web apps is lost. You'd need a mechanism (a statics would work) to store and retrieve the DataSource. > Also, if there were base properties set in a <Resource> element, would > there be a problem using a copy of the properties from that data source, > modified as necessary, to instantiate a new DataSource? No, but you'd need to be careful since multiple requests for the same DataSource should return the same object. You'd need to be able to differentiate between a request for a new DataSourece and a previously created one. Mark --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org