17 August, 2009

Download FreeBSD packages with dependencies

Just code, no comments:
<?php

#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(
'bash-3.2.48_1',
'bzip2-1.0.5',
'curl-7.19.4',
'mc-4.6.2',
'mysql-server-5.0.77_1',
'php5-mysqli-5.2.9',
'postgresql-server-8.3.7',
'vim-7.2.132',
'wget-1.11.4',
'zip-3.0'
);

#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];
}
fclose($handle);
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;
}
}
}
}

#Downloading
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:


cbr_daily.xml
<ValCurs Date="30/05/2009" name="Foreign Currency Market">
<Valute ID="R01035">
<NumCode>826</NumCode>
<CharCode>GBP</CharCode>
<Nominal>1</Nominal>
<Name>Фунт стерлингов Соединенного королевства</Name>
<Value>49,7887</Value>
</Valute>
<Valute ID="R01235">
<NumCode>840</NumCode>
<CharCode>USD</CharCode>
<Nominal>1</Nominal>
<Name>Доллар США</Name>
<Value>30,9843</Value>
</Valute>
<Valute ID="R01239">
<NumCode>978</NumCode>
<CharCode>EUR</CharCode>
<Nominal>1</Nominal>
<Name>Евро</Name>
<Value>43,3780</Value>
</Valute>
</ValCurs>



ValCursType.java
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;

@XmlAttribute
public String name;
}




ValuteType.java
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;
}




CbrCurrencyTask.java
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:
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:

CREATE TYPE log_level AS ENUM ('INFO', 'WARN', 'ERROR');

CREATE TABLE logs (
id SERIAL PRIMARY KEY,
message VARCHAR(255) NOT NULL,
level log_level NOT NULL,
);


And Java enum:

public enum LogLevel {
INFO, WARN, ERROR
}


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

PHP-reference

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