Mind.random()

random ramblings of an eccentric programmer

Rerunning Me

Gist : All new tech items will be posted on to rerun.me

What? Another blog? Why ?

Over the past few years, as part of my college and outside, I managed to read some very interesting books and some cool stuff managed to stick in my brain. A few of them got retained in few notebooks and evernotes - all of which are terribly difficult to find when you need them. So, I decided to take a deliberate attempt to take some time and log all the most interesting information in an organized manner. And in the process of doing so, I don’t want to be a selfish @$$. So, here you go. http://rerun.me

So, what’s up with arunma.com?

Oh. Yeah. I am still unsure of how I am going to use this one. Now that all my mail ids are bound by google apps, I can’t shut it down (Frankly, I don’t want to). Few of the options that I could think of is to start whining about life or how great my family is. One of my friends was suggesting that I write an autobiography (huh??!!!). Just kidding.

As always, my ideas are dumb. Mail yours to arun AT arunma.com

Multiple Connections to a Server or Shared Resource

I was trying to connect to my NAS drive from my Windows 7 machine and got an interesting error. A little googling told me that a restart of the PC should solve the problem. I don’t want to and I know that is why you are here too.

So, if your problem as stated here is this

The network folder specified is currently mapped using a different user name and password. To connect using a different user name and password, first disconnect any existing mappings to this network share.

and this

Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.

then all you have to do is this

1) Open your Run window or Hit Window+R and type cmd. That should open your command window.

2) Type net use as in

1
2
3
4
5
6
7
8
9
C:\Users\Jason>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK                     \\192.168.1.7\IPC$        Microsoft Windows Network
The command completed successfully.

3) Type net use /delete <your serverpath\sharedfoldername> as in

1
2
C:\Users\Jason>net use /delete \\192.168.1.7\IPC$
\\192.168.1.7\IPC$ was deleted successfully.

Now, try reconnecting.

I am still experimenting with octopress and don’t know how to trackback to the original source. Apologies.

Prototype Design Pattern : Mindmap and Implementation

I was going through the Gang of Four again after a long time as part of my college elective and had a very interested experience.

There are some which I misunderstood or realised had few other interesting implementations or forgot they even existed.

Now, comparing and contrasting design patterns is a funny task when all my struggle goes into remembering them.

I have been using mindmaps lately to help organize my thoughts and thought I’ll convert few design patterns into maps. Hope you would find it useful too.

Click to see the full web optimized image. Mail me/Drop a comment for a bigger one or the iMindmap file.

MindMap

Implementation

This implementation snippet is from my college CA task. The idea is to read a property file and create new instances of Coin objects. The CashPropertyLoader uses the StoreObjectFactory to clone and initialize the Coin object. Both these methods are available in the Coin object. The StoreObjectFactory also uses a StoreObjectRegistry to cache the prototypes. The StoreObjectRegistry acts as the prototype manager

Class Diagram

Cash Property Loader
CashPropertyLoader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 public CashStoreItem getItem(int idx){
  
      CashStoreItem   cit;

      Map<String,String> params=new HashMap<String, String>();
  
      String  item, itemv;

      item = new String("Name" + idx);
      itemv = getValue(item);
      params.put("name", itemv);
  
      item = new String("Weight" + idx);
      itemv = getValue(item);
      params.put("weight", itemv);

      item = new String("Value" + idx);
      itemv = getValue(item);
      params.put("value", itemv);
  
      Coin coin = (Coin)StoreObjectRegistry.lookup(StoreObjectEnum.COIN);
      coin.initialize(params);
  
      System.out.println("ToString" +("Idx :"+idx+ "\nCoin "+coin));

      item = new String("Quantity" + idx);
      itemv = getValue(item);

      cit = new CashStoreItem(coin, Integer.parseInt(itemv));
      //System.out.println("ToString" +("Idx :"+idx+ "\nCashstore item "+cit));
  
      return cit;
  }
StoreObjectRegistry
StoreObjectRegistry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class StoreObjectRegistry {

  private static Map<StoreObjectEnum, StoreObject> storeObjectRegistry=new HashMap<StoreObjectEnum, StoreObject>();
  
  public static StoreObject lookup(StoreObjectEnum storeObjecType)  {
      
      StoreObject storeObject=null;
      
      try {
          StoreObject storeObjectImpl=null;
          
          switch(storeObjecType){
              
              case COIN:
                  storeObjectImpl=new Coin();
                  break;
                  
              case DRINK:
                  storeObjectImpl=new DrinksBrand();
                  break;
              }
              
              if (storeObjectImpl!=null){
                  storeObjectRegistry.put(storeObjecType, storeObjectImpl);  
              }
              else{
                  System.err.println("Store Object Implementation could not be resolved. Please check your store object type and store object mapping");
              }
              
              storeObject =(StoreObject)storeObjectRegistry.get(storeObjecType).clone();
          
      } catch (CloneNotSupportedException e) {
          e.printStackTrace();
      }
      return storeObject;
      
  }
  
}
StoreObject
StoreObject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public void initialize(Map<String, String> params){

      ArrayList<Field> fieldsList=new ArrayList<Field>();
      addDeclaredAndInheritedFields(this.getClass(), fieldsList);
      
      for (Field field : fieldsList) {
          
          field.setAccessible(true);
          
          if (params.get(field.getName())==null){
              continue;
          }
          try {

               if (field.getType() == boolean.class) {
                  
                  field.setBoolean(this, new Boolean(params.get(field.getName())));
                  
              } else if (field.getType() == int.class) {
                  
                  field.setInt(this,Integer.parseInt(params.get(field.getName())));
                  
              } else if (field.getType() == double.class) {
                  field.setDouble(this,Double.parseDouble(params.get(field.getName() )));
                  
              } else {
                  field.set(this,params.get(field.getName()));
              }
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println("Unable to map field : " + field.getName());
              e.printStackTrace();
          }
      }
      
    }


    private static void addDeclaredAndInheritedFields(Class c, List<Field> fields) {
      
        fields.addAll(Arrays.asList(c.getDeclaredFields()));
        Class superClass = c.getSuperclass();
        if (superClass != null) {
            addDeclaredAndInheritedFields(superClass, fields);
        }
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

Flexing Time to the Extreme

Seriously, what’s up with Java Swing?  Trust me, I tried really hard to understand what could have gone into the minds of the Swing designers. But still,  applets are the pioneers of the interactive web (and way better than programming in the naturally 128-bit encrypted Perl on CGI). So, I am going to go easy on applets.  It’s like what they say in my native tongue - beating up a dead snake.

Alright let me tell the background of why would anyone touch Swing in 2011 unless they are maintaining a legacy JNLP talking to MQ backed by Pro-C to Oracle (btw, that combo is a real working application in Verizon).  As you know, I am taking this postgrad in Software engineering and one of the core modules is Java.  As part of the module, each group were given an small application to build using only the vanilla JDK.  No external libraries. Sounded simple initially but the project was a real eye opener at so many levels.

Don’t forget the basics :

The assignment project was JDK alone.  Over the years, I started taking the value of open source libraries for granted.  Say, log4j, commons-lang, collections, xml parsers or even an RDBMS database.  While it was funny to notice how dependent I’ve become on many of these libraries, it was very interesting to write your own and really challenging to keep the code decently readable.  So, next time I blame a framework for bad design,  I have this project to bring me down to earth.  One thing that I realised is that if your creativity is “in the flow”, you dont want to get disturbed by design constraints.  Resisting the temptation and still sticking on to a good design is a hard thing to do but you have to do it for the sake of the psychopath programmer.

Team work and delegation :

All developers are loners.  They think that they do their best when alone.  I am no exception.  However, being a part of a team and pulling each other up is real fun.  Understanding what each person is good at and adjusting your tasks according to that is real art.  We had humble beginnings - I could work only one day of the weekend but when code started pouring in, I could see what synergy really means.  It’s like the story of the stone soup. No wonder the best projects in the world are delivered by groups.

Time management :

This year (even from the close of last year),  day job has been super crazy. May be that is the reason they call it a deadline.  You really have to die to finish the project on time.  This project really means a lot to me, my team and the entire center. Working 80+ hours in the week is already taxing.   That killed the 2011 resolution right from the first week - Weekends only for personal projects and experiments.   And with the MTech, family and learning hadoop and flex, I had to do something to manage time.  Like my mentor used to say, you can’t manage time, you can just manage priorities.  I don’t have a choice at office.  I can’t do badly on MTech. If I do badly on the core module, I am out of the course.  Call it a horrible decision, but I decided to send my family on vacation to India (that was 2 months ago and they are coming back next week. I am super excited).  I’ll be learning Hadoop and cloud as part of my elective module in May.  So, pause on my hadoop/solr/lucene projects until then (trust me, they are really addictive).  Now that I have a little free time on the trains and before bed, I am currently learning Flex in what I would like to call “micro-installments”.  I am taking the Flex in a week course for the past month and still I am on Day 2 of the course.  I think I am taking “It doesn’t matter how slow you go as long as you don’t stop”, a little too seriously.  So, why am I learning flex now?  You’ll see.

All things are difficult before they are easy :

Being aware of your weakness is good.  Even better is to minimize the intensity of the weakness - from scary to manageable.  I was really worried when the front end is supposed to be on Swing and Swing gave me nightmares in 2003.  So, after designing and coding the back end of the project, I still wanted to give swing a try.  Surprisingly, I actually managed to pull it off. It is not that bad….  Ah who am I kidding. I was horrible but Google’s window builder made my job way way easier.  For some reason, I found it much better than Netbeans Matisse.  I pulled up a couple of JTables and did some fancy stuff on it. (Swing developers, please dont laugh at me).

Over all, this year has now personally rated the toughest year in the past 10 years.  Like they say, when the going gets tough, the tough gets rough (whoaa that’s a little too much boasting even for me).

Log4j JSONLayout and JLogIndexer

As I mentioned in my previous post, I am working on building a Solr based log indexing and search.  My idea was simple.  Write a custom appender, use XML Layout, parse the XML into a Document and use SolrJ to push to Solr. Very soon reality struck me :

1)  log4j XML Layout is not a good idea for our requirement.  I really dont want to spend time on parsing about 100 XMLs every second.  That is a lot of time even for a StAX based Woodstox.  So, I just extended log4j layout and over-rid a couple of methods to create a JSON Layout.

2)  Using Jackson Streaming API to parse the JSON string from the Layout opened my eyes.  There were too many external dependency libraries (jackson, solrj and their dependent jars) that I was adding on to the application which ultimately will all go to the host application.  That is totally insane.

So, I figured out that way to go is to use

1) log4j JMS appender to put JSON strings into

2) Active MQ running on a app server running Solr.

I am hoping that this should give us the following benefits :

1) No complications on the host application. (copying libraries, classloader issues with library version incompatibility etc)

2) Non-log4j based java applications and non-java applications could use one of the various connectivity options available with ActiveMQ to put messages into its queue.

http://activemq.apache.org/connectivity.html

3)  Extension and replacing the layout and message parsing.

4)  Solr Document construction and the interface (initial version will still be on SolrJ) can be customized.

This weekend should be pretty awesome then.

Log4j Log Indexing Using Solr

We are finding it very hard to monitor the logs spread over a cluster of four managed servers. So, I am trying to build a simple log4j appender which uses solrj api to store the logs in the solr server. The idea is to use leverage REST of solr to build a better GUI which could help us

1) search the logs and the display the previous and the next 50 lines or so and

2) tail the logs

Being awful on front ends, I am trying to cookup something with GWT (a prototype version). I am planning to host the project on googlecode under ASL.

Greatly appreciate if you could throw some insights on

1) Whether it makes sense to create a project like this ?

2) Is using Solr for this an overkill?

3) Any suggestions on web framework/tool which will help me build a tab-based front end for tailing.

I owe you guys. Thanks.

Back on Track - Watch Out for Updates

I have been doing so many crazy things in the last quarter of last year and this year so far. And interestingly, all turned out to be very good.  Lets say, staying hungry and foolish really pays. So, I felt the need to revamp my blog (my blog being brought down to its knees last month by an attack is really a blessing in disguise).

So, please watch out for the updates.  I will try my best to make it regular and useful.

Shutdown Windows XP From Remote Desktop/VPN Connection

Funny story. I wanted to login to my office PC from my Mac due to some emergency work only to see Murphy staring at me. My office windows PC got hung and I was unable to do anything useful. I tried doing a ”taskmgr” on the Run Window but the window simply wouldnt come up.

I wanted to restart but unfortunately the Ctrl+Alt+Delete doesnt work on my Mac the way it is intended to. After a lot of struggle, I managed to open a command window and hit ”shutdown -r”.  Does the job.

Resolution : Apache Commons Configuration (XMLConfiguration) in a Spring Bean

I was trying to inject apache commons configuration (XMLConfiguration) into a Spring bean and faced the error :

hint: specify index/type/name arguments for simple parameters to avoid type ambiguities

I was breaking my head for a while only to find later that my config XML is not even in the classpath. You can find the configuration below

1
2
3
4
5
6
<bean id="xmlConfig"> <constructor-arg type="java.net.URL" value="classpath:alert-config.xml" />
<property name="reloadingStrategy"/>
</bean>

<bean id="alertConfig"> <property name="xmlConfig" ref="xmlConfig"></property>
</bean>