2013年9月20日金曜日

Arial Black on Firefox

Firefox has a bug due to which Arial Black is not treated correctly. A workaround is to add "font-weight: 900;", but an answer at StackOverflow seems better.

Firefox には「Arial Blackフォントが効かない」という不具合があり、それを回避するために、「font-weight: 900;」を加える、という方法があるそうだが、StackOverflow にあった回答のほうがスマート。

@font-face {
  font-family: 'arial-black';
  src: local('Arial Black');
}

#nav {
  font-family: "Arial Black", arial-black, sans-serif;
}

2013年4月11日木曜日

Message digests (MD5, SHA1, etc.) on iOS with dedicated classes

Project nv-ios-digest (Apache License, Version 2.0) provides Objective-C classes that are dedicated to computation of message digests such as MD5 and SHA-1. An example code below prints MD5 hash value of "Hello, world.".

// Create an MD5 instance using the convenience constructor.
MD5 *md5 = [MD5 md5WithString:@"Hello, world."];

// Print the MD5 hash value as string.
// This will show "md5 = 080aef839b95facf73ec599375e92d47".
NSLog(@"md5 = %@", md5);


Another example code below shows another different way to compute MD5 with update and final.

// Create MD5 instance for computation.
md5 = [[MD5 alloc] init];

// Update. See also
// updateWith:(const void *)data length:(CC_LONG)length
[md5 updateWithString:@"Hello, world."];

// Final. The returned pointer points to the internal buffer
// of md5 whose size is CC_MD5_DIGEST_LENGTH (=16).
unsigned char *md = [md5 final];

// After 'final', a valid NSString expression is available
// through 'description' method.
NSLog(@"md5 = %@", md5);


Supported algorithms are as follows.

  • MD5
  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512

A simple implementation to add md5 method to NSString will look like the following.

// NSString+MessageDigest.h
#import <Foundation/Foundation.h>
#import "MD5.h"
@interface NSString (MessageDigest)
- (MD5 *)md5;
@end

// NSString+MessageDigest.m
#import "NSString+MessageDigest.h"
@implementation NSString (MessageDigest)
- (MD5 *)md5
{
    return [MD5 md5WithString:self];
}
@end

And its usage will be:

#import "NSString+MessageDigest.h"

NSLog(@"md5 = %@", @"Hello, world.".md5);


Project Page
https://github.com/TakahikoKawasaki/nv-ios-digest


2013年4月3日水曜日

Get and compare iOS version at runtime with Version class

With Version class that is contained in nv-ios-version project (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and check whether the version is greater than or equal to 6.0.

// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];

// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];

// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
    version.major, version.minor, version.micro);

// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
    // The iOS version is greater than or equal to 6.0.
}

// Another way to check whether iOS version is
// greater than or equal to 6.0
if (6 <= version.major)
{
    // The iOS version is greater than or equal to 6.0.
}

In Version API, the valid range of major, minor and micro is between 0 and 255. If an invalid value is given, NSInvalidArgumentException is thrown. The default values of major, minor and micro are 0.

number in the API is "(major << 16) | (minor << 8) | micro".

description method returns "major.minor" when micro is 0. Otherwise, "major.minor.micro". For example, "6.1" is returned for version 6.1.0, and "6.1.1" for version 6.1.1.

Version class provides many comparison methods.

#pragma mark -
#pragma mark Comparison
- (NSComparisonResult)compareToMajor:(int)major;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor;
- (NSComparisonResult)compareToMajor:(int)major minor:(int)minor micro:(int)micro;
- (NSComparisonResult)compareToNumber:(int)number;
- (NSComparisonResult)compareTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (equal to)
- (BOOL)isEqualToMajor:(int)major;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isEqualToNumber:(int)number;
- (BOOL)isEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (not equal to)
- (BOOL)isNotEqualToMajor:(int)major;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isNotEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isNotEqualToNumber:(int)number;
- (BOOL)isNotEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (less than)
- (BOOL)isLessThanMajor:(int)major;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanNumber:(int)number;
- (BOOL)isLessThan:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (less than or equal to)
- (BOOL)isLessThanOrEqualToMajor:(int)major;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isLessThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isLessThanOrEqualToNumber:(int)number;
- (BOOL)isLessThanOrEqualTo:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (greater than)
- (BOOL)isGreaterThanMajor:(int)major;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanNumber:(int)number;
- (BOOL)isGreaterThan:(Version *)aVersion;

#pragma mark -
#pragma mark Comparison (greater than or equal to)
- (BOOL)isGreaterThanOrEqualToMajor:(int)major;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor;
- (BOOL)isGreaterThanOrEqualToMajor:(int)major minor:(int)minor micro:(int)micro;
- (BOOL)isGreaterThanOrEqualToNumber:(int)number;
- (BOOL)isGreaterThanOrEqualTo:(Version *)aVersion;

Convenience constructors are listed below.

#pragma mark -
#pragma mark Convenience Constructors
+ (Version *)versionWithMajor:(int)major;
+ (Version *)versionWithMajor:(int)major minor:(int)minor;
+ (Version *)versionWithMajor:(int)major minor:(int)minor micro:(int)micro;
+ (Version *)versionWithString:(NSString *)string;


The following sample code shows how to implement System class that provides version method which returns a Version instance for iOS version.

// System.h
#import <Foundation/Foundation.h>
#import "Version.h"

@interface System : NSObject
/**
 * Get iOS version.
 */
+ (Version *)version;
@end

// System.m
#import <UIKit/UIDevice.h>
#import "System.h"

static Version *_version;

@implementation System
/**
 * Initialization for this class.
 */ 
+ (void)initialize
{
    // Get the system version of iOS at runtime.
    NSString *versionString = [[UIDevice currentDevice] systemVersion];

    // Convert the version string to a Version instance.
    Version *version = [Version versionWithString:versionString];

    // Set as a class variable.
    _version = version;
}

/**
 * Get iOS version.
 */
+ (Version *)version
{
    return _version;
}
@end

With System class implemented like above, code to check iOS version becomes shorter.

if (System.version.major >= 6)
{
    // iOS version is greater than or equal to 6.0.
}


Project Page
https://github.com/TakahikoKawasaki/nv-ios-version

2013年1月16日水曜日

The remote end hung up unexpectedly (git)

One day, git clone from GitHub failed with error messages like below.

remote: Counting objects: 505, done.
remote: Compressing objects: 100% (293/293), done.
fatal: The remote end hung up unexpectedly
fatal: early EOFs: 92% (465/505)
fatal: index-pack failed

In the Internet, there were some suggestions to solve this issue, but none of them worked in my case. So, I set 1 to GIT_TRACE environment variable and tried git clone again.

$ GIT_TRACE=1 git clone ...
....
POST git-upload-pack (386 bytes)
error: RPC failed; result=22, HTTP code = 504
fatal: The remote end hung up unexpectedly

According to the HTTP specification, 504 is returned when "The server was acting as a gateway or proxy and did not receive a timely response from the upstream server." In general, 504 is caused due to slow network. If it was the case in my git clone failure, I thought that either the network between my git client and GitHub or the network between GitHub front-end and back-end might have a trouble. For the latter case, we cannot do anything but wait for GitHub to recover. But, for the former case, there may be some workarounds, so I started to read the source code of git.

In the source code of git, the error message "The remote end hung up unexpectedly" is written only in safe_read() function in pkt-line.c. Reading the source code further made me conclude that the error occurred in git-upload-pack command in my case (as the log message implied).

The man page of git-upload-pack says that the command accepts "--timeout=<seconds>" option and that git-upload-pack is invoked by git-fetch-pack. I thought that passing a long timeout value to git-upload-pack via git-fetch-pack might solve the problem.

The man page of  git-fetch-pack says that git-fetch-pack provides "--upload-pack=<path>" option to specify the path of git-upload-pack command and that if the option is omitted, PATH environment variable is referred to to search for git-upload-pack.

Because I could not find a smart way to pass "--timeout=<seconds>" option to git-upload-pack, I created my own git-upload-pack (shell script) like below,

$ mkdir bin
$ vi bin/git-upload-pack

#!/bin/sh
exec /usr/local/bin/git-upload-pack --timeout=300 "$@"

and ran git clone as follows.

$ PATH="${PWD}/bin:${PATH}" git clone ...

This worked for me! I found a workaround?!

Right after the above successful trial, I tried git clone again without the workaround to check if the workaround had been the key for the success. However, the trial without the workaround also succeeded. Oh, it seemed that GitHub had recovered before I could confirm my workaround... Hmm...

Well, so, my conclusion is that I should simply wait for GitHub to recover when GIT_TRACE=1 git clone reports "HTTP code = 504".

2013年1月15日火曜日

Additional JVM options for maven-javadoc-plugin

I had a trouble. "mvn release:prepare" generated javadoc not in English but in Japanese.

"mvn release:prepare" invokes maven-javadoc-plugin to generate javadoc. In my local environment, javadoc command uses HTML templates for Japanese unless -Duser.language=en option is given. So, I looked for the way to pass the option to javadoc and finally found it.

Additional JVM options can be passed to maven-javadoc-plugin by adding the block shown below to pom.xml.

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <additionalJOption>-J-Duser.language=en</additionalJOption>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

However, note that, according to maven-javadoc-plugin FAQ, "Where in the pom.xml do I configure the Javadoc Plugin?", places to put configuration for maven-javadoc-plugin at vary depending on your purposes.




Steps to release a maven artifact

# Old title = "mvn release:prepare" stucks in "git push"

  1. Build a maven artifact and upload it to Sonatype OSS.

    $ eval `ssh-agent` Agent pid 6700

    $ ssh-add
    Enter passphrase for /c/Users/TakahikoKawasaki/.ssh/id_rsa:
    Identity added: /c/Users/TakahikoKawasaki/.ssh/id_rsa (/c/Users/TakahikoKawasaki/.ssh/id_rsa)


    $ mvn clean
    $ mvn release:clean
    $ mvn release:prepare
    $ mvn release:perform

  2. Login Sonatype OSS.

  3. Click "Staging Repositories" on the left menu and then search the list on the right for the uploaded maven artifact 

  4. Check the uploaded maven artifact and click "Close" button. The status will change to "closed" after a while.

  5. Click "Release" button.






Maven artifact をリリースする手順
# 旧タイトル = "mvn release:prepare" が "git push" で止まってしまった場合

  1. Maven artifact をビルドして Sonatype OSS にアップロードする。
    $ eval `ssh-agent`
    Agent pid 6700

    $ ssh-add
    Enter passphrase for /c/Users/TakahikoKawasaki/.ssh/id_rsa:
    Identity added: /c/Users/TakahikoKawasaki/.ssh/id_rsa (/c/Users/TakahikoKawasaki/.ssh/id_rsa)


    $ mvn clean
    $ mvn release:clean
    $ mvn release:prepare
    $ mvn release:perform

  2. Sonatype OSS にログインする。

  3. 左メニューの「Staging Repositories」をクリックし、右側のリストからアップロードした Maven artifact を探す。

  4. アップロードした Maven artifact にチェックマークをつけて「Close」ボタンをクリックする。しばらくすると Status が closed に変わる。

  5. 「Release」ボタンをクリックする。


    2013年1月9日水曜日

    "MIME media type application/json was not found" (Jersey client)

    If your Jersey client reports an error saying "MIME media type application/json was not found", try to enable FEATURE_POJO_MAPPING feature to support mapping between JSON and Java classes. Below is one example to create a Jersey client instance with FEATURE_POJO_MAPPING enabled.

    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;
    import com.sun.jersey.api.json.JSONConfiguration;

    private Client createJerseyClient()
    {
        // Configuration of Jersey client.
        ClientConfig config = new DefaultClientConfig();

        // Enable mapping between JSON and Java classes.
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

        // Create a Client instance.
        return Client.create(config);
    }

    Note that jersey-json-*.jar is needed. If you are using Maven, add a dependency entry like below.

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.16</version>
    </dependency>