Thursday, August 16, 2012

Django's unicdoe encode error

It's a common and ugly problem when using non-ascii characters in Django.

The general solution is below,

  1. put # -*- coding: utf-8 -*- at beginning of every python source files that are using utf-8 characters
  2. declare every string variable as unicode, such as str_var = u'中文字符'
  3. add a __unicode__ method in your model classes
  4. if you are running server on apache/mod_wsgi or ngnix, you need configure web server to use utf-8 encoding

Saturday, July 28, 2012

The workaround of making Zend CE/Zend debugger work on mountain lion

I installed both Zend CE and zend debugger of Eclipse on my Mac. Both of them work well in Mac lion.  However they don't work any more after I upgraded my Mac to mountain lion. 

After some investigation I found some extensions of Zend PHP can't be loaded due to shared library dependency can't be found in mountain lion. The xslt module of PHP depends on some system libraries(suc as /usr/local/libxslt-1.1.23/lib/libxslt.1.dylib) that have been removed by mountain lion.

The temporary solution is disabling xlst module of zend PHP if your application doesn't need them. 

The workaround fix of Zend CE on Mac, 
rename /usr/local/zend/lib/php_extensions/xsl.so to any other name

The workaround fix of zend debugger for Eclipse, 

Delete the line extension=xsl.so from file <your eclipse>/plugins/org.zend.php.debug.debugger.macosx_5.3.18.v20110322/resources/php53/php.ini

Monday, March 19, 2012

Dual monitors on Ubuntu

I had two monitors for my workstation. One is 22' and the another is 17'. I used the small one as a extend desktop.

Today I get a another 23' monitor to replace the small one. However the resolution of the 23' monitor can't be changed after pluging it in. It always used the resolution matching the 17' one.

Both 'Setting - Display' and 'AMD Catalyst control' can't adjust it as higher resolution.

After some tuning, I found a workaround.

I totally remove all config of small one from /etc/X11/xorg.conf. Then change its resolution in 'AMD Catalyst control', it works!

Monday, March 5, 2012

Embedding an HTTP server in Equinox

I want to create a test server for my application. Using embedding Http server in equinox is my first option.

I had experience using simple http service implementation of equinox, however I want to play with Jetty this time.

Following the guide of Equinox server, I can't running a Jetty server with my servlet in Eclipse Indigo. Obviously the guide is out of date.

After tuning it, I found below bundles are minimum collection to run Jetty inside OSGi runtime.


You only need create a run configuration of OSGi framework, add your bundles with servlets and above bundles.

Friday, February 17, 2012

Acess Intranet without VPN

Sometimes I need access the Intranet of company, however I don't like to create VPN connection. The connection is slow, waste time to create the connection and have to change password due to security policy.

My workstation is Linux, which has a lot of utility tools to help me access Intranet at home without VPN.

Firstly I set up a ssh server on my personal computer. It's quite easy if you are using Linux, for Windows I installed Copssh.
Then register a free domain name and configure it in my router. And let router forward port 22(or any port you wan to use) to my personal computer.
In my working Linux machine, create a ssh tunnel to my personal computer. Must use the public/private key for authenticating. For example,

It means remote server can access my workstation's port 22 via accessing its port 1002 after the ssh tunnel is created successfully. Above command line also forwards the ports 5900 and 6500. The default VNC session will listen the port 5900.
But it only works when my personal computer is running. And the connection can't be reconnected after it fails once.
The graceful solution is installing 'autossh' in my Linux, which is an utility to retry the ssh connection with an interval if it's disconnected or failed.

 Then create a script and running it when OS is booted. The script will be executed by root user, so we need configure it ran by the normal user.

After my personal computer is booted a while(the default interval of autossh is 300 seconds), I can use localhost:10002 to login my workstation, localhost:5900 to access my VNC session. Of course you can use 'froxyproxy' of Firefox via a localport to access web page of Intranet.

Wednesday, February 15, 2012

How to reuse the existing OpenID accounts after the host name of Gerrit server is changed

An internal Gerrit server was moved, so the hostname of server is changed. However we are using OpenID for user control, the OpenID provider(such as Google account) will generate a new token for the new server(hostname changing will impact the identity token of Google account) when we login Gerrit with same OpenID account. Gerrit will create a new internal account by default even though my OpenID account has existed in the system and has a lot of activities.

The solution is updating the 'ACCOUNT_EXTERNAL_IDS' table of Gerrit via gsql. Setting the 'ACCOUNT_ID' to your existing account_id for the new record whose 'EXTERNAL_ID' is the new token gotten from Google.

update ACCOUNT_EXTERNAL_IDS set ACCOUNT_ID='1000001' where EXTERNAL_ID='https://www.google.com/accounts/o8/id?id=xxxxxxxxxx';

Then search the documentation of Gerrit, I find a configuration property looks like supporting such a migration for OpenID authentication.
auth.allowGoogleAccountUpgrade
Allows Google Account users to automatically update their Gerrit account when/if their Google Account OpenID identity token changes. Identity tokens can change if the server changes hostnames, or for other reasons known only to Google. The upgrade path works by matching users by email address if the identity is not present, and then changing the identity.
This setting also permits old Gerrit 1.x users to seamlessly upgrade from Google Accounts on Google App Engine to OpenID authentication.
Having this enabled incurs an extra database query when Google Account users register with the Gerrit server.
By default, unset/false.