On Wed, Jan 2, 2013 at 2:57 PM, Harsha Thirimanna <[email protected]> wrote:
> Hi,
>
> I checked this out. After patching to the kernal , admin and tenents can
> access their own Data Sources through the web application.
> Another point that I found is now without having the
> 'java.naming.factory.initial' property ,it worked.
>
It is better to keep the environment property. This is because when trying
to create the intialContext (getInitialContext), it uses this property.
Here, it worked because the initial context was already created using the
same (default) property value from carbon.xml. But there will be situation
that when trying create the intialContext for the first time using a
different ContextFactory class, then this property will be needed.
Thanks,
Kishanthan.
>
>
> protected void doGet(HttpServletRequest httpServletRequest,
> HttpServletResponse httpServletResponse)
> throws ServletException, IOException {
> try {
>
> Context initContext = new InitialContext();
>
> Object result = initContext.lookup("jdbc/myjndiDatasource");
> System.out.println("Result ==> " + result);
>
> } catch (NamingException e) {
> e.printStackTrace(); //To change body of catch statement
> use File | Settings | File Templates.
> }
>
> httpServletResponse.getOutputStream().print("testString");
> }
>
>
> thanks
> harsha thirimanna
>
>
>
>>
>>
>>
>> On Wed, Jan 2, 2013 at 10:30 AM, Manjula Rathnayake <[email protected]>wrote:
>>
>>>
>>>
>>> ---------- Forwarded message ----------
>>> From: Amila Suriarachchi <[email protected]>
>>> Date: Sat, Dec 29, 2012 at 7:49 AM
>>> Subject: [Dev] Accessing Carbon data sources from web applications
>>> To: WSO2 Developers' List <[email protected]>, Dimuthu Leelarathne <
>>> [email protected]>
>>>
>>>
>>> hi,
>>>
>>> Currently it is not possible to access the carbon data sources created
>>> from the Admin console from the web applications. The reason for this
>>> the current configuration context factory class (i.e.
>>> org.apache.naming.java.javaURLContextFactory) checks for the class
>>> loaders.
>>>
>>> It has following method.
>>>
>>> public Context getInitialContext(Hashtable environment)
>>> 97 throws NamingException {
>>> 98 if (ContextBindings.isThreadBound() ||
>>> 99 (ContextBindings.isClassLoaderBound())) {
>>> 100 // Redirect the request to the bound initial context
>>> 101 return new SelectorContext(environment, true);
>>> 102 } else {
>>> 103 // If the thread is not bound, return a shared
>>> writable context
>>> 104 if (initialContext == null)
>>> 105 initialContext = new NamingContext(environment,
>>> MAIN);
>>> 106 return initialContext;
>>> 107 }
>>> 108 }
>>>
>>> We can avoid this by creating a new Context Factory by extending the
>>> given context factory. Do the following steps.
>>>
>>> 1. Apply the patch to org.wso2.carbon.tomcat bundle at the kernal.
>>> This adds the new over-ridden class. and copy the jar file.
>>>
>>>
>>> 2. change the carbon.xml jndi context factory to
>>>
>>> <DefaultInitialContextFactory>org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory</DefaultInitialContextFactory>
>>>
>>> 3. Create a data source from the Admin console ui.
>>>
>>> 4. Access that within a servelet as follows.
>>>
>>> protected void doGet(HttpServletRequest httpServletRequest,
>>> HttpServletResponse httpServletResponse)
>>> throws ServletException, IOException {
>>> try {
>>> Hashtable hashtable = new Hashtable();
>>> hashtable.put("java.naming.factory.initial",
>>> "org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory");
>>> Context initContext = new InitialContext(hashtable);
>>> Object result = initContext.lookup("jdbc/myjndiDatasource");
>>> System.out.println("Result ==> " + result);
>>>
>>> } catch (NamingException e) {
>>> e.printStackTrace(); //To change body of catch statement
>>> use File | Settings | File Templates.
>>> }
>>>
>>> httpServletResponse.getOutputStream().print("testString");
>>> }
>>>
>>> @Dimuthu is this fix your issue at AppFactory?
>>>
>>> thanks,
>>> Amila.
>>>
>>> --
>>> *Amila Suriarachchi*
>>>
>>> Software Architect
>>> WSO2 Inc. ; http://wso2.com
>>> lean . enterprise . middleware
>>>
>>> phone : +94 71 3082805
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> [email protected]
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>>
>>>
>>> --
>>> Manjula Rathnayaka
>>> Software Engineer
>>> WSO2, Inc.
>>> Mobile:+94 77 743 1987
>>>
>>
>>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
*Kishanthan Thangarajah*
Software Engineer,
Development Technologies Team,
WSO2, Inc.
lean.enterprise.middleware
Mobile - +94773426635
Blog - *http://kishanthan.wordpress.com*
Twitter - *http://twitter.com/kishanthan*
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev