Thank you for visiting! If you liked the site, please add a bookmark, else add a critical comment! Would you like to visit Greece? Traditional Greek flag This real estate site is only available in Greek! The holy mountain

Sunday, 16 September 2012

Processing BLOBs via the ImageServlet

In this post an alternative code version to the traditional ImageServlet  will be given, supplementing the one presented in the book named Oracle Fusion Developer Guide (p.409), by F.Nimphius and L.Munsinger and other official Oracle aces' and employees' blogs. This alternative version will use the  EJB 3 way to get rid of the surplus database definition hard coded in the doGet() method. As an epilogue, further criticism will be provided, against using that technique of obtaining jdbc connections to the database.

Here is the modified code, with the original straight jdbc code commented out:

package actionbazaar.view.servlet;


import actionbazaar.buslogic.PlaceItem;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.jbo.domain.BlobDomain;

import org.apache.log4j.Logger;


public class ImageServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "image/jpg; charset=utf-8";
    @SuppressWarnings("compatibility:988932384557339885")
    private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger(this.getClass().getName());
   
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
        response.setContentType(CONTENT_TYPE);
        Long itemId = new Long(request.getParameter("itemId"));
        if (logger.isDebugEnabled())
            logger.debug("itemId = " + itemId);
        try {
                Context context = new InitialContext();
                PlaceItem placeItem = (PlaceItem)context.lookup("ejb3inaction-Model-PlaceItem#actionbazaar.buslogic.PlaceItem");
                byte[] image  = placeItem.getImageByItemId(itemId);

            if (image != null) {
                BlobDomain bld = new BlobDomain(image);

                OutputStream os = response.getOutputStream();
                BufferedInputStream in =  new BufferedInputStream(bld.getBinaryStream());
                int b;
                byte[] buffer = new byte[10240];

                while ((b = in.read(buffer, 0, 10240)) != -1) {
                    os.write(buffer, 0, b);
                }
                os.close();
            } else {
                    if (logger.isDebugEnabled())
                        logger.debug("No  image exists yet for itemId: " + itemId);
                }
        } catch (NamingException e){
            e.printStackTrace();   
        }
        catch (IOException e){
            e.printStackTrace();   
        }
       /*
        Connection conn = null;


        try {
            Context ctx = new InitialContext();

//Datasource as defined in <res-ref-name> element of weblogic.xml
            DataSource ds =
                (DataSource)ctx.lookup("jdbc/ActionBazaarEJB3inActionDS");

            conn = ds.getConnection();
            PreparedStatement statement =
                conn.prepareStatement("SELECT IMAGE " +
                                      " FROM ITEMS" +
                                    " WHERE ITEM_ID= ? ");
            statement.setInt(1, new Integer(itemId));
            ResultSet rs = statement.executeQuery();

            if (rs.next()) {
                Blob blob = rs.getBlob("IMAGE");

                BufferedInputStream in =
                    new BufferedInputStream(blob.getBinaryStream());
                int b;
                byte[] buffer = new byte[10240];

                while ((b = in.read(buffer, 0, 10240)) != -1) {
                    os.write(buffer, 0, b);
                }
                os.close();

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {

                    conn.close();
                }

            } catch (SQLException sqle) {
                sqle.printStackTrace();

            }

        }*/
      
    }

   }


Besides hard coding the database connection string and the SQL query too, using doGet() instead of init() to setup a database connection and destroy() to close it, is not widely considered a good idea. Thus, a newer version of the servlet code  will be most welcome, due to performance reasons as well. Presenting that code would go beyond the scope of such a post, so I am turning once again to the Oracle experts mentioned above for another alternative solution!

References

http://tompeez.wordpress.com/2011/12/16/jdev11-1-2-1-0-handling-imagesfiles-in-adf-part-3/
http://www.baigzeeshan.com/2010/09/store-images-in-blob-in-oracle-adf.html
http://ranajitsahoo.blogspot.gr/2008/04/how-to-display-content-of-blob-column.html
http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html

Thursday, 19 April 2012

Farewell to ...ANT?

Following the articles by J.Stegeman and E.Biemond about building via ant scripts,  external to JDeveloper, one can quickly find out that the former does show how to create a war file, out of mostly .xml files, but not an ear; and the latter  misses on creating a war file out of the ViewController project! This article comes as supplement of both, showing how to jar the ejb3 model project,  to war the  ViewController, and lastly the external log4j library jar, assemble them in an ADF application ear and finally deploy to weblogic 10.3.4 application server.

First,  comes the model ant script:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Ant buildfile runs only outside of JDeveloper-->
<!--Generated Apr 7, 2012 8:59:13 AM-->
<project name="Model" default="all" basedir=".">
  <property file="../Model/build.properties"/>
  <import file="../JDeveloperLibs/jdev-libs.xml"/>
  <path id="model.classpath">
    <pathelement location="classes"/>
    <pathelement location="../lib/log4j-1.2.15.jar"/>

    <path refid="JDeveloperLibs.library.EJB.3.0"/>
    <path refid="JDeveloperLibs.library.TopLink"/>
    <path refid="JDeveloperLibs.library.Java.EE.1.5.API"/>
    <path refid="JDeveloperLibs.library.WebLogic.10.3.Remote-Client"/>
    <path refid="JDeveloperLibs.library.ADF.Model.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Model.Generic.Runtime"/>
    <path refid="JDeveloperLibs.library.Resource.Bundle.Support"/>
    <path refid="JDeveloperLibs.library.ADF.Common.Runtime"/>
    <!--path refid="JDeveloperLibs.library.Log4j-1.2.15.jar"/-->
    <path refid="JDeveloperLibs.library.Oracle.JDBC"/>
    <path refid="JDeveloperLibs.library.MDS.Runtime"/>
    <path refid="JDeveloperLibs.library.Oracle.XML.Parser.v2"/>
  </path>
  <target name="model.init">
    <tstamp/>
    <mkdir dir="${model.output.dir}"/>
    <mkdir dir="${model.deploy.dir}"/>
  </target>
  <target name="model.all" description="Build the project"
          depends="model.compile,model.copy"/>
  <target name="model.clean" description="Clean the project">
    <delete includeemptydirs="true" quiet="true">
      <fileset dir="${model.output.dir}" includes="**/*"/>
      <fileset dir="${model.deploy.dir}" includes="**/*"/>
    </delete>
  </target>
  <target name="model.compile" description="Compile Java source files"
          depends="model.init">
    <javac destdir="${model.output.dir}" classpathref="model.classpath"
           debug="${javac.debug}" nowarn="${javac.nowarn}"
           deprecation="${javac.deprecation}" encoding="UTF-8" source="1.6"
           target="1.6">
      <src path="../lib"/>
    </javac>
    <javac destdir="${model.output.dir}" classpathref="model.classpath"
           debug="${javac.debug}" nowarn="${javac.nowarn}"
           deprecation="${javac.deprecation}" encoding="UTF-8" source="1.6"
           target="1.6">
      <src path="${model.src.dir}"/>
    </javac>
  </target>
  <target name="model.copy" description="Copy files to output directory"
          depends="model.init">
    <patternset id="copy.patterns">
      <include name="**/*.gif"/>
      <include name="**/*.jpg"/>
      <include name="**/*.jpeg"/>
      <include name="**/*.png"/>
      <include name="**/*.properties"/>
      <include name="**/*.xml"/>
      <include name="**/*.ejx"/>
      <include name="**/*.xcfg"/>
      <include name="**/*.cpx"/>
      <include name="**/*.dcx"/>
      <include name="**/*.sva"/>
      <include name="**/*.wsdl"/>
      <include name="**/*.ini"/>
      <include name="**/*.tld"/>
      <include name="**/*.tag"/>
      <include name="**/*.xlf"/>
      <include name="**/*.xsl"/>
      <include name="**/*.xsd"/>
      <exclude name="**/persistence.xml"/>
        <exclude name="**/weblogic-ejb-jar.xml"/>
    </patternset>
    <copy todir="${model.output.dir}">
      <fileset dir="${model.src.dir}">
        <patternset refid="copy.patterns"/>
      </fileset>
      <fileset dir="../lib">
        <patternset refid="copy.patterns"/>
      </fileset>
    </copy>
    <copy todir="${model.output.dir}">
      <fileset dir="../Model/adfmsrc">
        <patternset refid="copy.patterns"/>
         <include name="../Model/adfmsrc/actionbazaar/DataControls.dcx"/>
      </fileset>
    </copy>
  </target>
  <target name="model.jar" depends="model.compile,model.copy">
    <jar destfile="${model.deploy.dir}/${model.jar.file}" >
      <fileset dir="${model.output.dir}">
        <include name="**/*.class"/>
        <include name="**/*.xml"/>
        <include name="**/*.dcx"/>
         <exclude name="**/*.java"/>
        <exclude name="**/**/actionbazaar/buslogic/client/**"/>
      </fileset>
      <fileset dir="${model.src.dir}/">
        <include name="META-INF/persistence.xml"/>
        <include name="META-INF/**"/>
        <include name="**/*.xml"/>
        <include name="**/*.dcx"/>
        <include name="**/*.properties"/>
        <exclude name="**/*.java"/>
        <exclude name="**/build.xml"/>
        <exclude name="**/build.properties"/>
      </fileset>
    </jar>
  </target>
</project>

Second, the userinterface or ViewController one:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Generated Apr 7, 2012 9:24:35 AM-->
<project name="UserInterface" default="UserInterface.ear" basedir=".">
  <taskdef name="wldeploy"
           classname="weblogic.ant.taskdefs.management.WLDeploy"/>
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${ant.lib.folder}/lib/ant-contrib.jar"/>
    </classpath>
  </taskdef>
  <property file="../UserInterface/build.properties"/>
  <import file="../JDeveloperLibs/jdev-libs.xml"/>
  <import file="../Model/build.xml"/>
  <import file="Datasource.xml"/>
  <path id="UserInterface.classpath">
    <pathelement location="../Model/classes"/>
    <pathelement location="../lib/log4j-1.2.15.jar"/>
    <!--path refid="JDeveloperLibs.library.JSTL.1.2"/-->
    <path refid="JDeveloperLibs.library.JSTL.1.2.Tags"/>
    <path refid="JDeveloperLibs.library.JSF.1.2"/>
    <path refid="JDeveloperLibs.library.ADF.Faces.Runtime.11"/>
    <path refid="JDeveloperLibs.library.ADF.Common.Runtime"/>
    <path refid="JDeveloperLibs.library.JSP.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.DVT.Core.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.DVT.Faces.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.DVT.Faces.Databinding.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Faces.Databinding.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Page.Flow.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Model.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Controller.Runtime"/>
    <path refid="JDeveloperLibs.library.ADF.Web.Runtime"/>
    <path refid="JDeveloperLibs.library.MDS.Runtime"/>
    <path refid="JDeveloperLibs.library.EJB.Data.Control"/>
    <path refid="JDeveloperLibs.library.EJB.3.0"/>
    <path refid="JDeveloperLibs.library.TopLink"/>
    <path refid="JDeveloperLibs.library.Java.EE.1.5.API"/>
    <path refid="JDeveloperLibs.library.WebLogic.10.3.Remote-Client"/>
    <path refid="JDeveloperLibs.library.Resource.Bundle.Support"/>
    <path refid="JDeveloperLibs.library.ADF.Controller.Schema"/>
    <path refid="JDeveloperLibs.library.MDS.Runtime.Dependencies"/>
    <path refid="JDeveloperLibs.library.Commons.Beanutils.1.6"/>
    <path refid="JDeveloperLibs.library.Commons.Logging.1.0.4"/>
    <path refid="JDeveloperLibs.library.Commons.Collections.3.1"/>
    <path refid="JDeveloperLibs.library.ADF.DVT.Faces.Databinding.MDS.Runtime"/>
    <path refid="JDeveloperLibs.library.Oracle.JDBC"/>
    <path refid="JDeveloperLibs.library.Oracle.JEWT"/>
    <path refid="JDeveloperLibs.library.BC4J.Security"/>
    <path refid="JDeveloperLibs.library.ADF.Common.Web.Runtime"/>
    <path refid="JDeveloperLibs.library.Servlet.Runtime"/>
    <path refid="JDeveloperLibs.library.Resource.Bundle.Variable.Resolver"/>
    <path refid="JDeveloperLibs.library.Trinidad.Runtime.11"/>
    <path refid="JDeveloperLibs.library.Trinidad.Databinding.Runtime"/>
  </path>
  <target name="UserInterface.init">
    <tstamp/>
    <mkdir dir="${UserInterface.output.dir}"/>
    <mkdir dir="${UserInterface.deploy.dir}"/>
  </target>
  <target name="UserInterface.all" description="Build the project"
          depends="UserInterface.compile,UserInterface.copy"/>
  <target name="UserInterface.clean" description="Clean the project">
    <delete includeemptydirs="true" quiet="true">
      <fileset dir="${UserInterface.output.dir}" includes="**/*"/>
      <fileset dir="${UserInterface.deploy.dir}" includes="**/*"/>
      <fileset dir="${model.output.dir}" includes="**/*"/>
      <fileset dir="${model.deploy.dir}" includes="**/*"/>
    </delete>
  </target>
  <target name="UserInterface.compile" description="Compile Java source files"
          depends="UserInterface.init,model.compile">
    <javac destdir="${UserInterface.output.dir}"
           classpathref="UserInterface.classpath" debug="${javac.debug}"
           nowarn="${javac.nowarn}" deprecation="${javac.deprecation}"
           encoding="UTF-8" source="1.6" target="1.6">
      <src path="../lib"/>
    </javac>
    <javac destdir="${UserInterface.output.dir}"
           classpathref="UserInterface.classpath" debug="${javac.debug}"
           nowarn="${javac.nowarn}" deprecation="${javac.deprecation}"
           encoding="UTF-8" source="1.6" target="1.6">
      <src path="${UserInterface.src.dir}"/>
    </javac>
  </target>
  <target name="UserInterface.copy" description="Copy files to output directory"
          depends="UserInterface.init">
    <patternset id="copy.patterns">
      <include name="**/*.jspx"/>
      <include name="**/*.gif"/>
      <include name="**/*.jpg"/>
      <include name="**/*.jpeg"/>
      <include name="**/*.png"/>
      <include name="**/*.properties"/>
      <include name="**/*.xml"/>
      <include name="**/*.ejx"/>
      <include name="**/*.xcfg"/>
      <include name="**/*.cpx"/>
      <include name="**/*.dcx"/>
      <include name="**/*.sva"/>
      <include name="**/*.wsdl"/>
      <include name="**/*.ini"/>
      <include name="**/*.tld"/>
      <include name="**/*.tag"/>
      <include name="**/*.xlf"/>
      <include name="**/*.xsl"/>
      <include name="**/*.xsd"/>
      <include name="**/*.sso"/>
    </patternset>
    <patternset id="webfile.patterns">
      <include name="**/*.html"/>
      <include name="**/*.jspx"/>
      <include name="**/images/*.jpg"/>
    </patternset>
    <patternset id="webinf.patterns">
      <include name="WEB-INF/**/*.xml"/>
      <include name="**/WEB-INF/lib/*.jar"/>
    </patternset>
    <patternset id="library.patterns">
      <!--include name="**/glassfish.jsf_1.0.0.0_1-2-15.jar"/>
      <include name="**/glassfish.jstl_1.2.0.1.jar"/>
      <include name="**/javax.jsf_1.1.0.0_1-2.jar"/>
      <include name="**/wls.jsf.di.jar"/-->
      <include name="**/log4j-1.2.15.jar"/>
      <include name="**/adf-loc.jar"/>
      <exclude name="**/modules/javax.jsf_1.1.0.0_1-2.jar"/>
    </patternset>
    <copy todir="${UserInterface.output.dir}">
      <fileset dir="${UserInterface.src.dir}">
        <patternset refid="copy.patterns"/>
      </fileset>
      <fileset dir="../lib">
        <patternset refid="copy.patterns"/>
      </fileset>
    </copy>
    <copy todir="${UserInterface.output.dir}">
      <fileset dir="../UserInterface/adfmsrc">
        <patternset refid="copy.patterns"/>
      </fileset>
    </copy>
  </target>
  <target name="UserInterface.war"
          depends="UserInterface.copy,UserInterface.compile,model.copy">
    <war destfile="${UserInterface.deploy.dir}/${UserInterface.war.file}"
         webxml="${UserInterface.html.dir}/WEB-INF/web.xml">
      <zipfileset dir="${UserInterface.html.dir}">
        <patternset refid="webinf.patterns"/>
      </zipfileset>
      <zipfileset dir="${UserInterface.html.dir}">
        <patternset refid="webfile.patterns"/>
      </zipfileset>
      <zipfileset dir="${UserInterface.output.dir}" prefix="WEB-INF/classes">
        <exclude name=".data/**"/>
        <exclude name="**/*.cdi"/>
        <exclude name="**/build.xml"/>
        <exclude name="**/build.properties"/>
        <exclude name="**/*.java"/>
        <exclude name="**/log4j-1.2.15.jar"/>
        <exclude name="**/WEB-INF/temp/**"/>
      </zipfileset>
    </war>
  </target>
  <target name="UserInterface.ear" depends="UserInterface.war,model.jar">
    <mkdir dir="${UserInterface.deploy.dir}\META-INF"/>
    <mkdir dir="${UserInterface.deploy.dir}\lib"/>
    <copy todir="${UserInterface.deploy.dir}\lib">
      <zipfileset file="..\lib\*.jar" prefix="lib"/>
    </copy>
      <copy todir="${UserInterface.deploy.dir}\META-INF">
         <fileset dir="${project.src.dir}\META-INF" excludes="application.xml"
               includes="cwallet.sso">
        <patternset refid="copy.patterns"/>
      </fileset>
    </copy>
    <copy todir="${UserInterface.deploy.dir}\adf\META-INF">
      <fileset dir="..\.adf\META-INF">
        <patternset refid="copy.patterns"/>
      </fileset>
    </copy>
    <copy file="${UserInterface.deploy.dir}/${UserInterface.war.file}"
          todir="${UserInterface.deploy.dir}"/>
    <copy file="${model.deploy.dir}/${model.jar.file}"
          todir="${UserInterface.deploy.dir}"/>
    <ear basedir="${UserInterface.deploy.dir}"
         destfile="${UserInterface.deploy.dir}/${UserInterface.ear.file}"
         appxml="${project.src.dir}/META-INF/application.xml">
      <zipfileset dir="../JDeveloperLibs" prefix="lib">
        <patternset refid="library.patterns"/>
      </zipfileset>
          </ear>
  </target>
  <target name="UserInterface.autodeploy" depends="UserInterface.ear">
    <copy file="${UserInterface.deploy.dir}/${UserInterface.ear.file}"
          todir="${weblogic.home}/${weblogic.deploy.dir}"/>
  </target>
  <target name="UserInterface.deploy"
          depends="UserInterface.clean,UserInterface.ear,JDBC">
    <echo message="Start undeploying application..."></echo>
    <wldeploy action="undeploy" verbose="true" debug="true" name="${project}"
              adminurl="t3://${host}:${port}" user="${username}"
              password="${password}" targets="${target.server}"
              failonerror="false"/>
    <wldeploy action="deploy" verbose="true" debug="true" name="${project}"
              adminurl="t3://${host}:${port}" user="${username}"
              password="${password}" targets="${target.server}"
              failonerror="false"
              source="${UserInterface.deploy.dir}/${UserInterface.ear.file}"
              remote="true" upload="true"/>
    <echo message="END OF DEPLOYING APPLICATION"></echo>
  </target>
</project>


Finally, a word of caution: using ant nowdays seems to many people a waste of time or efford, when compared to modern solutions such as maven, well presented here by Dana Singleterry. As usual, one can find far more information, including source code, below.

REFERENCES
http://biemond.blogspot.com/2009/04/weblogic-jdeveloper-ant-tasks.html
http://www.oracle.com/technetwork/articles/adf/part4-098813.html
http://tompeez.wordpress.com/2012/02/07/jdeveloper-gem-debug-ant-scripts by Timo Hahn