Changeset 3726 in main


Ignore:
Timestamp:
04/18/12 00:39:59 (10 years ago)
Author:
Garth Braithwaite
Message:

java and view - blob appears to be inserting, temp commit. Fixed a couple of spring db security.xml issues. web.xmls have updated comments.

Location:
trunk/src/main
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/main/java/org/ibisph/jdbc/JDBCTemplate.java

    r3535 r3726  
    11package org.ibisph.jdbc;
    22
     3import java.io.BufferedInputStream;
     4import java.io.ByteArrayInputStream;
     5import java.io.ByteArrayOutputStream;
     6import java.io.IOException;
     7import java.io.InputStream;
    38import java.math.BigDecimal;
    4 
     9import java.sql.Blob;
    510import java.sql.Connection;
    611import java.sql.PreparedStatement;
     
    3540 * 11/26/03, restructured the connection stuff a little bit to try and get the
    3641 * speed up a little.  Put the synch back on get connection and made it check
    37  * for being closed.  Also made it manditory to create object with jndi source.
     42 * for being closed.  Also made it mandatory to create object with jndi source.
    3843 * Made DataSource kept alive with the object to try and help with speed.
    3944 *
    40  * 12/27/04, redid the way prep udpate statements handle null values.  Used to
    41  * simply call prepStatement.setOjbect(index, object) but put in a loop that
     45 * 12/27/04, refactored the way prep update statements handle null values.  Used
     46 * to simply call prepStatement.setOjbect(index, object) but put in a loop that
    4247 * calls specific set method and if null object set as string.
    4348 *
     
    4550 */
    4651public class JDBCTemplate {
    47 
    4852  protected static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JDBCTemplate.class);
    4953
     
    9599  public synchronized void close() {
    96100    try {
    97       if(resultSet  != null)resultSet.close();
    98       if(statement  != null)statement.close();
    99       if(preparedStatement != null)preparedStatement.close();
    100       if(connection != null)connection.close();
     101      if(null != resultSet)resultSet.close();
     102      if(null != statement)statement.close();
     103      if(null != preparedStatement)preparedStatement.close();
     104      if(null != connection)connection.close();
    101105    }
    102106    catch(SQLException ex) {
     
    198202    return(false);
    199203  } //-------------------------- End of Method ------------------------------
     204  public boolean setPreparedStatementBlob(int position, Blob blob) {
     205    try {
     206      preparedStatement.setBlob(position, blob);
     207      return(true);
     208    }
     209    catch(SQLException ex) {
     210      logException("setPreparedStatementBLOB", "Position: "+ position+ ", blob: "+blob, ex);
     211    }
     212    return(false);
     213  } //-------------------------- End of Method ------------------------------
     214
     215
     216  public boolean setPreparedStatementInputStream(int position, InputStream inputStream) {
     217    try {
     218      preparedStatement.setBinaryStream(position, inputStream);
     219      return(true);
     220    }
     221    catch(SQLException ex) {
     222      logException("setPreparedStatementBLOB", "Position: "+ position+ ", inputStream: "+inputStream, ex);
     223    }
     224    return(false);
     225  } //-------------------------- End of Method ------------------------------
     226
     227  public boolean setPreparedStatementBlob(int position, byte[] byteArray) {
     228    ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
     229    try {
     230      preparedStatement.setBinaryStream(position, bais);
     231//      setPreparedStatementInputStream(position, bais);
     232      return(true);
     233    }
     234    catch(SQLException ex) {
     235      logException("setPreparedStatementBLOB", "Position: "+ position+ ", byteArray length: "+byteArray.length, ex);
     236    }
     237    finally {
     238      try { bais.close(); }
     239      catch(Exception e) {}
     240    }
     241    return(false);
     242  } //-------------------------- End of Method ------------------------------
     243
     244
     245
     246
     247
     248
    200249
    201250  public ResultSet executePreparedStatementQuery() {
     
    289338        else if(parameter[i] instanceof java.util.Date)
    290339          setPreparedStatementTimestamp(i+1, new Timestamp( ((java.util.Date)parameter[i]).getTime() ) );
     340        else if(parameter[i] instanceof Blob)
     341          setPreparedStatementBlob(i+1, (Blob)parameter[i]);
    291342        else if(parameter[i] == null)
    292343          setPreparedStatementString(i+1, null);
     
    372423  public boolean next() {
    373424    try {
    374       if(resultSet != null)return(resultSet.next());
     425      if(null != resultSet)return(resultSet.next());
    375426    }
    376427    catch(SQLException ex) {
     
    392443  } //-------------------------- End of Method ------------------------------
    393444  public Object getObject(int columnIndex) {
    394     Object o=null;
     445    Object o = null;
    395446    try {
    396447      o = resultSet.getObject(columnIndex);
     
    404455
    405456  public String getString(String columnName) {
    406     String s=null;
     457    String s = null;
    407458    try {
    408459      s = resultSet.getString(columnName);
     
    414465  } //-------------------------- End of Method ------------------------------
    415466  public String getString(String columnName, String defaultValueIfNull) {
    416     String s=null;
     467    String s = null;
    417468    try {
    418469      s = resultSet.getString(columnName);
     
    425476  } //-------------------------- End of Method ------------------------------
    426477  public String getString(int columnIndex) {
    427     String s=null;
     478    String s = null;
    428479    try {
    429480      s = resultSet.getString(columnIndex);
     
    557608  } //-------------------------- End of Method ------------------------------
    558609
     610  public Blob getBlob(int columnIndex) {
     611    Blob blob = null;
     612    try {
     613      blob = resultSet.getBlob(columnIndex);
     614    }
     615    catch(SQLException ex) {
     616      logException("getBlob", "columnIndex: "+columnIndex, ex);
     617    }
     618    return(blob);
     619  } //-------------------------- End of Method ------------------------------
     620  public Blob getBlob(String columnName) {
     621    Blob blob = null;
     622    try {
     623      blob = resultSet.getBlob(columnName);
     624    }
     625    catch(SQLException ex) {
     626      logException("getBlob", "columnName: "+columnName, ex);
     627    }
     628    return(blob);
     629  } //-------------------------- End of Method ------------------------------
     630
     631  public byte[] getBlobByteArray(String columnName) {
     632    Blob blob = null;
     633    try {
     634      blob = resultSet.getBlob(columnName);
     635      return( blob.getBytes(1, (int)blob.length()) );
     636    }
     637    catch(SQLException ex) {
     638      try {
     639        byte[] byteArray = resultSet.getBytes(columnName);
     640        logger.info(".getBlobByteArray - Unsuccessful call to blob.getBytes - getBytes() seems to work.  SQL: {}, Array length: {}", this.lastSQL, byteArray.length);
     641        return(byteArray);
     642      }
     643      catch(SQLException se) {
     644      }
     645      logException("getBlobByteArray", "columnName: "+columnName, ex);
     646    }
     647    return(null);
     648  } //-------------------------- End of Method ------------------------------
    559649
    560650
     
    706796  } //-------------------------- End of Method ------------------------------
    707797
     798
     799
     800
     801  public byte[] selectBlob(int rowid) {
     802
     803// CREATE TABLE blobs (
     804//     ROWID INT NOT NULL,
     805//     ROWDATA BLOB,
     806//     PRIMARY KEY (rowid)
     807// );
     808
     809    byte[] returnData = null;
     810
     811    try {
     812      Statement statement = this.getStatement();
     813      String sql = "SELECT rowid, rowdata FROM blobs WHERE rowid = " + rowid;
     814      ResultSet resultSet = statement.executeQuery(sql);
     815
     816      if(resultSet.next()) {
     817
     818        try {
     819          ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
     820
     821          // Create an input stream from the BLOB column.
     822          // By default, rs.getBinaryStream()
     823          // returns a vanilla InputStream instance.
     824          // We override this for efficiency
     825          // but you don't have to:
     826          BufferedInputStream bis = new BufferedInputStream( resultSet.getBinaryStream("fieldblob") );
     827
     828          // A temporary buffer for the byte data:
     829          byte[] buffer = new byte[1024];
     830
     831          // Used to return how many bytes are read with each read() of the input stream:
     832          int bytesRead = 0;
     833
     834          // Make sure its not a NULL value in the column:
     835          if(!resultSet.wasNull()) {
     836
     837            if((bytesRead = bis.read(buffer,0,buffer.length)) != -1) {
     838              // Write out 'bytesread' bytes to the writer instance:
     839              baos.write(buffer,0,bytesRead);
     840            }
     841
     842            else {
     843              // When the read() method returns -1 we've hit the end of
     844              // the stream,
     845              // so now we can get our bytes out of the writer object:
     846              returnData = baos.toByteArray();
     847            }
     848          }
     849
     850          // Close the binary input stream:
     851          bis.close();
     852        }
     853        catch(IOException ioe) {
     854          System.err.println("Problem retrieving binary data: " + ioe);
     855        }
     856//        catch(ClassNotFoundException cnfe) {
     857//          System.err.println("Problem retrieving binary data: " + cnfe);
     858//        }
     859      }
     860
     861      resultSet.close();
     862      statement.close();
     863    }
     864    catch(SQLException se) {
     865      System.err.println("Couldn't retrieve binary data: " + se);
     866    }
     867    finally {
     868//      this.getConnection().close();
     869    }
     870
     871    return(returnData);
     872  } //-------------------------- End of Method ------------------------------
     873 
    708874} //============================ END OF CLASS =================================
    709875
  • trunk/src/main/webapps/ibisph-admin/WEB-INF/web.xml

    r3688 r3726  
    194194                BASIC STEPS ON HOW TO SETUP A RESOURCE:
    195195                1) Put any resource associated jar files within the app server's libs.
    196                 2) Within the application server define the resource and give it a
     196                2) Within the application server, define the resource and give it a
    197197                name.  This resource definition includes things like the object's
    198198                class name, username, password, addresses, any and all properties
    199199                that the resource needs to be able to function. 
    200                 3) If above resource is defined as a "global" type resource then create
    201                 a ResourceLink element that lists the resource's global name and then
    202                 defines the matching ibisph JNDI lookup name.  If the resource is defined
    203                 within the application then it's a local definition and the only thing
    204                 that must be done is to name the resource according to the expected IBIS
    205                 name.
    206                 4) IF global and the ResourceLink is not defined or the resource is
    207                 defined locally and the name does not match the value configured within
    208                 the IBIS spring context bean definition files then the name values MUST
    209                 be updated or the resource will not be accessible (this also should the
    210                 app's META-INF/context.xml entry as this value is used by some app servers
    211                 when deploying .war type webapps.
     200                3) Associate/expose the resource to the application.  In the case of
     201                Tomcat if the resource is a global resource you can create a reference
     202                via the ResourceLink element.  This lists the resource's global name
     203                and then defines the matching ibisph JNDI lookup name. Again in Tomcat
     204                you can simply define the resource within the application context def.
     205                In the case of enterprise application servers, you typically use the
     206                above discussed resource-ref.  This resource-ref is linked to the
     207                actual server defined resource via some sort of mapping element that
     208                usually resides within a special application app server specific
     209                xml configuration file. 
     210                4) Make sure the JNDI name set within the app's Spring bean file(s)
     211                match the name specified in the above.  Note also that the app's
     212                META-INF/context.xml entry can be used for some definitions.
    212213        -->
    213214        <resource-ref>
  • trunk/src/main/webapps/ibisph-view/WEB-INF/config/spring/common.xml

    r3710 r3726  
    183183        <bean id="Common.XML.DAO" class="org.ibisph.xml.dao.Document">
    184184                <property name="outputFormat" ref="Common.XML.OutputFormat"/>
    185         </bean>
     185        </bean>
     186<bean id="Common.BLOB.DAO" class="org.ibisph.xml.dao.BLOBDocument">
     187        <property name="outputFormat" ref="Common.XML.OutputFormat"/>
     188        <property name="JDBCTemplateFactory">
     189                <bean class="org.ibisph.jdbc.JDBCTemplateFactory">
     190                        <property name="dataSource">
     191                                <bean class="org.springframework.jndi.JndiObjectFactoryBean">
     192                                        <property name="jndiName" value="java:comp/env/jdbc/doh_ibisph"/>
     193                                </bean>
     194                        </property>
     195                </bean>
     196        </property>
     197</bean>
     198<!--
     199        <bean id="Common.JDBCTemplate.Factory" class="org.ibisph.jdbc.JDBCTemplateFactory">
     200                <property name="dataSource" ref="Common.DataSource.JNDI"/>
     201        </bean>
     202                <property name="JDBCTemplateFactory" ref="Common.JDBCTemplate.Factory"/>
     203-->
    186204
    187205
  • trunk/src/main/webapps/ibisph-view/WEB-INF/config/spring/query_definition.xml

    r3710 r3726  
    3636        <bean id="QueryDefinition.XML.Service" class="org.ibisph.querydefinition.service.QueryDefinitionXML">
    3737                <property name="XMLPath"     ref="QueryDefinition.XML.Path"/>
    38                 <property name="documentDAO" ref="Common.XML.DAO"/>
     38                <property name="documentDAO" ref="Common.BLOB.DAO"/>
    3939        </bean>
    4040        <bean id="QueryDefinition.DefinitionToModule.Service" class="org.ibisph.querydefinition.service.QueryDefinitionXMLToQueryModuleXML"/>
  • trunk/src/main/webapps/ibisph-view/WEB-INF/config/spring/security-db_authentication.xml

    r3687 r3726  
    3535        </bean>
    3636
    37         <bean id="Security-DB.JDBCTemplate.Factory" class="org.ibisph.jdbc.JDBCTemplateFactory">
    38                 <property name="dataSource" ref="Security-DB.DataSource.JNDI"/>
    39         </bean>
    40 
    4137        <!--
    4238                For MS-SQL Server wrap the "USER" table name with [] as USER is a reserved
     
    4440                MySQL does not need anything as USER is not reserved.
    4541        -->
    46         <bean id="Security-DB.DAO.Factory" class="org.ibisph.user.databean.dao.DataBeanJDBCTemplateDAOFactory">
     42        <bean id="Security-DB.DAO.Factory" class="org.ibisph.databean.dao.JDBCTemplateDAOFactory">
    4743                <property name="dataSource" ref="Security-DB.DataSource.JNDI"/>
    4844                <property name="dataBeanClassNameToJDBCTableName">
     
    5955
    6056        <!--  D A O   S E R V I C E S  -->
    61         <bean id="Security-DB.DataBeanDAO.Service" class="org.ibisph.service.bean.DataBeanDAO">
     57        <bean id="Security-DB.DataBeanDAO.Service" class="org.ibisph.databean.dao.DataBeanDAOService">
    6258                <property name="daoFactory" ref="Security-DB.DAO.Factory"/>
    6359        </bean>
  • trunk/src/main/webapps/ibisph-view/WEB-INF/config/spring/user.xml

    r3716 r3726  
    114114            <property name="session">
    115115                        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
    116 <property name="jndiName" value="mail/doh_ibisph"/>
    117 <!--
    118116                                <property name="jndiName" value="mail/doh_ibisph"/>
    119 -->
    120117                                <property name="resourceRef" value="true"/>
    121118                        </bean>
  • trunk/src/main/webapps/ibisph-view/WEB-INF/web.xml

    r3710 r3726  
    4343
    4444        <!-- =========================================== J N D I   R E S O U R C E S -->
    45         <!-- not all adopters will use a db.  this is provided for those who do:
     45        <!-- For a detailed discussion of JNDI resources, see the comments within
     46                the admin app's web.xml.  This gist is that resource refs provide an
     47                alias mechanism so that the app server can define the actual name
     48                according to their IT naming convention.  An intermediary xml config/
     49                mapping file then maps the real JNDI server name with the aliased
     50                resource name that this application uses.  In the case of Tomcat, this
     51                entry is not needed.  In the case of enterprise app servers it typically
     52                is used.
     53
    4654        <resource-ref>
    4755                <description>Resource reference to the app server container managed JNDI database connection factory</description>
Note: See TracChangeset for help on using the changeset viewer.