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
Jose, By default RTC uses direct JDBC, it can use Java EE DS from the App Server if configured to do so, you can set it in the Database connection on the admin page. Check the topic, Configuring Database Connection in the IC: Sandy Comments
Jose Miguel Ordax Cassa
commented Jul 11 '13, 10:25 a.m.
Hi Sandy, I understand that documentation is to setup RTC itself.
Jose Miguel Ordax Cassa
commented Jul 11 '13, 11:50 a.m.
I try to create a JNDI InitialContext and it is always null:
Context initialContext = new InitialContext(); I always get a javax.naming.NoInitialContextException so it seems to me I haven't access to Java EE environment from a RTC Extension. Should I add anything to dependencies? |
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.