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

📄 logconfigimpl.java

📁 OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return configCollection.get(key);        }    }    private void set(String key, Object value) {        synchronized (configCollection) {            configCollection.put(key, value);        }    }    /** **************** Called from set methods ********************* */    private void updateConfig() {        try {            ServiceReference sr = bc                    .getServiceReference(ConfigurationAdmin.class.getName());            if (sr != null) {                ConfigurationAdmin ca = (ConfigurationAdmin) bc.getService(sr);                if (ca != null) {                    Configuration conf = ca.getConfiguration(pid);                    if (conf != null) {                        conf.update(configCollection);                    }                }                bc.ungetService(sr);            }        } catch (IOException io) {        } catch (java.lang.IllegalArgumentException iae) {        } catch (java.lang.IllegalStateException ise) {        }    }    /* ======================================================================== */    /* Initializes configuration */    /* ======================================================================== */    private Hashtable getDefault() {        Hashtable ht = new Hashtable();        Vector bundleLogFilters = new Vector();        String o = getProperty(PROP_LOG_OUT, "false");        String levelStr = getProperty(PROP_LOG_LEVEL, LogUtil                .fromLevel(org.osgi.service.log.LogService.LOG_WARNING));        ht.put(L_FILTER, levelStr);        ht.put(MEM, new Integer(250));        ht.put(OUT, new Boolean(("true".equalsIgnoreCase(o)) ? true : false));        ht.put(GRABIO, new Boolean(("true".equalsIgnoreCase(getProperty(                PROP_LOG_GRABIO, "false")) ? true : false)));        ht.put(FILE, new Boolean(("true".equalsIgnoreCase(getProperty(                PROP_LOG_FILE, "false")) ? true : false)));        ht.put(DIR, getProperty(PROP_LOG_FILE_DIR, ""));        ht.put(FILE_S, new Integer(20000));        ht.put(GEN, new Integer(4));        ht.put(FLUSH, new Boolean(true));        ht.put(BL_FILTERS, bundleLogFilters);        ht.put(PID, this.pid);        return ht;    }    static String getProperty(String key, String def) {        String result = def;        try {            result = System.getProperty(key);            if (result == null) {                result = def;            }        } catch (Exception e) {            System.err.println("Failed to get property " + key + " : " + e);            result = def;        }        return result;    }    /* ======================================================================== */    /* Method implementing the ManagedService interface */    /* gets called whenever the configuration for this */    /* ManagedService is updated. */    /* ======================================================================== */    public synchronized void updated(Dictionary cfg)            throws ConfigurationException, IllegalArgumentException {        if (cfg == null) {            DEFAULT_CONFIG = true;            checkChange(getDefault());        } else {            checkValidity(cfg);        }        updateGrabIO();    }    boolean bGrabbed = false;    PrintStream origOut = null;    PrintStream origErr = null;    LogReaderServiceFactory lsrf;    void setLogReaderServiceFactory(LogReaderServiceFactory lsrf) {        this.lsrf = lsrf;    }    void updateGrabIO() {        boolean bDebugClass = "true".equals(System.getProperty(                "org.knopflerfish.framework.debug.classloader", "false"));        if (!bDebugClass) {            if (getGrabIO()) {                if (!getOut()) {                    if (!bGrabbed) {                        origOut = System.out;                        origErr = System.err;                        System.setOut(new WrapStream("[stdout] ", System.out,                                org.osgi.service.log.LogService.LOG_INFO));                        System.setErr(new WrapStream("[stderr] ", System.out,                                org.osgi.service.log.LogService.LOG_ERROR));                    }                }            } else {                if (bGrabbed) {                    System.setOut(origOut);                    System.setErr(origErr);                    bGrabbed = false;                }            }        }    }    class WrapStream extends PrintStream {        String prefix;        int level;        WrapStream(String prefix, PrintStream out, int level) {            super(out);            this.prefix = prefix;            this.level = level;        }        public void println(String s) {            super.print(prefix);            super.println(s);            log(s);        }        public void print(String s) {            super.print(s);        }        public void println(Object x) {            super.print(prefix);            super.println(x);            log("" + x);        }        public void print(Object x) {            super.print(x);        }        void log(String s) {            if (-1 != s.indexOf(prefix)) {                return;            }            if (lsrf != null) {                lsrf.log(new LogEntryImpl(bc.getBundle(0), level, prefix + s,                        null));            }        }    }    /*------------------------------------------------------------------------*/    /* Methods for checking incoming configuration, */    /* Methods for setting incoming configuration localy. */    /*------------------------------------------------------------------------*/    /*     * Check that incoming configuration is correct. If not an exception will be     * thrown and the default configuration or the former configuration will be     * used for ALL properties, i.e., no property will be set if one item in the     * configuration is invalid.     */    private void checkValidity(Dictionary cfg) throws ConfigurationException,            IllegalArgumentException {        boolean valid = false;        Hashtable rollBack = (Hashtable) configCollection.clone();        try {            checkLogLevel(cfg);            checkBundleLogLevel(cfg);            valid = true;        } finally {            if (!valid) {                // Removed call to updateConfig() because all it accomplishes                // is to cause an endless loop of ConfigurationAdmin                // calling ManagedService.update(Dictionary) on this class                // over and over and over with the same invalid Dictionary                // updateConfig();            } else {                valid = false;                try {                    acceptConfig(cfg);                    valid = true;                } catch (Exception all) {                    throw new ConfigurationException(null,                            "Fault occurred when " + "setting configuration. "                                    + "Check that all properties "                                    + "are valid.");                } finally {                    if (!valid) {                        configCollection = rollBack;                        // Removed call to updateConfig() because all it                        // accomplishes                        // is to cause an endless loop of ConfigurationAdmin                        // calling ManagedService.update(Dictionary) on this                        // class                        // over and over and over with the same invalid                        // Dictionary                        // updateConfig();                    }                }            }        }    }    /* Check log level property for faults. */    private void checkLogLevel(Dictionary cfg) throws ConfigurationException,            IllegalArgumentException {        String filter = null;        Object obj = cfg.get(L_FILTER);        try {            filter = ((String) obj).trim();        } catch (ClassCastException cce) {            throw new IllegalArgumentException(                    "Wrong type supplied when attempting to set log level."                            + " Correct type to use is String. " + obj + " "                            + obj.getClass().getName());        }        if (filter == null) {            throw new IllegalArgumentException(                    "No log level given. Please supply a valid log level.");        }        int filterVal = LogUtil.toLevel(filter, -1);        if (filterVal == -1) {            throw new ConfigurationException(L_FILTER, "Undefined log level <"                    + filter + ">.");        }        if (filterVal == 0) {            cfg.put(L_FILTER, LogUtil                    .fromLevel(org.osgi.service.log.LogService.LOG_WARNING));        }    }    /* Check bundle log level property for faults. */    private void checkBundleLogLevel(Dictionary cfg)            throws ConfigurationException, IllegalArgumentException {        Vector v = null;        try {            v = (Vector) cfg.get(BL_FILTERS);        } catch (ClassCastException cce) {            throw new IllegalArgumentException(                    "Wrong type supplied when attempting to set log level for "                            + "specific bundle."                            + " Correct type to use is Vector of String[].");        }        if (v != null) {            String[] bundle = null;            for (int i = 0; i < v.size(); i++) {                try {                    bundle = getBL(v.elementAt(i));                } catch (ClassCastException cce) {                    throw new IllegalArgumentException(                            "Wrong type supplied when attempting to set log level for "                                    + "specific bundle."                                    + " Correct type to use is String.");                }                if (bundle == null) {                    throw new IllegalArgumentException(                            "Empty configuration supplied when attempting to set log level "                                    + " for specific bundle.");                }                bundle[LOCATION_POS] = bundle[LOCATION_POS].trim();                if (bundle[LOCATION_POS] == null                        || bundle[LOCATION_POS].length() <= 0) {                    throw new IllegalArgumentException(                            "No bundle location given when setting log level for specific "                                    + "bundle.");                }                if (bundle[FILTER_POS] == null) {                    throw new IllegalArgumentException(                            "No log level given for bundle: "                                    + bundle[LOCATION_POS] + ". "                                    + "Please supply a valid log level.");                }                int testFilter = 0;                testFilter = LogUtil.toLevel((bundle[FILTER_POS].trim()), -1);                if (testFilter == -1) {                    throw new ConfigurationException(BL_FILTERS,                            "Undefined log level <" + bundle[FILTER_POS]                                    + "> specified for bundle <"                                    + bundle[LOCATION_POS] + ">.");                }                if (testFilter == 0) {                    v.removeElementAt(i);                    i--;                } else {                    bundle[FILTER_POS] = LogUtil.fromLevel(testFilter);                    checkLocation(bundle[LOCATION_POS]);                }            }        }    }    /* Check whether given location exists. */    private void checkLocation(String location) {        try {            if (location.indexOf("/") != -1 || location.indexOf("\\") != -1) {                URL u = new URL(getCommonLocation(location));                InputStream is = u.openStream();                is.close();            }        } catch (IOException ignore) {            log("File <" + location + "> set at configuration of logcomponent "                    + "does not exist at the given location. ");        }    }    /*     * Called when an incoming configuration seems correct. Should the     * checkChange method throw an exception the configuration will be reset to     * the former valid state.     */    private void acceptConfig(Dictionary cfg) {        firstValid = DEFAULT_CONFIG;        DEFAULT_CONFIG = false;        checkChange(cfg);    }    /*     * Checking which property actually changed. Called once the validity of the     * incoming configuration has been checked. If some property changed notify     * about change.     */    private void checkChange(Dictionary cfg) {        setFilterCfg((Vector) cfg.get(BL_FILTERS));        Object newV = null;        if ((newV = diff(L_FILTER, cfg)) != null) {            set(L_FILTER, newV);        }        if ((newV = diff(MEM, cfg)) != null) {            notify(MEM, newV);            set(MEM, newV);        }        if ((newV = diff(OUT, cfg)) != null) {            if (DEFAULT_CONFIG) {                set(OUT, "true".equalsIgnoreCase(getProperty(PROP_LOG_OUT,                        "false")) ? Boolean.TRUE : Boolean.FALSE);            } else {                set(OUT, newV);            }        }        if ((newV = diff(DIR, cfg)) != null) {            File currentDir = bc.getDataFile("/");            newV = ((String) newV).trim();            if (currentDir != null && !(newV.equals(""))) {                currentDir = new File((String) newV);            }            if (dir != null) {                synchronized (dir) {                    dir = currentDir;                }            } else {                dir = currentDir;            }            set(DIR, newV);        }        if ((newV = diff(FILE_S, cfg)) != null) {            set(FILE_S, newV);        }        if ((newV = diff(FLUSH, cfg)) != null) {            set(FLUSH, newV);        }        if ((newV = diff(FILE, cfg)) != null) {            if (dir != null) {                if (firstValid) {                    synchronized (configCollection) {                        configCollection.remove(FILE);                    }                    firstValid = false;                }                notify(FILE, newV);            }            set(FILE, (DEFAULT_CONFIG) ? new Boolean(false) : newV);        }        if ((newV = diff(GEN, cfg)) != null) {            notify(GEN, newV);            set(GEN, newV);        }    }    /*     * Check bundle log level and see if changes has been made. If so change     * internal representation of bundle log levels.     */    private void setFilterCfg(Vector newV) {        if (newV != null) {            String[] bundle = null;            String common;            int newFilter = -1;            HashMap tmpFilters = new HashMap();            for (int i = 0; (i < newV.size()); i++) {                bundle = getBL(newV.elementAt(i));                common = getCommonLocation(bundle[LOCATION_POS]);                newFilter = LogUtil.toLevel((bundle[FILTER_POS].trim()),                        org.osgi.service.log.LogService.LOG_WARNING);                tmpFilters.put(common, new Integer(newFilter));            }            synchronized (blFilters) {                blFilters = tmpFilters;            }            set(BL_FILTERS, newV);        }    }    /* Utility methods serving this and LogConfigCommandGroup. */    String getCommonLocation(String location) {        if (location.endsWith(".jar")) {            return location;        }        return location + ".jar";    }    private Object diff(String key, Dictionary cfg) {        Object newV = null;        return ((newV = cfg.get(key)) != null && !newV.equals(configCollection                .get(key))) ? newV : null;    }    private void notify(String key, Object newV) {        if (logReaderCallback != null) {            logReaderCallback                    .configChange(key, configCollection.get(key), newV);        }    }    private void log(String msg) {        if (logReaderCallback != null) {            logReaderCallback.log(new LogEntryImpl(bc.getBundle(),                    org.osgi.service.log.LogService.LOG_INFO, msg));        }    }}

⌨️ 快捷键说明

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