【问题标题】:How to create a simple OSGI module如何创建一个简单的 OSGI 模块
【发布时间】:2011-12-28 07:59:10
【问题描述】:

我在编写 OSGI 模块方面需要帮助。这是我写的代码。它还没有完成,但我可以用 Netbeans 编译它。

/*
 * OSGI Module for Sessions handling
 */

package com.SessionHandle;
/** include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.annotation.Resource;
   // or import javax.faces.bean.ManagedBean;   

public class sessionLogger {
    public String error_Message = null;
    public String error_Database = null;

    /** Call the Oracle JDBC Connection driver */
    @Resource(name="java:/Oracle")
    private DataSource ds;


    /** method for checking active sessions into the Oracle database.
     *  The calling module sends the UserId, UserIP, UserBrowserId.
     *  If the check is successful the LastRefreshTime is updated with
     *  with the current time.
     */

    /*
     CREATE TABLE "ACTIVESESSIONS"(
  "SessionId" Char(20 ) NOT NULL,
  "UserId" Varchar2(30 ) NOT NULL,
  "LoginTime" Timestamp(6),
  "LastRefreshTime" Timestamp(6),
  "UserIP" Varchar2(30 ),
  "UserBrowserID" Varchar2(30 )) 
     */

       public Integer activeSessionCheck(String sessionId, String userId, 
                                         String loginTime, String lastRefreshTime, 
                                         String userIp,    String userBrowserId) throws SQLException {

            String storedSession = null;
            error_Message = null;
            String SQL_Statement = null;

            if (ds == null) throw new SQLException( error_Database = "No data source");      
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException( error_Database = "No connection");      

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       SQL_Statement = "SELECT * from ACTIVESESSIONS WHERE SessionId = ? AND UserIP = ? AND UserBrowserID = ?";

                       PreparedStatement sessionQuery = conn.prepareStatement(SQL_Statement);
                       sessionQuery.setString(1, sessionId);
                       sessionQuery.setString(2, userIp);
                       sessionQuery.setString(3, userBrowserId);

                       ResultSet result = sessionQuery.executeQuery();

                       if(result.next()){
                            storedSession = result.getString("SessionId");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {               
                conn.close();

                }  
         /** If the session is not null update the session expire time */
         if (storedSession != null){

                 try {
                    conn.setAutoCommit(false);
                    boolean committed = false;
                    try {           /* insert into Oracle the default system(Linux) time */
                           SQL_Statement = "UPDATE ACTIVESESSIONS SET LastRefreshTime = SYSDATE WHERE SessionId = ?";

                           PreparedStatement insertQuery = conn.prepareStatement(SQL_Statement);                                                             
                           insertQuery.setString(1, sessionId);                       
                           insertQuery.executeUpdate();                  

                           conn.commit();
                           committed = true;
                     } finally {
                           if (!committed) conn.rollback();
                           }
                    }
                    finally {               
                    conn.close();

                    }      
               /** if the session is registered successfully return 0 */ 
               return 0;
           } else {
               /** if the session is not registered return 1 */ 
               return 1;
           }     
         /*!!!!!! dobavi vav faces-config novo navigation rule - ako se varne otgovor 1 da prepra6ta klienta na login menu */
       }

       /** method for recording user activity into the Oracle database */

       /*
        CREATE TABLE "SESSIONSLOG"(
  "SessionID" Varchar2(30 ) NOT NULL,
  "Username" Varchar2(30 ),
  "IpAddress" Varchar2(30 ),
  "WebBrowserID" Varchar2(30 ),
  "LoginTime" Timestamp(6),
  "LogoutTime" Timestamp(6)) 
        */       

       public void sessionLog(String sessionId, String userName,
                                String ipAddress, String webBrowserId,
                                String loginTime, String logoutTime) throws SQLException {

            String storedPassword = null;
            error_Message = null;
            String SQL_Statement = null;

            if (ds == null) throw new SQLException( error_Database = "No data source");      
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException( error_Database = "No connection");      

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       SQL_Statement = "SELECT passwd from USERS WHERE userz = ?";

                       PreparedStatement passwordQuery = conn.prepareStatement(SQL_Statement);
                       passwordQuery.setString(1, sessionId);

                       ResultSet result = passwordQuery.executeQuery();

                       if(result.next()){
                            storedPassword = result.getString("passwd");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {               
                conn.close();

                }  
       /** if the user is not found or password don't match display error message*/
       if (storedPassword == null){
           error_Message = "Invalid Username!";
       } else {
           error_Message = "Invalid Password!";
       }

       return;       
       }



       /** method for recording sessions activity into the Oracle database */

       /*
        CREATE TABLE "ACTIVESESSIONSLOG"(
  "SessionId" Varchar2(30 ) NOT NULL,
  "UserId" Varchar2(30 ),
  "ActivityStart" Timestamp(6),
  "ActivityEnd" Timestamp(6),
  "Activity" Clob) 
        */

       public void activeSessionLog(String sessionId,     String userId,
                                      String activityStart, String activityEnd,
                                      String Activity) throws SQLException {
            String storedPassword = null;
            error_Message = null;
            String SQL_Statement = null;

            if (ds == null) throw new SQLException( error_Database = "No data source");      
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException( error_Database = "No connection");      

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       SQL_Statement = "SELECT passwd from USERS WHERE userz = ?";

                       PreparedStatement passwordQuery = conn.prepareStatement(SQL_Statement);
                       passwordQuery.setString(1, sessionId);

                       ResultSet result = passwordQuery.executeQuery();

                       if(result.next()){
                            storedPassword = result.getString("passwd");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {               
                conn.close();

                }  
       /** if the user is not found or password don't match display error message*/
       if (storedPassword == null){
           error_Message = "Invalid Username!";
       } else {
           error_Message = "Invalid Password!";
       }

       return;       
       }


}

这是 OSGI 包的文件结构:

rcbandit@rcbandit-laptop:~/NetBeansProjects$ tree SL_24
SL_24
├── nbactions.xml
├── pom.xml
├── src
│   └── main
│       ├── assembly
│       │   └── felix.xml
│       ├── java
│       │   └── com
│       │       ├── SessionHandle
│       │       │   └── sessionLogger.java
│       │       └── SL_24
│       │           └── Activator.java
│       └── resources
│           └── com
│               └── SL_24
└── target
    ├── classes
    │   ├── com
    │   │   ├── SessionHandle
    │   │   │   └── sessionLogger.class
    │   │   └── SL_24
    │   │       └── Activator.class
    │   └── META-INF
    │       └── MANIFEST.MF
    ├── generated-sources
    │   └── annotations
    ├── SL_24-1.0-SNAPSHOT.jar
    └── surefire

19 directories, 9 files

这是我不知道怎么写的Activator类:

package com.SL_24;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        System.out.println("Module SL_24 is Loaded!");
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Module SL_24 in Unloaded!");
    }

}

当我尝试将它部署到 JBoss 7.1.0 服务器时出现问题。激活器类似乎没有正确编写。您能帮我以正确的方式编写它吗?之后我如何调用 OSGI 捆绑洞察 EAR 包的方法?

国王的问候,

彼得

【问题讨论】:

  • 附注事实证明,当我删除这一行“@Resource(name="java:/Oracle")”时,捆绑包已成功部署。也许我在 POM 文件中遗漏了一些东西?还是 OSGI 包无法查询数据库?
  • 请始终说明您收到的错误消息,而不仅仅是“它不起作用”或“它似乎没有正确写入”。这类陈述根本无助于找出问题所在。
  • 哦,当然 OSGi 包可以进行数据库查询。
  • 这里是错误堆栈:pastebin.com/sKsCUnS8

标签: java jsf jsf-2 ejb-3.0 osgi


【解决方案1】:

根据您评论中给出的错误跟踪,我看到 JBoss 中抛出了 NullPointerException。这似乎是 JBoss 中的一个错误,因此应该在 JBoss 论坛上讨论。

【讨论】:

  • 我应该更改 Activator 类吗?
  • 为什么?它似乎没有任何问题(除了它没有做任何有用的事情,但我想你已经知道了!)
猜你喜欢
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多