Esempio n. 1
0
 /**
  * Adds a new provider, at a specified position. The position is the preference order in which
  * providers are searched for requested algorithms. Note that it is not guaranteed that this
  * preference will be respected. The position is 1-based, that is, 1 is most preferred, followed
  * by 2, and so on.
  *
  * <p>If the given provider is installed at the requested position, the provider that used to be
  * at that position, and all providers with a position greater than <code>position</code>, are
  * shifted up one position (towards the end of the list of installed providers).
  *
  * <p>A provider cannot be added if it is already installed.
  *
  * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> method is called
  * with the string <code>"insertProvider."+provider.getName()</code> to see if it's ok to add a
  * new provider. If the default implementation of <code>checkSecurityAccess</code> is used (i.e.,
  * that method is not overriden), then this will result in a call to the security manager's <code>
  * checkPermission</code> method with a <code>
  * SecurityPermission("insertProvider."+provider.getName())</code> permission.
  *
  * @param provider the provider to be added.
  * @param position the preference position that the caller would like for this provider.
  * @return the actual preference position in which the provider was added, or -1 if the provider
  *     was not added because it is already installed.
  * @throws NullPointerException if provider is null
  * @throws SecurityException if a security manager exists and its <code>{@link
  *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies access to add a
  *     new provider
  * @see #getProvider
  * @see #removeProvider
  * @see java.security.SecurityPermission
  */
 public static synchronized int insertProviderAt(Provider provider, int position) {
   String providerName = provider.getName();
   check("insertProvider." + providerName);
   ProviderList list = Providers.getFullProviderList();
   ProviderList newList = ProviderList.insertAt(list, provider, position - 1);
   if (list == newList) {
     return -1;
   }
   Providers.setProviderList(newList);
   return newList.getIndex(providerName) + 1;
 }
Esempio n. 2
0
  /**
   * Looks up providers, and returns the property (and its associated provider) mapping the key, if
   * any. The order in which the providers are looked up is the provider-preference order, as
   * specificed in the security properties file.
   */
  private static ProviderProperty getProviderProperty(String key) {
    ProviderProperty entry = null;

    List providers = Providers.getProviderList().providers();
    for (int i = 0; i < providers.size(); i++) {

      String matchKey = null;
      Provider prov = (Provider) providers.get(i);
      String prop = prov.getProperty(key);

      if (prop == null) {
        // Is there a match if we do a case-insensitive property name
        // comparison? Let's try ...
        for (Enumeration e = prov.keys(); e.hasMoreElements() && prop == null; ) {
          matchKey = (String) e.nextElement();
          if (key.equalsIgnoreCase(matchKey)) {
            prop = prov.getProperty(matchKey);
            break;
          }
        }
      }

      if (prop != null) {
        ProviderProperty newEntry = new ProviderProperty();
        newEntry.className = prop;
        newEntry.provider = prov;
        return newEntry;
      }
    }

    return entry;
  }
Esempio n. 3
0
 /**
  * Returns the provider installed with the specified name, if any. Returns null if no provider
  * with the specified name is installed or if name is null.
  *
  * @param name the name of the provider to get.
  * @return the provider of the specified name.
  * @see #removeProvider
  * @see #addProvider
  */
 public static Provider getProvider(String name) {
   return Providers.getProviderList().getProvider(name);
 }
Esempio n. 4
0
 /**
  * Returns an array containing all the installed providers. The order of the providers in the
  * array is their preference order.
  *
  * @return an array of all the installed providers.
  */
 public static Provider[] getProviders() {
   return Providers.getFullProviderList().toArray();
 }
Esempio n. 5
0
 /**
  * Removes the provider with the specified name.
  *
  * <p>When the specified provider is removed, all providers located at a position greater than
  * where the specified provider was are shifted down one position (towards the head of the list of
  * installed providers).
  *
  * <p>This method returns silently if the provider is not installed or if name is null.
  *
  * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> method is called
  * with the string <code>"removeProvider."+name</code> to see if it's ok to remove the provider.
  * If the default implementation of <code>checkSecurityAccess</code> is used (i.e., that method is
  * not overriden), then this will result in a call to the security manager's <code>checkPermission
  * </code> method with a <code>SecurityPermission("removeProvider."+name)</code> permission.
  *
  * @param name the name of the provider to remove.
  * @throws SecurityException if a security manager exists and its <code>{@link
  *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies access to remove
  *     the provider
  * @see #getProvider
  * @see #addProvider
  */
 public static synchronized void removeProvider(String name) {
   check("removeProvider." + name);
   ProviderList list = Providers.getFullProviderList();
   ProviderList newList = ProviderList.remove(list, name);
   Providers.setProviderList(newList);
 }
Esempio n. 6
0
  public TypeUniverse() {
    Providers providers =
        Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders();
    metaAccess = providers.getMetaAccess();
    constantReflection = providers.getConstantReflection();
    snippetReflection = Graal.getRequiredCapability(SnippetReflectionProvider.class);
    Unsafe theUnsafe = null;
    try {
      theUnsafe = Unsafe.getUnsafe();
    } catch (Exception e) {
      try {
        Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafeField.setAccessible(true);
        theUnsafe = (Unsafe) theUnsafeField.get(null);
      } catch (Exception e1) {
        throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1);
      }
    }
    unsafe = theUnsafe;

    Class<?>[] initialClasses = {
      void.class,
      boolean.class,
      byte.class,
      short.class,
      char.class,
      int.class,
      float.class,
      long.class,
      double.class,
      Object.class,
      Class.class,
      ClassLoader.class,
      String.class,
      Serializable.class,
      Cloneable.class,
      Test.class,
      TestMetaAccessProvider.class,
      List.class,
      Collection.class,
      Map.class,
      Queue.class,
      HashMap.class,
      LinkedHashMap.class,
      IdentityHashMap.class,
      AbstractCollection.class,
      AbstractList.class,
      ArrayList.class
    };
    for (Class<?> c : initialClasses) {
      addClass(c);
    }
    for (Field f : Constant.class.getDeclaredFields()) {
      int mods = f.getModifiers();
      if (f.getType() == Constant.class
          && Modifier.isPublic(mods)
          && Modifier.isStatic(mods)
          && Modifier.isFinal(mods)) {
        try {
          Constant c = (Constant) f.get(null);
          if (c != null) {
            constants.add(c);
          }
        } catch (Exception e) {
        }
      }
    }
    for (Class<?> c : classes) {
      if (c != void.class && !c.isArray()) {
        constants.add(snippetReflection.forObject(Array.newInstance(c, 42)));
      }
    }
    constants.add(snippetReflection.forObject(new ArrayList<>()));
    constants.add(snippetReflection.forObject(new IdentityHashMap<>()));
    constants.add(snippetReflection.forObject(new LinkedHashMap<>()));
    constants.add(snippetReflection.forObject(new TreeMap<>()));
    constants.add(snippetReflection.forObject(new ArrayDeque<>()));
    constants.add(snippetReflection.forObject(new LinkedList<>()));
    constants.add(snippetReflection.forObject("a string"));
    constants.add(snippetReflection.forObject(42));
    constants.add(snippetReflection.forObject(String.class));
    constants.add(snippetReflection.forObject(String[].class));
  }