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
Index: 
src/main/java/org/wso2/carbon/tomcat/jndi/CarbonJavaURLContextFactory.java
===================================================================
--- src/main/java/org/wso2/carbon/tomcat/jndi/CarbonJavaURLContextFactory.java  
(revision 0)
+++ src/main/java/org/wso2/carbon/tomcat/jndi/CarbonJavaURLContextFactory.java  
(revision 0)
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.carbon.tomcat.jndi;
+
+import org.apache.naming.java.javaURLContextFactory;
+import org.apache.naming.NamingContext;
+
+import javax.naming.Name;
+import javax.naming.Context;
+import javax.naming.NamingException;
+import java.util.Hashtable;
+
+public class CarbonJavaURLContextFactory extends javaURLContextFactory {
+
+    public Object getObjectInstance(Object o,
+                                    Name name,
+                                    Context context,
+                                    Hashtable<?, ?> hashtable) throws 
NamingException {
+        return null;
+    }
+
+    public Context getInitialContext(Hashtable hashtable) throws 
NamingException {
+        if (initialContext == null) {
+            initialContext = new NamingContext(hashtable, MAIN);
+        }
+        return initialContext;
+    }
+}
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to