Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

JavaURLContextFactory ERROR when execute REST service

Ramesh Udari1 April 15, 2014

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);
		}
	}


1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

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);
}

Ramesh Udari1 April 15, 2014

Hi Boris,

Thanks for your response, where should we add above code ?

Boris Georgiev _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

In the setupDataSource method before doing the lookup. Probably you should have separate method for loading the datasource from REST

TAGS
AUG Leaders

Atlassian Community Events