简体   繁体   中英

Connecting Adobe CQ 5 to external database?

I know this question has been asked before, but I am facing trouble in my case. I know the various steps to connect to external Database, I used this one- Convert JDBC Driver Jar to OSGi Bundle using this link I successfully did the above. Then In my SlingServlet I tried to load driver but I get CLASS NOT FOUND Exception This is the JDBC Jar See bundle is Active 在此处输入图片说明

This is my Servlet code

package com.videojet.hiresite.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.videojet.hiresite.customer.CustomerService;



@SlingServlet(
        paths={"/services/videojet/v1/LoginServlet"}
)
@Properties({
    @Property(name="service.pid", value="com.videojet.hiresite.controllers.LoginServlet",propertyPrivate=false),
    @Property(name="service.description",value="Validates the user", propertyPrivate=false),
    @Property(name="service.vendor",value="Zensar Tech", propertyPrivate=false)
})
public class LoginController extends SlingAllMethodsServlet{
    private static final long serialVersionUID = 1L;
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    protected CustomerService customerService;


    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        log.info("Do Get **************");

    }

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {

        log.info("Do Post **************");
        log.info("Major Man");
        String  username = request.getParameter("username");
        String password = request.getParameter("password");

        log.info("The Username :::::"+username);
        log.info("The Password ::::::"+ password);
        log.info("Very IMportant ++++++++++++++"+request.getParameter("./action"));

        try {
            //customerService.insertData(username, password);
            //customerService.validateUser(username, password); This works fine !!


            Class.forName("org.h2.Driver");

            Connection connection= DriverManager.getConnection("jdbc:h2:file:./samples/database/DATA_SERV_SAMP","wso2ds","wso2ds");
            if (connection==null)
            {
                log.error("Connection Failed ");


            }
            else{

                /*Statement  statement =connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            statement.executeQuery("select * from products where productcode")*/


                log.error("Connected");

            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            log.error("VIDEOJET", e);

        }

        PrintWriter writer =response.getWriter();
        writer.print("Called ");
        writer.close();
    }



}

I am calling this servlet at a form submit(doPost) and here I want to save data in database. But the Driver just doesn't load WHY??

This is Error log.

24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Do Post **************
24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Major Man
24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Username :::::wwwwwwwwwww
24.04.2014 18:36:55.300 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Password ::::::w
24.04.2014 18:36:55.300 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Very IMportant ++++++++++++++null
24.04.2014 18:36:55.358 *ERROR* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController VIDEOJET java.lang.ClassNotFoundException: org.h2.Driver not found by com.videojet.hiresite.Controllers [235]
    at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:787)
    at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:71)
    at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1768)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.videojet.hiresite.controllers.LoginController.doPost(com.videojet.hiresite.controllers.LoginController.java:65)
    at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:148)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:500)
    at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:147)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:308)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:141)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:257)
    at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:106)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:296)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:59)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:220)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.theme.impl.ThemeResolverFilter.doFilter(ThemeResolverFilter.java:76)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:89)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:83)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter.doFilter(BackgroundServletStarterFilter.java:135)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:153)
    at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:206)
    at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
    at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
    at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:179)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:238)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:75)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.felix.http.base.internal.dispatch.FilterPipeline.dispatch(FilterPipeline.java:48)
    at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:39)
    at org.apache.felix.http.base.internal.DispatcherServlet.service(DispatcherServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at com.day.j2ee.servletengine.ServletRuntimeEnvironment.service(ServletRuntimeEnvironment.java:250)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.doFilter(RequestDispatcherImpl.java:315)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:334)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:377)
    at com.day.j2ee.servletengine.ServletHandlerImpl.process(ServletHandlerImpl.java:351)
    at com.day.j2ee.servletengine.HttpListener$Worker.run(HttpListener.java:625)
    at java.lang.Thread.run(Unknown Source)

You should never use this in OSGi:

Class.forName("org.h2.Driver");

It tries to load the class from your bundles classloader, but it is not there!

First as I see h2database's jar is an OSGi bundle: MANIGEST.MF

Also see the service: registering DataSourceFactory

So you can install the jar into OSGi and you can use the DataSourceFactory to create connections, which will solve your classloader issues.

Here is an other example how to solve classloader issue: osgi-jdbc-h2

** UPDATE **

More info about Class.forName: nice article If I were you I would also check this: Day.com article about Connecting to SQL Databases

It is a classic problem and well discribed in enterprise OSGi specification. see: Page 375, Chapter: JDBC™ Service Specification

Some keywords: jdbc, OSGi, class.forName, dataservicefactory

I resolved the Issue myself. I used an older jar OJDBC14 instead of OJDBC6. And I found out that when these Jars are converted to OSGi bundel and loaded in Felix they are dependend on some other jars, Example rt.jar that comes with JRE. this jar need to be converted to OSgi bundle and deplyoed with other dependent jars.

And the Imported packages Manifest of OJDBC mus have this.

Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.security.auth.callback,
 javax.crypto.spec,
 javax.naming.directory,
 javax.sql.rowset.spi,
 javax.transaction.xa,
 javax.security.auth.kerberos,
 javax.security.cert,
 org.ietf.jgss,
 org.xml.sax,
 javax.xml.transform,
 javax.xml.transform.stax,
 javax.net,
 javax.management,
 org.xml.sax.helpers,
 javax.security.auth,
 javax.xml.transform.sax,
 javax.xml.stream,
 org.w3c.dom,
 javax.net.ssl,
 javax.naming.spi,
 javax.sql,
 javax.crypto,
 javax.xml.transform.dom,
 javax.naming,
 javax.xml.transform.stream,
 javax.xml.parsers,
 javax.sql.rowset

IT alsor requires some Apache Lucene jars ... Weired but worked fine !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM