📄 main.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.framework;import java.io.*;import java.util.Properties;import java.util.Vector;import java.util.Enumeration;import java.util.StringTokenizer;import java.security.*;import java.net.URL;import java.net.URLConnection;import java.net.HttpURLConnection;import java.net.MalformedURLException;import org.osgi.framework.*;import java.lang.reflect.Constructor;/** * This is the main startup code for the framework and enables * basic operations as install, start, stop, uninstall * and update. * * @author Jan Stein, Erik Wistrand */public class Main { static Framework framework; static long bootMgr = 0; // Verbosity level of printouts. 0 is low. static int verbosity = 0; // Will be filled in from manifest entry during startup static String version = "<unknown>"; // Top directory (including any trailing /) static String topDir = ""; // Xargs to use for FW init static String defaultXArgsInit = "init.xargs"; // Xargs to use for FW init static final String defaultXArgsInit2 = "remote-init.xargs"; // Xargs to use for FW restart static final String defaultXArgsStart = "restart.xargs"; // Set to true if JVM is started without any arguments static boolean bZeroArgs = false; // will be initialized by main() - up for anyone for grabbing public static String bootText = ""; static final String JARDIR_PROP = "org.knopflerfish.gosg.jars"; static final String JARDIR_DEFAULT = "file:"; static final String FWDIR_PROP = "org.osgi.framework.dir"; static final String FWDIR_DEFAULT = "fwdir"; static final String CMDIR_PROP = "org.knopflerfish.bundle.cm.store"; static final String CMDIR_DEFAULT = "cmdir"; static final String VERBOSITY_PROP = "org.knopflerfish.verbosity"; static final String VERBOSITY_DEFAULT = "0"; static final String XARGS_DEFAULT = "default"; static final String PRODVERSION_PROP = "org.knopflerfish.prodver"; static final String EXITONSHUTDOWN_PROP = "org.knopflerfish.framework.exitonshutdown"; /** * Help class for starting the OSGi framework. */ public static void main(String[] args) { try { verbosity = Integer.parseInt(System.getProperty(VERBOSITY_PROP, VERBOSITY_DEFAULT)); } catch (Exception ignored) { } version = readVersion(); bootText = "Knopflerfish OSGi framework, version " + version + "\n" + "Copyright 2003-2004 Knopflerfish. All Rights Reserved.\n\n" + "See http://www.knopflerfish.org for more information."; System.out.println(bootText); // Check if framework is started with no args at all. // This typically happens when starting with "java -jar framework.jar" // or similar (e.g by double-clicking on framework.jar) bZeroArgs = (args.length == 0); // Check if there is a default xargs file // Uses "init" variant if fwdir exists, otherwise // uses "restart" variant. String xargsPath = getDefaultXArgs(args); if(xargsPath != null) { if(bZeroArgs) { args = new String[] {"-xargs", xargsPath}; } else if(args.length == 1 && "-init".equals(args[0])) { args = new String[] {"-init", "-xargs", xargsPath}; } } // expand all -xargs options args = expandArgs(args); if(verbosity > 5) { for(int i = 0; i < args.length; i++) { println("argv[" + i + "]=" + args[i], 5); } } // redo this since it might have changed try { verbosity = Integer.parseInt(System.getProperty(VERBOSITY_PROP, VERBOSITY_DEFAULT)); } catch (Exception ignored) { } if(bZeroArgs) { // Make sure we have a minimal setup of args args = sanityArgs(args); } // Set default values to something reasonable if not supplied // on the command line (or in xargs) setDefaultSysProps(); String[] base = getJarBase(); // Handle -init option before we create the FW, otherwise // we might shoot ourself in the foot. Hard. for(int i = 0; i < args.length; i++) { if("-init".equals(args[i])) { doInit(); } } try { framework = new Framework(new Main()); } catch (Exception e) { e.printStackTrace(); error("New Framework failed!"); } // Save these for possible restart() initArgs = args; initOffset = 0; initBase = base; handleArgs(args, initOffset, base); } static String[] initArgs = null; static int initOffset = 0; static String[] initBase = null; static void doInit() { String d = System.getProperty(FWDIR_PROP); FileTree dir = (d != null) ? new FileTree(d) : null; if (dir != null) { if(dir.exists()) { boolean bOK = dir.delete(); if(bOK) { println("Removed existing fwdir " + dir.getAbsolutePath(), 0); } else { println("Failed to remove existing fwdir " + dir.getAbsolutePath(), 0); } } } } static String[] getJarBase() { String jars = System.getProperty(JARDIR_PROP, JARDIR_DEFAULT); String[] base = Util.splitwords(jars, ";", '\"'); for (int i=0; i<base.length; i++) { try { base[i] = new URL(base[i]).toString(); } catch (Exception ignored) { } println("jar base[" + i + "]=" + base[i], 3); } return base; } /** * Handle arg line options. * * @param args argument line * @param startOffset index to start from in argv */ private static void handleArgs(String[] args, int startOffset, String[] base) { for (int i = startOffset; i < args.length; i++) { try { if ("-exit".equals(args[i])) { println("Exit.", 0); System.exit(0); } else if ("-init".equals(args[i])) { // This is done in an earlier pass, otherwise we // shoot the FW in the foot } else if ("-version".equals(args[i])) { printResource("/tstamp"); printResource("/revision"); System.exit(0); } else if ("-help".equals(args[i])) { printResource("/help.txt"); System.exit(0); } else if ("-readme".equals(args[i])) { printResource("/readme.txt"); System.exit(0); } else if ("-jvminfo".equals(args[i])) { printJVMInfo(); System.exit(0); } else if ("-install".equals(args[i])) { if (i+1 < args.length) { String bundle = args[i+1]; long id = framework.installBundle(completeLocation(base,bundle), null); println("Installed: " + framework.getBundleLocation(id) + " (id#" + id + ")", 0); i++; } else { error("No URL for install command"); } } else if ("-istart".equals(args[i])) { if (i+1 < args.length) { String bundle = args[i+1]; long id = framework.installBundle(completeLocation(base,bundle), null); framework.startBundle(id); println("Installed and started: " + framework.getBundleLocation(id) + " (id#" + id + ")", 0); i++; } else { error("No URL for install command"); } } else if ("-launch".equals(args[i])) { if (i+1 < args.length && !args[i+1].startsWith("-")) { bootMgr = Long.parseLong(args[i+1]); framework.launch(bootMgr); i++; } else { framework.launch(0); } println("Framework launched", 0); } else if ("-shutdown".equals(args[i])) { framework.shutdown(); println("Framework shutdown", 0); } else if ("-sleep".equals(args[i])) { if (i+1 < args.length) { long t = Long.parseLong(args[i+1]); try { println("Sleeping...", 0); Thread.sleep(t * 1000); } catch (InterruptedException e) { error("Sleep interrupted."); } i++; } else { error("No time for sleep command"); } } else if ("-start".equals(args[i])) { if (i+1 < args.length) { long id = getBundleID(base,args[i+1]); framework.startBundle(id); println("Started: " + framework.getBundleLocation(id) + " (id#" + id + ")", 0); i++; } else { error("No ID for start command"); } } else if ("-stop".equals(args[i])) { if (i+1 < args.length) { long id = getBundleID(base,args[i+1]); framework.stopBundle(id); println("Stopped: " + framework.getBundleLocation(id) + " (id#" + id + ")", 0); i++; } else { error("No ID for stop command"); } } else if ("-uninstall".equals(args[i])) { if (i+1 < args.length) { long id = getBundleID(base,args[i+1]); String loc = framework.getBundleLocation(id); framework.uninstallBundle(id); println("Uninstalled: " + loc + " (id#" + id + ")", 0); i++; } else { error("No id for uninstall command"); } } else if ("-update".equals(args[i])) { if (i+1 < args.length) { long[] ids = null; if("ALL".equals(args[i+1])) { Bundle[] bl = framework.getSystemBundleContext().getBundles(); ids = new long[bl.length]; for(int n = 0; n < bl.length; n++) { ids[n] = bl[n].getBundleId(); } } else { ids = new long[] { getBundleID(base,args[i+1]) }; } for(int n = 0; n < ids.length; n++) { long id = ids[n]; if(id != 0) { framework.updateBundle(id); println("Updated: " + framework.getBundleLocation(id) + " (id#" + id + ")", 0); } } i++; } else { error("No id for update command"); } } else if ("-initlevel".equals(args[i])) { if (i+1 < args.length) { int n = Integer.parseInt(args[i+1]); if(framework.startLevelService != null) { framework.startLevelService.setInitialBundleStartLevel(n); } else { println("No start level service - ignoring init bundle level " + n, 0); } i++; } else { error("No integer level for initlevel command"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -