Pages

Saturday, May 15, 2010

JNDI Resource Configuration in Tomcat 5.5

JNDI Resource Configuration to maintain database connections. Follow the the steps below :

1. Open Context.xml file, which is located under tomcat/conf/context.xml
2. Add Following Resource configuration under tag.



3. Open your applications web.xml file and add resource-reference in that.


DB Connection
jdbc/test
javax.sql.DataSource
Container


4. Look at the following code to lookup JNDI

private static InitialContext ctx = null;
private static DataSource ds = null;
try
{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
if(ds != null){
log.info("DbUtils: datasource setup successfully.");
}
}catch (Exception e)
{
System.out.println("Error is .. "+e.getMessage());
}


5. Finally, deploy your application and use it.

Tuesday, April 6, 2010

Java - Date & Time

This code can helps us to separate Time from Date.
String d="1-2-2010 3:43:57 PM";
DateFormat formatterDateFormat;
Date date;
formatterDateFormat=new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
date=(Date)formatterDateFormat.parse(d);
DateFormat df=DateFormat.getTimeInstance(DateFormat.SHORT);
String time=df.format(date);
System.out.println("Time is ... "+time);

Output of this code is like...
Time is ... 3:43 PM

Saturday, March 27, 2010

Custom User Id

1. Using this code we can generate customized user id's.


package com.myspace;
import java.util.HashMap;
import Thor.API.tcResultSet;
import Thor.API.Exceptions.tcAPIException;
import Thor.API.Operations.tcUserOperationsIntf;
import com.thortech.util.logging.Logger;
import com.thortech.xl.client.events.tcBaseEvent;
import com.thortech.xl.dataobj.tcDataObj;
import com.thortech.xl.util.logging.LoggerModules;

public class generateCustomUserId extends tcBaseEvent {
                private static Logger logger = Logger.getLogger(LoggerModules.XL_JAVA_CLIENT);
                private tcUserOperationsIntf userInf;
                public generateCustomUserId ()
                {
                                setEventName("Generating a userid for a User");
                }
/************************************************************************
                 *  METHOD NAME  :- implementation
                 *  PURPOSE      :-
                 *  CALLED FROM  :-
                 *  CREATED BY   :- DAGADU AKAMBE
                 ************************************************************************/
                protected void implementation() throws Exception
                {
                                tcDataObj data = getDataObject();
                                String isServiceAccount = data.getString("usr_udf_serviceacct");
                                if (isServiceAccount.equals("Y")) {
                                                generateServiceAccountId(data);
                                } else {
                                                generateUserAccountId(data);
                                }
                }
                /************************************************************************
                 *  METHOD NAME  :- implementation
                 *  PURPOSE      :-
                 *  CALLED FROM  :-
                 *  CREATED BY   :- DAGADU AKAMBE
                 ************************************************************************/
                private void generateUserAccountId(tcDataObj data) throws Exception {
                                String firstName = data.getString("usr_udf_firstnamestripped") + "0";
                                String middleName = data.getString("usr_udf_middlenamestripped") + "0";
                                String lastName = data.getString("usr_udf_lastnamestripped") + "0000";
                                //Integer maxNumber = 0;
                                String userid = firstName.substring(0,1) + middleName.substring(0,1) +lastName.substring(0,4);
                                int count = 0;
                    while (true)
                    {
                      boolean flag = checkUserID(userid, count);
                      ++count;
                      if (flag == false)
                      {
                        continue;
                      }
                      if (count <= 0) break;
                           userid=userid.concat(Integer.toString(count - 1));
                    }
                                data.setString("usr_login", userid);
                }
                                /************************************************************************
                 *  METHOD NAME  :- generateServiceAccountId
                 *  PURPOSE      :-
                 *  CALLED FROM  :-
                 *  CREATED BY   :- DAGADU AKAMBE
                 ************************************************************************/
                private void generateServiceAccountId(tcDataObj data) throws Exception {
                                String userid = data.getString("usr_udf_serviceacctuserid");
                                String surname = data.getString("usr_udf_serviceacctsurname");
                                String emplid = data.getString("usr_udf_emplid");
                                String firstName = data.getString("usr_first_name");
                                String lastName = data.getString("usr_last_name");
                                data.setString("usr_login", userid);
                                data.setString("usr_udf_title", firstName + " " + lastName + ", Owner");
                                data.setString("usr_first_name", "Service Account");
                                data.setString("usr_last_name", surname);
                                data.setString("usr_udf_fullname", "Service Account " + surname);
                                data.setString("usr_udf_serviceacctowner", emplid);
                }
                /************************************************************************
                 *  METHOD NAME  :- checkUserID
                 *  PURPOSE      :-
                 *  CALLED FROM  :-
                 *  CREATED BY   :- DAGADU AKAMBE
                 ************************************************************************/
                private boolean checkUserID(String userId, int count)
                  {
                    if (count > 0)
                    {
                      userId = userId.concat(Integer.toString(count));
                    }
                    HashMap userHash = new HashMap();
                    userHash.put("usr_login", userId);
                    try
                    {
                      tcResultSet userSet = this.userInf.findUsers(userHash);
                      if (userSet.getTotalRowCount() > 0) {
                        return false;
                      }
                      return true;
                    }
                    catch (tcAPIException e)
                    {
                    }
                    return false;
                  }
}





Friday, March 26, 2010

Email Notification -Java Mail

 This code is used to send email notification using Java Mail. This can be used as an Adapter.


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMail
{
   
     
    /*********************************************************************
         * METHOD NAME :sendPasswordENotification()
         * PUROPOSE    :Send Mail
         * CREATED BY  :DAGADU AKAMBE
         * CREATED ON  :--
         * MODIFIED BY :--
         *********************************************************************/
       
       
   
      public void sendEMail(String firstName,String lastName,String middleName,String emailId,String userPassword)
       {
           
          
           try
           { 
                   StringBuffer stringBuffer=new StringBuffer();
                                 
                   Properties prop=System.getProperties();
                   prop.setProperty("mail.smtp.host","mail.tennant.tco.corp");
                   Session session=Session.getDefaultInstance(prop);
                   MimeMessage message=new MimeMessage(session);
                  
                   stringBuffer.append("
Welcome to OIM.
");
                   stringBuffer.append("Your name:"+lastName+" "+firstName+" "+middleName+"
");
                   stringBuffer.append("Your password: "+userPassword+"
");
                   stringBuffer.append("
Thank you !
");
                   //From Email Address goes here...
                   message.setFrom(new InternetAddress("toemailaddress"));
                 
                   //To Email Address goes here..
                   message.addRecipient(Message.RecipientType.TO,new InternetAddress(emailId));
                   message.setSubject("***Welcome to OIM! Your Password.***");
                   message.setContent(stringBuffer.toString(),"text/html; charset=UTF-8");
                   Transport.send(message);
                  
           }
           catch(MessagingException e)
           {
                   e.printStackTrace();
           }

       }
     
    
      
      
    
      
}

Thursday, March 25, 2010

Password Email Notification- OIM

import com.thortech.xl.dataaccess.tcDataProvider;
import com.thortech.xl.dataobj.util.tcEmailNotificationUtil;

public class JavaMail
{
  
    
    /*********************************************************************
         * METHOD NAME :sendPasswordENotification()
         * PUROPOSE    :Send Mail
         * CREATED BY  :DAGADU AKAMBE
         * CREATED ON  :--
         * MODIFIED BY :--
         *********************************************************************/
      
        public void sendPasswordNotification(tcDataProvider dataProvider,
                                         String password,String recipient,String fName,
                                           String mName, String lName)
        {
          
            tcEmailNotificationUtil    sendMail = new tcEmailNotificationUtil(dataProvider);
            sendMail.setBody("Welcome to OIM. Your name:"+lName+" "+fName+" "+mName+". Your     password: "+password+"");
            sendMail.setSubject("***Welcome to OIM! Your Password.***");
            sendMail.setFromAddress("touseremailaddress");
            sendMail.sendEmail(recipient);
          
        }
      
      

     
     
   
     
}