Posted by: ranvijay | January 17, 2012

nohup not returning immediately

If a process is to be started in the background, generally the following command works:


nohup somecommand.sh &

But I observed that on some linux machines the above command waits for an ‘enter’ key to be pressed and doesn’t return immediately. This is annoying especially when the command is being run as part of some java code that is trying to start some remote process in background. The java process would simply hang in such a case.

To fix the problem, use the following command instead:


nohup somecommand.sh >>nohup.out 2>&1 &

Posted by: ranvijay | December 9, 2010

Passing data to Flex ItemRenderers

Well, this took me some time to figure out. I was wondering for quite some time about a neat way to pass some data to my custom itemRenderer. Well, let me explain a bit what I was trying to achieve. I was trying to render a combobox in a datagrid column and the tricky part was that the dataprovider of the combobox was not static. So harcoding of the combobox dataprovider values inside the itemRenderer was not an option for me. Oh, I forgot to mention that since the datagrid was a dynamic one in my case, the datagrid was being created through actionscript and I was using ClassFactory to set the itemRenderer.

One workable option came out to be store the dynamic ‘combobox dataprovider’ in the Application object, and then retrieve it from the Application from the itemRenderer. But that was neat enough, for the simple reason that why would I set something in the Application for the mere purpose of a storing datagrid column dataprovider.

Finally the solution came out to be to simple and elegant. It’s all about the ClassFactory. I was already using it for setting my customRenderer for the datagrid column. What I didn’t know about this class was that I could also set properties of my customRenderer using this class. Here is some comment from ClassFactory class:

var productRenderer:ClassFactory = new ClassFactory(ProductRenderer);
productRenderer.properties = { showProductImage: true };
myList.itemRenderer = productRenderer;

Here showProductImage is a property of type boolean in ProductRenderer class. Needless to say, you can set function and event handlers as well this way :)

Posted by: ranvijay | September 10, 2010

XML parsing and java.net.UnknownHostException: java.sun.com

Yesterday I was writing code for parsing the the ejb descriptor file contained in a ejb jar file.

The code was something like this:

private void parseAndPopulateResourceReferences(File ejbJarFile) {
try {
JarFile jarFile = new JarFile(ejbJarFile);
JarEntry jarEntry = jarFile.getJarEntry("META-INF/ejb-jar.xml");
if (jarEntry != null) {
InputStream is = jarFile.getInputStream(jarEntry);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
//do something with the doc
populateEntriesFromDoc(doc);
}
} catch (Exception e) {
throw new RuntimeException("Exception found while trying to parse ejb descriptor file for ejb named " + ejbJarFile.getName(), e);
}
}

The code worked fine in my office, but when I tried it from home in the evening, I started to get exception from the same code. The exception I am talking about is this one:


Caused by: java.net.UnknownHostException: java.sun.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)

So it was clear that it was looking for something on internet.. and failing because there was no internet connection available.
I realized that this exception was thrown because the XML which was being parsed contained this at the top:

<!DOCTYPE ejb-jar PUBLIC “-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN” “http://java.sun.com/dtd/ejb-jar_2_0.dtd”>

Now there are were 2 solutions which I could think of: Either download the DTD files from internet and let the XML refer to the local copy of DTD, something like this:

<!DOCTYPE root-element SYSTEM “ejb-jar_2_0.dtd”>

But this option was not viable for me, as I didn’t know what XML I would be parsing at run time, so I opted for the second option of disabling the DTD checking. This can be done by setting a custom entity resolver on the DocumentBuilder object.


private static void disableDtdValidation(DocumentBuilder db) {
// disable the dtd validation by setting a custom entity resolver
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
System.out.println("Ignoring " + publicId + ", " + systemId);
return new InputSource(new StringReader(""));
}
});
}

Posted by: ranvijay | August 17, 2010

m2eclipe over eclipse 3.4.x – No repository found at…

Have been using the latest and greatest eclipse helios for quite some time, but recently had to switch back to eclipse ganymede because the latest flash builder 4 was not compatible with helios. Although I did had ganymede on my system, which was stuffed with too many plugins including the old version of m2eclipse, I thought of starting with a fresh installation of eclipse ganymede with the latest version of m2ecilpse over it. But as soon as I entered the m2eclipse update site ULR: http://m2eclipse.sonatype.org/sites/m2e, it started to give “No repository found at http://m2eclipse.sonatype.org/sites/m2e” error.

A little bit of googling and I found this:

http://blog.gmane.org/gmane.comp.ide.eclipse.plugins.m2eclipse.user/month=20100701

In short, use http://m2eclipse.sonatype.org/sites/archives/0.10.2.20100623-1649/

instead of:

http://m2eclipse.sonatype.org/sites/m2e

if you are using eclipse 3.4.x

Posted by: ranvijay | March 9, 2010

tomcat-context path different from the war name

Sometimes a simple looking thing takes a lot of time. I knew that a web application can be configured in such a way that it is callable using any custom context path, and not necessarily it’s war name, but I was finding it difficult to configure the context path correctly. For example, if I have a war named ‘test.war’, then I can call it using http://localhost:8080/test, but what if I want to call it using http://localhost:8080/mytest? This can be helpful in cases when we have a war named test-1.2.0-alpha.war but I don’t want to call my application using such a long context path. How can I call it simply by invoking http://localhost:8080/test. So here is how to do it:

1. Since modifying the conf/server.xml is not the recommended way anymore, create a context file mytest.xml in ${TOMCAT_HOME}/conf/Catalina/localhost directory. Note the file name of the context file, it has to be same as the desired context path value.

2. Add the context entry in the context file created in step 1 above.

<Context path=”/sometest” docBase=”/opt/apache-tomcat-6.0.24/test”>

Point to note here is that the ‘path’ attribute is not picked up by tomcat. You can give any value to it, it won’t matter. You can even skip it. But I kept it intentionally to prove that the application is not accessible on http://localhost:8080/sometest, but only on http://localhost:8080/mytest

3. Third and final point, and it took me some time to figure it out. Keep you war or the exloded-war directory in any location except ${TOMCAT_HOME}/webapps. If you will keep the war in the ${TOMCAT_HOME}/webapps, the application will only be available on the context path equal to the war name i.e. http://localhost:8080/test. So, in my case, I have put the exploded war directory at /opt/apache-tomcat-6.0.24/test.war, but you can choose any other directory as per your convenience.

From the last one month or so I was getting a “Network connection problems encountered during search” error when I was trying to update or install any plugin from inside Eclipse. I use Ubuntu 64 bit, but I have 32 bit java5 and 32 bit Eclipse Europa installed on it. The primary reason for using 32 bit eclipse and java is the flex-builder which is still in alpha phase of it’s life and works only with 32 bit java and eclipse(only europa) for ubuntu.

Now, whenever I tried to install subclipse, I always got this error message:
"Network connection problems encountered during search.
Unable to access "http://subclipse.tigris.org/update_1.4.x".
Error parsing site stream. [Premature end of file.]
Premature end of file.
Error parsing site stream. [Premature end of file.]
Premature end of file.

Initially I thought it to be some problem with the subclipse update site, but very soon I realised that I was getting the same problem while updating any pre-installed plugin also. I even tried it out with a fresh extracted eclipse(again 32 bit) but with no success. In a quest to pinpoint the source of the problem, I tried it out with 64-bit eclipse too, and to my surprise, it just worked like a charm. But actually, that was not a solution was me as I had to use only 32-bit of eclipse because of the Flex Builder constraint.

A bit of more googling, and I landed on zend forum page. It solved my problem.

The solution is pretty simple, you just have to install these 2 libraries, and you are ready to go.

I did:

sudo apt-get install libc6-i386 lib32nss-mdns

Posted by: ranvijay | August 24, 2009

Eclipse on Ubuntu-Changing the icon and font size

Have you recently switched to Ubuntu from Windows, and have started using Eclipse in it. I am sure, you must not have liked the big fonts and large icons on the top tool-bars in it. Apart from being big, the icons are generally needlessly too much spaced from each other. The end result? The main editor panel looks too small, sometimes awkward. Even decreasing the font size(after going through windows–>preferences–>general–>appearance–>’colour and fonts’ drill) don’t help much. If I leave aside the speed benefit which Ubuntu offer over Windows, I always disliked using Eclipse on Ubuntu (because of aesthetic reasons. :) )

But recently, I found a way to get rid of this weird look and feel of Eclipse on Ubuntu. The trick lies in the .gtkrc-2.0 file which is to be dumped under your home folder. If there is no such file in your home directory, just go and create one. As an example, these are the entries in my .gtkrc-2.0 file:

style “gtkcompact” {
font_name=”Sans 8″
GtkButton::default_border={0,0,0,0}
GtkButton::default_outside_border={0,0,0,0}
GtkButtonBox::child_min_width=0
GtkButtonBox::child_min_heigth=0
GtkButtonBox::child_internal_pad_x=0
GtkButtonBox::child_internal_pad_y=0
GtkMenu::vertical-padding=1
GtkMenuBar::internal_padding=0
GtkMenuItem::horizontal_padding=4
GtkToolbar::internal-padding=0
GtkToolbar::space-size=0
GtkOptionMenu::indicator_size=0
GtkOptionMenu::indicator_spacing=0
GtkPaned::handle_size=4
GtkRange::trough_border=0
GtkRange::stepper_spacing=0
GtkScale::value_spacing=0
GtkScrolledWindow::scrollbar_spacing=0
GtkExpander::expander_size=10
GtkExpander::expander_spacing=0
GtkTreeView::vertical-separator=0
GtkTreeView::horizontal-separator=0
GtkTreeView::expander-size=8
GtkTreeView::fixed-height-mode=TRUE
GtkWidget::focus_padding=0
}
class “GtkWidget” style “gtkcompact”

style “gtkcompactextra” {
xthickness=0
ythickness=0
}
class “GtkButton” style “gtkcompactextra”
class “GtkToolbar” style “gtkcompactextra”
class “GtkPaned” style “gtkcompactextra”

Hope it will help few of us who also care for aesthetics apart from functionality ;)

Posted by: ranvijay | July 25, 2009

ssh and running processes in background.

I am not a expert in unix. I am basically a windows guy and have been using it for the last 6 years. But due to the kind of assignment I am currently involved in, I had to switch to Ubuntu. And I must admit that I am enjoying it. The old college-days impression about unix has started to fade away now, and now I get excited as soon as I get to learn some new cool unix command to ease a task.

Recently I was using a VMware image which runs my weblogic installation. I just don’t like to switch back and forth between my machine and the terminal in the image window, so I use ssh to login into the VMware image machine and then run commands from the same terminal.

I started to feel the pain when I had to keep a terminal window open just because I started the weblogic server which is in the VMware image. I thought “Why the hell I have to keep a terminal open when all I wanted is to start the weblogic server and nothing else. Even if I want to stop the server after some time, can’t I simply do a ssh to the VMware image again and stop the server?”

Although I was aware of a way to run a process in background using the ‘&’ and some basic usage of ‘bg’ and ‘fg’ commands, that didn’t solve my problem completely. Sometimes, the terminal running the ssh would hang and yet some other time, it would kill the process while logging out from the server.

Then I came to know about these 3 ways to accomplish what I wanted:

1. Redirecting the background process stdin/stdout/stderr streams (e.g. to files, or /dev/null if you don’t care about them). For example, in my case I had to start the weblogic server in the background(so that it is not stopped when I log out), what I used was:

[root@localhost ~]# ./startWebLogic.sh < /dev/null >& /dev/null &

now, I could log-out from the session and the process would continue to run( I tested it by accessing the weblogic console). For complete explanation of it, please check the ssh faqs.

2. Use the nohup: It provides the same functionality as the above one and more simpler to use.

nohup

as you can see, the output is appended to nohup.out, so if you login again after some time, you can see the logs for a  process in this file.

3. Using a screen: This was the exciting one. Although the above two methods work, they are not that elegant. Once you have started to process(using above techniques) and log-out, and then you again log-in into the terminal using ssh, the only way to find out about the running process is using a ps -ef thingy. For example, when I wanted to stop the weblogic server, I used to call:

ps -ef | grep weblogic

to find out the process details corresponding to the running weblogic server and then I would call kill -9 pid to kill the server. Not elegant! I am still looking for a better way to get hold of the previously started process and then stop it.

The screen utility, on the other hand, provides a much elegant solution. In a nutshell, you can start(and enter into) a new named screen in the same terminal, do whatever commands you like or start a process it it, then detach the screen. and that’s it. You can log-out from the session and the screen would still be alive on the remote machine. And the best part, when you again choose to go back to the terminal, just log-in and re-get the screen. Here is how I do it:

rkumar@rkumar-laptop:~$ ssh root@wls-103
root@wls-103's password:
Last login: Sun Jul 26 08:16:55 2009 from 172.16.152.1
[root@localhost ~]# screen -dR weblogic_screen

above, I have opened a new screen named ‘weblogic_screen’. This opens a new screen in the same terminal(and not a new tab). In the new screen and I would go to the weblogic domain directory and then start my server there.

[root@localhost ~]# cd /opt/bea-10.3/user_projects/domains/adDomain/
[root@localhost adDomain]# ./startWebLogic.sh

then to detach my screen(without killing it), I will type Ctrl+a,d(Ctrl a then d). This would bring you back to your original screen and you continue to work with your original task or log-out of the session if you wish.

Now, if I would like to stop the running weblogic server, I would simply do a ssh to my VMware image, then check for all running screens using the following command:

[root@localhost ~]# screen -ls
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Detached)
2 Sockets in /var/run/screen/S-root.

It shows that I have the weblogic_screen still running. To enter again into the screen, I would again call ‘screen -dR weblogic_screen’ command and that’s it. I am there. I can stop the server and again detach the screen using Ctrl a,d.

One last thing, if you want to ‘wipe-out’ a screen, you first kill the process then use screen -wipe command. For example, in my case, if I want to delete the screen named ‘weblogic’(because I don’t need it any more) then I will do something like this:

[root@localhost ~]# kill -9 12322
[root@localhost ~]# screen -ls
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Dead ???)
Remove dead screens with 'screen -wipe'.
2 Sockets in /var/run/screen/S-root.

[root@localhost ~]# screen -wipe
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Removed)
1 socket wiped out.
1 Socket in /var/run/screen/S-root.

Categories

Follow

Get every new post delivered to your Inbox.