17 August, 2009

Download FreeBSD packages with dependencies

Just code, no comments:

#Where to save downloaded packages
$dir = '/tmp/packages';

#FTP server with all packages
$ftp = 'ftp://ftp6.ru.freebsd.org/pub/FreeBSD/ports/i386/packages-7.2-release/All';

#Target packages we need to retrieve
$packages = array(

#Load packages info from previously downloaded INDEX file (ftp://ftp6.ru.freebsd.org/pub/FreeBSD/ports/i386/packages-7.2-release/All/INDEX)
function get_tree() {
$handle = fopen('INDEX', 'r');
$tree = array();
while (($data = fgetcsv($handle, 0, '|')) !== FALSE) {
$tree[$data[0]] = $data[8];
return $tree;

$tree = get_tree();

#Calculate dependencies
for ($i = 0; $i < sizeof($packages); $i++) {
if (isset($tree, $packages[$i])) {
$deps = explode(' ', $tree[$packages[$i]]);
foreach ($deps as $dep) {
if (strlen($dep)>0 && !in_array($dep, $packages)) {
$packages[] = $dep;

foreach ($packages as $package) {
file_put_contents($dir.'/'.$package.'.tbz', file_get_contents($ftp.'/'.$package.'.tbz'));
echo "$package.tbz\n";


14 August, 2009

Make local FreeBSD mirror via rsync

rsync -va --delete --progress ftp.nl.FreeBSD.org::FreeBSD/ports/i386/packages-7.2-release/ FreeBSD-7.2-i386

13 August, 2009

Bind XML to Java classes

Just follow this example:

<ValCurs Date="30/05/2009" name="Foreign Currency Market">
<Valute ID="R01035">
<Name>Фунт стерлингов Соединенного королевства</Name>
<Valute ID="R01235">
<Name>Доллар США</Name>
<Valute ID="R01239">

package xqx;

import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "ValCurs")
@XmlType(name = "ValCursType")
public class ValCursType {

@XmlElement(name = "Valute")
public List<ValuteType> valuteType;

@XmlAttribute(name = "Date")
public String date;

public String name;

package xqx;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "Valute")
@XmlType(name = "ValuteType")
public class ValuteType {
@XmlElement(name = "NumCode")
public String numCode;

@XmlElement(name = "CharCode")
public String charCode;

@XmlElement(name = "Nominal")
public int nominal;

@XmlElement(name = "Name")
public String name;

@XmlElement(name = "Value")
public String value;

@XmlAttribute(name = "ID")
public String id;

package xqx;

import ...;

public class CbrCurrencyTask {

//stream - For example FileInputStream created from the "cbr_daily.xml" file
protected ValCursType parse(InputStream stream) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(ValCursType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

ValCursType valCursType = (ValCursType) unmarshaller.unmarshal(stream);
return valCursType;


Simple! Isn't it?

Mount CD in FreeBSD

See all disks:
atacontrol list

mount -t cd9660 /dev/acd0 /cdrom

27 July, 2009

JDBC: Postgres enums and prepared statements

There is simple way how to map Java enums to PostgreSQL enums via JDBC's prepared statements.

For example we have these PostgreSQL enum and table definitions:


message VARCHAR(255) NOT NULL,
level log_level NOT NULL,

And Java enum:

public enum LogLevel {

This form of prepared statement should work:

LogLevel logLevel = LogLevel.INFO;
PreparedStatement sql = conn.prepareStatement("INSERT INTO logs (message, log_level) VALUES (?, ?::log_level)");
sql.setString(1, "Hello world");
//sql.setString(2, "INFO");
sql.setString(2, logLevel.name());

28 February, 2009

Get browser language via JavaScript

This is simple cross-browser solution:

var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en').substr(0, 2).toLowerCase();

And usage example:

var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en').substr(0, 2).toLowerCase();
if (lang == 'ru') {
    document.write('Specific Russian text');
} else {
    document.write('Common English text');

24 February, 2009


I'm happy to introduce one more my project: PHP-reference.

This is the interactive engine for PHP documentation: the client side is implemented with ExtJS/JavaScript, and PHP for server side.
It uses original PHP documentation (processed via regexps and colored with CSS styles) as a data source.

I've tried to create it as usable as possible... any requests, ideas welcome!

18 February, 2009

TOAD connection problem under Windows XP x64

I've catch this problem with my installation of TOAD (and with PLSQL Developer) under Windows XP 64-bit edition:
"ORA-12154: TNS:could not resolve the connect identifier specified" (but under Windows XP 32-bit all OK).

And here is the simplest solution:
run TOAD via command line using short path: C:\PROGRA~2\QUESTS~1\TOAD\TOAD.exe
instead of long: C:\Program Files (x86)\Quest Software\TOAD\TOAD.exe

Unfortunately Windows doesn't allow to change desktop's shortcut from long mode to short (Windows always expands short paths).

Or... move TOAD from "Program Files (x86)" to "Program Files" directory.

19 January, 2009

Run java with non-system locale

java -Duser.language=en -Duser.region=US JavaClass