Sunday, September 23, 2012

Using alias_method to implement AOP in Ruby

This week I just realize the power of ruby alias_method, you use alias_method to intercept the existing method, and modify its behaviour,  this is exactly what AOP does the jobs.

The following code example tell you how to use the alias_method, to add a logging logic to the exit() method.

module Mod
alias_method :orig_exit, :exit
def exit(code=0)
puts "Exiting with code #{code}"
orig_exit(code)
end
end
include Mod
exit(99)
view raw alias_method hosted with ❤ by GitHub
This reminds me the javaasist framework, I used before, which is a powerful tool to do bytecode manipulation, it can intercept the method by renaming the existing method to another method,  and modifying the existing method, which is the exactly similar strategy.
the code example:
private static void addTiming(CtClass clas, String mname)
throws NotFoundException, CannotCompileException {
// get the method information (throws exception if method with
// given name is not declared directly by this class, returns
// arbitrary choice if more than one with the given name)
CtMethod mold = clas.getDeclaredMethod(mname);
// rename old method to synthetic name, then duplicate the
// method with original name for use as interceptor
String nname = mname+"$impl";
mold.setName(nname);
CtMethod mnew = CtNewMethod.copy(mold, mname, clas, null);
// start the body text generation by saving the start time
// to a local variable, then call the timed method; the
// actual code generated needs to depend on whether the
// timed method returns a value
String type = mold.getReturnType().getName();
StringBuffer body = new StringBuffer();
body.append("{\nlong start = System.currentTimeMillis();\n");
if (!"void".equals(type)) {
body.append(type + " result = ");
}
body.append(nname + "($$);\n");
// finish body text generation with call to print the timing
// information, and return saved value (if not void)
body.append("System.out.println(\"Call to method " + mname +
" took \" +\n (System.currentTimeMillis()-start) + " +
"\" ms.\");\n");
if (!"void".equals(type)) {
body.append("return result;\n");
}
body.append("}");
// replace the body of the interceptor method with generated
// code block and add it to class
mnew.setBody(body.toString());
clas.addMethod(mnew);
// print the generated code block just to show what was done
System.out.println("Interceptor method body:");
System.out.println(body.toString());
}
Now when you compare two solution, you will tell how easy to do it in Ruby!

Resource
1. alias_method API document
2. IBM Javaassist tutorial 
3. Spring AOP document

Saturday, September 15, 2012

Fix the iOS code signing issue when using Jenkins


This week I setup the Jenkins on my Mac and try to build iOS applications. unfortunately I got the code signing issues, either I use xcode plugin or xcode command line tool. Through a couple days of googling and I could not find any solution that works for me, but I finally solve this issue by myself through different try out, and the solution is such an easy, now I would like to share with my solution.

The core reason is Jenkins is running as daemon mode in Mac, just assume it is a different user - "Jenkins", so it will not have access to the keychain or provision profile as a you login using your credentials, which cause the code signing issue.
I found I have following 2 errors

1. "Code Sign error: There are no valid certificate/private key pairs in the default keychain"
Solution: Copy your iPhone developer certificate from "login" keychain to "System" keychain.
Detailed steps:
  open the "Keychain Access" application, click the login tab, right click the certificate like "iPhone Developer: your_name (XXXXXXX)", choose copy, then click the "System" tab, right click mouse, choose "Paste 2 items"; you might need to do the same thing with the certificate like "iPhone Distribution: your_name".

After doing this, you will get the second error.
2. "Code Sign error: Provisioning profile 'xxxxx-xxxx-xxxx-xxxxx' can't be found"
Solution: Copy the provision profile to Jenkins user folder.
The provision profile is under in the folder
/YourUserName/Library/MobileDevice/Provisioning Profiles,
for example in my machine, the provision profile files are under /Users/steve/Library/MobileDevice/Provisioning Profiles
In the mac, the Jenkins will be in /Users/Shared/Jenkins, create the following folder:
/Users/Shared/Jenkins/Library/MobileDevice/Provisioning Profile,  then copy the .mobileprovision file to this folder.

After doing this, the code signing issues will be fixed. Hope my finding will be helpful to other Jenkins users.