Saturday, August 29, 2009

Cookies and Javascript

Handling cookies in Javascript
=================================



function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

function delCookie(name) {
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +
"; path=/";
}

Thursday, August 27, 2009

Regular expressions in javascript and Java

Regular expressions lets you to search a text in a text string or do a search and
replace a text in a text string.



Java Script:

Create the expression
================================================================================

var exp = /expression/;
Or
var exp = new RegExp("expression");

Testing availability of a text.
================================================================================

if(/text/.test("The text string.")) alert("Found text");

OR

if ("The text string".match(/text/)) alert("Match found for text");

Doing a search and replace.
================================================================================

var output = "The text string".replace(/text/g, "replacement");

OR

var re = new RegExp("text", "g");
var output = "The text string".replace(re,"replacement");

Using the /g OR "g" modifier makes sure that all occurrences of "text" are replaced


Splitting a text string by a delimiter
================================================================================

var myArray = "comma, separated, text, string".split(/,/);


Retrieving the part of the string that was matched
================================================================================

var mymatch = /text/.exec("The text string")

if (mymatch == null) {
alert("No match");
} else {
var s = "Match at position " + mymatch .index + ":\n";
for (i = 0; i < s =" s" mystringarr = "comma, separated, text, string" replaced = "change, delimiter, of, text, string" regex = "match this string" regex = "match.*" class ="=" mypattern =" Pattern.compile(" mymatcher =" myPattern.matcher("> The "^" says check the beginning of the text. [A-Z] specify range of
numbers.

Check for any text starting with a digit 1 to 5.
==============================================================================

^[1-5]

See reference [http://www.regular-expressions.info/reference.html] for details.

Tuesday, August 25, 2009

Password protect D-Link wireless router DSL-2640U

Security options to choose from.

Wired Equivalent Privacy (WEP) - Less secure (The key can be recovered from network sniffing)
Wi-Fi Protected Access (WPA and WPA2) - The recommended solution to WEP security problems.
WPA-PSK (Pre-shared key mode) is the personal mode which is designed for home and small office network setups.
WPA supports two encription methods to choose from.
TKIP - Temporal Key Integrity Protocol.
AES - Advanced Encryption Standard which is considered to be fully secured.


Seting up a security key (password)
===================================

Type http://192.168.1.1/ in your browser to open the router admin console.
Go to the wireless security tab and choose the follwing settings for each
option.
Network Authentication: WPA-PSK
WPA Pre-Shared Key (Password): Combination of numbers and letters.
WPA Group Rekey Interval: 0
WPA Encryption: AES
WEP Encryption: Disabled.

Following is a screenshot of the required settings.






Adding the wireless network password to Windows Vista


Setingup the password in Windows Vista can be done throught the "Manage Wireless Networks". Access "Manage Wireless Networks" from Control Panel>Network and Sharing Center>. Right click and select properties of the DLink network. Select the security tab and enter the following.
Security type: WPA-Personal
Encryption type: AES
Network security key: (Enter the previousely setup password)




Now your wireless network is fully protected.

Monday, July 20, 2009

Start a brand new PostgreSQL server

Create a directory to hold the postgres database cluster files. (Eg. c:\pgdata or /usr/local/pgsql/pgdata in linux)

Cluster is a collection of databases that are managed by a single server instance.
Be sure the pgdata directory owned by postgres user and the directory is empty

Type the following command to initialise the cluster.
initdb -D c:\pgdata -U postgres

Start the cluster by invoking the following command.
pg_ctl -D c:\pgdata -l c:\pgdata\logfile start

To stop the database cluster use the following command.
pg_ctl -D c:\pgdata stop

Other Info

Create a user called "user1"
createuser -U postgres -P user1 (Prompted for a password)

Create a database called "database1"
createdb -U user1 -E UTF-8 database1

Create Language
createlang -U postgres -d database1 plpgsql

Exporting data from a database.
pg_dump -U user1 database1 > dumpfile.dmp

Importing data to the database
psql -U user1 database1 < dumpfile.dmp

Check the status and data file location:
ps aux | grep post

Default data file location: /var/lib/postgresql/

Change default password ALTER USER postgres WITH PASSWORD ‘password’;
change the Linux user password of postgres to the same.

(Note: "postgres" is the user created when installing Postgre. If you create a user account named postgres in the OS and installed Postgre into this account "postgres" can be ommited. This is valid for both Windows OS and Linux. )

Wednesday, July 8, 2009

Starting oracle admin web console

Go to the ORACLE_HOME/bin directory
Eg. /u01/app/oracle/product/11.1.0/db_1/bin

** To check the status enter ./emctl status dbconsole
** To start the enterprise db console enter ./emctl start dbconsole
** To stop the admin console enter ./emctl stop dbconsole

Wednesday, June 24, 2009

Mixing transactions manual and container managed with JTA

How-to Mix manual transactions and container managed transactions


Container managed transactions use JTA for transaction management. If we need to mix the manual transactions and container managed transactions we have to use the JTA for the manual transactions as well.


You can use the following two classes for handling manual transactions with JTA.





Listing : Class CustomTransactionCallbackWithoutResult





public abstract class CustomTransactionCallbackWithoutResult {

   public abstract void doInTransactionWithoutResult();

}




Listing : Class CustomTransactionTemplate



import javax.transaction.UserTransaction;

import javax.transaction.SystemException;



public class CustomTransactionTemplate {

 


   
UserTransaction ut;
 


    public CustomTransactionTemplate(UserTransaction ut) {

        this.ut = ut;

   
}
    public void execute(CustomTransactionCallbackWithoutResult transactionCallbackWithoutResult) {
      

            try {
        

              try {

                ut.begin();

                transactionCallbackWithoutResult.doInTransactionWithoutResult();

                ut.commit();
            } catch (Throwable ex) {

                try {

                  ut.rollback();

                } catch (SystemException syex) {

                  throw new Exception( "Rollback failed: " + syex.getMessage());

                }

                 throw new Exception( "Transaction failed: " + ex.getMessage());

           }

        } catch (Throwable e) {

            throw new RuntimeException(e);

        }
    }

} 





Using the above two classes

 


InitialContext ic = new InitialContext();

UserTransaction ut = (UserTransaction) c.lookup("java:comp/UserTransaction");

final DataSource ds = (DataSource) c.lookup("java:/sampleDS");

 

CustomTransactionTemplate txTemplate = new CustomTransactionTemplate(ut);

txTemplate.execute(new CustomTransactionCallbackWithoutResult() {

   public void doInTransactionWithoutResult() {

     //We are in the transaction
                        // Use ds and update a table

     // call a session bean which in turn call Entity bean

    // and cause Container  managed transactions                     

     //

     }

 });



 

When mixing container managed transactions and manual transactions if  you try to use the setAutoCommit() or Hibernate TransactionTemplate the following errors will be fired. To avoid the error the best bet is to use the JTA transactions in every place.

 


java.sql.SQLException: Connection handle has been closed and is unusable at org.jboss.resource.adapter.jdbc.WrappedConnection.checkStatus(WrappedConnection.java:537) at org.jboss.resource.adapter.jdbc.WrappedConnection.checkTransaction(WrappedConnection.java:524) at org.jboss.resource.adapter.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:184) at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505) at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:423) at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547) at org.hibernate.loader.Loader.doQuery(Loader.java:673) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2220)     


 

Tuesday, June 9, 2009

Oracle XE change http port and enable remote access to web console

To change the http port use the following command in sqlplus

call dbms_xdb.cfg_update( updateXML(dbms_xdb.cfg_get(), '/xdbconfig/sysconfig/protocolconfig/httpconfig/http-port/text()', 8090))

or

EXEC DBMS_XDB.SETHTTPPORT(8090);

To enable remote access use the following command in sqlplus

EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);

Subscribe