Hi,
We have a coomman code which actually connects external database by using datasoure concept. When we call this code from JIRA customized Services (configured in jira adminstrotor), this code executes without any issues and connect to database, but where as when we call same code from REST service which gives ------Root exception is java.lang.ClassNotFoundException: org.apache.naming.java.javaURLContextFactory. error and not able to connect database.
Both services (Rest and jira service) are implemented as plugin type 2 plugins.
Below is the JNDI look up code:
public static void setupDataSource(String dataSourceName) { Context envCtx; try { envCtx = (Context) new InitialContext().lookup("java:comp/env"); // Look up our data source if(dataSourceName != null && dataSourceName.equalsIgnoreCase(getProperty("database"))) { dsForDW = (DataSource) envCtx.lookup("jdbc/"+dataSourceName); } } catch (NamingException e) { logger.error("Problem Occured in getting Datasource",e); } }
Community moderators have prevented the ability to post new answers.
See this dicussion
https://answers.atlassian.com/questions/6374/how-do-i-access-jndi-from-a-version-2-osgi-plugin
As @Radu Dumitriuexplained
There are some things that you should be aware. From REST annotated classes, it will give you error (Classloader on the generated proxy is wrong)
To get do the lookup use
ClassLoader origCL = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(ComponentManager.class.getClassLoader());
//::TODO:: do lookup here
} finally {
Thread.currentThread().setContextClassLoader(origCL);
}
Hi Boris,
Thanks for your response, where should we add above code ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the setupDataSource method before doing the lookup. Probably you should have separate method for loading the datasource from REST
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.