2012-03-13 121 views
1

我想在OSGI包中開發日誌記錄系統,它可以將應用程序錯誤寫入硬盤上的文件。Java - 覆蓋抽象方法

這是激活:

import org.osgi.framework.BundleActivator; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.Constants; 
import org.osgi.framework.Filter; 
import org.osgi.framework.ServiceReference; 
import org.osgi.framework.ServiceRegistration; 
import org.osgi.util.tracker.ServiceTracker; 


public class LoggingSystemApp implements BundleActivator { 

    LoggingSystemImpl log = null; 

    @Override 
    public void start(final BundleContext bc) throws Exception { 
     debug("Activator started"); 


     ServiceRegistration registerService = bc.registerService(LoggingSystemImpl.class.getName(), new LoggingSystemImpl(), new Properties()); 
      /* Start Logger System */ 
      log = LoggingSystemImpl.getInstance(); 
      log.start();  


    } 

    public void stop(BundleContext bc) throws Exception { 
     boolean ungetService = bc.ungetService(bc.getServiceReference(LoggingSystem.class.getName())); 
     st.close(); 

     log.stop(); 
    } 

    private void debug(String msg) { 
     System.out.println("JDBCBundleActivator: " + msg); 
    } 

} 

這是記錄制度的落實:

import java.io.BufferedWriter; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.nio.charset.Charset; 
import java.sql.Connection; 
import javax.sql.DataSource; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.Calendar; 
import java.util.Locale; 
import org.DX_57.osgi.LS_27.api.LoggingSystem; 

public class LoggingSystemImpl implements LoggingSystem { 

     public LoggingSystemImpl() { 
     } 

     public DataSource ds; 

     @Override 
     public void setDataSource(DataSource ds){ 
      this.ds = ds; 
     } 


     private final static Calendar calendar = Calendar.getInstance(); 
     private final static String user = System.getenv("USERNAME").toLowerCase(); 
     private final static String sMonth = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH); 
     private final static int y = calendar.get(Calendar.YEAR); 

     // the name of the log file 
     //private final String logName = sysDrive + "\\fttb_web - " + sMonth.toLowerCase() + ", " + y + ".log"; 
     private final String logName = "logger - " + sMonth.toLowerCase() + ", " + y + ".log"; 

     private static boolean closed; 
     private static LoggingSystemImpl log = null; 
     private static BufferedWriter bw = null; 
     private static FileOutputStream fos = null; 
     private static OutputStreamWriter osw = null; 


     /* Utilialize Buffer and wait for data to write */ 
     public void start() throws IOException{    
      log = LoggingSystemImpl.getInstance(); 
     } 

     public void stop(){    
      log.close();   
     } 

     public void WriteLog(String WriteString){ 
      log.writeln(WriteString);    
     } 

     public void LoggingSystemImpl() throws IOException 
     { 
      fos = new FileOutputStream(logName, true); 

      // set encoding to cyrillic (if available) 
      if (Charset.isSupported("windows-1251")) 
      { 
       osw = new OutputStreamWriter(fos, Charset.forName("windows-1251")); 
      } 
      else { osw = new OutputStreamWriter(fos); } 

      bw = new BufferedWriter(osw, 2048); // 2Mb buffer 


     } 

     // intro header for log session 
     public static synchronized LoggingSystemImpl getInstance() throws IOException 
     { 
      boolean exc = false; 
      try 
      { 
       if (log == null || closed) 
       { 
        log = new LoggingSystemImpl(); 
        closed = false; 
        log.writeln("logged in."); 
        log.nl(); 
       } 
      } 
    //  catch(IOException x) { exc = true; throw x; } 
      catch(Exception x) { exc = true; x.printStackTrace(); } 
      finally 
      { 
       if (exc) 
       { 
        try 
        { 
         if (fos != null) { fos.close(); fos = null; } 
         if (osw != null) { osw.close(); fos = null; } 
         if (bw != null) { bw.close(); bw = null; } 
        } 
        catch(Exception x) { x.printStackTrace(); } 
       } 
      } 
      return log; 
     } 


     public synchronized void nl() 
     { 
      try { bw.newLine(); } 
      catch(IOException x) {x.printStackTrace();} 
     } 

     public synchronized void nl(int count) 
     { 
      try 
      { 
       for (int i = 0; i < count; i++) bw.newLine(); 
      } 
      catch(IOException x) {x.printStackTrace();} 
     } 
     public synchronized void writeln(String s) 
     { 
      try { bw.write(getTime() + ": " + s); bw.newLine(); } 
      catch(IOException x) {x.printStackTrace();} 
     } 

     public synchronized void write(String s) 
     { 
      try { bw.write(s); } 
      catch (IOException x) {x.printStackTrace();} 
     } 

     public synchronized void close() 
     { 
      try 
      { 
       if (bw != null) 
       { 
        writeln("logged out."); 
        nl(); 
        bw.flush(); 
        bw.close(); 
        closed = true; 

        fos = null; 
        osw = null; 
        bw = null; 
       } 
      } 
      catch(IOException x) { x.printStackTrace(); } 

     } 

     public synchronized boolean isClosed() { return closed; } 

     public synchronized void writeException(Exception x) 
     { 
      writeln(""); 
      write("\t" + x.toString()); nl(); 
      StackTraceElement[] ste = x.getStackTrace(); 
      int j = 0; 
      for (int i = 0; i < ste.length; i++) 
      { 

       if (i < 15) { write("\t\tat " + ste[i].toString()); nl(); } 
       else { j++; } 

      } 

      if (j > 0) { write("\t\t... " + j + " more"); nl(); } 

      nl(2); 
     } 

     private String getTime() 
     { 
      Calendar c = Calendar.getInstance(); 
      int month = c.get(Calendar.MONTH) + 1; 

      int d = c.get(Calendar.DAY_OF_MONTH); 
      int h = c.get(Calendar.HOUR_OF_DAY); 

      int m = c.get(Calendar.MINUTE); 
      int s = c.get(Calendar.SECOND); 
      int y = c.get(Calendar.YEAR); 

      String dd = d < 10 ? "0"+d : ""+d; 
      String hh = h < 10 ? "0"+h : ""+h; 
      String mm = m < 10 ? "0"+m : ""+m; 
      String ss = s < 10 ? "0"+s : ""+s; 
      String sm = month < 10 ? "0"+month : ""+month; 

      return user + " [" + y + "." + sm + "." + dd + " " + hh + ":" + mm + ":" + ss + "]"; 
     }   



} 

當我嘗試編譯NetBeans中的代碼,我得到這個錯誤:

COMPILATION ERROR : 
------------------------------------------------------------- 
org/DX_57/osgi/LS_27/impl/LoggingSystemImpl.java:[34,7] error: LoggingSystemImpl is not abstract and does not override abstract method SessionRegister(String,String,String) in LoggingSystem 
1 error 

我該如何解決這個問題?

P.S 這是接口

public interface LoggingSystem { 

     public void setDataSource(DataSource ds); 


} 

編輯的代碼 你能告訴我你在代碼中尤其是Activator類看到任何其他錯誤?

+1

它說「LoggingSystem」(它有助於顯示該接口的代碼)有一個名爲'SessionRegister(String,String,String)'的方法。所以你需要在'LoggingSystemImpl'中實現它... – assylias 2012-03-13 22:15:05

回答

3

那麼,錯誤信息很清楚。您可以聲明LoggingSystemImplabstract或實施缺少的方法 - SessionRegister(String,String,String)

原因是接口LoggingSystem已聲明方法SessionRegister(String,String,String)。因爲它沒有實現,所以需要在所有非抽象的孩子中實施,包括你的班級。

速戰速決是:

public class LoggingSystemImpl implements LoggingSystem { 
    void SessionRegister(String,String,String) 
    { //dummy implementation 
    } 
    //other methods 
} 
+0

謝謝。你能告訴我你是否看到代碼中的其他錯誤,特別是Activator類? – 2012-03-13 22:52:56

+0

@ user1103606不,但是我不知道'BundleActivator'是什麼。 – 2012-03-13 22:56:52

1

看起來有在你還沒有實現的接口中聲明的方法SessionRegister(String,String,String) ...你或許應該實現它...

4

你有在你的班級實施上述方法。該消息說,你的班級不是抽象的公交車並沒有具體實現其父母的所有抽象方法。

1

我的猜測是,如果你聲明LoggingSystem如您在代碼中顯示,然後在實現類進口的問題是:

import org.DX_57.osgi.LS_27.api.LoggingSystem; 

你確定這就是你想要實現的接口?