Saturday, September 14, 2013

Work with org.apache.commons.configuration.PropertiesConfiguration

When standard tools (java.util.prefs.Preferences) not enough for solving your goals with saving some properties, org.apache.commons.configuration.PropertiesConfiguration can help you.

  1. Configuration file creation.

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientSettings.class)
    String propertyPath = "/userdir";
    String fileName = "fileName.property";
    File propertyDir = new File(propertyPath);
    if(!propertyDir.exists()) {
        if (!propertyDir.mkdirs()) {
                    throw new IllegalStateException(String.format("Cannot create property folder: %s", propertyDir.getAbsoluteFile()));
        }
    }
          File propertiesFile = new File(propertyDir, fileName);
          if (!propertiesFile.exists()) {
              try {
                 if (!propertiesFile.createNewFile())
                    throw new IllegalStateException(String.format("Cannot create property file: %s", propertiesFile.getAbsoluteFile()));
              } catch (IOException e) {
                LOGGER.error("Cannot create configuration file: %s", e);
              }
           }

  1. Specify the location of the file this configuration is based on and load the configuration.

    try {
           //allows to specify the configuration source as a file
           configuration.setFile(propertiesFile);
           //Locate the specified file and load the configuration.
           configuration.load();
           //Save the configuration to the specified file.
           configuration.save(propertiesFile);
    } catch (ConfigurationException e) {
           LOGGER.error("Cannot initializing a Configuration object.",e);
           return null;
    }


    The following possibilities exist:
    • URLs: With the method setURL() a full URL to the configuration source can be specified. This is the most flexible way. Note that the save() methods support only file: URLs.
    • Files: The setFile() method allows to specify the configuration source as a file. This can be either a relative or an absolute file. In the former case the file is resolved based on the current directory.
    • As file paths in string form: With the setPath() method a full path to a configuration file can be provided as a string.
    • Separated as base path and file name: This is the native form in which the location is stored. The base path is a string defining either a local directory or a URL. It can be set using the setBasePath() method. The file name, non surprisingly, defines the name of the configuration file.
      The configuration source to be loaded can be specified using one of the methods described above. Then the parameterless 
      load() method can be called. 
  1. Get String properties.

    configuration.getString(String key);
  2. Set properties.
    configuration.setProperty(String key, Object value);
    configuration.save();

    If property with such key is exist, value will be overridden.
  3. Add new property.

    configuration.addProperty(String key, Object value);configuration.save();

    If property with such key is exist, new property will be add anyway.

Monday, September 9, 2013

Install Oracle Java 8 in Ubuntu 12.04.

Run in terminal simple comands:
  1. Update repositories and install Java:

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update && sudo apt-get install oracle-java8-installer
  2. Check java version:

    java -version

    Output must be similar to:

    java version "1.8.0-ea"
    Java(TM) SE Runtime Environment (build 1.8.0-ea-b54)
    Java HotSpot(TM) Server VM (build 24.0-b21, mixed mode)
  3. If you have several version of Java, you can switch between them:

    Check all installed JVM  on your computer:
    sudo update-java-alternatives -l

    Choose JVM and switch:
    sudo update-java-alternatives -s java-8-oracle

Convertion Russian characters to one with Unicode-encoded characters for Java application


  1.  Online http://native2ascii.net .
  2. With utillity natime2ascii in Sun JDK.

    Documentation: http://docs.oracle.com

    Create a file:
    touch ~/Test.txt

    Write to file Russian characters:
    echo Русский > Test.txt

    Go to the Java jdk bin directory:
    cd 
    /usr/lib/jvm/jdk1.7.9/bin

    And run the natime2ascii:
    ./native2ascii -encoding UTF-8 ~/Test.txt ~/Test.txt

    Look in the file:
    less ~/Test.txt
     \u0420\u0443\u0441\u0441\u043a\u0438\u0439