Deprecated Garbage Collections – Kenny Lee Chee Wei

A truckload of garbage by Kenny Lee Chee Wei

Archive for August 2007

JobsDB is either still in 20th century and/or likes M$ more than firefox

leave a comment »

If you log-in to jobsdb.com using Firefox, you see this message.

JobsDB and IE

I wonder if jobsdb.com itself is hiring

Written by kennii

August 30, 2007 at 1:37 pm

Posted in Job Hunting

Strings

leave a comment »

  1. String s = "f" + "o" + "o"
  2. is faster than

    String s = "f";
    s+="o";
    s+="o";

  3. The comparison
    ('a' == s.charAt(0))
  4. is faster than

    s.startsWith("a");

  5. The StringBuffer default capacity is 16. If your string is going to be larger than that, you might as well reserve a larger capacity by passing a realistic length such asStringBuffer sb = new StringBuffer(1024);

Written by kennii

August 9, 2007 at 5:02 pm

Posted in Performance

Spring Hibernate DAO Implementation with interface

leave a comment »

Intro

Assuming we have a module called Account Management in our project called foo,

We might can make use of the following package structure.

  1. foo.service.AccountManagement
  2. foo.service.AccountManagementImpl
  3. foo.dao.AccountDAO
  4. foo.dao.hibernate.AccountDAOImpl (In this case hibernate)
  5. foo.model.Account

Sample Spring-Hibernate Hierarchy UML

Spring Wiring

<bean id="accountDAO" class="rb.dao.hibernate.AccountDAOImpl"></bean>

<bean id="accountManagement" class="foo.service.AccountManagementImpl">
<property name="accountDAO">
<ref bean="accountDAO"/>
</property>
</bean>

So note that you can replace the accountDAO with some other implementation class, probably using pure JDBC or ibatis.

Summary

As the experts say, always code to interfaces to cater for different implementations.

Written by kennii

August 7, 2007 at 9:52 am

Posted in Java, Spring Framework

Marshalling Objects To/From XML (Java and Python)

with one comment

Python (Using PyXML)

#
# Install PyXML from http://pyxml.sourceforge.net/
#

from xml.marshal.generic import Marshaller, Unmarshaller

class Company:

def __init__(self, name, year_founded, ceo):
self.name = name
self.year_founded =year_founded
self.ceo = ceo

def __str__(self):
return "%s %s %s" % (self.name, self.year_founded, self.ceo)

c = Company('Google Inc.', '1998', 'Eric Schmidt')

# Marshalling
marshal = Marshaller()
xml = marshal.dumps(c)
print xml

# Un-Marshalling
um = Unmarshaller()
obj = um.loads(xml)
print obj

Java (Using XStream)

The people at ThoughtWorks makes it easy. Just read here.

Written by kennii

August 7, 2007 at 7:35 am

Posted in Java, Python

Collaborating threads using CyclicBarrier (Java 5)

with 6 comments

Recently, i came across this problem.

Write a java program that prints out the line “Started” to the console and then creates 10 threads. Each thread should print the lines “Thread x started”, “Thread x finished” where x is the thread number (0 to 9). The program should then print the line “Terminated”. No thread should print the finish message until all threads have printed the started message. The final “Terminated” message should be printed as soon after the last finished message has been printed.

The interesting thing is all the threads have to wait for each other to reach a common point before being allowed to terminate. This common point is known as the CyclicBarrier in java speak.

I did some quick research and came across this class under the java.util.concurrent package. So using this class i managed to complete this problem in about 1+ hrs (Includes refreshing my memory of threads =D).

Full Source Code (Server and ServerThread)

Written by kennii

August 7, 2007 at 7:02 am

Posted in Java

Python SOAP Clients

with 4 comments

Motivation

You want to invoke web services methods using python code.
There are few python libraries for python web service.
2 commonly used and stable python libraries to invoking web services are:

SOAPpy and ZSI

see http://pywebsvcs.sourceforge.net/

SOAPpy

Use SOAPpy for simple soap method calls that expect only simple type arguments(string, int, etc)

Setup

Follow the steps in http://www.diveintopython.org/soap_web_services/index.html

Sample Code

– List available methods offered by a SOAP server

from SOAPpy import WSDL
wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
server = WSDL.Proxy(wsdlFile)
print server.methods.keys()
- Calling a web service through a WSDL Proxy

from SOAPpy import WSDL
wsdlFile = ‘http://www.xmethods.net/sd/2001/TemperatureService.wsdl&#8217;)
server = WSDL.Proxy(wsdlFile)
print server.getTemp(‘90210’)

server.soapproxy.config.dumpSOAPOut = 1
server.soapproxy.config.dumpSOAPIn = 1
temperature = server.getTemp(‘90210’)
print temperature

See http://www.diveintopython.org/soap_web_services/introspection.html

ZSI (Zolera Soap Infrastructure)

Use ZSI for simple/complex WSDL types.

Setup

1. Download the latest ZSI Egg file from here
1. Download the Easy Install tool from here.
1. Follow this to install the ZSI python egg file

Code Generation from WSDL and XML Schema

In WSDL complex types, objects might be required to be the argument to the SOAP method you want to invoke. For example, the soap method might required a Shipment object as its argument, so how do you pass in an object which your client currently do not possess?

You can create the class(s) required through file generation by passing in the WSDL url.

For python, we will be using the wsdl2py script which comes with ZSI.

1. Go to your PYTHON_HOME\Scripts , there shld be a wsdl2py file there.
1. From the same directory, run this command

wsdl2py -bu http://webservices.wolfram.com/services/SearchServices/WolframSearch2.wsdl

1. The classes that you require are generated in the same directory and available to be used.

Sample Code

– Usage of the generated code to invoke web service

Using the previous example on WolframSearch,

# import the generated class stubs
from WolframSearchService_services import *

# get a port proxy instance
loc = WolframSearchServiceLocator()
port = loc.getWolframSearchmyPortType()

# create a new request
req = WolframSearchRequest()
req.Options = req.new_Options()
req.Options.Query = 'newton'

# call the remote method
resp = port.WolframSearch(req)

# print results
print 'Search Time:', resp.Result.SearchTime
print 'Total Matches:', resp.Result.TotalMatches
for hit in resp.Result.Matches.Item:
print '--', hit.Title

Note that WolframSearchRequest is the object which is required by the WolframSearch soap method.

see here for detailed step-by-step explaination

Summary

We covered the 2 most common python web service libraries that can be used as the client to SOAP servers of different programming largely dominated by Java.

Written by kennii

August 7, 2007 at 6:43 am

Posted in Python