Coding Challenges

http://codingbat.com/
https://projecteuler.net/
http://coderbyte.com/

List of Sites
http://codecondo.com/coding-challenges/

https://www.codeeval.com/
http://www.topcoder.com/
http://www.programmr.com/
http://www.codechef.com/

-0-

My suggestion:
Without learning fundamentals, doing all monkey business is useless.

-o-

Advertisements

Java JMX Client


import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMXClient {
public static void main(String[] args) {
try {
HashMap env = new HashMap();

String[] credentials = new String[] { "userid", "password" };
env.put("jmx.remote.credentials", credentials);

JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://server:1234/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("Domain[" + i + "] = " + domains[i]);
}

listMBeans(mbsc, "ABC:type=TYPENAME,name=name2");

jmxc.close();
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* List all MBeans and their attributes.
*/
public static void listMBeans(MBeanServerConnection server, String path)
throws Exception {
javax.management.ObjectName on = new javax.management.ObjectName(path);
final Set names = server.queryNames(on, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
// x.printStackTrace(System.err);
}
}
}

public static void listMBeanAttributes_all(MBeanServerConnection server)
throws IOException {
final Set names = server.queryNames(null, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
// x.printStackTrace(System.err);
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
x.printStackTrace(System.err);
}
}
}
}

Java – Graphs and Charts

How to draw graphs and charts

1. If you want to generate bmp, jpg formats better to use

http://www.jfree.org/jfreechart/ Better with Swing or to email images.
http://cewolf.sourceforge.net/new/index.html Better for JSP/Servlet integration.

2. For more detailed research, Excel is best. Create template with graphs and fill the data using Apache POI API.
http://poi.apache.org/

3. For better quick online graphs try following
http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html

http://code.google.com/apis/ajax/playground/?type=visualization#line_chart

http://webdesignledger.com/resources/13-useful-javascript-solutions-for-charts-and-graphs

-o-

Jetty JSP with Java 1.6

Problem statement: JSPs are not working in embedded Jetty.

http://docs.codehaus.org/display/JETTY/Jsp+Configuration

Add following to web.xml file

<servlet id="jsp">
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>logVerbosityLevel</param-name>
        <param-value>DEBUG</param-value>
    </init-param>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>keepgenerated</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspf</url-pattern>
    <url-pattern>*.jspx</url-pattern>
    <url-pattern>*.xsp</url-pattern>
    <url-pattern>*.JSP</url-pattern>
    <url-pattern>*.JSPF</url-pattern>
    <url-pattern>*.JSPX</url-pattern>
    <url-pattern>*.XSP</url-pattern>
  </servlet-mapping>

Add following to support JSP with Java 1.6

	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty</artifactId>
		<version>6.1.26</version>
	</dependency>       
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty-util</artifactId>
		<version>6.1.26</version>
	</dependency>
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>servlet-api-2.5</artifactId>
		<version>6.1.14</version>
	</dependency>
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jsp-2.1</artifactId>
		<version>6.1.14</version>
	</dependency>
	<dependency>
		 <groupId>org.mortbay.jetty</groupId>
		 <artifactId>jsp-api-2.1</artifactId>
		 <version>6.1.14</version>
	</dependency>
	<dependency>
		<groupId>org.apache.ant</groupId>
		<artifactId>ant</artifactId>
		<version>1.8.1</version>
	</dependency>

Windows Port Issue – Bind: Address Already

Many times we used to see “Bind: Address Already” when running servers like apache, tomcat, jboss, weblogic, …etc in windows.

Step 1: Generally the given port is took over by other process. Find it with following commands.

>netstat -a -n -o (This gives Process ID)
>netstat -an
>netstat -an |find /i “listening”

Step 2: Use task manager and find that process who took over the port.

Step 3: Kill the process or change the configurations of respective application.

Issue resolved.
-o-