Wednesday, March 18, 2009

How to measure distance on google maps ?

Use the page here , you just click on points and the distance will appear either in miles or in kilometers. You can also mark multiple points and calculate a real length of a path.

Wednesday, March 11, 2009

Check if a String contains letter

public static boolean containsLetter(String s) {
if ( s == null )
return false;
boolean letterFound = false;
for (int i = 0; !letterFound && i < s.length(); i++)
letterFound = letterFound
|| Character.isLetter(s.charAt(i));
return letterFound;
}

Friday, February 27, 2009

Get MimeType of A File

Use activation.jar to access javax.activation API.

File file = new File("foo.jpg");
String mimeType = new MimetypesFileTypeMap().getContentType(file)); // image/jpeg

alternatively , you can use filename to detech the mime type :
String mimeType = new MimetypesFileTypeMap().getContentType("foo.jpg")); // image/jpeg

Thursday, January 22, 2009

Redirect StdErr to Stdin in Batch and Shell Scripts

On windows command prompt :

mybatch.bat 1> log.txt 2>&1

This redirects stdin to log.txt and stderr to stdin

On Bash Linux Shell:
grep * &> allout.txt


Friday, October 31, 2008

Get Remote IP Address In Axis SOAP Server

Obtain remote IP from ServletRequest :

import javax.servlet.http.HttpServletRequest;

import org.apache.axis.MessageContext;

import org.apache.axis.transport.http.HTTPConstants;

public static String getSOAPRemoteIP() {

MessageContext context = MessageContext.getCurrentContext();

String address = null;

if(context!=null &&

context.containsProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST))

{

HttpServletRequest servletReq = (HttpServletRequest)

context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

if (servletReq != null)

address = servletReq.getRemoteAddr();

}

return address;

}

Tuesday, September 2, 2008

Google Chrome Ready For Download

Google has released a new browser named Google Chrome. This browser is expected to become most widespred computer program ever written. Google Chrome will set the standarts for Web2.0 and Web3.0 technologies.

Chrome is a ligtweight browser and does not consume your CPU and memory and it makes Chrome fast and reliable.

Download Here

Here is a gread comic book for chrome on googlebooks site

At first impression Google Chrome is not working properly with PHP-Admin !!!

Friday, August 22, 2008

Console Beep On Error Line of a Log File

Ok folks , this is non-java :)

Today I had to watch a log file and alert some ppl when an error phrase seen on log file. This is a terrible jıb todo. First I run on my linux server:

#tail -f mylogfile | grep ERRORSTRING

this worked but put me in front of the screen. Why not my console beep when ERRORSTRING exists in the log file ? I need an application to beep when read a line from stdin. This is piece of cake in java but i did not want to load JVM for this purpose because of memory needs. So I warped to good old days and decided to write a C program to do this job.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
char buf[1024];
int n;
while (fgets(buf, sizeof buf, stdin) != NULL) {
n = strlen(buf);
if (n > 0 && buf[n-1] == '\n') {
printf("\a");
printf("%s\n", buf);
}
}
}


and I compiled :
#cc beep.c -o mybeep

and later :

#tail -f mylogfile | grep ERRORSTRING | /home/hakan/utils/beep

and that's it , it worked ! I watch olympic games and when console beeps I called administration ppl :) There should be a lot of log watching utility but it is easier for me to write code than search google for utility programs and install them. Maybe this code can crash the server but it works for now , thanks in advance for comments.