JPragma Blog

Pragmatic Developers, Elegant Software

FlexBuilder code formatter

leave a comment »

Automatic code format and correcting indentation are essential features of any IDE. Unfortunately FlexBuilder currently doesn’t have them. Recently I found very cool plugin called flexformatter that solves this issue. Start by installing flexformatter using eclipse update site. After restaring Eclipse/FlexBuilder you will find “Flex Formatting” menu under “Preferences”.

Here is some description of various configuration options and my configuration file that can be simply imported.

Written by isaaclevin

February 14, 2010 at 7:20 pm

Posted in Flex

Adjust Windows keyboard shortcuts to match Mac OS X

leave a comment »

I use MacBook as my primary computer at home and Windows in the office.

First half of every day I keep pressing Alt-C, Alt-V in order to copy/paste… Well, that’s where Mac has its “Command” key. By the end of the day I get used to Ctrl-… combination, then I come home, turn on my MacBook and the hassle continues.

Here is solution I found:

  1. Download AutoHotKey and install on Windows
  2. Put following script into “Start->All Programs->Startup”(I called it mackeys.ahk):

!c::Send ^c
!x::Send ^x
!v::Send ^v
!s::Send ^s
!a::Send ^a
!z::Send ^z
!q::Send !{F4}

From now on Alt-C sends Ctrl-C, Alt-V -> Ctrl-V, etc. Default Ctrl key combinations still can be used.

Written by isaaclevin

February 14, 2010 at 7:19 pm

Posted in General

QuickLook plugins for Mac OS X 10.5 Leopard

leave a comment »

QuickLook is a feature in Leopard, which allows to view file content without opening it, directly from Finder.

Here is the list of additional plugins I have installed on my MacBook (to install simply copy.qlgenerator package into /Library/QuickLook or ~/Library/QuickLook):

  1. BetterZipQL – view content of an archive (many archive types are supported)
  2. FolderViewer – view content of selected folder
  3. QLEnscript – colorized preview of source code files
  4. ColorXML – colorized preview of XML files

Written by isaaclevin

February 14, 2010 at 7:17 pm

Posted in General

Combined Java+Flex project with hot redeployment

leave a comment »

In small applications it makes sense to combine Java and Flex code under same project. I’m a big fan of maven and one of my favorite maven plugins is embedded jetty container. This plugin runs jetty directly from maven and supports automatic monitoring and redeployment of java classes and resources. I thought it would be nice to run Java+Flex projects in the same way and have updated SWF file instantly available in the browser immediately after Flex Builder compiles it.

You can find step-by-step instructions on such project set up on my wiki: http://wiki.jpragma.com/creating-new-javaflex-project

Written by isaaclevin

February 14, 2010 at 7:16 pm

Posted in Flex, Java

Using Groovy in JMeter

leave a comment »

Note: This is a copy from my old blog posted in 2007. Some things mentioned here have been changed since then.

Groovy is a great scripting language intimately integrated with Java. Being able to leverage any code written in Java and at the same time having all the advantages of dynamic programming language, makes it an excellent choice for tasks such as file management, build systems and testing.

JMeter is an open source load test tool. It supports many protocols (HTTP, FTP, JMS and others) out-of-the-box.

Many times I found myself in situation when I wanted to run load test against stand-alone Java components or against an application that uses proprietary binary TCP based protocol. Groovy would be an excellent choice for this task. Some work on integrating JMeter and Groovy was done by Alexandru Popescu, however the project is not available to the public yet. Then I discovered that there is an indirect support for groovy in JMeter itself. JMeter supports BSF, which is an abstraction layer on top of many scripting languages including groovy. BSF distribution that comes with JMeter actually has some glitch in groovy support, but after upgrading to the latest version of bsf.jar it was very easy to start writing JMeter scripts in groovy.

Here the steps I followed:

  1. download latest groovy binaries from http://groovy.codehaus.org/
  2. copy groovy-all-1.0.jar to jmeter/lib directory
  3. download latest bsf binaries from http://jakarta.apache.org/bsf/ (I was using version 2.4.0)
  4. replace bsf-2.3.0.jar in jmeter/lib with bsf.jar from the version you just downloaded
  5. start jmeter
  6. create BSF Sampler and specify “groovy” as a scripting language (see screenshot below)
  7. type your groovy script and run it

This method is far from being perfect. It actually monitors how much time it takes to execute the script itself, but in most cases you’d like to specify exactly when to start counting execution time [results.sampleStart(); results.samplePause(); results.sampleResume(); results.sampleEnd();]
I hope Alexandru will make his project available soon, or may be I’ll find some time to create more sophisticated JMeter Groovy plug-in.

Another issue is how to transfer data between several scripts. Let’s say you’d like to create 1st script that establishes TCP connection; 2nd that sends proprietary login message and gets back some security token; 3rd that sends some business message (using the same TCP connection and providing the token) and 4th that just disconnects. How to transfer Socket and Token objects between scripts? Well, you can use ThreadLocal. Following code snippet defines ThreadLocalStorage class that can be used from the scripts (isn’t it great that you can just call Java from Groovy?) to save and retrieve any data:

package com.jpragma.utils;

import java.util.HashMap;

public class ThreadLocalStorage {
  private static ThreadLocal storage = new ThreadLocal() {
    protected Object initialValue() {
      return new HashMap();
    }
  };
  
  public static Object get(String name) {
    HashMap vars = (HashMap) storage.get();
    return vars.get(name);
  }
  
  public static void set(String name, Object value) {
    HashMap vars = (HashMap) storage.get();
    vars.put(name, value);
  }
}

Just package it as a jar file and put into jmeter/lib directory. Now you can write your scripts like this:

Connect:

import com.jpragma.utils.ThreadLocalStorage

def socket = new Socket("localhost", 18012);
def iStream = new DataInputStream(socket.getInputStream());
def oStream = new DataOutputStream(socket.getOutputStream());

ThreadLocalStorage.set('mySocket', socket)
ThreadLocalStorage.set('myInStream', iStream)
ThreadLocalStorage.set('myOutStream', oStream)

return "success"

Echo Message

import com.jpragma.utils.ThreadLocalStorage

def iStream = ThreadLocalStorage.get('myInStream')
def oStream = ThreadLocalStorage.get('myOutStream')

def str = "My String"
oStream.writeInt(str.length())
oStream.write(str.bytes)

def msgSize = iStream.readInt()
def buf = new byte[msgSize]
iStream.readFully(buf)
def responseStr = new String(buf)

if (str == responseStr)
    return "success"
else
    return "failure"

Disconnect

import com.jpragma.utils.ThreadLocalStorage

def socket = ThreadLocalStorage.get('mySocket')
if (socket != null) {
    socket.close()
}
return "success"

Written by isaaclevin

February 14, 2010 at 7:12 pm

Posted in Java