📄 logconfigimpl.java
字号:
/* * Copyright (c) 2003, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */package org.knopflerfish.bundle.log;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.net.URL;import java.util.Dictionary;import java.util.HashMap;import java.util.Hashtable;import java.util.Vector;import org.knopflerfish.service.log.LogConfig;import org.knopflerfish.service.log.LogUtil;import org.osgi.framework.Bundle;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import org.osgi.service.cm.Configuration;import org.osgi.service.cm.ConfigurationAdmin;import org.osgi.service.cm.ConfigurationException;import org.osgi.service.cm.ManagedService;/** * * This class implements the log configuration of the log * service. * Properties are defined using * set<propertyName>() and get<propertyName>() * methods.<br> * Ex. defining integer property Foo<br> * void setFoo(int * value)<br> * int getFoo()<br> * <br> * <br> * If only set method exists the * property is write-only.<br> * If only get method exists the property is * read-only.<br> * If both exist the property is read-write. * */class LogConfigImpl implements ManagedService, LogConfig { BundleContext bc; /* * Variables indicating whether CM configuration has been received. */ boolean DEFAULT_CONFIG = true; boolean firstValid = false; static final String PROP_LOG_OUT = "org.knopflerfish.log.out"; static final String PROP_LOG_GRABIO = "org.knopflerfish.log.grabio"; static final String PROP_LOG_LEVEL = "org.knopflerfish.log.level"; static final String PROP_LOG_FILE = "org.knopflerfish.log.file"; static final String PROP_LOG_FILE_DIR = "org.knopflerfish.log.file.dir"; private final static String OUT = "log.out"; private final static String GRABIO = "log.grabio"; private final static String L_FILTER = "default.level"; private final static String BL_FILTERS = "bundle.log.level"; private final static String DIR = "file.dir"; private final static String FILE_S = "file.size"; private final static String FLUSH = "file.flush"; private final static String PID = "service.pid"; private String pid; final static String MEM = "memory.size"; final static String GEN = "file.generations"; final static String FILE = "file"; private final static int LOCATION_POS = 0; private final static int FILTER_POS = 1; /* Variables containing configuration. */ private File dir; private Hashtable configCollection; private HashMap blFilters = new HashMap(); private LogReaderServiceFactory logReaderCallback; public LogConfigImpl(BundleContext bc) { this.bc = bc; start(); } synchronized void start() { this.pid = this.getClass().getName(); initDir(); configCollection = getDefault(); String[] clazzes = new String[] { ManagedService.class.getName(), LogConfig.class.getName() }; bc.registerService(clazzes, this, configCollection); } private void initDir() { File f = bc.getDataFile("/"); dir = (f != null) ? f : null; } void init(LogReaderServiceFactory lr) { this.logReaderCallback = lr; } void stop() { this.logReaderCallback = null; } /*************************************************************************** * org.knopflerfish.service.log.LogConfig methods **************************************************************************/ /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#commit() */ public synchronized void commit() { updateConfig(); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#isDefaultConfig() */ public boolean isDefaultConfig() { return DEFAULT_CONFIG; } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setMemorySize(int) */ public synchronized void setMemorySize(int size) { int oldSize = getMemorySize(); if (size != oldSize) { Integer newSize = new Integer(size); set(MEM, newSize); if (logReaderCallback != null) logReaderCallback.configChange(MEM, new Integer(oldSize), newSize); updateConfig(); } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getMemorySize() */ public int getMemorySize() { return ((Integer) get(MEM)).intValue(); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setFilter(int) */ public synchronized void setFilter(int filter) { int oldFilter = getFilter(); if (filter == 0) { filter = org.osgi.service.log.LogService.LOG_WARNING; } if (filter != oldFilter) { set(L_FILTER, LogUtil.fromLevel(filter)); updateConfig(); } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getFilter() */ public int getFilter() { return LogUtil.toLevel((String) get(L_FILTER), org.osgi.service.log.LogService.LOG_WARNING); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setFilter(java.lang.String, * int) */ public synchronized void setFilter(String bundleLocation, int filter) { bundleLocation = bundleLocation.trim(); synchronized (blFilters) { Integer f = (Integer) blFilters .get(getCommonLocation(bundleLocation)); if (filter == 0 && f != null) { blFilters.remove(getCommonLocation(bundleLocation)); setCollection(true, bundleLocation, filter); } else if ((f != null && filter != f.intValue()) || f == null) { blFilters.put(getCommonLocation(bundleLocation), new Integer( filter)); setCollection(false, bundleLocation, filter); } } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getFilters() */ public synchronized HashMap getFilters() { return (HashMap) blFilters.clone(); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setOut(boolean) */ public synchronized void setOut(boolean b) { if (!DEFAULT_CONFIG) { boolean oldOut = getOut(); if (b != oldOut) { set(OUT, new Boolean(b)); updateConfig(); } } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getOut() */ public boolean getOut() { return ((Boolean) get(OUT)).booleanValue(); } /** ****** Configuration for filing log entries. ************ */ /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setFile(boolean) */ public synchronized void setFile(boolean f) { if (!DEFAULT_CONFIG) { if ((dir != null)) { boolean oldFile = getFile(); if (f != oldFile) { Boolean newFile = new Boolean(f); set(FILE, newFile); if (logReaderCallback != null) logReaderCallback.configChange(FILE, new Boolean( oldFile), newFile); } } } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getFile() */ public boolean getFile() { return (((Boolean) get(FILE)).booleanValue() && (getDir() != null)); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getDir() */ public File getDir() { if (dir == null) { return dir; } synchronized (dir) { return dir; } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setFileSize(int) */ public synchronized void setFileSize(int fS) { int oldSize = getFileSize(); if (oldSize != fS) { set(FILE_S, new Integer(fS)); } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getFileSize() */ public int getFileSize() { return ((Integer) get(FILE_S)).intValue(); } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setMaxGen(int) */ public synchronized void setMaxGen(int maxGen) { int oldGen = getMaxGen(); if (oldGen != maxGen) { set(GEN, new Integer(maxGen)); if (logReaderCallback != null) logReaderCallback.configChange(GEN, new Integer(oldGen), new Integer(maxGen)); } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getMaxGen() */ public int getMaxGen() { int gen = ((Integer) get(GEN)).intValue(); return (gen < 1) ? 1 : gen; } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#setFlush(boolean) */ public synchronized void setFlush(boolean f) { boolean oldFlush = getFlush(); if (f != oldFlush) { set(FLUSH, new Boolean(f)); } } /* * (non-Javadoc) * * @see org.knopflerfish.service.log.LogConfig#getFlush() */ public boolean getFlush() { return ((Boolean) get(FLUSH)).booleanValue(); } /* * Implement method which checks if a bundle has a specific loglevel and * return to ReaderFactory. */ int getLevel(Bundle bundle) { Integer level; synchronized (blFilters) { level = (Integer) blFilters.get(bundle.getLocation()); if (level == null) { level = (Integer) blFilters.get(((String) (bundle.getHeaders()) .get("Bundle-Name")) + ".jar"); } } return (level != null) ? level.intValue() : getFilter(); } static String[] getBL(Object obj) { String bundleStr = (String) obj; String[] bundle = new String[] { null, null }; int ix = bundleStr.indexOf(";"); try { bundle[0] = bundleStr.substring(0, ix).trim(); bundle[1] = bundleStr.substring(ix + 1).trim(); } catch (Exception e) { throw new IllegalArgumentException( "Bundle entries must be in the format location;level"); } return bundle; } private void setCollection(boolean remove, String bundleLocation, int filter) { synchronized (configCollection) { Vector v = (Vector) configCollection.get(BL_FILTERS); String[] bundF; boolean notFound = true; if (v != null && v.size() > 0) { for (int i = (v.size() - 1); i >= 0; i--) { bundF = getBL(v.elementAt(i)); if (getCommonLocation(bundF[LOCATION_POS]).equals( getCommonLocation(bundleLocation))) { if (remove) { v.removeElementAt(i); } else { v.setElementAt(bundleLocation + ";" + LogUtil.fromLevel(filter), i); } notFound = false; break; } } } if (notFound && !remove) { v.addElement(bundleLocation + ";" + LogUtil.fromLevel(filter)); } } } boolean getGrabIO() { return ((Boolean) get(GRABIO)).booleanValue(); } private Object get(String key) { synchronized (configCollection) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -