Thursday, March 24, 2011

IBM_DB ActiveRecord Limit Issue

I use ibm-db-2.5.6 and activerecord-3.0.5. I encountered the same problem described in this lighthouse tick https://rails.lighthouseapp.com/projects/8994/tickets/6012-incorrect-sql-generation-for-db2-in-rails-303

It is very simple to fix that. I put the following code snippet into my config/application.rb after your application module. And everything works correctly.

Arel 2.0.9 doesn't have an ToSql visitor for DB2. The interesting thing is some versions of DB2 server actually support LIMIT clause. For example:

this windows version supports LIMIT

Database server = DB2/NT64 9.7.2

but this linux version doesn't

Database server = DB2/LINUXX8664 9.7.2


# this is a patch for ibm_db
if defined?(IBM_DB) && !Arel::Visitors::VISITORS.has_key?('ibm_db')
module Arel
module Visitors
class DB2 < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_Limit o
"FETCH FIRST #{visit o.expr} ROWS ONLY"
end
end

VISITORS['ibm_db'] = Arel::Visitors::DB2
end
end
end

Thursday, February 17, 2011

Start Ruby 1.8.7 without setting system path on Windows

I installed cygwin and cygwin-ruby on my Windows 7. If I put Ruby-1.8.7-win32 path in my system path, the path also appears in cygwin path. Although cygwin-ruby (/usr/bin/ruby) appears before Ruby-win32 in PATH, I'm still want to avoid any confusion, so I don't add Ruby-Win32 into my system path.

When I want to run Ruby-1.8.7-Win32, I can do like this:

C:\Windows\System32\cmd.exe /E:ON /K C:\workplace\apps\Ruby187\bin\setrbvars.bat

You can create a shortcut and pin this on the task bar.

Wednesday, February 9, 2011

Identify the Physical Host of a Virtual Server using PowerShell

Found this page by googling:

http://portal.sivarajan.com/2010/01/identify-physical-host-of-virtual.html

$regPath= "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"
$regValue = get-itemproperty -path $regPath
$regValue | fl "VirtualMachineName","PhysicalHostNameFullyQualified"

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.