org.eclipse.osgi_3.7.2.v20120110-1415

       Method register = factory.getClass().getMethod("register", new Class[] { Object.class });
          register.invoke(factory, new Object[] { shf });
        }
        catch (NoSuchMethodException localNoSuchMethodException)
        {
          shf.setParentFactory(factory);
          factory = shf;
        }
      }
      factoryField.set(null, null);
      
      resetURLStreamHandlers();
      URL.setURLStreamHandlerFactory(factory);
    }
  }
  
  private void uninstallURLStreamHandlerFactory()
  {
    try
    {
      Field factoryField = getField(URL.class, URLStreamHandlerFactory.class, false);
      if (factoryField == null) {
        return;
      }
      Object lock = getURLStreamHandlerFactoryLock();
      synchronized (lock)
      {
        URLStreamHandlerFactory factory = (URLStreamHandlerFactory)factoryField.get(null);
        if (factory == streamHandlerFactory)
        {
          factory = (URLStreamHandlerFactory)streamHandlerFactory.designateSuccessor();
        }
        else
        {
          Method unregister = factory.getClass().getMethod("unregister", new Class[] { Object.class });
          unregister.invoke(factory, new Object[] { streamHandlerFactory });
        }
        factoryField.set(null, null);
        
        resetURLStreamHandlers();
        if (factory != null) {
          URL.setURLStreamHandlerFactory(factory);
        }
      }
      return;
    }
    catch (Exception localException) {}
  }
  
  private static Object getURLStreamHandlerFactoryLock()
    throws IllegalAccessException
  {
    Object lock;
    try
    {
      Field streamHandlerLockField = URL.class.getDeclaredField("streamHandlerLock");
      streamHandlerLockField.setAccessible(true);
      lock = streamHandlerLockField.get(null);
    }
    catch (NoSuchFieldException localNoSuchFieldException)
    {
      Object lock;
      lock = URL.class;
    }
    return lock;
  }
  
  private static void resetURLStreamHandlers()
    throws IllegalAccessException
  {
    Field handlersField = getField(URL.class, Hashtable.class, false);
    if (handlersField != null)
    {
      Hashtable<?, ?> handlers = (Hashtable)handlersField.get(null);
      if (handlers != null) {
        handlers.clear();
      }
    }
  }
  
  public void run()
  {
    synchronized (this)
    {
      while (active) {
        try
        {
          wait(1000L);
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
  }
  
  void setForcedRestart(boolean forcedRestart)
  {
    this.forcedRestart = forcedRestart;
  }
  
  boolean isForcedRestart()
  {
    return forcedRestart;
  }
  
  public FrameworkEvent waitForStop(long timeout)
    throws InterruptedException
  {
    boolean waitForEver = timeout == 0L;
    long start = System.currentTimeMillis();
    long timeLeft = timeout;
    synchronized (this)
    {
      FrameworkEvent[] event = shutdownEvent;
      while ((event != null) && (event[0] == null))
      {
        wait(timeLeft);
        if (!waitForEver)
        {
          timeLeft = start + timeout - System.currentTimeMillis();
          if (timeLeft <= 0L) {
            break;
          }
        }
      }
      if ((event == null) || (event[0] == null)) {
        return new FrameworkEvent(512, systemBundle, null);
      }
      return event[0];
    }
  }
  
  public boolean isServiceAssignableTo(Bundle registrant, Bundle client, String className, Class<?> serviceClass)
  {
    AbstractBundle consumer = (AbstractBundle)client;
    if (consumer.isFragment()) {
      return false;
    }
    AbstractBundle producer = (AbstractBundle)registrant;
    if (consumer == producer) {
      return true;
    }
    String pkgName = BundleLoader.getPackageName(className);
    if (pkgName.startsWith("java.")) {
      return true;
    }
    BundleLoader producerBL = producer.getBundleLoader();
    if (producerBL == null) {
      return false;
    }
    BundleLoader consumerBL = consumer.getBundleLoader();
    if (consumerBL == null) {
      return false;
    }
    PackageSource consumerSource = consumerBL.getPackageSource(pkgName);
    if (consumerSource == null) {
      return true;
    }
    if (isBootDelegationPackage(pkgName))
    {
      SystemBundleLoader systemLoader = (SystemBundleLoader)systemBundle.getBundleLoader();
      if (systemLoader.isEEPackage(pkgName)) {
        return true;
      }
    }
    PackageSource producerSource = producerBL.getPackageSource(pkgName);
    if (producerSource == null)
    {
      if ((serviceClass != null) && (ServiceFactory.class.isAssignableFrom(serviceClass)))
      {
        Bundle bundle = packageAdmin.getBundle(serviceClass);
        if ((bundle != null) && (bundle != registrant)) {
          return true;
        }
      }
      producerSource = getPackageSource(serviceClass, pkgName);
      if (producerSource == null) {
        return false;
      }
    }
    return producerSource.hasCommonSource(consumerSource);
  }
  
  private PackageSource getPackageSource(Class<?> serviceClass, String pkgName)
  {
    if (serviceClass == null) {
      return null;
    }
    AbstractBundle serviceBundle = (AbstractBundle)packageAdmin.getBundle(serviceClass);
    if (serviceBundle == null) {
      return null;
    }
    BundleLoader producerBL = serviceBundle.getBundleLoader();
    if (producerBL == null) {
      return null;
    }
    PackageSource producerSource = producerBL.getPackageSource(pkgName);
    if (producerSource != null) {
      return producerSource;
    }
    Class[] interfaces = serviceClass.getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
    {
      producerSource = getPackageSource(interfaces[i], pkgName);
      if (producerSource != null) {
        return producerSource;
      }
    }
    return getPackageSource(serviceClass.getSuperclass(), pkgName);
  }
  
  public boolean isBootDelegationPackage(String name)
  {
    if (bootDelegateAll) {
      return true;
    }
    if (bootDelegation != null) {
      for (int i = 0; i < bootDelegation.length; i++) {
        if (name.equals(bootDelegation[i])) {
          return true;
        }
      }
    }
    if (bootDelegationStems != null) {
      for (int i = 0; i < bootDelegationStems.length; i++) {
        if (name.startsWith(bootDelegationStems[i])) {
          return true;
        }
      }
    }
    return false;
  }
  
  SignedContentFactory getSignedContentFactory()
  {
    ServiceTracker<SignedContentFactory, SignedContentFactory> currentTracker = signedContentFactory;
    return currentTracker == null ? null : (SignedContentFactory)currentTracker.getService();
  }
  
  ContextFinder getContextFinder()
  {
    return contextFinder;
  }
  
  public boolean isRefreshDuplicateBSNAllowed()
  {
    return allowRefreshDuplicateBSN;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.framework.internal.core.Framework
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.framework.internal.core;

import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;

public class FrameworkCommandInterpreter
  implements CommandInterpreter
{
  private static final String WS_DELIM = " \t\n\r\f";
  private StringTokenizer tok;
  private CommandProvider[] commandProviders;
  private FrameworkConsole con;
  private PrintWriter out;
  private String tab = "\t";
  private String newline = "\r\n";
  private boolean firstCommand = true;
  protected static int maxLineCount;
  protected int currentLineCount;
  
  public FrameworkCommandInterpreter(String cmdline, CommandProvider[] commandProviders, FrameworkConsole con)
  {
    tok = new StringTokenizer(cmdline);
    this.commandProviders = commandProviders;
    this.con = con;
    out = con.getWriter();
  }
  
  public String nextArgument()
  {
    if ((tok == null) || (!tok.hasMoreElements())) {
      return null;
    }
    return consumeQuotes(tok.nextToken());
  }
  
  private String consumeQuotes(String arg)
  {
    if ((!arg.startsWith("\"")) && (!arg.startsWith("'"))) {
      return arg;
    }
    String quote = arg.substring(0, 1);
    if (arg.endsWith(quote))
    {
      if (arg.length() >= 2) {
        return arg.substring(1, arg.length() - 1);
      }
      return "";
    }
    try
    {
      arg = arg.substring(1) + tok.nextToken(quote);
    }
    catch (NoSuchElementException e)
    {
      printStackTrace(e);
      return "";
    }
    try
    {
      tok.nextToken(" \t\n\r\f");
    }
    catch (NoSuchElementException localNoSuchElementException1) {}
    return arg;
  }
  
  public Object execute(String cmd)
  {
    if (!firstCommand) {
      return innerExecute(cmd);
    }
    firstCommand = false;
    resetLineCount();
    Object retval = null;
    if (cmd.equalsIgnoreCase("more"))
    {
      try
      {
        _more();
      }
      catch (Exception e)
      {
        printStackTrace(e);
      }
      return retval;
    }
    if (cmd.equalsIgnoreCase("disconnect"))
    {
      try
      {
        _disconnect();
      }
      catch (Exception e)
      {
        printStackTrace(e);
      }
      return retval;
    }
    if ((cmd.equalsIgnoreCase("help")) && (!tok.hasMoreElements()))
    {
      displayAllHelp();
      return retval;
    }
    Class[] parameterTypes = { CommandInterpreter.class };
    Object[] parameters = { this };
    boolean executed = false;
    int size = commandProviders.length;
    if ((cmd.equalsIgnoreCase("help")) && (tok.hasMoreElements()))
    {
      String commandName = nextArgument();
      
      String builtinHelp = getHelp(commandName);
      if (builtinHelp != null)
      {
        print(builtinHelp);
        return builtinHelp;
      }
      for (int i = 0; i < size; i++)
      {
        tok = new StringTokenizer(commandName);
        boolean isException = false;
        Object target = commandProviders[i];
        Method method = null;
        try
        {
          method = target.getClass().getMethod("_" + cmd, parameterTypes);
          retval = method.invoke(target, parameters);
        }
        catch (NoSuchMethodException localNoSuchMethodException1)
        {
          isException = true;
        }
        catch (InvocationTargetException e)
        {
          printStackTrace(e.getTargetException());
          isException = true;
        }
        catch (Exception ee)
        {
          printStackTrace(ee);
        }
        if (retval != null)
        {
          if ((retval instanceof Boolean))
          {
            executed = (executed) || (((Boolean)retval).booleanValue());
          }
          else
          {
            if ((retval instanceof String))
            {
              print(retval);
              return retval;
            }
            executed = true;
          }
        }
        else {
          executed = (executed) || (!isException);
        }
      }
    }
    else
    {
      for (int i = 0; (!executed) && (i < size); i++) {
        try
        {
          Object target = commandProviders[i];
          Method method = target.getClass().getMethod("_" + cmd, parameterTypes);
          retval = method.invoke(target, parameters);
          executed = true;
        }
        catch (NoSuchMethodException localNoSuchMethodException2) {}catch (InvocationTargetException ite)
        {
          executed = true;
          printStackTrace(ite.getTargetException());
        }
        catch (Exception ee)
        {
          executed = true;
          printStackTrace(ee);
        }
      }
    }
    if (!executed) {
      displayAllHelp();
    }
    return retval;
  }
  
  private void displayAllHelp()
  {
    int size = commandProviders.length;
    for (int i = 0; i < size; i++) {
      try
      {
        CommandProvider commandProvider = commandProviders[i];
        out.print(commandProvider.getHelp());
        out.flush();
      }
      catch (Exception ee)
      {
        printStackTrace(ee);
      }
    }
    out.print(getHelp(null));
    out.flush();
  }
  
  private Object innerExecute(String cmd)
  {
    if ((cmd != null) && (cmd.length() > 0))
    {
      CommandInterpreter intcp = new FrameworkCommandInterpreter(cmd, commandProviders, con);
      String command = intcp.nextArgument();
      if (command != null) {
        return intcp.execute(command);
      }
    }
    return null;
  }
  
  private int getMaximumLinesToScroll()
  {
    return maxLineCount;
  }
  
  private void setMaximumLinesToScroll(int lines)
  {
    if (lines < 0) {
      throw new IllegalArgumentException(ConsoleMsg.CONSOLE_LINES_TO_SCROLL_NEGATIVE_ERROR);
    }
    maxLineCount = lines;
  }
  
  private void resetLineCount()
  {
    currentLineCount = 0;
  }
  
  private void printline(Object o)
  {
    print(o + newline);
  }
  
  public void print(Object o)
  {
    synchronized (out)
    {
      check4More();
      out.print(o);
      out.flush();
    }
  }
  
  public void println()
  {
    println("");
  }
  
  public void printStackTrace(Throwable t)
  {
    t.printStackTrace(out);
    
    Method[] methods = t.getClass().getMethods();
    
    int size = methods.length;
    Class<Throwable> throwable = Throwable.class;
    for (int i = 0; i < size; i++)
    {
      Method method = methods[i];
      if ((Modifier.isPublic(method.getModifiers())) && (method.getName().startsWith("get")) && (throwable.isAssignableFrom(method.getReturnType())) && (method.getParameterTypes().length == 0)) {
        try
        {
          Throwable nested = (Throwable)method.invoke(t, null);
          if ((nested != null) && (nested != t))
          {
            out.println(ConsoleMsg.CONSOLE_NESTED_EXCEPTION);
            printStackTrace(nested);
          }
        }
        catch (IllegalAccessException localIllegalAccessException) {}catch (InvocationTargetException localInvocationTargetException) {}
      }
    }
  }
  
  public void println(Object o)
  {
    if (o == null) {
      return;
    }
    synchronized (out)
    {
      check4More();
      printline(o);
      currentLineCount += 1;
      currentLineCount += o.toString().length() / 80;
    }
  }
  
  public void printDictionary(Dictionary<?, ?> dic, String title)
  {
    if (dic == null) {
      return;
    }
    int count = dic.size();
    String[] keys = new String[count];
    Enumeration<?> keysEnum = dic.keys();
    int i = 0;
    while (keysEnum.hasMoreElements()) {
      keys[(i++)] = ((String)keysEnum.nextElement());
    }
    Util.sortByString(keys);
    if (title != null) {
      println(title);
    }
    for (i = 0; i < count; i++) {
      println(" " + keys[i] + " = " + dic.get(keys[i]));
    }
    println();
  }
  
  /* Error */
  public void printBundleResource(org.osgi.framework.Bundle bundle, String resource)
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_3
    //   2: aload_1
    //   3: aload_2
    //   4: invokeinterface 556 2 0
    //   9: astore_3
    //   10: aload_3
    //   11: ifnull +113 -> 124
    //   14: aload_0
    //   15: aload_2
    //   16: invokevirtual 535	org/eclipse/osgi/framework/internal/core/FrameworkCommandInterpreter:println	(Ljava/lang/Object;)V
    //   19: aload_3
    //   20: invokevirtual 517	java/net/URL:openStream	()Ljava/io/InputStream;
    //   23: astore 4
    //   25: sipush 1024
    //   28: newarray <illegal type>
    //   30: astore 5
    //   32: iconst_0
    //   33: istore 6
    //   35: goto +19 -> 54
    //   38: aload_0
    //   39: new 246	java/lang/String
    //   42: dup
    //   43: aload 5
    //   45: iconst_0
    //   46: iload 6
    //   48: invokespecial 492	java/lang/String:<init>	([BII)V
    //   51: invokevirtual 533	org/eclipse/osgi/framework/internal/core/FrameworkCommandInterpreter:print	(Ljava/lang/Object;)V
    //   54: aload 4
    //   56: aload 5
    //   58: invokevirtual 471	java/io/InputStream:read	([B)I
    //   61: dup
    //   62: istore 6
    //   64: iconst_m1
    //   65: if_icmpne -27 -> 38
    //   68: goto +22 -> 90
    //   71: astore 7
    //   73: aload 4
    //   75: ifnull +12 -> 87
    //   78: aload 4
    //   80: invokevirtual 470	java/io/InputStream:close	()V
    //   83: goto +4 -> 87
    //   86: pop
    //   87: aload 7
    //   89: athrow
    //   90: aload 4
    //   92: ifnull +47 -> 139
    //   95: aload 4
    //   97: invokevirtual 470	java/io/InputStream:close	()V
    //   100: goto +39 -> 139
    //   103: pop
    //   104: goto +35 -> 139
    //   107: pop
    //   108: getstatic 439	java/lang/System:err	Ljava/io/PrintStream;
    //   111: getstatic 444	org/eclipse/osgi/framework/internal/core/ConsoleMsg:CONSOLE_ERROR_READING_RESOURCE	Ljava/lang/String;
    //   114: aload_2
    //   115: invokestatic 549	org/eclipse/osgi/util/NLS:bind	(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;
    //   118: invokevirtual 472	java/io/PrintStream:println	(Ljava/lang/String;)V
    //   121: goto +18 -> 139
    //   124: aload_0
    //   125: getstatic 456	org/eclipse/osgi/framework/internal/core/ConsoleMsg:CONSOLE_RESOURCE_NOT_IN_BUNDLE	Ljava/lang/String;
    //   128: aload_2
    //   129: aload_1
    //   130: invokevirtual 489	java/lang/Object:toString	()Ljava/lang/String;
    //   133: invokestatic 550	org/eclipse/osgi/util/NLS:bind	(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/String;
    //   136: invokevirtual 535	org/eclipse/osgi/framework/internal/core/FrameworkCommandInterpreter:println	(Ljava/lang/Object;)V
    //   139: return
    // Line number table:
    //   Java source line #414	-> byte code offset #0
    //   Java source line #415	-> byte code offset #2
    //   Java source line #416	-> byte code offset #10
    //   Java source line #418	-> byte code offset #14
    //   Java source line #419	-> byte code offset #19
    //   Java source line #420	-> byte code offset #25
    //   Java source line #421	-> byte code offset #32
    //   Java source line #423	-> byte code offset #35
    //   Java source line #424	-> byte code offset #38
    //   Java source line #423	-> byte code offset #54
    //   Java source line #425	-> byte code offset #71
    //   Java source line #426	-> byte code offset #73
    //   Java source line #428	-> byte code offset #78
    //   Java source line #429	-> byte code offset #86
    //   Java source line #433	-> byte code offset #87
    //   Java source line #426	-> byte code offset #90
    //   Java source line #428	-> byte code offset #95
    //   Java source line #429	-> byte code offset #103
    //   Java source line #434	-> byte code offset #107
    //   Java source line #435	-> byte code offset #108
    //   Java source line #438	-> byte code offset #124
    //   Java source line #440	-> byte code offset #139
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	140	0	this	FrameworkCommandInterpreter
    //   0	140	1	bundle	org.osgi.framework.Bundle
    //   0	140	2	resource	String
    //   1	19	3	entry	java.net.URL
    //   23	73	4	in	java.io.InputStream
    //   30	27	5	buffer	byte[]
    //   33	30	6	read	int
    //   71	17	7	localObject	Object
    //   86	1	8	localIOException1	java.io.IOException
    //   103	1	9	localIOException2	java.io.IOException
    //   107	1	10	localException	Exception
    // Exception table:
    //   from	to	target	type
    //   35	71	71	finally
    //   78	83	86	java/io/IOException
    //   95	100	103	java/io/IOException
    //   14	104	107	java/lang/Exception
  }
  
  private void check4More()
  {
    int max = getMaximumLinesToScroll();
    if ((max > 0) && 
      (currentLineCount >= max))
    {
      out.print(ConsoleMsg.CONSOLE_MORE);
      out.flush();
      con.getInput();
      resetLineCount();
    }
  }
  
  public String getHelp(String commandName)
  {
    boolean all = commandName == null;
    StringBuffer help = new StringBuffer(256);
    if (all)
    {
      help.append(ConsoleMsg.CONSOLE_HELP_CONTROLLING_CONSOLE_HEADING);
      help.append(newline);
    }
    if ((all) || ("more".equals(commandName)))
    {
      help.append(tab);
      help.append("more - ");
      help.append(ConsoleMsg.CONSOLE_HELP_MORE);
      help.append(newline);
    }
    if ((all) || ("disconnect".equals(commandName)))
    {
      help.append(tab);
      help.append("disconnect - ");
      help.append(ConsoleMsg.CONSOLE_HELP_DISCONNECT);
      help.append(newline);
    }
    if ((all) || ("help".equals(commandName)))
    {
      help.append(tab);
      help.append("help <commmand> - ");
      help.append(ConsoleMsg.CONSOLE_HELP_HELP_COMMAND_DESCRIPTION);
      help.append(newline);
    }
    return help.length() == 0 ? null : help.toString();
  }
  
  public void _more()
    throws Exception
  {
    if (confirm(ConsoleMsg.CONSOLE_CONFIRM_MORE, true))
    {
      int lines = prompt(newline + ConsoleMsg.CONSOLE_MORE_ENTER_LINES, 24);
      setMaximumLinesToScroll(lines);
    }
    else
    {
      setMaximumLinesToScroll(0);
    }
  }
  
  private void _disconnect()
    throws Exception
  {
    if (confirm(ConsoleMsg.CONSOLE_CONFIRM_DISCONNECT, true)) {
      con.shutdown();
    }
  }
  
  protected boolean confirm(String string, boolean defaultAnswer)
  {
    synchronized (out)
    {
      if (string.length() > 0) {
        print(string);
      } else {
        print(ConsoleMsg.CONSOLE_CONFIRM);
      }
      print(" (" + ConsoleMsg.CONSOLE_CONFIRM_VALUES);
      if (defaultAnswer) {
        print(ConsoleMsg.CONSOLE_Y + ") ");
      } else {
        print(ConsoleMsg.CONSOLE_N + ") ");
      }
    }
    String input = con.getInput();
    resetLineCount();
    if (input.length() == 0) {
      return defaultAnswer;
    }
    return input.toLowerCase().charAt(0) == ConsoleMsg.CONSOLE_Y.charAt(0);
  }
  
  protected String prompt(String string, String defaultAnswer)
  {
    if (string.length() > 0) {
      if (defaultAnswer.length() > 0)
      {
        StringBuffer buf = new StringBuffer(256);
        buf.append(string);
        buf.append(" ");
        buf.append(ConsoleMsg.CONSOLE_PROMPT_DEFAULT);
        buf.append("=");
        buf.append(defaultAnswer);
        buf.append(") ");
        print(buf.toString());
      }
      else
      {
        print(string);
      }
    }
    String input = con.getInput();
    resetLineCount();
    if (input.length() > 0) {
      return input;
    }
    return defaultAnswer;
  }
  
  protected int prompt(String string, int defaultAnswer)
  {
    Integer i = new Integer(defaultAnswer);
    for (int j = 0; j < 3; j++)
    {
      String s = prompt(string, i.toString());
      try
      {
        int answer = Integer.parseInt(s);
        if (answer >= 0) {
          return answer;
        }
      }
      catch (NumberFormatException localNumberFormatException)
      {
        println(ConsoleMsg.CONSOLE_INVALID_INPUT);
      }
    }
    println(ConsoleMsg.CONSOLE_TOO_MUCH_INVALID_INPUT);
    return defaultAnswer;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.framework.internal.core;

import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.eclipse.osgi.internal.permadmin.SecurityAdmin;
import org.eclipse.osgi.internal.profile.Profile;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.SynchronousBundleListener;
import org.osgi.framework.Version;
import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.service.startlevel.StartLevel;

public class FrameworkCommandProvider
  implements CommandProvider, SynchronousBundleListener
{
  private final Framework framework;
  private final BundleContext context;
  private final StartLevelManager slImpl;
  private final SecurityAdmin securityAdmin;
  private ServiceRegistration<?> providerReg;
  private static final String tab = "\t";
  private static final String newline = "\r\n";
  private final List<Bundle> lazyActivation = new ArrayList();
  private Map<String, String[]> commandsHelp = null;
  private Map<String, String[]> commandGroups = null;
  static Class class$2;
  
  public FrameworkCommandProvider(Framework framework)
  {
    this.framework = framework;
    context = systemBundle.getContext();
    slImpl = startLevelManager;
    securityAdmin = securityAdmin;
  }
  
  void start()
  {
    Dictionary<String, Object> props = new Hashtable();
    props.put("service.ranking", new Integer(Integer.MAX_VALUE));
    providerReg = context.registerService(CommandProvider.class.getName(), this, props);
    context.addBundleListener(this);
  }
  
  void stop()
  {
    context.removeBundleListener(this);
    if (providerReg != null) {
      providerReg.unregister();
    }
  }
  
  public String getHelp()
  {
    return getHelp(null);
  }
  
  private String getHelp(String commandName)
  {
    StringBuffer help = new StringBuffer(1024);
    if (commandsHelp == null) {
      initializeCommandsHelp();
    }
    if (commandGroups == null) {
      initializeCommandGroups();
    }
    if (commandName != null)
    {
      if (commandsHelp.containsKey(commandName)) {
        addCommand(commandName, (String[])commandsHelp.get(commandName), help);
      }
      return help.toString();
    }
    int j;
    int i;
    for (Iterator localIterator = commandGroups.entrySet().iterator(); localIterator.hasNext(); i < j)
    {
      Map.Entry<String, String[]> groupEntry = (Map.Entry)localIterator.next();
      addHeader((String)groupEntry.getKey(), help);
      String[] arrayOfString;
      j = (arrayOfString = (String[])groupEntry.getValue()).length;i = 0; continue;String command = arrayOfString[i];
      addCommand(command, (String[])commandsHelp.get(command), help);i++;
    }
    return help.toString();
  }
  
  private void initializeCommandsHelp()
  {
    commandsHelp = new HashMap();
    
    commandsHelp.put("launch", new String[] { ConsoleMsg.CONSOLE_HELP_LAUNCH_COMMAND_DESCRIPTION });
    commandsHelp.put("shutdown", new String[] { ConsoleMsg.CONSOLE_HELP_SHUTDOWN_COMMAND_DESCRIPTION });
    commandsHelp.put("close", new String[] { ConsoleMsg.CONSOLE_HELP_CLOSE_COMMAND_DESCRIPTION });
    commandsHelp.put("exit", new String[] { ConsoleMsg.CONSOLE_HELP_EXIT_COMMAND_DESCRIPTION });
    commandsHelp.put("init", new String[] { ConsoleMsg.CONSOLE_HELP_INIT_COMMAND_DESCRIPTION });
    commandsHelp.put("setprop", new String[] { ConsoleMsg.CONSOLE_HELP_KEYVALUE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SETPROP_COMMAND_DESCRIPTION });
    commandsHelp.put("setp", new String[] { ConsoleMsg.CONSOLE_HELP_KEYVALUE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SETPROP_COMMAND_DESCRIPTION });
    
    commandsHelp.put("install", new String[] { ConsoleMsg.CONSOLE_HELP_INSTALL_COMMAND_DESCRIPTION });
    commandsHelp.put("i", new String[] { ConsoleMsg.CONSOLE_HELP_INSTALL_COMMAND_DESCRIPTION });
    commandsHelp.put("uninstall", new String[] { ConsoleMsg.CONSOLE_HELP_UNINSTALL_COMMAND_DESCRIPTION });
    commandsHelp.put("un", new String[] { ConsoleMsg.CONSOLE_HELP_UNINSTALL_COMMAND_DESCRIPTION });
    commandsHelp.put("start", new String[] { ConsoleMsg.CONSOLE_HELP_START_COMMAND_DESCRIPTION });
    commandsHelp.put("sta", new String[] { ConsoleMsg.CONSOLE_HELP_START_COMMAND_DESCRIPTION });
    commandsHelp.put("stop", new String[] { ConsoleMsg.CONSOLE_HELP_STOP_COMMAND_DESCRIPTION });
    commandsHelp.put("sto", new String[] { ConsoleMsg.CONSOLE_HELP_STOP_COMMAND_DESCRIPTION });
    commandsHelp.put("refresh", new String[] { ConsoleMsg.CONSOLE_HELP_REFRESH_COMMAND_DESCRIPTION });
    commandsHelp.put("r", new String[] { ConsoleMsg.CONSOLE_HELP_REFRESH_COMMAND_DESCRIPTION });
    commandsHelp.put("update", new String[] { ConsoleMsg.CONSOLE_HELP_UPDATE_COMMAND_DESCRIPTION });
    commandsHelp.put("up", new String[] { ConsoleMsg.CONSOLE_HELP_UPDATE_COMMAND_DESCRIPTION });
    
    commandsHelp.put("status", new String[] { ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_STATUS_COMMAND_DESCRIPTION });
    commandsHelp.put("s", new String[] { ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_STATUS_COMMAND_DESCRIPTION });
    commandsHelp.put("ss", new String[] { ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SS_COMMAND_DESCRIPTION });
    commandsHelp.put("services", new String[] { ConsoleMsg.CONSOLE_HELP_FILTER_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_SERVICES_COMMAND_DESCRIPTION });
    commandsHelp.put("packages", new String[] { ConsoleMsg.CONSOLE_HELP_PACKAGES_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_PACKAGES_COMMAND_DESCRIPTION });
    commandsHelp.put("p", new String[] { ConsoleMsg.CONSOLE_HELP_PACKAGES_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_PACKAGES_COMMAND_DESCRIPTION });
    commandsHelp.put("bundles", new String[] { ConsoleMsg.CONSOLE_HELP_STATE_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_BUNDLES_COMMAND_DESCRIPTION });
    commandsHelp.put("bundle", new String[] { ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_BUNDLE_COMMAND_DESCRIPTION });
    commandsHelp.put("b", new String[] { ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_BUNDLE_COMMAND_DESCRIPTION });
    commandsHelp.put("headers", new String[] { ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_HEADERS_COMMAND_DESCRIPTION });
    commandsHelp.put("h", new String[] { ConsoleMsg.CONSOLE_HELP_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_HEADERS_COMMAND_DESCRIPTION });
    
    commandsHelp.put("exec", new String[] { ConsoleMsg.CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_EXEC_COMMAND_DESCRIPTION });
    commandsHelp.put("fork", new String[] { ConsoleMsg.CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_FORK_COMMAND_DESCRIPTION });
    commandsHelp.put("gc", new String[] { ConsoleMsg.CONSOLE_HELP_GC_COMMAND_DESCRIPTION });
    commandsHelp.put("getprop", new String[] { ConsoleMsg.CONSOLE_HELP_GETPROP_ARGUMENT_DESCRIPTION, ConsoleMsg.CONSOLE_HELP_GETPROP_COMMAND_DESCRIPTION });
    commandsHelp.put("props", new String[] { ConsoleMsg.CONSOLE_PROPS_COMMAND_DESCRIPTION });
    commandsHelp.put("pr", new String[] { ConsoleMsg.CONSOLE_PROPS_COMMAND_DESCRIPTION });
    commandsHelp.put("threads", new String[] { ConsoleMsg.CONSOLE_THREADS_COMMAND_DESCRIPTION });
    commandsHelp.put("t", new String[] { ConsoleMsg.CONSOLE_THREADS_COMMAND_DESCRIPTION });
    
    commandsHelp.put("sl", new String[] { ConsoleMsg.CONSOLE_HELP_OPTIONAL_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SL });
    commandsHelp.put("setfwsl", new String[] { ConsoleMsg.STARTLEVEL_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETFWSL });
    commandsHelp.put("setbsl", new String[] { ConsoleMsg.STARTLEVEL_IDLOCATION_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETBSL });
    commandsHelp.put("setibsl", new String[] { ConsoleMsg.STARTLEVEL_ARGUMENT_DESCRIPTION, ConsoleMsg.STARTLEVEL_HELP_SETIBSL });
    
    commandsHelp.put("profilelog", new String[] { ConsoleMsg.CONSOLE_HELP_PROFILELOG_DESCRIPTION });
  }
  
  private void initializeCommandGroups()
  {
    commandGroups = new LinkedHashMap();
    commandGroups.put(ConsoleMsg.CONSOLE_HELP_CONTROLLING_FRAMEWORK_HEADER, new String[] { "launch", "shutdown", "close", "exit", "init", "setprop" });
    commandGroups.put(ConsoleMsg.CONSOLE_HELP_CONTROLLING_BUNDLES_HEADER, new String[] { "install", "uninstall", "start", "stop", "refresh", "update" });
    commandGroups.put(ConsoleMsg.CONSOLE_HELP_DISPLAYING_STATUS_HEADER, new String[] { "status", "ss", "services", "packages", "bundles", "bundle", "headers" });
    commandGroups.put(ConsoleMsg.CONSOLE_HELP_EXTRAS_HEADER, new String[] { "exec", "fork", "gc", "getprop", "props", "threads" });
    commandGroups.put(ConsoleMsg.STARTLEVEL_HELP_HEADING, new String[] { "sl", "setfwsl", "setbsl", "setibsl" });
    commandGroups.put(ConsoleMsg.CONSOLE_HELP_PROFILE_HEADING, new String[] { "profilelog" });
  }
  
  private void addHeader(String header, StringBuffer help)
  {
    help.append("---");
    help.append(header);
    help.append("---");
    help.append("\r\n");
  }
  
  private void addCommand(String command, String description, StringBuffer help)
  {
    help.append("\t");
    help.append(command);
    help.append(" - ");
    help.append(description);
    help.append("\r\n");
  }
  
  private void addCommand(String command, String parameters, String description, StringBuffer help)
  {
    help.append("\t");
    help.append(command);
    help.append(" ");
    help.append(parameters);
    help.append(" - ");
    help.append(description);
    help.append("\r\n");
  }
  
  private void addCommand(String command, String[] attributes, StringBuffer help)
  {
    if (attributes.length == 1) {
      addCommand(command, attributes[0], help);
    } else if (attributes.length == 2) {
      addCommand(command, attributes[0], attributes[1], help);
    }
  }
  
  public void _exit(CommandInterpreter intp)
    throws Exception
  {
    intp.println();
    System.exit(0);
  }
  
  public void _launch(CommandInterpreter intp)
    throws Exception
  {
    framework.launch();
  }
  
  public void _shutdown(CommandInterpreter intp)
    throws Exception
  {
    framework.shutdown(64);
  }
  
  public void _sta(CommandInterpreter intp)
    throws Exception
  {
    _start(intp);
  }
  
  public void _start(CommandInterpreter intp)
    throws Exception
  {
    String nextArg = intp.nextArgument();
    if (nextArg == null) {
      intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR);
    }
    while (nextArg != null)
    {
      AbstractBundle bundle = getBundleFromToken(intp, nextArg, true);
      if (bundle != null) {
        bundle.start();
      }
      nextArg = intp.nextArgument();
    }
  }
  
  public void _sto(CommandInterpreter intp)
    throws Exception
  {
    _stop(intp);
  }
  
  public void _stop(CommandInterpreter intp)
    throws Exception
  {
    String nextArg = intp.nextArgument();
    if (nextArg == null) {
      intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR);
    }
    while (nextArg != null)
    {
      AbstractBundle bundle = getBundleFromToken(intp, nextArg, true);
      if (bundle != null) {
        bundle.stop();
      }
      nextArg = intp.nextArgument();
    }
  }
  
  public void _i(CommandInterpreter intp)
    throws Exception
  {
    _install(intp);
  }
  
  public void _install(CommandInterpreter intp)
    throws Exception
  {
    String url = intp.nextArgument();
    if (url == null)
    {
      intp.println(ConsoleMsg.CONSOLE_NOTHING_TO_INSTALL_ERROR);
    }
    else
    {
      AbstractBundle bundle = (AbstractBundle)context.installBundle(url);
      intp.print(ConsoleMsg.CONSOLE_BUNDLE_ID_MESSAGE);
      intp.println(new Long(bundle.getBundleId()));
      
      String nextArg = intp.nextArgument();
      if (nextArg != null)
      {
        String start = nextArg.toLowerCase();
        if (matchCommand("start", start, 1)) {
          bundle.start();
        }
      }
    }
  }
  
  private static boolean matchCommand(String command, String input, int minLength)
  {
    if (minLength <= 0) {
      minLength = command.length();
    }
    int length = input.length();
    if (minLength > length) {
      length = minLength;
    }
    return command.regionMatches(0, input, 0, length);
  }
  
  public void _up(CommandInterpreter intp)
    throws Exception
  {
    _update(intp);
  }
  
  public void _update(CommandInterpreter intp)
    throws Exception
  {
    String token = intp.nextArgument();
    if (token == null) {
      intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR);
    }
    while (token != null)
    {
      if ("*".equals(token))
      {
        AbstractBundle[] bundles = (AbstractBundle[])context.getBundles();
        
        int size = bundles.length;
        if (size > 0) {
          for (int i = 0; i < size; i++)
          {
            AbstractBundle bundle = bundles[i];
            if (bundle.getBundleId() != 0L) {
              try
              {
                bundle.update();
              }
              catch (BundleException e)
              {
                intp.printStackTrace(e);
              }
            }
          }
        } else {
          intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR);
        }
      }
      else
      {
        AbstractBundle bundle = getBundleFromToken(intp, token, true);
        if (bundle != null)
        {
          String source = intp.nextArgument();
          try
          {
            if (source != null) {
              bundle.update(new URL(source).openStream());
            } else {
              bundle.update();
            }
          }
          catch (BundleException e)
          {
            intp.printStackTrace(e);
          }
        }
      }
      token = intp.nextArgument();
    }
  }
  
  public void _un(CommandInterpreter intp)
    throws Exception
  {
    _uninstall(intp);
  }
  
  public void _uninstall(CommandInterpreter intp)
    throws Exception
  {
    String nextArg = intp.nextArgument();
    if (nextArg == null) {
      intp.println(ConsoleMsg.CONSOLE_NO_BUNDLE_SPECIFIED_ERROR);
    }
    while (nextArg != null)
    {
      AbstractBundle bundle = getBundleFromToken(intp, nextArg, true);
      if (bundle != null) {
        bundle.uninstall();
      }
      nextArg = intp.nextArgument();
    }
  }
  
  public void _s(CommandInterpreter intp)
    throws Exception
  {
    _status(intp);
  }
  
  private Object[] processOption(CommandInterpreter intp)
  {
    String option = intp.nextArgument();
    String filteredName = null;
    int stateFilter = -1;
    if ((option != null) && (option.equals("-s")))
    {
      String searchedState = intp.nextArgument();
      if (searchedState == null) {
        searchedState = "";
      }
      StringTokenizer tokens = new StringTokenizer(searchedState, ",");
      while (tokens.hasMoreElements())
      {
        String desiredState = (String)tokens.nextElement();
        Field match = null;
        try
        {
          match = Bundle.class.getField(desiredState.toUpperCase());
          if (stateFilter == -1) {
            stateFilter = 0;
          }
          stateFilter |= match.getInt(match);
        }
        catch (NoSuchFieldException localNoSuchFieldException)
        {
          intp.println(ConsoleMsg.CONSOLE_INVALID_INPUT + ": " + desiredState);
          return null;
        }
        catch (IllegalAccessException localIllegalAcc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd