sensimevanidus

2 July, 2009

Java Notes - 2 (Thinking In Java)

Java uses namespaces to prevent clashing of datas. To produce an unambiguous name for a library, the Java creators want you to use your Internet domain name in reverse since domain names are guaranteed to be unique. For example, a utility library created by ajoen.com would be named as:

com.ajoen.utility

After you reversed domain name, the dots are intended to represent subdirectories.

sensimevanidus

2 July, 2009

Java Notes - 1 (Thinking In Java)

- You treat (almost) everything as an object, using a single consistent syntax. Although you treat everyting as an object, the identifier you manipulate is actually a reference to an object.

- Just because you have a reference doesn’t mean there’s necessarily an object connected to it. Take this example:

String s;

But in this situation, if you decided to send a message to s (the reference), you’d get a compile time error. If your main class contains the following code:

String s;
char c = s.charAt(0);

The compiler would give this error:

variable s might not have been initialized

Because here, s isn’t actually attached to anything. A safer practice, then, is to initialize a reference when you create it:

String s = "qwerty";

- Strings have a special feature that they can be initialized with quoted text, as seen previously. Normally, you must use a more general type of initialization for objects.

- Primitive types are stored on the stack (as in C and C++) whereas objects are stored on the heap.

- All numeric types are signed, so don’t look for unsigned types.

- The wrapper classes for the primitive types allow you make a non-primitive object on the heap to represent that primitive type. For example:

Character ch = 'x';

Here, Character is the wrapper class for char and Java uses autoboxing that automatically converts from a primitive type to a wrapper type.

- BigInteger and BigDecimal classes are to be used for performaing high-precision arithmetic. BigInteger supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations. BigDecimal is for arbitrary-precision fixed-point numbers.

- Arrays are guaranteed to be initialized and cannot be accessed outside of their ranges (the primary goal of Java is safety). The compiler guarantees initialization of arrays of primitives because it zeroes the memory for that array. For example:

int[] onur = new int[2];
System.out.println(onur[0]);

The output for this code snippet would be:

0

- When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null. If you try to use a reference that’s still null, the problem will be reported at run time. Take this as an example:

String[] onur = new String[2];
char c = onur[0].charAt(0);

The compiler won’t throw an error during compilation; but the Java interpreter will throw an exception as follows:

Exception in thread “main” java.lang.NullPointerException at …

- A variable defined within a scope is available only to the end of that scope.

- Hiding a variable in a larger scope is not allowed; thus the following is illegal:

public static void main(String[] args) {
int x = 100;
{
int x = 105;
}
}

If you try to do that, you’ll get a compile time error announcing that the variable x has already been defined, as follows:

… x is already defined in main(java.lang.String[])

- When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it. This ensures that member variables of primitive types will always be initialized. For example:

class Hede {
private int x;

public static void main(String[] args) {
Hede onur = new Hede();
onur.init();
}

public void init() {
System.out.println(x);
}
}

The output of this code snippet would be:

0

This guarantee doesn’t apply to local variables-those that are not fields of a class (as previously shown). Another example for this situation is as follows:

class Hede {
public static void main(String[] args) {
Hede onur = new Hede();
onur.init();
}

public void init() {
int y;
System.out.println(y);
}
}

This time, we get an error at compile time as:

Hede.java:9: variable y might not have been initialized

- The method name and argument list is called the signature of the method and it uniquely identifies that method.

- If you try to call the wrong method for an object, you’ll get an error message at compile time as follows:

String onur = "Onur";
onur.hede();

The compile time error is:

…: cannot find symbol
symbol: method hede()
location: class java.lang.String

- The type of the return value must be compatible with the type of the variable that the return value will be assigned to.

- A method definition must be placed withing a class definition for it to be compiled. For example:

class Hede {
}
public void hede() {
}

This time the compile time error is:

Hede.java:4: class, interface, or enum expected

- When the return type is void, then the return keyword is used only to exit the method, and is therefore unnecessary when you reach the end of the method.

- You can return from a method at any point; but if you’re given a non-void return type, then the compiler will force you (with error message) to return the appropriate type of value regardless of where you return. For example:

public int init() {
if (true) {
return;
}
return 1;
}

This time the compiler gives the following error:

… missing return value

- Just like the previous note; if you’ve defined a method whose return type is void (meaning that it won’t actually return a value) and you’re trying to return some value, the compiler will give an error. For instance:

public void init() {
return 1;
}

The compile time error will be:

… cannot return a value from method whose result type is void

But, this will work as expected:

public void init() {
return;
}

sensimevanidus

2 July, 2009

If we spoke a different language, we would perceive a somewhat different world.

— Ludwig Wittgenstein

sensimevanidus

30 June, 2009

Using Gmail SMTP within CodeIgniter

I tried to send emails via Gmail SMTP (smtp.gmail.com) using CodeIgniter; but got the following error:

ERROR - 2009-06-30 16:54:21 —> 220 mx.google.com ESMTP g9sm76291gvc.10 
<br /><pre>hello: 250-mx.google.com at your service, [85.107.180.116]
250-SIZE 35651584 
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
</pre>Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>from: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>to: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br /><pre>data: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
</pre>The following SMTP error was encountered: 530 5.7.0 Must issue a STARTTLS command first. g9sm76291gvc.10 
<br />502 5.5.1 Unrecognized command. g9sm76291gvc.10 
<br />The following SMTP error was encountered: 502 5.5.1 Unrecognized command. g9sm76291gvc.10

As you can see, it kept saying “Must issue a STARTTLS command first” which means Gmail allowed only SSL connections. So I had to change the host value to:

ssl://smtp.gmail.com

and port value to:

465

And it worked! Note that you need to have OpenSSL support in your PHP installation.

sensimevanidus

24 June, 2009

phpMyAdmin Apache configuration

Even if you’ve installed phpMyAdmin via packet manager, it’s possible that you can’t reach it’s web panel. That’s probably because you’ve not defined phpMyAdmin’s Apache configuration. In order to do that, you can create a symbolic link to phpMyAdmin’s default Apache configuration file under the /etc/apache2/conf.d directory like this:

sudo ln -s /etc/phpmyadmin/apache.conf phpmyadmin.conf

Then restart your server by typing:

sudo /etc/init.d/apache2 restart

And you’re ready to go!

sensimevanidus

24 June, 2009

Compiling locale definition files

While installing packages, it kept throwing this error:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “en_US.UTF-8”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).

So, I checked the output of the command locale:

onur@sensimevanidus:/etc/init.d$ locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

The first 3 lines of the output showed similar errors; so I checked available locales:

onur@sensimevanidus:/etc/init.d$ locale -a
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
POSIX

en_US.UTF-8 was not there! So I had to compile locale definition files by typing:

sudo localedef --no-archive -i en_US -c -f UTF-8 en_US.UTF-8

And I re-checked the available locales:

onur@sensimevanidus:/etc/init.d$ locale -a
C
en_US.utf8
POSIX

That was it.

sensimevanidus

24 June, 2009

How to make a user a sudoer

Sudo is the basic mechanism on Ubuntu to perform tasks that normally are reserved for the user root only. A sudoer is a user that can use the sudo mechanism.

By default, all users of the group admin are sudoers. That means adding a user to the admin group makes him/her also a sudoer. To do that, in your root account type this in the console:

adduser <name_of_the_user> admin

If you don’t want to add this user to the admin group; but make him/her a sudoer, you have to add the user to the sudoers file via the editor visudo. This editor is used to open a temporary file with the name /etc/sudoers. In this file, you can define all sudo tasks that are available on your server. You should never open the /etc/sudoers file for editing directly; because that involves the risk of completely locking yourself out if you make an error.

Default configuration in /etc/sudoers is as follows:

# /etc/sudoers
#
# This file MUST be edited with the ‘visudo’ command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults    env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

You could add a user (named onur) to the end of this file like this:

onur ALL=(ALL) ALL

That would allow the user onur to be a sudoer (and here, who can perform all sudo commands) after you saved the file. You could also restrict commands to him by specifying each command he could use, like this:

onur ALL=/sbin/shutdown

This would only allow onur to use the /sbin/shutdown command instead of all sudo commands.

If the admin group is not present in your installation, you can create one by typing:

Sudo is the basic mechanism on Ubuntu to perform tasks that normally are reserved for the user root only. A sudoer is a user that can use the sudo mechanism.

By default, all users of the group admin are sudoers. That means adding a user to the admin group makes him/her also a sudoer. To do that, in your root account type this in the console:

adduser <name_of_the_user> admin

If you don’t want to add this user to the admin group; but make him/her a sudoer, you have to add the user to the sudoers file via the editor visudo. This editor is used to open a temporary file with the name /etc/sudoers. In this file, you can define all sudo tasks that are available on your server. You should never open the /etc/sudoers file for editing directly; because that involves the risk of completely locking yourself out if you make an error.

Default configuration in /etc/sudoers is as follows:

# /etc/sudoers
#
# This file MUST be edited with the ‘visudo’ command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults    env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

You could add a user (named onur) to the end of this file like this:

onur ALL=(ALL) ALL

That would allow the user onur to be a sudoer (and here, who can perform all sudo commands) after you saved the file. You could also restrict commands to him by specifying each command he could use, like this:

onur ALL=/sbin/shutdown

This would only allow onur to use the /sbin/shutdown command instead of all sudo commands.

If the admin group is not present in your installation, you can create one by typing:

addgroup --system admin

This created a system group named admin and then you can manually add this line to the /etc/sudoers file via visudo:

%admin ALL=(ALL) ALL

sensimevanidus

17 June, 2009

MySQL 5.1 installation and remote-access configuration

MySQL 5.1 database server can be installed on an Ubuntu Server from the terminal by typing:

apt-get install mysql-server-5.1

The configuration files for MySQL are located in the /etc/mysql/ directory and the default MySQL database server configuration file is my.cnf (used to set global options). You can also create a file in your home directory (~/.my.cnf) to set user-specific options.

If you want your server to allow remote connections, first thing to do is to make your server listen to its external IP address. To do that, you must set the bind-address option to the external IP address in the my.cnf file, just like:

bind-address = <servers-external-ip-address>

But that’s not enough. You have to add users that have remote access. Detailed information can be found here:

http://dev.mysql.com/doc/refman/5.1/en/adding-users.html