"; */ ?>

java


19
Nov 08

ZX-Spectrum Child

zx speccy logoMy first computer book was “Basic for Children with Arturo”. It was 1989, the book was in Russian, and I was 10…

Back then, in USSR, to own a 8086 compatible PC was somewhat mystical, and therefore people, who knew a thing or two about microelectronics, were putting together Speccy(s) (ZX Spectrums), which were based on Zilog Z80 8-bit microprocessor and ran at 3.5 MHz.

The official ZX-Spectrum that was sold by “Sinclair Research Ltd” at that time was a bit pricey for middle class families, and therefore many cities across Europe and USSR started to come up with their own “clones“. There were over 50(!) semi-official clones that came out by the end of 1980s, beginning of 1990s. But to cut cost even further, people who knew “how to solder” created hybrids of the clones at home, which came out to be very cheap.

The process to build ZX-Spectrum’s clone’s hybrid was “a bit” involved, and included many days/weeks of hacking poor, incomplete electrical circuit documentation and sleepless nights of manual calibration of the hardware. The one had to “get” a ZX-Spectrum full circuit schema:

zx spectrum electrical circuit

Study it, change it to fit the budget. Go to several flea markets to first find a CPU, which was a quite rare find:

zilog z80 cpu

plus many more components: an empty electrical board, all other chip, transistors, capacitors, resistors, etc…

After some time and a combination of new ideas, documentation, hardware and labour, the one ended up with something like this:

zx-spectrum circut board

ZX-Spectrum keyboard had an interesting feature – pretty much every key, depending on the combination of special keys used, could represent 3-4, sometimes even 5 different things (commands/letters/symbols/special characters/etc). This made it much smaller (“compacter”) than for example PC keyboards:

zx-spectrum keyboard

And due to its small size, it most of the time served as a ZX-Spectrum case, and fit it the Speccy’s “motherboard”:

zx-spectrum under the keyboard

There were also some alternatives, and some people used “an extended” keyboards, but “a single key multi-purpose” pattern was still there. Here is a snippet of the Russian ZX-Spectrum keyboard, for example:

zx-spectrum russian keyboard

which is “a bit extended” – has a separate cursor, and some other keys. But take a look at the individual keys – due to the additional Cyrillic letters, “an average key” is now used to represent even more (+1) values.

At this point, when most of the work with oscillograph was behind, the only true way to see if the creation works was to see it with your own eyes. The word “monitor” in a sense of a “computer monitor” was not a widely used due to its price mostly, therefore most of ZX-Spectrum people used old black and white (at first) TVs. Speccy (well it’s clone’s hybrid at least) was designed in a way that it could be pugged into a regular TV Antena’s jack:

zx-spectrum hookup to TV

And due to the fact that family is usually more than one person, the main TV often was not used, and people used old TVs as a Speccy “monitor” instead:

zx-spectrum TV monitor

The whole process above was about 50% of the job – yea, just 50%. The other half was split on two. First quarter was spent in calibrating hundreds of little circuits, in order to get a sacred “© 1982 Sinclair Research Ltd” message at the bottom of TV (monitor):

1982 Sinclair Research Ltd

This message brought lots and lots of joy into families once it appeared on the screen. It meant a lot – CPU was working (so that guy on the flea market who sold it to you was ok.. sigh…). ROM was working (so that guy on the flea mark…). CPU and ROM were calibrated to work together, along with RAM and some other parts.

Usually the one plugged in Speccy to TV with no RAM/ROM/CPU at first, and calibrated all the circuits to reach a documented pattern on TV (for example small squares). After that was achieved, RAM/ROM and CPU were inserted one after the other by the same technique – each had to produce a certain visual pattern. So if all the parts were inserted and the message “© 1982 Sinclair Research Ltd” appeared, job was 75% done.

The last quarter was usually spent to tune all the parts to work as designed. Most of the time Speccy would restart in 3-4 minutes of use, just because something was “out of it”. To find that “something” was an art on its own. There was no Internet, no books or literature on this – it was pure work of art!

Fortunately my father is such an artist, and by the time I was 10, I got one of those Speccy beasts that my dad and I put together. The only problem though was that although it was designed to have 64Kb of RAM, it only had 16Kb. Well, 7 months later we solder more capacitors around 4Kb RAM memory chips, and that fixed it, but for 7 months I could not really play any games, because most of them required 64Kb to be there. Hence, the “Basic for Children with Arturo” book did not get a chance to get too dusty…

Where ZX-Spectrum shined the most is of course boot time – there was none :) Well, everything was in a ROM chip, including Sinclair Basic.

So having spent about 4-5 months to build Speccy, I was out of luck, and could not use it. Well, that is how I saw it for the first one or two weeks. But then…. I pressed a “p” key, which by default was a Basic “print” command, and then…. I typed what was on my mind: “I want to play!”, and to my astonishment Speccy “said“: “I want to play!”.

It is probably hard to surprise anybody right now by the fact that computer prints something on the screen, when you ask him/her to, but for me, being 10, and haven’t seen anything cooler than that, it was the biggest technical discovery of all times. I picked up my Basic book, and started to type, and type, and type, and then…. type a little more. That is how I started. That, in programming sense, makes me a “Speccy Child”.

A program in Basic in Speccy looked something like this:

zx-spectrum Sinclair Basic

where every line had to have a number, and the usual increment was 10. Quickly my favourite commands became “peek”, “poke” and “randomize” which worked directly with the memory (absolute addresses). And although I felt like a memory King, something was missing. I figured out what it was after I bought a system program that exposed me to something much more powerful than Basic – it was 8-bit Assembly language, the language that Zilog Z80 talked natively. Therefore me together with Z80, could control pretty much everything in the Speccy kingdom. That was awesome!

Here is an example on how you’d print a line of text to the screen in Speccy’s native language:

zx-spectrum z80 assembly

Cool – huh!? Another thing that was very useful at the beginning of my Assembly journey, was a documented character table ( like an “ASCII” ):

zx-spectrum ascii table

The only problem was that in order to work in Assembly, or in any other language, or with any other system program – it needed to be loaded since it was not in RAM/ROM – and that took time. No, really took time. It needed to be loaded from a tape – a regular cassette that is used in a tape recorder. In fact these very tape recorders were used to load all these neat programs in:

zx-spectrum tape

But not only it did take time to load, it was a constant pain to tune the head of the tape recorder for each tape bought on the flea market, since nobody followed the recording standards. A lot of times, you would have to sit and hold a screwdriver against the recorder’s head for 3-5, even 10 minutes, depending on how big the program was.

Here is what the screen looked like when the program was loading from tape into RAM. You can see the zebra looking lines, they were the moving loading indicator:

zx-spectrum loading screen

But time went, flea markets grew along with demand, and we upgraded to the real “RGB deal”. It was done by soldering an external controller (kind of like modern video cards, but not really) that had an RGB jack:

zx-spectrum rgb jack

Although it had 9 pins, only 5 were used: Red / Green / Blue / Sync / Ground. But we did not stop on this. Soldered another external controller, but this time for a disk drive! Oh yea – that was a huuuge deal. Looking at programs to load for seconds (well in rear occasions minutes) from these beautiful 5 inch disks!

Since we had a disk drive, Speccy needed to catch up with us, and the disk controller brought another cool thing, which was TR-DOS, where, of course, DOS stands for Disk Operating System. That is how pretty it looked:

zx-spectrum tr-dos os

Did you notice some colour? Good catch :) So now it was possible to actually browse the disk, and start programs with a button click. There were tons of system utilities with different browsers that you could start at the start up time (sort of like in MBS, but not).

Right after TR-DOS, I felt like I need more. I need more.. I need more.. RAM! And we soldered another level of RAM on top of 64Kb chips. So now my Speccy was equipped with 128Kb of RAM, which could free up some ROM real estate, which enabled ROM to fit a little menu on startup:

zx-spectrum 128 menu

At this point not only I could swim in the Assembly language ocean, but also play games, and use various utilities!

First let’s look at Photoshop:

zx-spectrum artstudio

well, maybe not the Photoshop you’re used to see right now, but Art Studio rocked! I was drawing away for hours!

Now, how about CuBase/ProTools:

zx-spectrum wham the music box

well, maybe not exactly the latest CuBase, but I wrote lots of tunes in Wham. Some tunes I actually recorded along with playing a real guitar – it was cool!

As to games, my favourite control was my keyboard, but for many friends that came to play, I bought a joystick:

zx-spectrum joystick

Now we could play multiplayer games like:

zx-spectrum match point game

Match Point (Tennis) or:

zx-spectrum fist game

Fist (Karate) game. Or even:

zx-spectrum tetris2 game

Tetris2 – which was a bit different from the regular Tetris in a way it gave different patterns and challenges on different levels, and was also a multiplayer game, which was of course cool.

There were tons of other games, but two I still remember are Robocop:

zx-spectrum robocop game

and Bomb Jack:

zx-spectrum bomb jack game

I could play long long time in Bomb Jack, not longer than I spent writing my own little games in Assembly language of course, but still – it was good amount of Bomb Jack time.

All the above was of course some time ago… 25, or even soon 30 years. Everything is a bit different now. Speccy lives somewhere in museum behind the glass:

zx-spectrum in museum behind the glass

But I remember him/her – those were times of big discoveries for me!

Like probably right now for some other 10 year old kid who just accidentally pressed a “p” button, and discovered….. time traveling…

Good luck to you kid!


22
Oct 08

Use HSQLDB Functions with Hibernate Mapping Files

Let’s say you need to do a “local integration” test, where you code does not depend on external systems (queues/external servers/DB/etc..). Creating unit tests, mocking/stubbing everything out is all good, but sometimes you need to be able to run tests that are as close as possible to the “real world” deal, while you are in “local mode” – e.g. plane, subway, basically somewhere without access to the real external systems.

One of ways you can approach it with databases is to load schemas you need in memory, and work (test) against those schemas. This is relatively easy to do with HSQLDB, and there are many “googlable” guides on how to do it. However in this little howto, I want to show you how you can define your own HSQLDB functions, and use them as either “stubs” (or even “real deal”).

Here is an example on why you may need it. Consider this Hibernate mapping file that is used in your application:

 
<?xml version="1.0"?>
 
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping
	package="org.project.customer.dto.search">
	<class name="CustomerCreditCardSearchResultDto" table="CR_CREDIT_CARD">
 
		<id name="id" column="CR_CREDIT_CARD_ID">
			<generator class="native">
                                 <param name="sequence">SQ_CR_CREDIT_CARD</param>
			</generator>
		</id>
 
                <property name="accountNumber" formula="some_pkg.decode( ACCOUNT_NUMBER )" />
                <property name="secretNumber" formula="some_pkg.decode( SECRET_NUMBER )" />
                <property name="expirationDate" column="EXPIRATION_DATE" type="date" />
                <property name="zipCode" column="ZIP_CODE" />
 
	</class>
</hibernate-mapping>

Let’s say you created a CR_CREDIT_CARD table in in-memory DB, populated it, started your test. Now when you try to read a “CustomerCreditCardSearchResultDto” object somewhere in your test, Hibernate will construct an SQL query from the mapping file above, and execute it on the in-memory DB.

However there is a problem – it is going to fail with a similar Exception:

Caused by: java.sql.SQLException: Unexpected token: DECODE in statement [select <query here> .... ]
	at org.hsqldb.jdbc.Util.throwError(Unknown Source)
	at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
	at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
	at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:248)
	at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:302)
	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:442)
	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:368)
	at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561)
	at org.hibernate.loader.Loader.doQuery(Loader.java:661)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
	at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
	... 60 more

If there is an Exception – there is a Reason for it :) Notice these two mappings in the Hibernate mapping file above:

<property name="accountNumber" formula="some_pkg.decode( ACCOUNT_NUMBER )" />
<property name="secretNumber" formula="some_pkg.decode( SECRET_NUMBER )" />

That says Hibernate to construct SQL that uses this “some_pkg.decode()” custom function/store procedure after (in case of SELECT) reading these values from a database. However, in you local testing environment, you do not have this function defined. It is probably defined in the DB (like Oracle/DB2/etc) itself.

But for most, if not for all, technical problems there is a solution, so don’t worry – you can define this function(s) yourself. Since it is a test, the most logical thing, since you want to abstract the code out from the external systems (and their functions) would be to define stubs to these functions.

Let’s create a utility “HsqlFunctions” Java class with static methods to be used by HSQLDB (it can only use static Java methods in its SQL, btw):

package org.project.test.util;
 
/**
 * HSQL functions to be aliased.
 *
 *    DDL example:
 *
 *    CREATE ALIAS DECODE FOR "org.project.test.util.HsqlFunctions.decodeString"
 *
 *
 * @author blog.dotkam.com
 *
 */
 
public final class HsqlFunctions
{
   private HsqlFunctions()
   {
     // static utility class - does not need to be constructed.
   }
 
    /**
     * Stub for the decode function.
     * Usually used to "please" Hibernate Mapping Files.
     *
     * @param value - String value for the column
     * @return - returns the same String value
     */
    public static String decodeString( String value )
    {
        return value;
    }
}

Now, when creating a test schema, you can ALIAS this static method as HSQLDB function like this:

     CREATE ALIAS DECODE FOR "org.project.test.util.HsqlFunctions.decodeString"

This will tell HSQLDB to call “decodeString” static Java method on the column value, every time it sees “decode( COLUMN )” in SQL.

One thing to notice, though – make sure the type that the Java methods take are exactly the same as defined by DDL (Database Schema). For example, if you pass in column value as an Object:

    public static String decodeString( Object value )
    {
        return String.valueOf (value );
    }

And the column is defined as VARCHAR in schema, HSQLDB will try to pack that String into an Object, and it will fail with a similar exception:

Caused by: java.sql.SQLException: Wrong data type: hexadecimal string with odd number of characters in statement [select ... <query here>]
	at org.hsqldb.jdbc.Util.throwError(Unknown Source)
	at org.hsqldb.jdbc.jdbcPreparedStatement.executeQuery(Unknown Source)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
	at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
	at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
	at org.hibernate.loader.Loader.doQuery(Loader.java:662)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
	at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
	... 60 more

Below is just an excerpt on how you would set up the schema to use it with HSQLDB in your test:

    ....
 
    private static final String CREATE_ALIASES =
        "CREATE ALIAS DECODE " +
        "FOR \"org.project.test.util.HsqlFunctions.decodeString\"";
 
    private static final String CREATE_SEQUENCES =
        "DROP SEQUENCE SQ_CR_CREDIT_CARD IF EXISTS;" +
 
        "CREATE SEQUENCE SQ_CR_CREDIT_CARD " +
        "START WITH 1 INCREMENT BY 1";
 
    private static final String CREATE_TABLES =
        "DROP TABLE cr_credit_card IF EXISTS;" +
 
        "CREATE TABLE cr_credit_card (" +
            "cr_credit_id NUMERIC(15), " +
            "account_number VARCHAR(48)," +
             ....
             ....
   ....
 
        //  Creating the schema
 
        m_jdbcTemplate.execute( CREATE_ALIASES );
        m_jdbcTemplate.execute( CREATE_SEQUENCES );
        m_jdbcTemplate.execute( CREATE_TABLES );
 
        //  HSQLDB is ready to be populated with data at this point.
   ....

You can use DBUnit to create the schema above and populate it with the data. The above is just a straight forward hardcoded example.
Notice how it also creates a sequence “SQ_CR_CREDIT_CARD” to please the Hibernate, and others who might use it in the application.

Happy “local integration” testing!

Feel free to post questions/comments/suggestions, I’ll try to respond when have a free second or two :)


15
Sep 08

Reset Lost Password in Sun Application Server

Sun Application Server Password ResetHappens to the best of us, less with system admins more with developers. But we are all human, and believe it or not we DO forget and loose passwords at least once every so often. Some time ago I wrote a tutorial on how to reset lost root password in mysql, and here is another similar tutorial on how to reset the lost domain password but this time for Sun Application Server.

Before going any further with this article, please first check “.asadminprefs” file:

cat /home/toly/.asadminprefs
 
AS_ADMIN_USER=admin
AS_ADMIN_PASSWORD=YourSecretPasswordInTextHere!

the admin password could be there

If it is not there, there are two ways to reset it:

  • Reinstall or recreate the affected domain.
  • Create a new dummy domain and copy its security key file over to the real domain to substitute the password.

Below is an explanation for the second approach (in case when “reinstall or recreate affected domain” is not an option):

Given:

 >   Sun App server is installed in                 "/opt/SUNWappserver"
 >   Domain to which the password is lost:   "domain1"

Step 1. Creating a new dummy domain

/opt/SUNWappserver/bin/asadmin create-domain --adminport 7070 --adminuser admin --instanceport 7071 dummy-domain
 
Please enter the admin password>password
Please enter the admin password again>password
Please enter the master password>password
Please enter the master password again>password
 
Domain dummy-domain created.

Step 2. Copy dummy-domain’s “admin-keyfile” to domain1’s “admin-keyfile”

cp /opt/SUNWappserver/domains/dummy-domain/config/admin-keyfile  /opt/SUNWappserver/domains/domain1/config/admin-keyfile

now the password for domain1 is “password” – DONE :)

Step 3. Deleting the dummy domain

/opt/SUNWappserver/bin/asadmin delete-domain dummy-domain
Domain dummy-domain deleted.

NOTES:

The above is true for Sun’s Application Server 8.x and later.

For Sun’s Application Server 9.x check out “change-admin-password


11
Sep 08

Install Sun Application Server on Ubuntu

Sun Microsystems

On the client site when working on Java (or should I say JEE) projects, the application servers are most of the time “rotate” between these four: JBoss, Websphere, Weblogic, and someties Geronimo. However there are some clients who have pretty tight contracts with Sun, and in these cases the Sun Application Server is used.

I think now days in development world in general, well except maybe .NET part of it, Ubuntu and/or Mac OS slowly become OSs of choice for developers. Sun app server however is not the most used app server out there, and since its installation could be a bit non-straightforward, here are four simple steps on how to install it on Ubuntu box:

Step 1. Download the “.bin” form sun:

“Sun Java System Application Server”

http://java.sun.com/j2ee/1.4/download.html (I needed 8.2, but there are more recent ones)

Step 2. Change permissions, to make it runnable:

chmod 744 sjsas_pe-8_2-linux.bin

Step 3. If you just run it:

./sjsas_pe-8_2-linux.bin

It is going to complaint that it is missing a standard c++ library:

./sjsas_pe-8_2-linux.bin: error while loading shared libraries: libstdc++-libc6.2-2.so.3: cannot open shared object file: No such file or directory

Having done some “apt-cache” searches ( apt-cache search libstdc++ ), found that Ubuntu has “libstdc++.so.6” in /usr/lib.
From /usr/lib run:

 sudo ln -s libstdc++.so.6 libstdc++-libc6.2-2.so.3

Step 4. Run it now, it will install Sun App Server successfully! * **

* Do not run installation as root – it will fail (for most sun app servers versions)

** If you use any form of Beryl (or some Compiz’es), disable it, or reload window manager as a “Gnome Manager”. This is due to the fact the the installer is written in Swing, and Swing does not get along too well with some display managers.


9
Jul 08

Spring Web Application Context Visibility

spring framework logoWhile developing a Web App using Spring little things can take a lot of time to resolve. At the end they may appear to be very simple, and you may ask your self “how could I not think of it before – it is so obvious!”. Well, yea, it is obvious, but you just have to know it! One of the places that helps you to solve the “obvious” (and not so obvious), so you do not have to spin your wheels is, with no doubts, the spring forum. However you need to know the right search criteria to find what you need.

Here I just want to share something small but important about the visibility of Spring Contexts in a Web App.

Looking at org.springframework.web.servlet.DispatcherServlet API notice this:

“A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.”

This brings an interesting point – in a Spring Web App you have one root application context which is private, and many dispatcher servlet application contexts which are children of the root application context:

<context-param>
<param-name>contextConfigLocation</param-name>
   <param-value>
      /WEB-INF/classes/applicationContext.xml
      /WEB-INF/classes/otherContext.xml
   </param-value>
 </context-param>
 
...
 
 <servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
...
 
 <servlet>
  <servlet-name>dispatcher-servlet-number-x</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>

In the above, everything that is defined in “contextConfigLocation” is your root application context. Every “dispatcher-servlet-number-x” (“dispatcher-servlet-number-1”, “dispatcher-servlet-number-2”, etc..) would represent a child application context which will see (have the visibility to) all the beans from the parent (root) context, but will not see any of the beans defined by its siblings – another dispatcher servlet contexts.

The only gotcha in the above visibility is BeanFactoryPostProcessor / BeanPostProcessor (s) – like “PropertyPlaceholderConfigurer” for example. These guys apply ONLY to beans in its own context. For example, if you have PropertyPlaceholderConfigurer defined in the root application context, none of root’s children will be able to use it (see) the ${properties}.

Here is the semi-official version “why” from Juergen (Spring Lead Developer):

“PropertyPlaceholderConfigurer is an implementation of the BeanFactoryPostProcessor interface: This interface and its sibling BeanPostProcessor just apply to the BeanFactory that defines them, that is, to the application context that defines them.

If you combine multiple config files into a single contextConfigLocation, a PropertyPlaceholderConfigurer defined in any of the files will apply to all of the files, because they are loaded into a single application context.

However, a DispatcherServlet has its own application context, just using the root web application context as parent. Therefore, it needs to define its own BeanFactoryPostProcessors and/or BeanPostProcessors, in this case its own PropertyPlaceholderConfigurer.”

Happy Springing!