2011-12-28 3 views
1

OSGI 모듈 작성에 도움이 필요합니다. 이것은 내가 작성한 코드입니다. 아직 완성되지 않았지만 Netbeans로 컴파일 할 수 있습니다. 간단한 OSGI 모듈 작성 방법

/* 
* 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 번들의 파일 구조입니다 :

[email protected]:~/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 

그리고 이것은 내가 작성하는 방법을 모르는 액티베이터 클래스입니다 :

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 패키지에 대한 메소드를 호출하는 방법을 설명 할 수 있습니까? 귀하의 코멘트에 주어진 오류 추적을 바탕으로

왕 안부를

+0

p.s. 이 줄을 제거하면 "@Resource (name ="java :/Oracle ")"번들이 성공적으로 배포됩니다. POM 파일에 뭔가 빠져있을 수 있습니까? 또는 OSGI 번들은 데이터베이스에 쿼리를 할 수 없습니까? –

+0

단순히 "작동하지 않습니다"또는 "제대로 작성되지 않은 것"이 아니라 항상 오류 메시지를 알려주십시오. 그러한 종류의 진술은 문제가 무엇인지 알아내는 데 전혀 도움이되지 않습니다. –

+0

오 물론 ** ** OSGi 번들은 데이터베이스 질의를 할 수 있습니다. –

답변

1

피터, 내가 보스 내에서 발생되는 NullPointerException이 있음을 참조하십시오. 이것이 JBoss의 버그 일 가능성이 높으므로 JBoss 포럼에서 논의해야합니다.

+0

Activator 클래스를 변경해야합니까? –

+0

왜? 아무것도 잘못한 것 같지 않습니다. (아무것도 유용하지 않지만, 이미 알고있는 것 같습니다!) –