Thursday, July 28, 2011

Changing color scheme and font settings in gvim




I like using vim for quickly loading a file for view/edit etc on my windows m/c. 
At work, with the monitors being 21" in size.. I wanted vim to open up on a larger frame, and also wanted to have some tweaks incorporated (font, color scheme, tab width, etc). I thought I'd simply have to edit my .vimrc from my home directory and voila.. everything should work. Well.. not quite. It works fine for my command line vi invocation from cygwin, but not when I open up from vim. After googling a bit, found few suggestions which satisfied my needs. All you have to do is to edit the _vimrc file which is located in you vim installation folder. For instance, C:\Program Files\Vim 
You can either edit it directly, or open up vim editor and goto Edit -> Startup Settings. Once opened, you can add the settings to the file. In my case, I added the following: 

set tabstop=4
set number
set lines=50
set columns=120

If you need to change the color scheme, chose the color scheme that you liked, and add the following to the _vimrc file:

:color morning

For font settings, choose the font that you like, then type 
:set guifont?
This will display the current font setting information. Note that down and set it to guifont in _vimrc.
Here's mine:

if has ('gui_running') 
 set guifont=Bitstream_Vera_Sans_Mono:h10:cANSI
endif

Happy vimming!! And thanks for those folks who put out this nice little tidbit about vim settings.. 

Wednesday, July 6, 2011

JSON Hiccups

This is my first entry into the JSON world and everything looked pretty straight-forward from the outset.
When the time came to dig in and use JSON and map it back to POJO's, issues started sprouting, but nothing too daunting or complicated.

Issue 1: I had to call a RESTful webservice which responds with application/json type. I dont have the JSON schema available and the JSON object is quite large (requiring quite a few classes and fields). Now I needed to generate the java classes, but couldnt figure out an easy way (like how you would generate classes using XML schema). I could lookup the json string and then create the Java classes manually, which is what I did for the simpler json responses..but for the larger responses, it didnt seem to be a smart way to go about. After some googling, found this wonderful link http://jsongen.byingtondesign.com/ which generates the java files for you if you could provide the url for the json request! What a time-saver?

Issue 2:  This time, I was getting a json string which didnt follow the standard syntax. All the property names were capitalized and some properties were just named '$'. While reading about the json mapping, I understood that badgerfish, when creating json string from xml schema generates '$' as the property name to represent the text between the xml element. ex:
XML: <customer>Arnold</customer> is represented as
JSON: {"$": "Arnold"}
But due to the capitalized property names, when I tried to use ObjectMapper.readValue(json, Class)...it fails due to the fact that its looking for a property with lowercase name. To overcome that, I then had to use @JsonProperty annotation on each property in java file to match the property name in json and everything went smooth after that.  

Issue 3: Some of the generated java files didnt include all the property names that were found in the json string. So when I tried to map the values from json into java objects, it failed with an org.codehaus.jackson.map.exc.UnrecognizedPropertyException. If you need that property, you'd have to add that in your generated java file. But assuming you dont need that property and you dont want to keep adding those fields which is not needed by your app, the simplest way is to disable the FAIL_ON_UNKNOWN_PROPERTIES feature in Jackson's deserialization config by calling:

ObjectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Monday, April 18, 2011

Maven Token Replacement

I've been trying to create a maven pom file that would do the same stuff that the current ant build file does. In the process, I've been able to learn some good stuff about Maven.
One thing I had to accommodate is the directory structure that we have, which unfortunately is not the same as maven's default. Once I got the basic stuff working, I wanted to try couple of things:

* Add profiles, so I can build the app for different environments
* Add tokens to files that can replaced during build process

To setup the profiles, I just had to add something like this:
<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activebydefault>true</activebydefault>
    </activation>
    <properties>
      <env.properties>dev.properties</env.properties>
    </properties>
  </profile>

  <profile>
    <id>sso</id>
    <properties>
      <env.properties>sso.properties</env.properties>

    </properties>
  </profile>
</profiles>

This can be part of the pom itself or it could be saved into a file called profiles.xml

To get the token replacement working, I ran into some trouble. First I tried using filters which is specifically meant for this purpose, or so I thought.

Add the filter:
<filters>
  <filter>dev.properties</filter>
</filters>


Enable the filter (w/o this, filtering wont work):
<resource>
  <filtering>true</filtering>
  <directory>src/main/webapp/WEB-INF</directory>
  <includes><include>web.xml</include></includes>
</resource>

This did replace the token specified in web.xml file, but filtering actually just copies the included resources to WEB-INF/classes after replacing the token. So filters were out and had to look for another option. Then I ran into maven-replacer-plugin which does exactly what I need to do.

<plugin>
  <groupid>com.google.code.maven-replacer-plugin</groupid>
  <artifactid>maven-replacer-plugin</artifactid>
  <version>1.3.5</version>
  <executions>
    <execution>
    <id>replaceAuth</id>
    <phase><b>package</b></phase>
    <goals>
      <goal>replace</goal>
    </goals>
    </execution>
  </executions>
  <configuration>
    <file>target/simple-webapp/WEB-INF/web.xml</file>
    <replacements>
      <replacement>
          <token>AUTH_METHOD</token>
          <value>${auth.method}</value>
      </replacement>
    </replacements>
  </configuration>
</plugin>

I first tried the 'prepare-pacakage' phase as mentioned in some forums, but the file that I need to run replacement on wasnt copied over to the target directory at that point. So the maven build fails. I had to use the 'package' phase to apply the token replacement.

But when I ran
> mvn package
the final war file didnt have the replacement applied, even though the file under exploded directory had replaced value. The reason for this being the war:war task was run before the replacer:replace task.

Back to the forums and found that I also had to use the maven-war-plugin to get this working.
<plugin>
  <groupid>org.apache.maven.plugins</groupid>
  <artifactid>maven-war-plugin</artifactid>
  <version>2.1.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals><goal>war</goal></goals>
      <configuration>
         <webxml>target/simple-webapp/WEB-INF/web.xml</webxml>
      </configuration>
    </execution>
  </executions>
</plugin>


The forums suggested that I had to use 'exploded' goal, but that didnt do the job for me. I had to use the 'war' goal and also added the webXml element that points to the modified file. This finally got what I wanted.. all the tokens replaced in the target directory and also in the final .war file.
Glad I got it working, but Im still uncertain about the impact of including webXml. Theoretically, I should've had this working without specifying the configuration portion. What if I have to modify a file other that web.xml? I'll edit this after I try that and if I gain any further understanding.

Update: 
Turns out that I really didnt need the <webXml> tag defined within the war plugin. But I did notice that that the war goal runs twice...once by default and once after the replacement. Another note to keep in mind is that since both the plugins (replacer & war) are bound to the same phase (package), the order is important. If the war runs before replacer, then the final war will not have the replaced values. So make sure that the replacer plugin comes AFTER the war plugin.

To replace multiple tokens:
Use the following config in replacer plugin ...

<configuration>
       <includes>
             <include>target/${project.build.finalName}/sample1.txt</include>
             <include>target/${project.build.finalName}/sample2.txt</include>
       </includes>
       <replacements>
               <replacement>
                        <token>token1</token>
                        <value>value1</value>
                </replacement>
                <replacement>
                        <token>token2</token>
                        <value>value2</value>
                </replacement>
        </replacements>                       
</configuration>

Externalize the tokens in a properties file:
Use maven properties plugin to load the properties file as shown below:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>maven-properties-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>etc/config/dev.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
</plugin>