⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rmsappender.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2003-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.util;

import com.funambol.storage.DataAccessException;
import java.util.Date;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;


/**
 * Logger class for debugging porposes
 *
 */
public class RMSAppender implements Appender {
    //---------------------------------------------------------------- Constants
    //RMS Recordstore name:
    public static final String LOGDBNAME = "funambol.log";
    public static final int DEFAULTLOGFILESIZE = 5120;
    public static final int LOGSIZETOBEFREE = DEFAULTLOGFILESIZE/2;
    // if loglevel is debug the RMS Recordstore is doubled
    public static final int EXTENDED_SIZE = 10240;
    
    //--------------------------------------------------------------- Attributes
    /**
     * Default Log RMS Index Variables: when a RecordStoreFull Exception occurs
     * indicates the first record to be deleted before a new one could be added
     */
    
    //Default RMS RecordStore Name
    private String dbName = null;
    private RecordStore dbStore = null;
    private static int defaultLogFileSize;
    private static int defaultLogStoreSizeToBeFree;
    private int firstRecord;
    private int logFileSize = 0;
    private static int firstRecordtoBeDeleted;
    
    //------------------------------------------------------------- Constructors
    public RMSAppender(String logfilename) {
        this.dbName = logfilename;
        this.defaultLogFileSize = DEFAULTLOGFILESIZE;
        this.defaultLogStoreSizeToBeFree = LOGSIZETOBEFREE;
        this.firstRecord = 1;
    }
    //----------------------------------------------------------- Public methods
    /**
     * Logs a string to standard output and to the device's event logger
     * @param msg the string to be logged
     **/
    public void writeLogMessage(String level, String msg)
    throws DataAccessException {
        
        
        //Put a timestamp on log message
        msg = MailDateFormatter.dateToUTC(new Date() )
                        +"\n[" + level + "] " + msg;
        
        boolean writeSuccess = true;
        //Open DS-Log RecordStore
        openLogFile();
        
        this.logFileSize = getLogFilesize();
        
        //Rotate Log records if Log RecordStore exceed default dimension;
        //If log message is too big for default Log File size throws
        //DataAccessexception
        if (this.logFileSize + msg.length()>defaultLogFileSize) {
            if (msg.length()>defaultLogFileSize) {
                throw new DataAccessException("Log Message too big.\n" +
                        " Total Size: " +
                        logFileSize + msg.length() +
                        ".\n " +
                        "Default size set to: " + defaultLogFileSize +
                        "\nChange default log file size to be written\n");
            } else {
                
                rotateLogStore(msg.getBytes().length);
            }
        }
        if (writeSuccess) {
            //Add new record to the RecordStore
            try {
                dbStore.addRecord(msg.getBytes(), 0, msg.getBytes().length);
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreFullException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            } finally {
                closeLogFile();
            }
        } else {
            closeLogFile();
        }
    }
    
    /**
     * Opens DS-Log store and catch all possible exception
     */
    public void openLogFile(){
        try {
            dbStore = RecordStore.openRecordStore(dbName, true);
        } catch (RecordStoreException rse) {
            System.out.println("Exception opening " + dbName);
            System.out.println(rse);
            rse.printStackTrace();
        }
    }
    
    /**
     * Closes DS-Log store and catch all possible exception
     */
    public void closeLogFile() {
        try {
            dbStore.closeRecordStore();
        } catch (RecordStoreException rse) {
            System.out.println("Exception Closing " + dbName);
            System.out.println(rse);
            rse.printStackTrace();
        }
    }
    
    /**
     * Resets DS-Log store and catch all possible exception
     */
    public void deleteLogFile(){
        try {
            //Delete Log RecordStore
            RecordStore.deleteRecordStore(dbName);
            System.out.println("Log File deleted");
        } catch (RecordStoreNotFoundException ex) {
            System.out.println("Log file not found.");
        } catch (RecordStoreException ex) {
        }
    }
    
    /**
     * Mandatory method for all applications that means to use
     * DS-Log store method (non jsr-75 compliant applications)
     *
     */
    public void initLogFile() {
        try {
            //Delete Old Log RecordStore
            RecordStore.deleteRecordStore(dbName);
            System.out.println("Old Log File deleted");
        } catch (RecordStoreNotFoundException ex1) {
            //Create new Log RecordStore
            System.out.println("Log file not found. Creating new Log file");
            try {
                dbStore = RecordStore.openRecordStore(dbName, true);
                dbStore.closeRecordStore();
            } catch (RecordStoreException ex2) {
                ex2.printStackTrace();
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } finally {
            try {
                System.out.println("Creating new Log file");
                //Create new Log RecordStore
                dbStore = RecordStore.openRecordStore(dbName, true);
                dbStore.closeRecordStore();
            } catch (RecordStoreNotOpenException notOpenEx) {
                notOpenEx.printStackTrace();
            } catch (RecordStoreFullException fullEx) {
                fullEx.printStackTrace();
                System.out.println("Cannot create log file: " +
                        "Application reached RecordStore Maximum dimension");
            } catch (RecordStoreException rmsEx) {
                rmsEx.printStackTrace();
                System.err.println("Cannot create Log  file");
            }
        }
    }
    
    /**
     * Resizes DS-Log RecordStore
     * In case of Log RecordStore exceed default first indexed records are
     * deleted:
     * The size to be free will be decided by user setting the private variable
     * defaultLogStoreSizeToBefree; this class provides the default size:
     * <code>DEFAULTLOGFILESIZE</code>
     * @param msgSize number of byte of the next log message
     *
     */
    private void rotateLogStore(int msgSize) {
        int actualFreeSize = 0;
        int sizeToBeFree = defaultLogStoreSizeToBeFree;
        if (msgSize >= defaultLogStoreSizeToBeFree) {
            sizeToBeFree = msgSize;
        }
        while (actualFreeSize < sizeToBeFree) {
            try {
                int nextRecord = dbStore.getNextRecordID();
                firstRecord = nextRecord - dbStore.getNumRecords();
                firstRecordtoBeDeleted = firstRecord;
                int nextRecordSize = dbStore.getRecordSize(firstRecord);
                dbStore.deleteRecord(firstRecord);
                actualFreeSize += nextRecordSize;
//                System.out.println("Deleted Record: " + firstRecord);
//                System.out.println("Actual free size: " + actualFreeSize);
//                System.out.println("Size to be free: " + sizeToBeFree);
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (InvalidRecordIDException ex) {
                if (this.logFileSize - actualFreeSize == 0) {
                    break;
                } else {
                    ex.printStackTrace();
                    System.err.println("Record number: " 
                                               + firstRecord + " not exixtent");
                }
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * Setter method for defaultLogFileSize
     * @param newDefaultLogFileSize is the new default log size in bytes
     */
    public static void setDefaultLogFileSize(int newDefaultLogFileSize) {
        defaultLogFileSize = newDefaultLogFileSize;
    }
    
    /**
     * Getter method for defaultLogFileSize
     * @return defaultLogFileSize default log size in bytes
     */
    public static int getDefaultLogFileSize() {
        return defaultLogFileSize;
    }
    
    /**
     * Setter method for defaultLogFileSize
     * @param newDefaultLogStoreSizeToBeFree is the new default log size to be free
     */
    public static void setDefaultLogStoreSizeToBeFree
            (int newDefaultLogStoreSizeToBeFree) {
        defaultLogStoreSizeToBeFree = newDefaultLogStoreSizeToBeFree;
    }
    
    /**
     * Getter method for defaultLogFileSizeToBeFree
     * @return defaultLogFileSize log size to be free in bytes
     */
    public static int getDefaultLogStoreSizeToBeFree() {
        return defaultLogStoreSizeToBeFree;
    }
    
    /**
     * Getter method for firstRecord
     * @return defaultLogFileSize log size to be free in bytes
     */
    public static int getFirstRecord() {
        return firstRecordtoBeDeleted;
    }
    
    private int getLogFilesize() {
        int storeSize = 0;
        RecordEnumeration re;
        try {
            re = dbStore.enumerateRecords(null, null, false);
            
            while (re.hasNextElement()) {
                try {
                    storeSize += dbStore.getRecordSize(re.nextRecordId());
                } catch (RecordStoreNotOpenException ex) {
                    ex.printStackTrace();
                } catch (InvalidRecordIDException ex) {
                    ex.printStackTrace();
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
            }
            return storeSize;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } finally {
            return storeSize;
        }
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -