Code example for Properties

Methods: clear, getProperty, load

0
  } 
 
  protected String getSpi(final String key)
  { 
    initCache(); 
    return properties.getProperty(key);
  } 
 
  protected void removeSpi(final String key)
  { 
    initCache(); 
    properties.remove(key);
    lastModificationTime = Math.max (lastModificationTime + 1, System.currentTimeMillis());
    try 
    { 
      flush();
    } 
    catch (BackingStoreException e)
    { 
      if (logger.isInfoEnabled())
      { 
        logger.info("Failed to flush configuration changes");//NON-NLS
      } 
      else if (logger.isDebugEnabled())
      { 
        logger.info("Failed to flush configuration changes", e);//NON-NLS
      } 
    } 
  } 
 
  public long getLastModificationTime() 
  { 
    return lastModificationTime;
  } 
 
  protected String[] keysSpi() throws BackingStoreException
  { 
    initCache(); 
    return (String[]) properties.keySet().toArray(new String[properties.size()]);
  } 
 
  protected String[] childrenNamesSpi() throws BackingStoreException
  { 
    final ArrayList<String> result = new ArrayList<String>();
    final File pathForNode = new File(getPathForNode());
    final File[] dirContents = pathForNode.listFiles();
    if (dirContents != null)
    { 
      for (int i = 0; i < dirContents.length; i++)
      { 
        if (dirContents[i].isDirectory())
        { 
          result.add(decodePath(dirContents[i].getName()));
        } 
      } 
    } 
    return result.toArray(new String[result.size()]);
  } 
 
  protected AbstractPreferences childSpi(final String name)
  { 
    if (name == null || name.length() == 0)
    { 
      throw new IllegalArgumentException();
    } 
 
    return new BinaryPreferences(this, name);
  } 
 
  protected void syncSpi() throws BackingStoreException 
  { 
    final File pathForNode = new File(getPathForNode());
    if (pathForNode.exists() == false && properties.isEmpty())
    { 
      return; 
    } 
 
    load(pathForNode);
 
    if (pathForNode.exists() == false)
    { 
      if (pathForNode.mkdirs() == false)
      { 
        throw new BackingStoreException("Failed to write config " + pathForNode); //$NON-NLS-1$
      } 
    } 
 
    final File target = new File(pathForNode, "prefs.properties");//NON-NLS
    if (target.exists() == false || target.lastModified() < lastModificationTime)
    { 
      try 
      { 
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(target));
        try 
        { 
          properties.store(out, "");
        } 
        finally 
        { 
          out.close();
        } 
      } 
      catch (final Exception e)
      { 
        throw new BackingStoreException("Failed to write config " + target); //$NON-NLS-1$
      } 
    } 
  } 
 
  private void load(final File pathForNode)
      throws BackingStoreException 
  { 
    if (pathForNode.exists())
    { 
      // load .. 
      final File target = new File(pathForNode, "prefs.properties");//NON-NLS
      if (target.lastModified() > lastModificationTime)
      { 
        if (target.exists())
        { 
          try 
          { 
            final InputStream out = new BufferedInputStream(new FileInputStream(target));
            try 
            { 
              properties.clear();
              properties.load(out);
              lastModificationTime = Math.max (lastModificationTime + 1, System.currentTimeMillis());
            } 
            finally 
            { 
              out.close();