Friday, December 24, 2010

Backup a Windows system

1. Create a Fedora LiveCD on USB flash driver
2. Reboot the machine to Fedora
3. Backup partition information

# sfdisk -d /dev/sda | ssh serverip 'cat > /folder/sda.ptb'

4. Backup MBR or MBR+Bootloader

# dd if=/dev/sda bs=512 count=1 | ssh serverip 'cat > /folder/sda.mbr'
# dd if=/dev/sda bs=512 count=63 | ssh serverip 'cat > /folder/sda.vbr'

5. Backup the restore image (count is set to the value from sfdisk output for /dev/sda1)

# dd if=/dev/sda bs=512 skip=63 count=20980827 | ssh serverip 'cat > /folder/sda1.img'

6. Check NTFS system size and if it needs run 'chkdsk /f /r'

# ntfsresize --info /dev/sda2

7. Backup Windows NTFS system

# ntfsclone -s -o - /dev/sda2 | ssh serverip 'cat > /folder/sda2.ntfs.img'

8. Run sha256sum to verify if the hashes match

# dd if=/dev/sda bs=512 skip=63 count=20980827 | sha256sum | ssh 'cat > /tmp/sda.sum'

Monday, December 20, 2010

Install ibm_db gem on Cygwin 1.7 on Windows 7

Just got a brand new Windows 7 laptop. Had troubles in installation of ibm_db gem.

Here is the configuration

* Windows 7 64bit
* Cygwin 1.7.7-1
* DB2 Express-C 64bit v9.72 (db2exc_972_WIN_x86_64.exe)
* Cygwin Ruby 1.8.6
* Bash

Here are the steps how to install ibm_db:

* copy c:\Program Files\IBM\SQLLIB\lib\Win32\db2cli.lib to db2.lib
* export IBM_DB_LIB=/cygdrive/c/Program\ Files/IBM/SQLLIB/lib/Win32
* export IBM_DB_INCLUDE=/cygdrive/c/Program\ Files/IBM/SQLLIB/include
* gem install ibm_db

NOTEs:
* Because the ruby of Cygwin is still 32bit, you have to link to 32bit library.
* the native extension build uses "-ldb2", which means db2cli.lib is not recognized.
* w32api package in Cygwin (not sure if it is needed, may be not)

Wednesday, October 27, 2010

JRuby include_package doesn't define constants

Using include_package in a module to import java classes in JRuby usually works as expected, but not always. Try this code in JRuby 1.5.3
$ jruby ns.rb
ns.rb:

require 'java'
require 'pp'

class Rule
end

module MyCompany
module JavaModel
include_package 'com.model'
end
end

pp MyCompany::JavaModel.const_get('Foo')
pp MyCompany::JavaModel.const_get('Rule')
You can simple create two java files and compile them using javac.
com/model/Foo.java:

package com.model;
public class Foo {
}

com/model/Rule.java:
package com.model;
public class Rule {
}
$ javac com/model/Foo.java
$ javac com/model/Rule.java
Here is the output:
Java::ComModel::Foo
Rule
Why the second line is not Java::ComModel::Rule? include_package just overrides const_missing instead of defining constants in module MyCompany::JavaModel. When you call MyCompany::JavaModel.const_get("Rule"), the constant "Rule" is defined, const_missing is skipped.

Using java_import will work correctly although you have to list each class.

module MyCompany
module JavaModel
java_import 'com.model.Foo'
java_import 'com.model.Rule'
end
end
Note: if you use java_import outside the module, you will overwrite the constant Rule.

Saturday, August 28, 2010

Spring IDE "Build is incomplete" and commons-logging.properties

I use sts 2.3.2 and always got "Build path is incomplete" issues. And I found this was caused by this exception:

java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.initAspectJE xpressionPointcut(AspectDefinitionMatcher.java:229 )
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.createAspect JPointcutExpression(AspectDefinitionMatcher.java:1 86)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.internalMatc hes(AspectDefinitionMatcher.java:262)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.matches(Aspe ctDefinitionMatcher.java:92)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob$1.doWith ActiveProjectClassLoader(AopReferenceModelBuilderJ ob.java:253)
at org.springframework.ide.eclipse.core.java.JdtUtils $DefaultProjectClassLoaderSupport.executeCallback( JdtUtils.java:850)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBean(AopReferenceModelBuilderJob.java :209)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBeans(AopReferenceModelBuilderJob.jav a:281)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBeansConfig(AopReferenceModelBuilderJ ob.java:307)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesFromBeansConfigSets(AopReferenceModelBui lderJob.java:395)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForFile(AopReferenceModelBuilderJob.java :353)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop Model(AopReferenceModelBuilderJob.java:448)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.run(AopR eferenceModelBuilderJob.java:157)
at org.eclipse.core.internal.jobs.Worker.run(Worker.j ava:55)
Caused by: org.apache.commons.logging.LogConfigurationExcepti on: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable.
at org.apache.commons.logging.impl.LogFactoryImpl.dis coverLogImplementation(LogFactoryImpl.java:874)
at org.apache.commons.logging.impl.LogFactoryImpl.new Instance(LogFactoryImpl.java:604)
at org.apache.commons.logging.impl.LogFactoryImpl.get Instance(LogFactoryImpl.java:336)
at org.apache.commons.logging.impl.LogFactoryImpl.get Instance(LogFactoryImpl.java:310)
at org.apache.commons.logging.LogFactory.getLog(LogFa ctory.java:685)
at org.springframework.ide.eclipse.springframework.ao p.aspectj.AspectJExpressionPointcut.(Aspec tJExpressionPointcut.java:96)
... 20 more

Even upgrading to 2.3.3.M3 didn't help.

And I found org.apache.commons.logging.impl.Log4JLogger was not in
.metadata/.plugins/org.springframework.ide.eclipse.osgi.targetdefinition/2.5.0.201008251000-M3/target/com.springsource.slf4j.org.apache.commons.logging-1.5.6.jar. That explains why the exception happens.

My project uses common-logging.properties to config log4j with this line,

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

I tried to remove the dependency of common-logging, and common-logging.properties from my maven project, and cleaned all maven projects to remove common-logging.properties from target/classes and target/test-classes.

Then everything become ok. No "Build path is incomplete" any more.

Unfortunately, I couldn't reproduce it when I put common-logging.properties back to the project, and built the maven project.

I guess common-logging.properties somehow was picked by SPRING IDE.

Friday, August 27, 2010

My Cygwin/Xwin settings

This is my ~/.Xdefaults file:

Xft.dpi: 96.0
Xft.hinting: 1
Xft.rgba : rgb
Xft.antialias: 1
Xft.hintstyle: hintfull

XTerm*faceName: Bitstream Vera Sans Mono
XTerm*faceSize: 10
XTerm*scrollBar: True
XTerm*rightScrollBar: True
XTerm*renderFont: True

URxvt*background: #EFFFEF
URxvt*foreground: #000000
URxvt.font: xft:Bitstream Vera Sans Mono:size=10:light:roman:antialias=true,xft:NSimSun:size=16:medium:antialias=true
URxvt*boldFont: xft:Bitstream Vera Sans Mono:size=10:bold:roman:antialias=true,xft:SimHei:size=16:medium:antialias=true
URxvt*italicFont: xft:Bitstream Vera Sans Mono:size=10:medium:italic:autohint=true:antialias=true
URxvt*boldItalicFont: xft:Bitstream Vera Sans Mono:size=10:bold:italic:autohint=true:antialias=true
URxvt.scrollBar: True
URxvt.scrollBar_right: True
URxvt.Shading: 50
URxvt.fading: 50
URxvt.geometry:80x25

Friday, August 6, 2010

Without cropping in Cygwin Xwin during switch monitors

I have a laptop with a LCD 1400x900 and a monitor with 1280x1024 connecting to the dock. When I dock and undock the laptop, my Cygwin Xwin always has something cropped. XWin doesn't change the resolution automatically, and I cannot use xrandr to change it too. For example, after starting xwin using LCD, and docking the laptop, xrandr still returns:


bash-3.2$ xrandr -q
Screen 0: minimum 1440 x 900, current 1440 x 900, maximum 1440 x 900
default connected 1440x900+0+0 0mm x 0mm
1440x900 0.0*


and the following command will fail because the size can only be 1440x900.


bash-3.2$ xrandr -fb 1280x1024


This is annoying because I have to restart xwin after switching the monitor. Finally I found this thread solved my problem.

add the following options into /usr/bin/startx


serverargs="-screen 0 1440x1024x32 -multiwindow -clipboard"


Basically it creates a screen large enough to cover any monitor.

Thursday, August 5, 2010

Use mock_model outside of Rails

I want to use RSpec's mock_model outside of Rails, but only found this mail thread.

Actually it is not hard to use mock_model without rails. You need to install rspec-rails, and active_record gem first. Here is an example,


require 'active_support/core_ext/hash'
require 'spec/rails/mocks'
include Spec::Rails::Mocks

class TestTable
end

class View
def initialize(table)
@table = table
end

def print_keys
"name is #{@table.name} #{@table.class.primary_keys.join(",")}"
end

def print_name
"name is #{@table.name}"
end
end

describe "TestTable" do
before do
table = mock_model(TestTable, :name=>"table_abc")
@view = View.new(table)
end

it "should fail the expectation check" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc"
end

it "should complain that no primary_keys is called" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_name.should == "name is table_abc"
end

it "should print the table name" do
@view.print_name.should == "name is table_abc"
end

it "should print the table name and keys" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc pk1,pk2"
end
end

Monday, July 26, 2010


$ gem list gherkin cucumber
*** LOCAL GEMS ***
gherkin (2.2.0, 2.0.2)

$ gem install cucumber
ERROR: Error installing cucumber:
cucumber requires gherkin (~> 2.1.4, runtime)

The following command didn't work too
$gem install gherkin cucumber
You have to install gherkin 2.1.4 or 2.1.5 first.
$gem install gherkin --version 2.1.5

See "Specifying Versions" of RubyGems here

Thursday, July 22, 2010

Run cuke4duke from Eclipse

For a project using cuke4duke-maven-plugin, you can run integration test using Eclipse launcher, but you need to install the dependent packages into local repository. This is not convenient because you may change the dependent package in your workspace to make testes pass.

Just turn on "Resolve workspace artifacts" in "Run Configurations" dialog, and you don't have to install them.

Maven build without installing dependent packages in local repository

If you split your large project into several maven projects, and put them in sibling folders as follows:

+- parent
|
+- api
|
+- persistence
|
+- core

and core depends on api and persistence, while persistence depends on api. You can run maven command in parent/ to build them all without installing the packages into local maven repository:


parent $ mvn clean package

If you just want to build persistence, and you go to the folder core, then run maven, you will be asked to install api into repository. You can overcome this by running this command in parent folder:


parent $ mvn -o -am -pl groupId:core clean package

because all packages are in sibling folders, you may want to try this command:


parent $ mvn -o -am -pl ../core clean package

But this won't work and you may get an error message like "Couldn't find specified project in module list: M:\..\parent\..\core". Check this: MNG-4262.

Thursday, April 15, 2010

Debug Ruby Test::Unit::TestCase in Eclipse DLTK

It took me a while to figure out how to debug unit test using DLTK in eclipse.

Originally I tried to debug as "Ruby Test" and the breakpoints in unit test never worked, but it did stop in "rubygems.rb", you can step into the unit test file, but you cannot stop into a test method.

Because using "rdebug test-case.rb" did work in command line, I thought it might be something wrong with my gems. Tried to install a brand new ruby(mingw32 version) and install all gems ruby-debug and ruby-debug-ide. Unfortunately, it still didn't work.

Finally I found it is pretty ease to debug the unit test. The trick is "Debug as Ruby Script", you will stop at the breakpoint you set.

Wednesday, April 14, 2010

Install ibm_db on Ruby-mingw32

Here are the steps:

1. Install mingw compiled Ruby from http://rubyinstaller.org/download.html
2. Install Ruby Development Kit from http://rubyinstaller.org/download.html
3. follow the instruction of Development Kit
4. If you install IBM DB2 client in "c:\Program Files", you can do like this:

set IBM_DB_LIB=%ProgramFiles%/IBM/SQLLIB/LIB
set IBM_DB_INCLUDE=%ProgramFiles%/IBM/SQLLIB/INCLUDE
gem install ibm_db

Because there is a space in "Program Files", adding quotes or escaping the space like "\ " do not work for me.

Install ibm_db on Cygwin 1.7

Here is how to install ibm_db on Cygwin 1.7


bash-3.2$ export IBM_DB_INCLUDE=/cygdrive/c/Program\ Files/IBM/SQLLIB/include
bash-3.2$ export IBM_DB_LIB=/cygdrive/c/Program\ Files/IBM/SQLLIB/lib
bash-3.2$ gem install ibm_db

Tuesday, April 13, 2010

Compile ruby-odbc on Cygwin 1.7

The ruby-odbc gem won't compile in Cygwin 1.7 because the uft8 version failed.

You can solve this by run the followings in a shell
$ export cflags="-DHAVE_SQLCONFIGDATASOURCEW -DHAVE_SQLWRITEFILEDSNW -DHAVE_SQLREADFILEDSNW -DHAVE_SQLINSTALLERERROR -DHAVE_SQLINSTALLERERRORW"
$ gem install ruby-odbc
And you can also use this modified "extconf.rb"

require 'mkmf'

if ! defined? PLATFORM
PLATFORM = RUBY_PLATFORM
end

def have_library_ex(lib, func="main", headers=nil)
checking_for "#{func}() in -l#{lib}" do
libs = append_library($libs, lib)
if !func.nil? && !func.empty? && COMMON_LIBS.include?(lib)
true
elsif try_func(func, libs, headers)
$libs = libs
true
else
false
end
end
end

def try_func_nolink(func, libs, headers = nil, &b)
headers = cpp_include(headers)
try_compile(<<"SRC", libs, &b)
#{COMMON_HEADERS}
#{headers}
/*top*/
int t() { void ((*volatile p)()); p = (void ((*)()))#{func}; return 0; }
SRC
end

def have_func_nolink(func, headers = nil, &b)
checking_for "#{func}()" do
if try_func_nolink(func, $libs, headers, &b)
$defs.push(format("-DHAVE_%s", func.upcase))
true
else
false
end
end
end

def headers(header)
if PLATFORM !~ /(mingw|cygwin)/ then
hdrs = header
else
hdrs = ["windows.h", header]
end
hdrs
end

def check_for_type(type)
begin
if defined? have_type
have_type(type, headers("sqltypes.h"))
else
throw
end
rescue
puts "WARNING: please check sqltypes.h for #{type} manually,"
puts "WARNING: if defined, modify CFLAGS in Makefile to contain"
puts "WARNING: the option -DHAVE_TYPE_#{type}"
end
end

dir_config("odbc")
have_header("sql.h") || begin
puts "ERROR: sql.h not found"
exit 1
end
have_header("sqlext.h") || begin
puts "ERROR: sqlext.h not found"
exit 1
end
testdlopen = enable_config("dlopen", true)

check_for_type("SQLTCHAR")
check_for_type("SQLLEN")
check_for_type("SQLULEN")

$have_odbcinst_h = have_header("odbcinst.h")

if PLATFORM =~ /mswin32/ then
if !have_library_ex("odbc32", "SQLAllocConnect", "sql.h") ||
!have_library_ex("odbccp32", "SQLConfigDataSource", "odbcinst.h") ||
!have_library_ex("odbccp32", "SQLInstallerError", "odbcinst.h") ||
!have_library("user32", "CharUpper") then
puts "Can not locate odbc libraries"
exit 1
end
have_func("SQLConfigDataSourceW", "odbcinst.h")
have_func("SQLWriteFileDSNW", "odbcinst.h")
have_func("SQLReadFileDSNW", "odbcinst.h")
have_func("SQLInstallerError", "odbcinst.h")
have_func("SQLInstallerErrorW", "odbcinst.h")
# mingw untested !!!
elsif PLATFORM =~ /(mingw|cygwin)/ then
have_library("odbc32", "")
have_library("odbccp32", "")
have_library("user32", "")
hdrs = headers("odbcinst.h")
have_func("SQLConfigDataSourceW", hdrs)
have_func("SQLWriteFileDSNW", hdrs)
have_func("SQLReadFileDSNW", hdrs)
have_func("SQLInstallerError", hdrs)
have_func("SQLInstallerErrorW", hdrs)
elsif (testdlopen && PLATFORM !~ /(macos|darwin)/ && CONFIG["CC"] =~ /gcc/ && have_func("dlopen", "dlfcn.h") && have_library("dl", "dlopen")) then
$LDFLAGS+=" -Wl,-init -Wl,ruby_odbc_init -Wl,-fini -Wl,ruby_odbc_fini"
$CPPFLAGS+=" -DHAVE_SQLCONFIGDATASOURCE"
$CPPFLAGS+=" -DHAVE_SQLINSTALLERERROR"
$CPPFLAGS+=" -DUSE_DLOPEN_FOR_ODBC_LIBS"
# but test the UNICODE installer functions w/o linking
# in case we need to provide fwd declarations
have_func_nolink("SQLConfigDataSourceW", "odbcinst.h")
have_func_nolink("SQLWriteFileDSNW", "odbcinst.h")
have_func_nolink("SQLReadFileDSNW", "odbcinst.h")
have_func_nolink("SQLInstallerErrorW", "odbcinst.h")
else
have_library("odbc", "SQLAllocConnect") ||
have_library("iodbc", "SQLAllocConnect")
($have_odbcinst_h &&
have_library("odbcinst", "SQLConfigDataSource")) ||
($have_odbcinst_h &&
have_library("iodbcinst", "SQLConfigDataSource"))
$have_odbcinst_h &&
have_func("SQLConfigDataSourceW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLWriteFileDSNW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLReadFileDSNW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLInstallerError", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLInstallerErrorW", "odbcinst.h")
end

create_makefile("odbc_utf8")

Tuesday, April 6, 2010

Don't let Aptana's XML Editor mess your Eclipse

After installing Aptana's RadRails in my Eclipse 3.5, I found my Maven project's pom.xml didn't behave as previous any more. Double click invoked a XML editor instead of Maven POM editor. After an investigation, I found that RadRails installed "Aptana XML editor" and set it as default, which caused Eclipse never chose the correct editor according to content-type of XML files. This is annoying for some XML files, such as POM and JUnit surefire-reports.

After you remove RadRails, Eclipse's default behavior comes back. However, you cannot remove "Aptana XML editor" only because it is packed with other packages. Finally I figured out a way so that you don't need to remove RadRails.

The following steps show how to fix this problem:
  1. Go to Windows->Preferences
  2. Then General->Editors->File Associations
  3. Find "*.xml" in File types, click it
  4. You will find the list of XML editors, you may find "Aptana XML Editor" with default
  5. Click "Remove" button for "File Types" to delete the file type "*.xml"
  6. Click "Add .." button for "File types", and fill "*.xml" in the dialog
  7. Then you will find all editors are listed in "Associated Editors", but "Aptana XML editor" is not default any more.
  8. Restart Eclipse, this configuration should be kept for your workspace.

The following two solutions are tested and didn't work:
  1. Just deleting "*.xml" file type doesn't work because Aptana XML editor becomes default again when you restart Eclipse.
  2. Setting another editor as default cause the same problem because no content-type detection if there exists an default editor.
Aptana should respect Eclipse's content-type.