Tuesday, October 25, 2011

Default $PATH value for Mac OS X

When I messed up the PATH variable in my Mac, I found a life saver here.

I'm reposting here the default value of $PATH env variable, in case you screwed up your $PATH.

Just execute, export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

But ideally, remember to do this before you change your PATH variable.

1. In CL, echo $PATH
2. copy the result in file that is already open
3. change your $PATH.
4. If something is wrong, in CL execute "export PATH=savedvalue"

Tuesday, September 13, 2011

Subclipse in Eclipse [MAC]

To install subclipse in Eclipse ide:

Help->Install new software->Add->http://subclipse.tigris.org/update_1.6.x

If you are behind firewall you would get error like this

Unable to connect to repository http://subclipse.tigris.org/update_1.6.x/content.xml Unable to connect to repository http://subclipse.tigris.org/update_1.6.x/content.xml Connection refused

Then do the following,

  1. In Eclipse (go to Preferences > General > Network connections).
  2. Select "Active Provider" as "Manual".
  3. Select HTTP and click edit.
  4. Enter the host and port [proxy host and port get it from office admin]
  5. Select "Reuries Authentication" and enter the username and password.
  6. Repeat Step 1 to 5 for Https.


    Src: http://stackoverflow.com/questions/4598167/eclipse-updates-not-working

Thursday, August 11, 2011

Try Catch Finally

In case of sudden doubts, here are few pointers from specifications:

A try statement with a finally block is executed by first executing the try block. Then there is a choice:


  • If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement completes normally.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.
  • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
    • If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice:
      • If the catch block completes normally, then the finally block is executed. Then there is a choice:
        • If the finally block completes normally, then the try statement completes normally.
        • If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason.
      • If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:
        • If the finally block completes normally, then the try statement completes abruptly for reason R.
        • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
    • If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
      • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
      • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).
  • If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
    • If the finally block completes normally, then the try statement completes abruptly for reason R.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).



If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:

  • If the run-time type of V is assignable to the Parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. If that block completes normally, then the try statement completes normally; if that block completes abruptly for any reason, then the try statement completes abruptly for the same reason.
Src: http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#24134

Thursday, July 28, 2011

SVN : tips and tricks

1. Checkout only a single file from repository without having to download the entire directory.

Sometimes we want to work on only a single file out of 1000's of files in a repository folder. You don't have to download them all. You can do this

svn co --depth empty
cd
svn up
Thanks to source here

2. When you access subversion from Eclipse you will be prompted for username and password. You can select an option to cache the password so you don't have to enter them every time you connect to repository. Suppose if you want to reset the cache and to be prompted for password again, you need to manually delete the file containing the cached password. In Mac OSX, its

~/.subversion/auth/.svn.simple

Now restart the IDE, you will be prompted for password again.


3. If you want to ignore certain files from ever "accidentally" checked in to SVN, do the following. I'm talking about those .settings, .classpath, .project, target, log server.out, depedency-reduced-pom.xml, etc.

  1. Go to ~/.subversion/config file
  2. Find "global-ignores" line. It would be commented by default.
  3. Add this instead "global-ignores = *.classpath *.project *.settings target

The location of config file based on the system:

In Mac/Linux:

~/.subversion/config or /etc/subversion/config

In Window:

%appdata%\subversion\config

Thursday, April 21, 2011

Mac: TcpMon

What: TcpMon is a monitor tool that comes with Apache Axis package and can be downloaded.

Why: If you have a WS client or service that you want to monitor, use TcpMon. Lets say you have written a client for axis2 service as stated here. You also may need to look at the SOAP messages that are sent in the wire.

How:

1. Download TcpMon binary distribution here
2. Execute tcpmon-1.0-bin/build/tcpmon.sh . This will launch the tcpMon console.
3. In 'Admin' tab, enter 'Listen Port#:' Any_port_Number. Set 'Act as: Proxy'. And click Add. New tab as 'Port Any_port_Number' will come up where you can see all traffic.
4. Now you need to set proxy for the web based traffic. In Mac, to System Preferences->Network->Advanced->Proxies->Web proxy. Set 127.0.0.1 and Any_port_Number. Note: you may need to revert back to original settings when you are done with debugging.
5. Run the client in the eclipse. Go to the new tab in tcpmon and you can see the incoming and outgoing SOAP messages for debugging.


Thanks to the source!

Thursday, April 14, 2011

Axis2 Maven Web service client Example

Aim: To learn how to generate java client stubs from WSDL using wsdl2java script of axis2. Use the client stubs to consume a web service using SOAP over HTTP.

Requirements:
1. you need WSDL of the service you wish to consume using your generated client.
2. If you are going to test both service generation and consumption in local system, download axis2 binary distribution.
3. SoapUI if you need to test the remote Server and its operations with sample requests.


Steps:

I followed this source to set up the axis2's http server and also download SimpleService.aar file to deploy. Here is the simple steps to follow:

1. Download and extract axis2 binary distribution to /usr/local, at the time of writing[Apr 14,2011] we have axis2-1.5.4. Set /usr/local/axis2-1.5.4 as AXIS_HOME. You can find the scripts in AXIS_HOME/bin folder. You need to copy .aar file of your service into repository/services folder.

2. Start the axis2 server by executing AXIS_HOME/bin/axis2server.sh. In a browser if you access, http://localhost:8080 it would list all services deployed on the axis2server. You can get the WSDL of the service from its links.

SimpleService.wsdl:
==============

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://ws.apache.org/axis2" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://ws.apache.org/axis2">
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.apache.org/axis2">
<xs:element name="echo">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="echoResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="echoRequest">
<wsdl:part name="parameters" element="ns:echo"/>
</wsdl:message>
<wsdl:message name="echoResponse">
<wsdl:part name="parameters" element="ns:echoResponse"/>
</wsdl:message>
<wsdl:portType name="SimpleServicePortType">
<wsdl:operation name="echo">
<wsdl:input message="ns:echoRequest" wsaw:Action="urn:echo"/>
<wsdl:output message="ns:echoResponse" wsaw:Action="urn:echoResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SimpleServiceSoap11Binding" type="ns:SimpleServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echo">
<soap:operation soapAction="urn:echo" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="SimpleServiceSoap12Binding" type="ns:SimpleServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echo">
<soap12:operation soapAction="urn:echo" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="SimpleServiceHttpBinding" type="ns:SimpleServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="echo">
<http:operation location="SimpleService/echo"/>
<wsdl:input>
<mime:content type="text/xml" part="echo"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="echo"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SimpleService">
<wsdl:port name="SimpleServiceHttpSoap11Endpoint" binding="ns:SimpleServiceSoap11Binding">
<soap:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="SimpleServiceHttpSoap12Endpoint" binding="ns:SimpleServiceSoap12Binding">
<soap12:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="SimpleServiceHttpEndpoint" binding="ns:SimpleServiceHttpBinding">
<http:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


Things you need to know about any WSDL is
a. What are the possible 'operation' from the service. Their input and output element type and their arguments.
b. What is the address Location of the service.

3. I use Maven2Eclipse plugin to generate and execute my project. Create a maven project. Update its pom.xml with the following:

<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>wsdl2code</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>axis.test</packageName>
<wsdlFile>src/main/wsdl/SimpleService.wsdl</wsdlFile>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.11</version>
</dependency>
...
</dependencies>
...
</project>


These are the dependencies you will need to execute your Client stub generated by wsdl2java. The plugin is used to generate the stubs. When you run Maven Install on the project it should generate SimpleServiceCallbackHandler.java and SimpleServiceStub.java in target/generated-sources/../axis/test folder. Move it to your project's src folder under needed package name.

4. Here is a sample Client.java that you need to write to consume the SimpleService using the stubs we just generated.

Client.java:
========
package axis.test;

import java.rmi.RemoteException;

public class Client {

/**
* @param args
* @throws RemoteException
*/
public static void main(String[] args) {

try {
SimpleServiceStub stub = new SimpleServiceStub();

SimpleServiceStub.Echo request = new SimpleServiceStub.Echo();

request.setArgs0("This is my message");

SimpleServiceStub.EchoResponse response = stub.echo(request);

System.out.println("Response from Simple service: "+response.get_return());

}
catch(RemoteException ex) {
System.out.println(ex);
ex.printStackTrace();
}
}
}

This is a very simple client code to start with. You need to create an instance of stub class which would be ServiceName+Stub.java. You need to create a request object. It will be a inner class of stub. You can check wsdl for operation name(that would request name). Set the arguments for the operation request and verify you are complying the format specified in WSDL. In this case its a string to echo back. Similarly create instance of response to hold the response of the operation. You actually consume the service when you call the operation via stub object. get_return() method always returns the value of response object.


5. Run Client as Java application and make sure service is up by checking via browser. You will get response in console. Viola.


Possible errors/exceptions:

1. When the service is down, the SOAP Fault would say Connection refused.
2. There are 3 data binding frameworks possible. Default is adb(Axis data binding framework). You can specify it in in maven plugin for wsdl2code.

References:
[1]. Hello world with Apache Axis2

Saturday, February 12, 2011

Ubuntu: Install Tomcat

  1. Download the latest version of apache-tomcat from here.
  2. "cd /downloaded/directory" --> In terminal, go to the downloaded directory.
  3. "sudo cp apache* /usr/local" --> Copy the downloaded file to /usr/local (can be changed, I prefer local dir).
  4. "sudo chmod a+rwx apache*" --> change the access permissions.
  5. "sudo tar zxvf apache*" --> unzip the tar.gz file.
  6. "export JAVA_HOME=/usr/local/java/jdk1.6.0_17" --> set up JAVA_HOME env variable
  7. "export PATH=$JAVA_HOME/bin:$PATH" --> add java bin to path
  8. "export CATALINA_HOME=/usr/local/apache-tomcat-6.0.32" --> optional to set up these env variables.
  9. "cd $CATALINA_HOME/bin" --> go to bin folder to access scripts.
  10. "sudo sh startup.sh" --> to manually start Tomcat
  11. "localhost:8080" --> Go to browser and access the page to see configuration page of Tomcat.
  12. If step 11 returns error, "cd $CATALINA_HOME/logs" --> go to logs folder and view "catalina..log" file to see what went wrong. If everything went well, you should see "INFO: Starting Coyote HTTP/1.1 on http-8080". If you see "java.net.BindException: Address already in use", shutdown and restart Tomcat.
  13. "sudo sh $CATALINA_HOME/bin/shutdown.sh" --> to shutdown Tomcat.

Src: http://www.puschitz.com/InstallingTomcat.html#InstallingTomcatSoftware


EXTRA:

Mac: Installing Tomcat

src: http://developer.apple.com/internet/java/tomcat1.html

1. Download tomcat tar.gz from archives.
2. do sudo and extract it to /usr/local
3. give ownership to your username by chown -R username foldername
4. set up startup and stop scripts.
5. Access via localhost:8080 to check installation.