简体   繁体   English

Java-覆盖抽象方法

[英]Java - override abstract method

I want to develop logging system in OSGI bundle which can write application errors into file on the HDD. 我想开发OSGI软件包中的日志记录系统,该系统可以将应用程序错误写入HDD上的文件中。

This is the Activator: 这是激活器:

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);
    }

}

This is the implementation of the Logging system: 这是日志系统的实现:

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 + "]";
        }         



}

When I try to compile the code in Netbeans I get this error: 当我尝试在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

How I can fix this problem? 我该如何解决这个问题?

Best wishes 最好的祝愿

PS this is the code of the interface PS这是界面的代码

public interface LoggingSystem {

        public void setDataSource(DataSource ds);


}

EDIT Can you tell me do you see any other errors in the code especially the Activator class? 编辑你能告诉我在代码中是否看到其他错误,尤其是Activator类吗?

You have to implement the mentioned method in your class. 您必须在类中实现上述方法。 The message says that your class is not abstract bus has not concretely implemented all abstract methods from its parents. 消息说您的类不是抽象总线,尚未从其父级具体实现所有抽象方法。

Well, the error message is pretty clear. 好吧,错误消息很清楚。 You can either declare LoggingSystemImpl as abstract or implement the missing method - SessionRegister(String,String,String) . 您可以将LoggingSystemImpl声明为abstract或实现缺少的方法SessionRegister(String,String,String)

The reason for this is that the interface LoggingSystem has the method SessionRegister(String,String,String) declared. 原因是接口LoggingSystem具有声明的方法SessionRegister(String,String,String) Because it has no implementation, it needs to be implemented in all non-abstract children, including your class. 由于没有实现,因此需要在所有非抽象儿童中实现,包括您的班级。

A quick fix would be: 一个快速的解决方法是:

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

好像在接口中声明了尚未实现的SessionRegister(String,String,String)方法...您可能应该实现它...

My guess is that if you declared LoggingSystem as you show in your code, then the import in the implementation class is the problem: 我的猜测是,如果您在代码中显示时声明了LoggingSystem,那么实现类中的导入就是问题所在:

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

Are you sure that's the interface you're trying to implement? 您确定这是您要实现的接口吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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