Friday, July 31, 2009

Configure postfix to send emails for root to your account

Goal: make our small ubuntu server forward email which has "to: root" to your private mail.

Install postfix.
sudo apt-get install postfix
Some console app for sending emails can be handy too:
sudo apt-get install mutt
Edit /etc/postfix/main.cf. In mydestination enter host, host.domain, localhost.domain, and also empty domain:
mydestination = serverek.przepompownia.com, localhost.przepompownia.com, , localhost
Restart postfix. I'm not sure if it is really needed, but won't hurt ;)
sudo /etc/init.d/postfix restart
Ok, now edit /etc/aliases. Add something like this:
root: franek
franek: mickey@my.mail.com
Dont forget to run:
sudo newaliases
This way everything sent to root is forwarded to franek, and everything sent to franek is forwarded to mickey@my.mail.com

Now you can try sending some mail to "root". You can use mutt for this.
UPDATE:
Recently my ISP (neostrada, TPSA) blocked outgoing connections to port 25 (standard port for connecting with SMTP servers). What to do to send emails to port 587 is well described here. Author only forgot to add that you also need this:
transport_maps = hash:/etc/postfix/transport
in your main.cf

Tuesday, July 21, 2009

JavaFX - Operations on Strings

Use any quote you like

Ok, so let's start with Strings. You can use both single and double quote to define simple String. Both works:
var a = 'Single';
var b = "Double";
Also you can use another quote in String as part of this String. I.e. if you use single quote to declare String, you can put double quote inside, and if you use double one, you can put single one inside.
var a = 'This is String "with double quote" inside';
var b = "This is String 'with single quote' inside";
Both are valid Strings. However, if you need to use both types in your String, you have to escape it:
var a = 'Here we have "double quotes" and \'single quotes\' in one String';
var b = "And 'here' \"too!\""
Expressions in String

In JavaFX you can also embed expressions in Strings. To do it, put expression in curly brackets:
println("2 + 3 = {2+3}");
This code prints: 2 + 3 = 5 By the way, you can see new println function here. It is in javafx.lang.Builtins, which is included automatically in all JavaFX scripts.

As expression, you can use also variables, function calls, operations on variables (like in example above) etc.

Concatenation

You don't need any operator to concatenate Strings. If you split String over few lines, you don't have to add "+" at the end of each line.
var one = "This" 'is'
"one"
'String';
println(one);
And it outputs: ThisisoneString

Formatting

You can quite easy format Strings in JavaFX. It is based on java.util.Formatter class.

println("{%x 12}");
println("{%c 120}");
var d = Date{};    //Here new java.util.Date is created
println("{%tH d}:{%tM d}:{%tS d}")
And it prints:
c x 23:12:18 First there is 12 in hex, then there is character, which code in Unicode is 120, and then it is Date object, first we get hour from it, then minute, and then second.

Localization

Localization in JFX is realized by properties files. It's name is connected with your script (file with your JFX code) by name convention. If your script is House.fx, then your localization file is House_en.fxproperties and it has to be somewhere in the classpath. fxproperties file looks like this:
"dog"="pies"
"cat"="kot"
And then, to use it in script:
println(##"dog");
println(##[cat]"Cat");
If there is fxproperties found, it is used. If not found, default is used. Default is either properties key (like "dog" in above example) or another word (like "Cat", not "cat" which is only fxproperties key).

Tuesday, July 14, 2009

JavaFX - Defining simple variables.

Ok, so I thought to write JavaFX tutorial. Hope it's going to be useful for somebody. Welcome to part one, about language basics, defining variables and values.

First, you should know what variables/value types are available. There are all the familiar ones (String, Long, Double etc.), but no primitives (long, double etc.). Well, in JavaFX everything is an object. Really. There is also one new type: Duration. It is especially useful in animations, but we'll get to it later.

So there are variables and values. Values are a little like final constants in Java, but only a little. Value of value ( ;) ) can change, but you can't change it explicitly. It can be changed by binding - we'll talk about it in some later part.

So how do you define variables and values? Quite simple:
var a:Float = 5;
Defines variable a of type Float. We can also do something like this:
var a = 5;
Where is variable's type? Well, you don't need to specify it in such case. Type is inferred automatically by JavaFX Script compiler from value assigned to this variable. Isn't it obvious that it is Integer? It could be also Float, yes, and if you want it to be Float, please specify it like in first example.

What about "almost-like-java-constants" values? You define it like that:
def a:Float = 5;
So now there is value defined, and you can't change it. a = 6 will not compile.

Ok, let's look at this new Duration type. Few examples:
var s = 5s;
var m = 2m;
var h = 4h;
s is 5 seconds, m is 2 minutes and h is 4 hours. Quite simple, isn't it? Also arithmetics like adding, multiplying etc works here:
var s = 5s * 4;
var m = 2m + 3m;
var h = (2h + 3m) * 4;
Ok, that's all in this part. In the next one I'll write something about String operations and sequences. Stay tuned!

Monday, July 6, 2009

Review: JavaFX in Action, by Simon Morris

JavaFX in Action JavaFX (JFX) is a new technology. There are not many books about it yet. Then the more happy I am I can read one of them - "JavaFX in Action" by Simon Morris from Manning Publications.

At the beginning of the book there is explanation of what JavaFX platform is, and it's language JavaFX Script. And why it is great at creating GUIs. The next step is not language basics as one could expect, but first small application in JavaFX. You don't fully understand how it works and what all this words, numbers and braces mean, but you can run it and say "WOW!" :) It is there to make an impression, and it serves this purpose well. It's like an appetizer, making you even more hungry for knowledge and more eager to learn this new great language. I like it, because it makes further reading "taste" better. It also shows what is told in first chapter is true - in JavaFX you can create fancy graphical effects easily.

Chapters two and three are about language itself, variables declaration, data types (new one: duration), language structure - all the fun begins. After learning basic stuff about language, we start creating "real" applications. Nice looking, fancy and colorful small applications (JavaFX is for GUI, isn't it?). What I didn't like here is a bit waterfall, not iterative process. All applications are created class by class, and then at the end run. Many times I wanted to see how single component looks like before finishing whole application. I could do it by writing small app by myself, but I'd prefer to see it in book.

The language used in the book is very light, even funny. It is pleasure to read, still everything is explained clearly and understandably. One of difficult topics are transformations. Such constructs are not common and I didn't see them in any other language (at least not done this way). For me it was quite difficult topic, yet everything was well explained. I understood transformations after playing a little with the code. Another thing was new data type "duration" - no problems with understanding here too (but this one was much easier).

I like it when in book you can see effect of running application. So you don't need to code or even copy code from sources attached to the book and run it. In this book there always was output included after source code, or there were pictures if output was not on console. You still can copy code to your favorite IDE and see it running, but you don't have to.

The book I read was based on JFX 1.1, while there is 1.2 available now. However it was MEAP version, and I'm sure final version is going to be based on 1.2 or even newer JFX (depending when it is going to be released). Anyway author has a lot of knowledge and he predicted (or just knew about it) many things that were missing in JFX 1.1 but are available in 1.2.

Appendixes are also great. There is one for people who don't have anything installed yet, just opened the book and want to code. I had everything installed before, but if you don't, don't worry, the book will tell you what to do step by step. I often missed such appendix in other books, especially when I was less experienced developer. There is even appendix for people not familiar with java, so you can see what static typing vs dynamic typing is all about, what are packages etc.

Reading this book was a pleasure. I learned a lot, and by the best and most effective way - by having fun :)