Accessing to Java EE DataSource from an extension
![]()
Jose Miguel Ordax Cassa (2.4k●4●126●100)
| asked Jul 11 '13, 6:34 a.m.
edited Feb 23 '14, 8:55 a.m. by Sreerupa Sen (1.0k●4)
Is it possible? How?
RTC seems to use Java EE DataSources defined in the Application Server but I haven't found any way to do it. Thanks in advance, Chemi. |
3 answers
![]()
I think I found a way to do it, although I am sure it is not supported:
1.- Add following plug-in as a dependency in your manifest file: com.ibm.team.repository.jndi 2.- Make sure you have an Activator in your plug-in 3.- Use following code: try { BundleContext bundleContext = Activator.getContext(); IJNDIService testService = (IJNDIService)bundleContext.getService(bundleContext.getServiceReference(IJNDIService.class.getName())); Context initialContext = testService.getInitialContext(); DataSource datasource = (DataSource)initialContext.lookup("java:comp/env/jdbc/YourDB"); Connection con = datasource.getConnection(); .... .... .... I have tested successfully with RTC 4.0.4 on Tomcat Hope it helps, Chemi. |
![]()
Hi, I found this very useful, just a few comments, in case of Team Concert, the examples recommend do not creat the Activator class cause these plugins are not pure osgi, in that case we can get the bundle like this
try {
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference jndiServiceReference = bundleContext.getServiceReference(IJNDIService.class.getName());
IJNDIService jndiService = (IJNDIService) bundleContext.getService(jndiServiceReference);
Context initialContext = jndiService.getInitialContext();
DataSource datasource = (DataSource) initialContext.lookup("java:comp/env/jdbc/YourDB");
Connection con = datasource.getConnection();
...
or if you set the prerequisites in your Team Concert service then you just need to do that inside the component
try {
IJNDIService jndiService = (IJNDIService) getService(IJNDIService.class);
Context initialContext = jndiService.getInitialContext();
DataSource datasource = (DataSource) initialContext.lookup("java:comp/env/jdbc/YourDB");
Connection con = datasource.getConnection();
...
also in the plugin.xml
<prerequisites>
<requiredService interface="com.ibm.team.repository.jndi.IJNDIService"/>
...
more prerequisites
...
</prerequisites>
Hope this help
Amaury
|
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.