Tagged: sun Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 4:22 pm on February 11, 2009 Permalink |
    Tags: , , , , migration, , sun, ,   

    Another JBoss 5 hack by Venu 

    jbosscorp_logo

    Venu had another JBoss hack and he was able to fix one more issue that may bubble up during the migration from JBoss 4.x to 5. This time found out how solve “Adding multiple last resources is disallowed. Current resource is ….” exception which happens while trying to access an EJB from a different EAR with in a transaction and trying to commit that transaction.

    Read the article here

    Advertisements
     
  • Subinkrishna Gopi 12:14 pm on February 11, 2009 Permalink |
    Tags: , , , , , servlet filter, sun, , ,   

    How-to: Write a simple servlet filter 

    What is a filter?
    From the JSR 154 specification,

    A filter is a reusable piece of code that can transform the content of HTTP requests, responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.

    How to write a filter?
    We can create a filter by writing a new class which implements javax.servlet.Filter. We need to specify the filters in the deployment descriptor so that the container can create a filter chain to process the request/response.

    The filters in deployment descriptor:

    <web-app>
    	<filter>
    		<filter-name>MyFilter</filter-name>
    		<filter-class>subin.rnd.enterprise.filter.MyFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>MyFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<filter-mapping>
    		<filter-name>MyFilter</filter-name>
    		<servlet-name>MyServlet</servlet-name>
    		<dispatcher>REQUEST</dispatcher>
    	</filter-mapping>
    </web-app>

    We can have a filter (or a chain of filters) processing the request/response. In the above descriptor, we are linking a filter for a URL pattern as well as for a specific servlet. So in the above case, for MyServlet, the filter will get executed twice as we have mapped the filter MyFilter to all URLs and for MyServlet. So the order of execution will be MyFilter.doFilter() > MyFilter.doFilter() > MyServlet.doGet().

    MyServlet.java

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyServlet
    extends HttpServlet
    {
      @Override
      public void doGet(HttpServletRequest request,
        HttpServletResponse response)
      throws ServletException
      {
        System.out.println("doGet()");
        response.setContentType("text/plain");
        ServletUtility.write(response, "Hello world!".getBytes());
      }
    }
    

    MyFilter.java

    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyFilter
    implements Filter
    {
      public void destroy()
      {
      }
    
      public void doFilter(ServletRequest request, ServletResponse response,
         FilterChain filterChain)
      throws IOException, ServletException
      {
         System.out.println("doFilter()");
    
         ByteStreamResponseWrapper responseWrapper = null;
         byte[] responseAsBytes = null;
         String processedResponse = null;
    
         // Creating a response wrapper
         responseWrapper = new ByteStreamResponseWrapper(
            (HttpServletResponse) response);
         filterChain.doFilter(request, responseWrapper);
    
         // Process the response
         processedResponse = responseWrapper.toString();
         if (null != processedResponse)
         {
           processedResponse = processedResponse.toUpperCase();
           responseAsBytes = processedResponse.getBytes();
         }
    
         // Writing the response (as bytes) to the servlet output stream
         ServletUtility.write(response, responseAsBytes);
      }
    
      public void init(FilterConfig filterConfig)
      throws ServletException
      {
        // We can initialize a filter using the init-params here
        // (which we defined in the deployment descriptor - web.xml)
      }
    }
    

    Other files
    There are two more Java files – ByteStreamResponseWrapper & ServletUtility. ByteStreamResponseWrapper is an extension of javax.servlet.http.HttpServletResponseWrapper with some methods – toString(), getOutputStream() & getWriter() – overriden. ServletUtility is a utility class to write a byte stream to the servlet output stream.

    Output
    The “Hello world!” from MyServlet is filtered to “HELLO WORLD!”  by MyFilter.

    Fig: Output in browser

    Output
    Fig: Jboss console output

    Jboss Console output

    Note:
    I wanted to put above XML in syntax-highlighted mode; but the WordPress plug-in failed miserably. Tough luck.

     
  • Subinkrishna Gopi 4:16 pm on January 29, 2009 Permalink |
    Tags: , jar, , , sun,   

    Running an executable JAR from command-line 

    An “executable JAR” is nothing but a JAR in which the entry point of execution is defined. That definition can be given in its manifest file (META-INF/Manifest.mf).

    The general structure of a JAR file:

    +-MyJar.jar
    |-+-META-INF
    |-|----Manifest.mf
    |-+-com
    |-|-+-package
    |-|-|----MyJavaFile.java
    |-|-|----MySecondJavaFile.java

    The manifest file should contain the details of the JAR file. It keeps the attribute as key-value pairs.

    The structure of a manifest file:

    Main-Class: com.package.MyJavaFile
    Specification-Title: "My Classes"
    Specification-Version:
    "1.0"
    Specification-Vendor: "Sun Microsystems, Inc."
    Implementation-Title:
    "subin.util"
    Implementation-Version: "build01"
    Implementation-Vendor: "Sun Microsystems, Inc."

    If the executable JAR is dependent on any other JAR, we need to specify it in the Manifest itself (even though there is an option to do it while invoking the application thru java -cp).

    Class-Path: aJar.jar anotherJar.jar

    I had some problems in building a JAR file using the conventional jar -cvf command. Whatever I do, it was not picking my custom manifest (I’m very bad). So, I used ANT to build a JAR.

    ANT file to build a JAR:

    <project name="Demo" default="build.jar.file">
      <target name="build.jar.file">
        <jar destfile="myJar.jar" basedir=".">
          <manifest>
            <attribute name="Main-Class" value="com.package.MyJavaFile" />
            <attribute name="Class-Path" value="aJar.jar anotherJar.jar" />
          </manifest>
        </jar>
        <copyfile dest="/path/to/deploy" src="myJar.jar"/>
      </target>
    </project>
    
    • use ANT or Eclipse IDE to build the JAR

    Running the JAR from the command-line:

    $ java -jar myJar.jar [optional parameters]

    External Links:
    http://en.wikipedia.org/wiki/Manifest_file
    http://en.wikipedia.org/wiki/JAR_file
    http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
    http://java.sun.com/docs/books/tutorial/deployment/jar/ – Packaging tutorial from Sun
    http://ant.apache.org/manual/
    http://en.wikipedia.org/wiki/Apache_Ant

     
    • Subinkrishna G 5:27 pm on January 29, 2009 Permalink

      In the ANT build file I’ve mentioned only two attributes – Class-Path & Main-Class. These two are the most important attributes in this scenario. So using the “attribute” tag in “manifest” we can define all the other attributes too.

    • Mathew Varghese 2:22 pm on February 13, 2009 Permalink

      Add-on: If we don’t want to keep dependent JARs external, we can bundle within our target JAR. Use zipfileset ANT task.

      <zipfileset src="firstJar.jar" includes="**/*.java **/*.class"/>

      Read more: http://ant.apache.org/manual/CoreTypes/zipfileset.html

  • Subinkrishna Gopi 12:51 pm on October 9, 2007 Permalink |
    Tags: , , sun,   

    Long Running Web Process (LRWP) in the Java Platform using GlassFish 

    Today while surfing the net I found this article in Sun’s website. I thought this will be useful to some of us. According to the web site, LRWP is,

    “The Long Running Web Process (LRWP) is a protocol used by a web server to communicate with its peers. LRWP is similar to CGI, but faster, since the peer is persistent across requests. In LRWP, a TCP connection is established between the LRWP peer and a LRWP agent. The LRWP agent could be the web container or a process running within the web container and the LRWP peer could be any process running on a network. The LRWP peer at connection registers the web context that the peer is interested in. The web context could be any context, such as “/osp”, “/tep”, or “/cgi-bin” itself. When a request for that context is made, the agent transfers the input to the LRWP peer and sends the output from the peer back to the web client. The LRWP agent should also be able to support multiple peers concurrently, and each peer makes a connection and registers the context it is interested in. “

    You can read this article here.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel