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

📄 consoletcp.java

📁 OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.consoletcp;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import java.net.UnknownHostException;import java.util.Dictionary;import java.util.Enumeration;import java.util.Hashtable;import org.knopflerfish.service.console.ConsoleService;import org.knopflerfish.service.console.Session;import org.knopflerfish.service.console.SessionListener;import org.knopflerfish.service.um.useradmin.ContextualAuthorization;import org.knopflerfish.service.um.useradmin.PasswdAuthenticator;import org.knopflerfish.service.um.useradmin.PasswdSession;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.Constants;import org.osgi.framework.ServiceReference;import org.osgi.service.cm.ConfigurationException;import org.osgi.service.cm.ManagedService;import org.osgi.service.log.LogService;import org.osgi.service.useradmin.Authorization;// ******************** ConsoleTcp ********************/** * * Bundle activator implementation. * * *  * @author Jan Stein * * @version $Revision: 1.1.1.1 $ */public class ConsoleTcp implements BundleActivator, ManagedService {    ServiceReference srefConsole;    private static final String PORT_PROPERTY = "port";    private static final String ADDRESS_PROPERTY = "address";    private static final String LOGIN_PROPERTY = "login";    private static final String UM_PROPERTY = "um";    private static final String EXIT_ON_LOGOUT_PROPERTY = "exitOnLogout";    private static final String REQUIRED_GROUP_PROPERTY = "requiredGroup";    private static final String FORBIDDEN_GROUP_PROPERTY = "forbiddenGroup";    private static final String USE_TIMEOUT_PROPERTY = "useTimeout";    private static final String DEFAULT_USER_NAME = "admin";    private static final String DEFAULT_PASSWORD = "admin";    static private final String logServiceName = LogService.class.getName();    static private final String consoleServiceName = ConsoleService.class            .getName();    static private final String TELNET_INTERRUPT = "\377\364";    private ConsoleService sConsole = null;    private AcceptThread tAccept = null;    BundleContext bc;    boolean quit = false;    Integer port;    InetAddress address;    Boolean login;    Boolean requireUM;    Boolean exitOnLogout;    Boolean useTimeout;    String requiredGroup;    String forbiddenGroup;    /*---------------------------------------------------------------------------*     *			  BundleActivator implementation     *---------------------------------------------------------------------------*/    // ==================== start ====================    /**     * * Called by the framework when this bundle is started. * *     *      * @param bc     *            Bundle context. *     */    public void start(BundleContext bc) {        this.bc = bc;        // Get config        Hashtable p = new Hashtable();        p.put(Constants.SERVICE_PID, getClass().getName());        bc.registerService(ManagedService.class.getName(), this, p);    }    // ==================== stop ====================    /**     * * Called by the framework when this bundle is stopped. * *     *      * @param bc     *            Bundle context.     */    synchronized public void stop(BundleContext bc) {        stopAccept();    }    /*---------------------------------------------------------------------------*     *			  ManagedService implementation     *---------------------------------------------------------------------------*/    // ==================== updated ====================    /**     * * Called by CM when it got this bundles configuration. * *     *      * @param bc     *            Bundle context.     */    public synchronized void updated(Dictionary cfg)            throws ConfigurationException, IllegalArgumentException {        if (cfg == null) {            stopAccept();            cfg = new Hashtable();        }        Integer oldport = port;        InetAddress oldAddress = address;        Boolean oldUseTimeout = useTimeout;        try {            port = (Integer) cfg.get(PORT_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + PORT_PROPERTY);        }        String addressStr = null;        try {            addressStr = (String) cfg.get(ADDRESS_PROPERTY);            if (addressStr != null) {                addressStr = addressStr.trim();                if ("".equals(addressStr))                    address = null;                else                    address = InetAddress.getByName(addressStr);            } else {                address = null;            }        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + ADDRESS_PROPERTY);        } catch (UnknownHostException e) {            throw new IllegalArgumentException("Cannot resolve "                    + ADDRESS_PROPERTY + ": " + addressStr);        }        try {            login = (Boolean) cfg.get(LOGIN_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + LOGIN_PROPERTY);        }        try {            requireUM = (Boolean) cfg.get(UM_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for " + UM_PROPERTY);        }        try {            exitOnLogout = (Boolean) cfg.get(EXIT_ON_LOGOUT_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + EXIT_ON_LOGOUT_PROPERTY);        }        try {            requiredGroup = (String) cfg.get(REQUIRED_GROUP_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + REQUIRED_GROUP_PROPERTY);        }        try {            forbiddenGroup = (String) cfg.get(FORBIDDEN_GROUP_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + FORBIDDEN_GROUP_PROPERTY);        }        try {            useTimeout = (Boolean) cfg.get(USE_TIMEOUT_PROPERTY);        } catch (ClassCastException e) {            throw new IllegalArgumentException("Wrong type for "                    + USE_TIMEOUT_PROPERTY);        }        if (port == null) {            port = new Integer(8999);        }        if (port.intValue() < 0 || port.intValue() > 65535) {            log(LogService.LOG_ERROR, "Illegal port number");            throw new IllegalArgumentException("Illegal port number");        }        if (login == null) {            login = new Boolean(true);        }        if (requireUM == null) {            requireUM = new Boolean(false);        }        if (exitOnLogout == null) {            exitOnLogout = new Boolean(false);        }        if ("".equals(requiredGroup)) {            requiredGroup = null;        }        if ("".equals(forbiddenGroup)) {            forbiddenGroup = null;        }        if (useTimeout == null) {            useTimeout = new Boolean(true);        }        boolean addressChanged = (oldAddress == null && address != null)                || (oldAddress != null && !oldAddress.equals(address));        boolean useTimeoutChanged = (oldUseTimeout == null && !useTimeout                .booleanValue())                || (oldUseTimeout != null && !oldUseTimeout.equals(useTimeout));        if (tAccept != null) {            if (port.equals(oldport) && !addressChanged && !useTimeoutChanged) {                // No need to restart                return;            }            stopAccept();        } else {            // Get console service            srefConsole = bc.getServiceReference(consoleServiceName);            sConsole = (ConsoleService) bc.getService(srefConsole);            if (sConsole == null) {                log(LogService.LOG_ERROR, "Failed to get ConsoleService");                throw new ConfigurationException(null,                        "Failed to get ConsoleService");            }        }        tAccept = new AcceptThread(sConsole);        tAccept.setDaemon(false);        quit = false;        tAccept.start();    }    /**     * * Utility to stop accept thread. *     */    private void stopAccept() {        if (tAccept != null) {            tAccept.quit();            try {                tAccept.join();            } catch (InterruptedException ignore) {            }            tAccept = null;        }    }    // ==================== log ====================    /**     * * Utility method used for logging. * *     *      * @param level     *            Log level *     * @param msg     *            Log message     */    void log(int level, String msg) {        try {            ServiceReference srLog = bc.getServiceReference(logServiceName);            if (srLog != null) {

⌨️ 快捷键说明

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