Java Code Examples for org.eclipse.jetty.server.session.DatabaseAdaptor

Following code examples demonstrate how to use org.eclipse.jetty.server.session.DatabaseAdaptor from jetty. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.eclipse.jetty.server.session.DatabaseAdaptor and various code implementation of this class.

    private static SessionHandler createSessionHandler(HikariDataSource d) {
        final DefaultSessionIdManager idmgr = new DefaultSessionIdManager(server);
        idmgr.setServer(server);
        try {
            idmgr.setWorkerName(InetAddress.getLocalHost().getHostAddress().replace(".", "_"));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        server.setSessionIdManager(idmgr);
        JDBCSessionDataStore sessionStore = new JDBCSessionDataStore();
        DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
        databaseAdaptor.setDatasource(d);
        sessionStore.setDatabaseAdaptor(databaseAdaptor);
        SessionHandler sessionHandler = new SessionHandler();
        DefaultSessionCache cacheSession = new DefaultSessionCache(sessionHandler);
        sessionHandler.setSessionCache(cacheSession);
        cacheSession.setSessionDataStore(sessionStore);
        sessionHandler.setSessionIdManager(idmgr);

        return sessionHandler;
    } 


  public SessionHandler createSessionHandler( Server server ) {
    SessionHandler handler = new SessionHandler();
    DefaultSessionCache sessionCache = new DefaultSessionCache( handler );
    JDBCSessionDataStore sessionDataStore = new JDBCSessionDataStore();
    sessionCache.setSessionDataStore( sessionDataStore );
    DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
    databaseAdaptor.setDriverInfo( databaseServer.getDriver(), databaseServer.getConnectionUrl() );
    sessionDataStore.setDatabaseAdaptor( databaseAdaptor );
    DefaultSessionIdManager sessionIdManager = new CleanJDBCSessionIdManager( server );
    sessionIdManager.setWorkerName( generateNodeName() );
    handler.setSessionCache( sessionCache );
    handler.setSessionIdManager( sessionIdManager );
    return handler;
  } 

    private static JDBCSessionDataStoreFactory jdbcDataStoreFactory(String driver, String url) {
        DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
        databaseAdaptor.setDriverInfo(driver, url);
        JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory();
        jdbcSessionDataStoreFactory.setDatabaseAdaptor(databaseAdaptor);
        return jdbcSessionDataStoreFactory;
    } 

        private DatabaseAdaptor getAdaptor(String dh) {
            DBConfig conf;
            try {
                conf= new DBConfig(dh);
            } catch (HongsException e) {
                throw new HongsError.Common(e);
            }

            if (conf.link != null && conf.link.length() != 0 ) {
                return getAdaptor(conf.link);
            } else
            if (conf.origin != null && !conf.origin.isEmpty()) {
                dh = (String) conf.origin.get("name");
                dh = ( dh + getOptions (conf.origin));
                DatabaseAdaptor da = new DatabaseAdaptor();
                da.setDatasourceName(dh);
                return da;
            } else
            if (conf.source != null && !conf.source.isEmpty()) {
                String dt ;
                dt = (String) conf.source.get("jdbc");
                dh = (String) conf.source.get("name");
                dh = ( DBConfig.fixSourceName( dh  ));
                dh = ( dh + getOptions (conf.source));
                DatabaseAdaptor da = new DatabaseAdaptor();
                da.setDriverInfo(dt, dh);
                return da;
            } else {
                throw new HongsError.Common("Wrong session manager jdbc!");
            }
        } 

    public static void main(String... args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

        AnnotationConfigWebApplicationContext rootContext =  new AnnotationConfigWebApplicationContext();
        rootContext.register(FrontendConfigBeans.class);
        context.addEventListener(new ContextLoaderListener(rootContext));

        ServletHolder holder = new ServletHolder(AppDefaultServlet.class);
        holder.setInitParameter("acceptRanges", "f");
        holder.setInitParameter("dirAllowed","f");
        holder.setInitParameter("redirectWelcome","f");
        holder.setInitParameter("welcomeServlets", "f");
        holder.setInitParameter("gzip", "t");
        holder.setInitParameter("etags", "t");
        holder.setInitParameter("cacheControl", "t");

        holder.setInitParameter("resourceBase", AppConfig.getConfig("html.json", "publicHtml", ""));


        context.addServlet(holder, "/");


        context.addServlet(HandlerWsServlet.class, "/hws");
        context.addServlet(ApiServlet.class, "/api context));

        String driverClassName = AppConfig.getConfig("jdbc.json", "driverClassName", "");
        String connectionUrl = AppConfig.getConfig("jdbc.json", "connectionUrl", "");
        DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
        databaseAdaptor.setDriverInfo(driverClassName, connectionUrl);

        JDBCSessionDataStore sessionDataStore = new JDBCSessionDataStore();
        sessionDataStore.setDatabaseAdaptor(databaseAdaptor);

        DefaultSessionCache sessionCache = new DefaultSessionCache(context.getSessionHandler());
        sessionCache.setSessionDataStore(sessionDataStore);

        context.getSessionHandler().setSessionCache(sessionCache);

        server.start();

        server.join();
    } 

    private SessionDataStoreFactory createSessionDataStoreFactory(String sessionStoreTable) throws SQLException {
        JDBCSessionDataStoreFactory jdbcSDSFactory = new JDBCSessionDataStoreFactory();
        SessionTableSchema schema = new SessionTableSchema();
        schema.setTableName(sessionStoreTable);
        jdbcSDSFactory.setSessionTableSchema(schema);

        
        DatabaseAdaptor dbAdapter = new DatabaseAdaptor() {
            @Override
            public String getBlobType() {
                if (_blobType == null && StringUtils.containsIgnoreCase(getDBName(), "sql server")) {
                    setBlobType("image");
                }
                return super.getBlobType();
            }
        };

        SqlSessionManager sqlSessionManager = SqlConfig.getInstance().getSqlSessionManager();
        sqlSessionManager.startManagedSession();
        Connection connection = sqlSessionManager.getConnection();
        try {
            dbAdapter.adaptTo(connection.getMetaData());
            dbAdapter.setDatasource(sqlSessionManager.getConfiguration().getEnvironment().getDataSource());
        } finally {
            if (sqlSessionManager.isManagedSessionStarted()) {
                sqlSessionManager.close();
            }
        }
        jdbcSDSFactory.setDatabaseAdaptor(dbAdapter);

        return jdbcSDSFactory;
    } 

Advertisement
Javadoc
DatabaseAdaptor Handles differences between databases. Postgres uses the getBytes and setBinaryStrea

m methods to access a "bytea" datatype, which can be up to 1Gb of binary data. MySQL is happy to use the "blob" type and getBlob() methods instead.

Read More
Advertisement