Jason’s Ranting & Raving

Those who don’t read have no advantage over those who can’t.

Posts Tagged ‘Programming’

Programming: Android setup for Ubuntu

Posted by jaystile on September 27, 2010

Android Setup
I’ve been kicking the idea around of developing some Android applications. This blog post is about setting up that initial environment.

After a quick google search, I found the Android developer page. Downloaded the Linux (i386) SDK. I was thinking that I might want to develop on Windows, but I already had a VirtualBox running Ubuntu 10.04 with the Required Eclipse and JDK. I browsed the System Retirements page http://developer.android.com/sdk/requirements.html, and it looks the VM (Virtual Machine) will be just fine.

Preconfiguration
Java development downloads go to into the /opt directory on this linux VM. Softlinks are used to help maintain version of the software.

/opt/ant/latest → /opt/app/apache-ant-1.8.1
/opt/java/latest → /opt/java/jdk1.6.0_21
/opt/eclipse/latest →/opt/eclipse/eclipse-jee-helios

The ~/.bashrc file gets updated to add variables to these locations and then export the $PATH so you can execute them on the command line.

JAVA_HOME=/opt/java/latest
ANT_HOME=/opt/ant/latest
export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH

Download the Android SDK

$ mkdir /opt/android/
$ wget http://dl.google.com/android/android-sdk_r07-linux_x86.tgz
$ tar zxvf android-sdk_r07-linux_x86.tgz
$ mv android-sdk-linux_x86 android-sdk-linux_x86-0.9.8 
$ ln -s android-sdk-linux_x86-0.9.8/ latest

ADT Plugin v 0.9.8
The Eclipse workspace has been setup to use /opt/workspace/android. Eclipse then needed to include the ADT plug-in . Add the site: https://dl-ssl.google.com/android/eclipse/ to Eclipse after selection Help → Install Software.

Install SDK Tools r7 into your SDK
The link describes adding new tools into your sdk http://developer.android.com/sdk/adding-components.html#UpdatingComponents .

Launch the android manager.

$ /opt/android/latest/tools/android

It looks like the Android SDK Tool revision 7 was already there! Moving on.
Odd
I got a pop-up from the Plug-in asking to enable statistics usage. But it was ‘unclickable’. So it’s just hanging out there now, so I’ll have to kill it from the command line. I could tell if it came from the Android manager or the ADT eclipse plugin.

First Project
Used Eclipse to try and create a new Android Project. This failed because an SDK was not defined.
Register SDK with Eclipse
Window → Preferences → Android
SDK Location: /opt/android/latest
Apply
Error
Failed to get the adb version: Cannot run program “/opt/android/latest/tools/adb”: java.io.IOException:error=2, No such file or directory.
That is very odd. The file is navigable from the command line. However, when trying to execute it from the command line like ./adb, the bash: ./adb: No such file or directory error occurs.
Attempting to restart (and kill that pop-up that is still hanging around).

$ /opt/android/latest/tools/android

Install packages
Installed Packages → Android SDK Tools, revision 7. Clicked update all and selected all recommended downloads.
Then removed all obsolete packages.

Try Again – With Galileo
Downloaded Eclipse JEE Galileo, I heard rumor through the grapevine that ADT Plugin did not play nice with Helios.
This still did not resolve the original issue of the adb not working.

Add the site: https://dl-ssl.google.com/android/eclipse/ to Eclipse after selection Help → Install Software. In this instance, I made it to the license agreement. However, when selecting the radio, “I agree” and scrolling down to the bottom of the license window, the ‘Finish’ button was never enabled. Thank you Mylyn for having 9 things to agree to when all I’m trying to is NOT add your plug in. It is my professional opinion that Eclipse with JEE comes with way too many plug-ins that do not have value add. Eclipse is pretty stable, but the plug-in developers really need to get on the ball. I keep running into resource hogs or plug-ins that don’t do their job very well. Have you seen the GIT plug-in? It doesn’t even come close to mapping what you can do on the command line. Going to get Eclipse just for Java to minimize plug-ins to see if I can get this to install.

Try Again – With Galileo sans JEE garbage.
Installed the basic Eclipse IDE for Java Developers. There are noticably less plug-ins install that might collide. But Mylyn is still there. Let’s see how it goes. Oh look at that! It appears that I am in the same situation. Here are the list of things to agree to:

Android DDMS
Android Development Tools
Mylyn Bridge: Eclipse IDE
Mylyn Bridge: Java Development
Mylyn Bridge: Team Support
Mylyn Connector: Bugzilla
Mylyn Task List (Required)
Mylyn Task-Focused Interface (Recommended)
Mylyn WikiText

Let’s see if my outcome is any different this time. I’ll scroll to the bottom of each agree and check I accept. Wow! Different behavior! The ‘Finish’ button was able to be clicked. Joyous happy day.

Back on track – Setup the SDK
Window → Preferences → Android
SDK Location: /opt/android/latest
Apply

Configure the SDK
Eclipse → Window → Android SDK and AVD Manager
Something weird happened again.
After selecting the option above I received and error. [2010-09-17 06:12:09 – adb]Failed to get the adb version: Cannot run program “/opt/android/latest/tools/adb”: java.io.IOException: error=2, No such file or directory
After selecting it a second time, the application launched.
Android SDK and AVD Manager (ASAM) → Installed Packages → Update All…
Now ‘Delete…’ any obsolete packages.

I got another error saying that there are packages dependent on adb, but it isn’t running. I think the 0.9.8 SKD might be fubar. After more research, there is a problem with Ubuntu 64bit (which I’m running). It looks like some 32bit libraries need to be installed. There was nothing explicit on the Android SDK site, but after more searching I found references to the 32 bit libraries under an Ubuntu forum. Then I found a matching troubleshooting article on the SDK site.

$ sudo apt-get install ia32-libs
$ /opt/android/latest/tools/adb 

Yay! ADB now does something! That was the root of my problem. Now everything is right with the world. Two hours later my quest continues.

Ubuntu Linux 13.10 64-bit
The error ‘bash: ./adb: No such file or directory’ came back. But this time ‘install ia32-libs’ did not work. However, this seemed to do the trick:

$ sudo apt-get install libc6:i386 libstdc++6:i386

Hello World
Follow the instructions from the Android Site. http://developer.android.com/resources/tutorials/hello-world.html
After the errors before, getting the Hello, Android app running was a piece of cake. That emulator is pretty cool. Then I said, f@#$ it, and I set it up to run on my windows environment.

Posted in Programming | Tagged: , | 1 Comment »

Programming: Rails with Postgres – Couldn’t create database

Posted by jaystile on August 31, 2010

Hello internet community! I wanted to share something with you that you may encounter. I was setting up a postgres database and trying to use rake db:create to create the sample database from the getting started tutorial.

I kept getting an error like:

Read error message: FATAL: Ident authentication failed for user "blog_development
...
Couldn't create database for {"encoding"=>"unicode", "username"=>"blog_development", "adapter"=>"postgresql", "database"=>"blog_development", "pool"=>5, "password"=>"tmppass"}

The default Ubuntu Postgres installation requires both a system user and a database user with matching user IDs. It turns out I had to change a setting in postgres to allow connections from just database users.

vi /etc/postgresql/8.4/main/pg_hba.conf
Change the line
local all all ident
to
local all all md5

Note: You cannot have two of the same defined combinations. The first once wins, when I had put “local all all ident” followed by “local all all md5” the second didn’t get defined. That caused me a couple of hours of heartache.

Posted in Programming | Tagged: , , | 1 Comment »

Setting up Eclipse with Windows 7 issues

Posted by jaystile on June 22, 2010

I’ve bought myself a new PC which came with Windows 7. I’ve been running versions of Ubuntu on old hardware since version 6.04 and I thought I’d give the new Windows a test drive. At work we run on linux distros so this is almost a new experience for me. My friend really likes Windows 7 and to be honest I was getting a little sick and tired of external hardware not working. Specifically my webcam (which Logitech doesn’t for Windows 7 for some damn reason, I’m done doing buying anything from them) and the scanner my other friend let me borrow.

I’m working on a few side development tasks on my own. I program a lot of java so I went through and installed the latest JDK and tried to find where in the heck you set the environment variables. But I got that resolved. Next, I go and download Eclipse. I extracted the zip file with the default Windows 7 ‘Extract’ option. When I tried to run eclipse I get a weird error about a shared library not being found.

Anyway, I went searching for the answer and it looks like the Windows 7 zip handler doesn’t handle the eclipse-jee-galileo-SR2-win32.zip very well. So, I downloaded cygwin, extracted the zip, moved the eclipse folder to /cygdrive/c/eclipse. Tried to fire up eclipse again and it failed with a permissions exception. chmod -R 755 /cygdrive/c/eclipse and I was up and running. You can color me unimpressed that Windows 7 failed to correctly extract an archive.

Posted in Programming | Tagged: | Leave a Comment »

INSERT CLOB with JDBC and Oracle Prepared Statement

Posted by jaystile on May 24, 2010

The oracle API made me crazy for about six hours. That is how long it took me before I could figure out how to do an insert that includes a clob. The key to do an insert with a clob is to use the oracle.sql.CLOB.createTemporary method. The javadoc associated with the CLOB class is very sparse and does not include any instructions.

In my scenario I was trying to do an insert with a prepared statement. The Prepared Statement has a setClob method, but it really doesn’t do you any good because you don’t have a clob object available. Most of the examples I found showed a technique of creating an empty clob object and then getting a reference to the clob and then streaming data to it. This was not going to work for me because I’m running batches of statements with thousands of inserts.

This is what worked for me. It might need a little tweaking since I’m blogging from home without my development environment, so take it with a grain of salt.

// Pseudo-code
import oracle.sql.CLOB
import java.sql.Connection
import java.sql.PreparedStatement
...
public void setClobInStatement(Connection conn, PreparedStatement ps, String longText) {

// I don't know what 'false' means versus 'true' in the following statement
CLOB c = CLOB.createTemporary(conn, false, CLOB.DURATION_SESSION);

// The 1 is the position to start inserting the string into the clob.
c.setString(1, longText);

// Set the temporary clob in the statement, it becomes permanent when the statement executes.
// If the clob is the first object to be set in the statement, use 1
ps.setClob(1, c);
}

Gotcha’s

  • The PreparedStatement.setString() will work with text up to 4000 characters. With strings longer than that it will fail with a cryptic exception, ORA-00600, I believe.
  • If you’re supporting multiple databases, you’ll have to create an Oracle specific class and implement an interface so you can swap out the implementation. This requires direct use of the Oracle API.

References
La Javadoc En Francais (It’s in english and the most complete)
oracle.sql.CLOB
oracle.jdbc.driver.OraclePreparedStatement

Posted in Programming | Tagged: | Leave a Comment »

Postgres 8.3, JDBC, JPA, and Hibernate example setup.

Posted by jaystile on April 1, 2009

I wanted to access my local postgresql database with JDBC and JPA in the J2SE environment. Refer to my previous post about setting up Postgres 8.3 on Ubuntu 8.04.

If you can’t tell, I’m using java. I’m compiling to java 5. I’m building my project with maven. Inside of a larger maven project which creates my ear file, I created a test project ‘runner’. This creates a jar.
runner/pom.xml

<project> <modelVersion>4.0.0</modelVersion> <groupId>org.myproject</groupId> <artifactId>runner</artifactId> <packaging>jar</packaging> <name>JAR Runner</name> <version>1.0</version> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>org.myproject</groupId> <artifactId>util</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.3-603.jdbc4</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>ejb3-persistence</artifactId> <version>1.0.2.GA</version> </dependency> <dependency> <!-- Note this added other dependencies - hibernate-commons-annotations-3.1.0.GA.jar - slf4j-api.1.4.2.jar - exclude this because it is bunk and causes exceptions. - hibernate-annotations-3.4.0.GA.jar - antlr-2.7.6.jar - commons-collections-3.1.jar - dom4j-1.6.1.jar - xml-apis-1.0.b2.jar - jta-1.1.jar - javassit-3.4.GA.jar --> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager </artifactId> <version>3.4.0.GA</version> <exclusions> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusions> </dependency> <!-- The org.slf4j needed to be included because the one included in org.hibernate.ejb3-persistence did not work --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> </dependencies> </project>

runner/src/main/resources/META-INF/persistence.xml file
The core of JPA uses the persistence.xml file to set up the database and entity mappings. I tried using Sun’s example but I found it to hard to configure. I know that hibernate provides a JPA implementation so I used that instead of the glassfish example. It was easy to add because it exists in the maven 2 repository. This persistence.xml is configured to talk to the postgresql 8.3 database. It also contains a mapping for my Entity.

<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL"> <class>org.myproject.runner.MapEntity</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <!-- <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> --> <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> <property name="hibernate.connection.username" value="myuser"/> <property name="hibernate.connection.password" value="mypassword"/> <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/mydb"/> <property name="hibernate.max_fetch_depth" value="3"/> <!-- cache configuration --> <!-- This was blowing up -v <property name="hibernate.ejb.classcache.org.hibernate.ejb.test.Item" value="read-write"/> <property name="hibernate.ejb.collectioncache.org.hibernate.ejb.test.Item.distributors" value="read-write, RegionName"/> --> <!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file --> <!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ --> </properties> </persistence-unit> </persistence>

Our database is populated by some sql.

create table map ( pk_id int unique primary key, width int, height int, name varchar(256), last_updated timestamp ); GRANT ALL ON map TO PUBLIC; INSERT INTO map (pk_id, width, height, name, last_updated) VALUES (1, 100, 200, 'test_map', CURRENT_TIMESTAMP);

MapEntity.java
Next, we have an entity to use against our database. It only maps one field, but this was just for testing purposes.

package org.myproject.runner; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="MAP") public class MapEntity { private long id; @Id @Column(name="PK_ID") public long getId() { return id; } public void setId(long id) { this.id = id; } }

DatabaseAccess.java
Finally, we have a java class that can call the database via JDBC or JPA

package org.myproject.runner; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class DatabaseAccess { /**
  • @param args
  • /
public static void main(String[] args) { testJdbc(); testJpa(); } public static void testJdbc() { String url = "jdbc:postgresql://localhost/mydb"; Properties props = new Properties(); props.setProperty("user","myuser"); props.setProperty("password","mypassword"); Connection conn = null; try { conn = DriverManager.getConnection(url, props); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from map"); while(rs.next()) { long id = rs.getLong(1); System.out.println("ID:" + id); } } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void testJpa() { // Use persistence.xml configuration EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1"); EntityManager em = emf.createEntityManager(); // Retrieve an application managed entity manager // Work with the EM MapEntity map = new MapEntity(); map.setId(5); EntityTransaction transaction = em.getTransaction(); transaction.begin(); em.persist(map); transaction.commit(); em.close(); emf.close(); //close at application end // Create EntityManagerFactory for persistent unit named "pu1" // to be used in this test } }

Posted in Programming | Tagged: , | 4 Comments »

Perl: Converting Decimal to Degrees Minutes Seconds (and vice-versa)

Posted by jaystile on February 21, 2009

Here is some code I playing with trying to get conversion from one coordinate system to another. There is some percision loss due to the floating point math in the conversions. So expect to see a loss of a few seconds when converting from decimal.


1 #!/usr/bin/perl
2
3 use strict;
4 use POSIX qw(ceil floor);
5
6 # flush print to the output after every write.
7 $| = 1;
8
9 testConvertDegMinSecToDecimal();
10 testConvertDecimalToDegMinSec();
11
12 ##### TEST ROUTINES
13 sub testConvertDegMinSecToDecimal {
14 my $result = convertDegMinSecToDecimal(121, 8, 6);
15 print "RESULT: $result\n";
16
17 my $result = convertDegMinSecToDecimal(121, 59, 59);
18 print "RESULT: $result\n";
19 }
20
21 sub testConvertDecimalToDegMinSec {
22 my $deg; my $min; my $sec;
23 ($deg, $min, $sec) = convertDecimalToDegMinSec(121.135);
24 print "RESULT - DEG: $deg MIN: $min SEC: $sec\n";
25
26 ($deg, $min, $sec) = convertDecimalToDegMinSec(121.99317);
27 print "RESULT - DEG: $deg MIN: $min SEC: $sec\n";
28 }
29
30 ##### SUB ROUTINES
31 sub convertDegMinSecToDecimal {
32 my $deg = $_[0];
33 my $min = $_[1];
34 my $sec = $_[2];
35
36 my $result = $deg + ($min/60) + (($sec/60) *(1/60));
37 $result = sprintf("%.5f", $result);
38 }
39
40 sub convertDecimalToDegMinSec {
41 my $input = $_[0];
42 my $deg = floor($input);
43
44 $input = $input - $deg;
45 $input = $input * 60;
46
47 my $min = floor($input);
48
49 $input = $input - $min;
50 $input = $input * 60;
51
52 my $sec = floor($input);
53
54 return ($deg, $min, $sec);
55 }

If you’re playing with degrees minutes seconds & decimal degree conversions you’re probably interested in calculating distance on the Earth. There are two common algorithms for calculating distance that are available as CPAN modules:

Posted in Programming | Tagged: , , | Leave a Comment »

Ubuntu: Howto Setup a Local Subversion Repository

Posted by jaystile on February 13, 2009

This Howto is targeted for Ubuntu 8.04, Hardy Heron. Specifically, this will go through a quick start to get subversion running on the file system. Which, by the way, is NOT the recommended way of setting it up. But if you’re doing some local (1-person) development it is the quickest. The documentation for subversion can be found at svnbook.red-bean.com

Get Subversion!
I used the Synaptec Package Manager to download the ‘subversion’ package. You could probably done just as well using ‘apt-get’.

Create Repository!

svnadmin create /var/svnroot/

Import your code into the repository!
Where LocalDir is like /home/username/MyProject
and Where ProjectName is like MyProject

svn import LocalDir file:///var/svnroot/ProjectName

Check out your code from the repository!

svn checkout file:///var/svnroot/PorjectName

Your code is now under version control! (Don’t you feel all grown up now?)

Posted in Programming | Tagged: , | Leave a Comment »

Ubuntu: Howto Setup a Local PostgreSQL and pgAdmin

Posted by jaystile on February 13, 2009

I’m setting up a project for some prototyping web development I’ve been doing. I’m looking at configuring Ubuntu 8.04 Hardy Heron to use Postgresql 8.3. I’ve decided to document my steps (so I don’t forget them).

First off, I’m borrowing heavily from this site: www.ubuntugeek.com which configured Postgresql 8.2 for Ubuntu 7.10. Because I’ve found that Ubuntu, while very helpful in making installations easier, usually does not follow the default installation instructions. Of course you could always RTFM: PostgreSQL Documentation

Download the Postgres 8.3
I used Synaptec Package Manager tool to download the packages ‘postgresql’, ‘postresql-client’, and ‘pgadmin3’. I’m sure you could use the ‘apt-get’ instead.

Configure Postgres Login
Now we need to reset the password for the ‘postgres’ admin account for the server. If you’re going to expose this to the outside world replace ‘password’ with a strong password, like ‘I3!ABaie#9’.

sudo su postgres -c "psql template1"
template1=# ALTER USER postgres WITH PASSWORD ‘password’;
template1=# \q

For version 8.4 I had to use the following code to get pdAdminIII to work:

sudo -u postgres psql postgres
psql (8.4.4)
Type "help" for help.

postgres=# \password postgres
Enter new password:
Enter it again:
postgres=# \q

That alters the password for within the database, now we need to do the same for the unix user ‘postgres’. Make sure you use the password you entered inside of psql.

sudo passwd -d postgres
sudo su postgres -c passwd

From here on in we can use both pgAdmin and command-line access (as the postgres user) to run the database server. Postgres 8.3 is configured to use local access only by default. If you want to open up PostgreSQL follow the link mentioned at the beginning of this article.

Restart the PostgreSQL server.

sudo /etc/init.d/postgresql-8.3 restart

Troubleshooting
I was having a hard time logging with pgadmin3. It turns out I was forgetting the ‘;’ semicolon in the ALTER USER statement.

Now What?
Now, that you have a database why don’t you do something with it? Like write a java application.

Posted in Programming | Tagged: | 2 Comments »

Book Review: Refactoring by Martin Fowler

Posted by jaystile on November 19, 2008

Originally Posted: Tuesday, October 02, 2007

I finished reading Martin Fowler’s “Refactoring”. This is a book regarding object oriented software development. I thought it was pretty good. I don’t think I learned that much from it since I’ve been developing software for a very long time now. It would be good for a newbie or average developer. This is definitely a reference book. I think I like “Head First: Design Patterns” better. That book invovles refactoring to design patterns, not just simply refactoring bad smelling code. Just so you know, “Code Smells” are a chapter in the book , not some strange euphemism I made up. “Refactoring” is one of the standard books to appear on most developers desks, they should try reading it sometime.

Posted in Book Review | Tagged: , | Leave a Comment »

Book Review: Agile Project Management with Scrum by Ken Schwaber

Posted by jaystile on November 18, 2008

This is the second book on Scrum by Ken Schwaber that I’ve read. The first book was ‘Agile Software Development with Scrum’. This book is ‘Agile Project Management with Scrum’. The book continues to drive home the values that the Scrum process brings to software development. It is essentially a series of case studies showing how scrum did or did not work in different scenarios. This leads to a general understanding of what Scrum should ‘feel’ like when working within its boundries. It’s worth the read if you are a software development manager or someone who is currently trying to implement Scrum on one of your programs.

Posted in Book Review | Tagged: , | Leave a Comment »