Posted 1 week ago

Groovy and Grails interview questions

One of the very few tests I could find around!

Posted 1 year ago

Install many instances of tomcat

Recently I put myself into a situation where I had to install 40+ tomcats on once machine. Yes, this machine was “large” enough to house all the tomcats. If one has to do this manually, not it will take away lots of time from your life where you could have been watching some youtube videos but it will also take lots of book keeping of which tomcat uses which port and what are the jmx, ajp ports etc…

Finally I just decided to write my own batch file for windows (sorry, linux guys, I love linux too but what can we do when the machine was windows!) This batch file will create tomcat services from one catalina home, this means you do not have to copy and paste any folders or files 10 times to install 10 tomcats.

I started with downloading copy of tomcat, in my case it was Apache tomcat-7.0.27. Then I edited to the server.xml file to parameterize it. I wanted my connector port to read like following rather then hard coding the values like 8080.

port=”${connectorPort}”

I did the same thing with AJP port and shutdown port

port=”${ajpPort}” 

port=”${shutdownPort}”

Once I made these changes I was ready to take a deep dive into writing a batch file (as a matter of fact learning to write a batch file)

My batch file looked like following. 

ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

set NUMOFSERVICES=10

SET PREFIX=Tomcat

SET STARTCONNECTORPORT=8000

SET STARTAJPPORT=7000

SET STARTSHUTDOWNPORT=6000

SET STARTJMXPORT=10000

SET JAVA_HOME=C:\Program Files\Java\jrockit-jdk6

set /P PREFIX=Tomcat service name prefix(%PREFIX%): %=%

set /P NUMOFSERVICES=Number of instances you want to install(%NUMOFSERVICES%): %=%

if “%NUMOFSERVICES%”==”” goto EXIT

set /P STARTCONNECTORPORT=Starting value for your connector port(%STARTCONNECTORPORT%): %=%

if “%STARTCONNECTORPORT%”==”” goto EXIT

set /P STARTAJPPORT=Starting value for your ajp port(%STARTAJPPORT%): %=%

if “%STARTAJPPORT%”==”” goto EXIT

set /P STARTSHUTDOWNPORT=Starting value for your shutdown port(%STARTSHUTDOWNPORT%): %=%

if “%STARTSHUTDOWNPORT%”==”” goto EXIT

set /P JAVA_HOME=Your Java HOME(%JAVA_HOME%): %=%

#if “%JAVA_HOME%”==”” goto EXIT

SET CURRENTCONPORT=%STARTCONNECTORPORT%

SET CURRENTAJPPORT=%STARTAJPPORT%

SET CURRENTSHUPORT=%STARTSHUTDOWNPORT%

FOR /L %%I in (1,1,%NUMOFSERVICES%) DO (

echo

ECHO %%I—————- && echo

#echo  %CURRENTCONPORT% %CURRENTAJPPORT% %CURRENTSHUPORT%

SET /a CURRENTCONPORT=%STARTCONNECTORPORT%+%%I

SET /a CURRENTAJPPORT=%STARTAJPPORT%+%%I

SET /a CURRENTSHUPORT=%STARTSHUTDOWNPORT%+%%I

SET /a CURRENTJMXPORT=%STARTJMXPORT%+%%I

ECHO installing tomcat with !CURRENTCONPORT! !CURRENTAJPPORT! !CURRENTSHUPORT!

bin\tomcat7 //IS//%PREFIX%-!CURRENTCONPORT! ^

—JvmOptions=-Dcatalina.base=%CD%;^

-Dcom.sun.management.jmxremote.port=!CURRENTJMXPORT!;^

-Dcom.sun.management.jmxremote.ssl=false;^

-Dcom.sun.management.jmxremote.authenticate=false;^

-Dcatalina.home=%CD%;^

-Djava.endorsed.dirs=%CD%\endorsed;^

-Djava.io.tmpdir=%CD%\temp;^

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;^

-Djava.util.logging.config.file=%CD%\conf\logging.properties;^

-Djvmarg=-XX:MaxPermSize=456m;-DconnectorPort=!CURRENTCONPORT!;^

-DshutdownPort=!CURRENTSHUPORT!;^

-DajpPort=!CURRENTAJPPORT!; ^

—Install=”%CD%\bin\tomcat7.exe” ^

—Jvm=”%JAVA_HOME%\jre\bin\jrockit\jvm.dll” ^

—StartMode=jvm —StopMode=jvm ^

—StartClass=org.apache.catalina.startup.Bootstrap ^

—StartParams=start ^

—StopClass=org.apache.catalina.startup.Bootstrap ^

—StopParams=stop ^

—JvmMx=1024 —JvmMs=512 ^

—Classpath=”%CD%\bin\bootstrap.jar;%CD%\bin\tomcat-juli.jar;%CD%\bin\tomcat-juli.jar” ^

—LogPath=”%CD%\logs” ^

—StdOutput=auto ^

—StdError=auto

net start %PREFIX%-!CURRENTCONPORT!

)

exit

:EXIT

echo invalid input

echo … exiting

Running the batch file will give you some thing like following 

My machine had no other servers installed so I could go ahead with the default values. In my case JDK is at the said path so all I had to do was hit “enter” few times and there I go. I had 10 tomcats installed on my machine as a service. :)

Posted 1 year ago
Why are search engine crawlers called spiders?
Because they crawl the web:)
Posted 1 year ago
Posted 1 year ago

Mercurial and netbeans

Mercurial is one of the rising source control system being used by Mozilla, OpenJDK, OpenOffice and many other popular opensource softwares.

Anyone who has experience with SVN, GIT or even VSS should be able to pick it up easily. One basic difference between Mercurial(hg) and other source control system is that Mercurial maintains one copy of source control repository on the client(your)  machine too.

Bitbucket.org is free code hosting service supporting mercurial.  

To clone a project hosted on bitbucket, login with a username and password and navigate to a project that is shared with you(or navigate to an opensource project). On the overview tab you will find “Clone this repository” label with a URL.

Copy this url ,open Netbeans, navigate to Team->Mercurial ->”Clone Other” and paste it under Repository URL textbox. Click through the wizard and when prompted for a password enter your bitbucket username and password. After the project has been downloaded netbeans will prompt you to open the downloaded project, click on “yes”

Now, it’s easy to make changes to the project and commit them by right clicking on projectname->mercurial->commit. Remember, unlike SVN or VSS committing a change only effects your machine, once it is committed you will have to Push it to the hosted repository by right clicking on project name and then clicking mercurial->push.

Similarly when you want to get changes from the repository you can select “Pull” option under mercurial mennu

As simple as that!!!

Posted 1 year ago
Posted 1 year ago

Multiple instances of Tomcat with, one install, one webapp, one bin

Computer memory is being so cheap now a days that it is not hard to find machines with 100+ gb of RAM. On such machines one might have to install 30, 40 or even 50 tomacts to cater same content. Content might a website, web service etc..

Recently I encountered a situation where I had to install 200 tomcats on a single machine which will host one webservice load balanced by tomcat. I was looking for the best way to create these many instances and there was none which would let me do it without copying and pasting logs, conf, bin directory 200 times. Finally i wrote my own batch file. Which would install as many services with one click, one catalina home and one catalina base. 

The batch file takes advantage of parameterization of server.xml file. For example

pass JAVA_OPTS=-DconnectorPort=8080 from command line and server.xml has  port=”${connectorPort}”


This is still work in progress, especially, arguments about logging directory are not yet configured, but still it will work. Try it and let me know your comments.

ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

set NUMOFSERVICES=3

SET PREFIX=TOMCAT

SET STARTCONNECTORPORT=8000

SET STARTAJPPORT=7000

SET STARTSHUTDOWNPORT=6000

SET JAVA_HOME=%JAVA_HOME%

set /P PREFIX=Tomcat service name prefix(%PREFIX%): %=%

set /P NUMOFSERVICES=Number of instances you want to install(%NUMOFSERVICES%): %=%

if “%NUMOFSERVICES%”==”” goto EXIT

set /P STARTCONNECTORPORT=Starting value for your connector port(%STARTCONNECTORPORT%): %=%

if “%STARTCONNECTORPORT%”==”” goto EXIT

set /P STARTAJPPORT=Starting value for your ajp port(%STARTAJPPORT%): %=%

if “%STARTAJPPORT%”==”” goto EXIT

set /P STARTSHUTDOWNPORT=Starting value for your shutdown port(%STARTSHUTDOWNPORT%): %=%

if “%STARTSHUTDOWNPORT%”==”” goto EXIT

set /P JAVA_HOME=Your Java HOME(%JAVA_HOME%): %=%

if “%JAVA_HOME%”==”” goto EXIT

SET CURRENTCONPORT=%STARTCONNECTORPORT%

SET CURRENTAJPPORT=%STARTAJPPORT%

SET CURRENTSHUPORT=%STARTSHUTDOWNPORT%

FOR /L %%I in (0,1,%NUMOFSERVICES%) DO (

echo

ECHO %%I—————- && echo

echo  %CURRENTCONPORT% %CURRENTAJPPORT% %CURRENTSHUPORT%

SET /a CURRENTCONPORT=!CURRENTCONPORT!+1

SET /a CURRENTAJPPORT=!CURRENTAJPPORT!+1

SET /a CURRENTSHUPORT=!CURRENTSHUPORT!+1

ECHO installing tomcat with !CURRENTCONPORT! !CURRENTAJPPORT! !CURRENTSHUPORT!

bin\tomcat7 //IS//%PREFIX%-!CURRENTCONPORT! ^

—JvmOptions=-Dcatalina.base=%CD%;^

-Dcatalina.home=%CD%;^

-Djava.endorsed.dirs=%CD%\endorsed;^

-Djava.io.tmpdir=%CD%\temp;^

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;^

-Djava.util.logging.config.file=%CD%\conf\logging.properties;^

-Djvmarg=-XX:MaxPermSize=456m;-DconnectorPort=!CURRENTCONPORT!;^

-DshutdownPort=%CURRENTSHUPORT%;^

-DajpPort=!CURRENTAJPPORT!; ^

—Install=”%CD%\bin\tomcat7.exe” ^

—Jvm=”%JAVA_HOME%\jre\bin\server\jvm.dll” ^

—StartMode=jvm —StopMode=jvm ^

—StartClass=org.apache.catalina.startup.Bootstrap ^

—StartParams=start ^

—StopClass=org.apache.catalina.startup.Bootstrap ^

—StopParams=stop ^

—JvmMx=2048 —JvmMs=2048 ^

—Classpath=”%CD%\bin\bootstrap.jar;%CD%\bin\tomcat-juli.jar;%CD%\bin\tomcat-juli.jar”

net start %PREFIX%-!CURRENTCONPORT!

)

:EXIT

echo invalid input

echo … exiting 

Well, after I am 100% done with this I am gonna have to create a batch file to stop/start all tomcat services, remove all tomcat services etc…

please feel free to drop in some comments if I am re-inventing the wheel, if there are other tools which does the same!

Posted 1 year ago

Ever had to work with lot's of Regex?

This is a great tool to create regex by examples. For example if you want a regex that matches “Exam”, “Examination”, “Exams” etc… Then feed it to the API and it will give you a most optimized regex..

Check out an example here 

https://code.google.com/p/graph-expression/wiki/RegexpOptimization

Posted 1 year ago
Posted 1 year ago

JSF Component creation at run-time

Introduction

Often programmers come across a situation where they have to create components on the fly (at run time). Components can be anything, from layout panels to command buttons to input components. One way to do this is to use the JSF table component which is simple to implement, but enforces adherence to the table component by taking away the freedom to come up with one’s own layouts.

In this tutorial, we will create some components like command buttons and input text at run time. The main idea is to understand the process of creating components and their action events at run time.

This tutorial works with the following technologies and resources:

  • Tomcat Server 5.X
  • NetBeans IDE
  • Visual Web Pack

For any kind of JSF development, NetBeans comes as a natural choice because it not only lets you do basic things very easily, such as providing drag and drop, but it also empowers you to do advance things by directly going into JSP and Java code.

  1. Start with a new project and name it Tutorial.
  2. Drag and drop a GridPanel component from the palette and name it parentPanel, then drop a StaticText component and name it resultTxt as shown here.
  3. Now switch to the Java code of the page, find the method called getParentPanel() and add the following code to the method:
try{
            FacesContext context = FacesContext.getCurrentInstance();
            UIComponent parent = parentPanel;
            TextField text1 = new TextField();
            TextField text2 = new TextField();
            Button btn1 = new Button();
            Button btn2 = new Button();
            btn1.setId("mult");
            btn2.setId("add");
            btn1.setText("Multiply them");
            btn2.setText("Add them");
            Class[] parameterList = { Class.forName("javax.faces.event.ActionEvent")};
            MethodBinding mb = (MethodBinding) 	
            context.getApplication().createMethodBinding("#{Page1.operations}", parameterList);
	    btn1.setActionListener(mb);
            btn2.setActionListener(mb);
	    parent.getChildren().add(text1);	
            parent.getChildren().add(text2);	
            parent.getChildren().add(btn1);	
            parent.getChildren().add(btn2);	
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

Initially, some lines will be underlined red to indicate errors. Right click in the code editor and click on the fix import option. The IDE will give two options for choosing certain packages; selectcom.sun.rave.web.ui.component.TextField and com.sun.rave.web.ui.component.Button from the drop down lists.

The fundamental concept here is that like every other class UI components can also be initialized and instantiated in the code. These classes offer their own set of methods. In the code above, we are using three of them: setId, setActionListner and setText.

  • setId(String id) sets the UI component’s id.
  • setText(String text) sets the text that will appear on the button.
  • setActionListner(mb) sets the method binding of that UI component.

In a nutshell, we are telling the compiler that whenever btn1 or btn2 are pressed, to call the operations method of Page1.

Finally, in last four lines we are adding these objects (which are actually UI components) to the parent component, the parentPanel.

We are ready to test run the project. You will see something like this, but the buttons will still not work. We have to make a method called operations to make these buttons work.


Add the following method to Page1’s Java code.

 public void operations(ActionEvent ae){
        String operation = ae.getComponent().getId();
        if (operation.equals("mult")){
            int a = Integer.parseInt(((TextField)parentPanel.getChildren().get(0)).getText().toString());
            int b = Integer.parseInt(((TextField)parentPanel.getChildren().get(1)).getText().toString());
            resultTxt.setText(new Integer(a*b));
        }
        else{
            
            int a = Integer.parseInt(((TextField)parentPanel.getChildren().get(0)).getText().toString());
            int b = Integer.parseInt(((TextField)parentPanel.getChildren().get(1)).getText().toString());
            resultTxt.setText(new Integer(a+b));
        }
    }

In the very first line, we are trying to get the name of the component that called this method: ae.getComponent().getId(). The method returns the ID of the component, which can be “mult” or “add” depending on the name of the component performing the operation.

Inside the if loop, Integer.parseInt(((TextField)parentPanel.getChildren().get(0)).getText().toString()); gets the number from the text box and converts it into int. If we analyze this line in detail we can see that we are getting all the children of parentPanel and then retrieving the first component that was added to it and then getting the text that was typed in.

In if {…} , we are multiplying the values typed in both of the text boxes, and in else{…} we are adding them. These values are then displayed on the resultTxt component by using methodresultTxt.setText(new Integer(a+b)).

Note that we are assuming in the above code that users will only enter numbers in the input component. To prevent users from entering other values we can use validators. An explanation of validators is beyond the scope of this tutorial.

At the end of this, we are ready to run the program. Try typing in numbers in both of the input boxes and click on either button. The result will be displayed in the static text box.

This kind of programming is very useful when designing dynamic forms, for example, several surveys or questionnaires for a job application. This kind of work needs extensive usage of runtime component creation because UI components in a questionnaire might change depending on the job type or application type.