afunms

          My Software,My Dream—Forge a more perfect NMS product.

          perfect DAO design - related classes

          package afu.framework;

          import java.sql.Connection;
          import java.lang.reflect.Constructor;

          import afu.framework.service.*;
          import afu.framework.action.*;
          import afu.framework.util.SysLogger;
          import afu.framework.config.ModuleConfig;
          import afu.framework.jdbc.BaseDao;

          public class BeanFactory
          {
              
          private BeanFactory()
              
          {        
              }

              
              
          public static BaseService newService(String beanId) 
              
          {
                  
          if(beanId==null)
                  
          {
                      SysLogger.error(
          "beanId can not be null.");
                      
          return null;
                  }
                  
                  
          if(!ModuleConfig.getServicesMap().containsKey(beanId))
                  
          {
                      SysLogger.error(
          "Service-class whose id is " + beanId + " doest not exist.");
                      
          return null;
                  }
                          
                  BaseService bs 
          = null;
                  
          try
                  
          {
                      ClassLoader classLoader 
          = Thread.currentThread().getContextClassLoader();
                      
          if (classLoader == null
                          classLoader 
          = BeanFactory.class.getClassLoader();
                  
                      Class clazz 
          = classLoader.loadClass(ModuleConfig.getServicesMap().get(beanId));
                      bs 
          = (BaseService)clazz.newInstance();
                  }

                  
          catch(Exception e)
                  
          {
                      SysLogger.error(
          "BeanFacade.newService()",e);
                  }

                  
          return bs;
              }

                  
              @SuppressWarnings(
          "unchecked")
              
          public static BaseAction newAction(String beanId) 
              
          {
                  
          if(beanId==null)
                  
          {
                      SysLogger.error(
          "beanId can not be null.");
                      
          return null;
                  }

                  
          if(!ModuleConfig.getActionsMap().containsKey(beanId))
                  
          {
                      SysLogger.error(
          "Action-class whose id is " + beanId + " doest not exist.");
                      
          return null;
                  }

                  BaseAction ba 
          = null;
                  
          try
                  
          {
                      ClassLoader classLoader 
          = Thread.currentThread().getContextClassLoader();
                      
          if (classLoader == null
                         classLoader 
          = BeanFactory.class.getClassLoader();
                  
                      Class clazz 
          = classLoader.loadClass(ModuleConfig.getActionsMap().get(beanId));
                      ba 
          = (BaseAction)clazz.newInstance();
                  }

                  
          catch(Exception e)
                  
          {
                      SysLogger.error(
          "BeanFacade.getAction()",e);
                  }

                  
          return ba;
              }
            
              
              
          public static BaseDao newDao(String beanId)
              
          {
                  
          return newDao(beanId,null);
              }

              
              
          public static BaseDao newDao(String beanId,Connection conn)
              
          {
                  
          if(beanId==null || !ModuleConfig.getDaosMap().containsKey(beanId))
                      
          return null;

                  BaseDao dao 
          = null;
                  
          try
                  
          {
                      Class[] types 
          = new Class[] { java.sql.Connection.class };
                      Class clazz 
          = Class.forName(ModuleConfig.getDaosMap().get(beanId));
                      Constructor cons 
          = clazz.getConstructor(types);
                      Object[] args 
          = new Object[] {conn};
                      dao 
          = (BaseDao)cons.newInstance(args);
                  }

                  
          catch(Exception e)
                  
          {
                      SysLogger.error(
          "BeanFactory.newDao with a connection",e);
                  }

                  
          return dao;
              }

          }

          package afu.framework.jdbc;

          import java.util.*;
          import java.sql.*;

          import javax.naming.*;
          import javax.sql.DataSource;

          import afu.framework.util.SysLogger;

          import afu.framework.config.ModuleConfig;


          public class ConnectionManager
          {
             
          private ConnectionManager() 
             
          {       
             }

             
             
          private static Map<String,DataSource> dsMap;   
             
          static
             
          {
                 dsMap 
          = new HashMap<String,DataSource>();
                 List
          <String> jndis = ModuleConfig.getJndis();
                 
          try
                 
          {
                    Context initCtx 
          = new InitialContext();
                    
          if("weblogic".equalsIgnoreCase(ModuleConfig.getContainer()))
                    
          {
                        
          for(String jndi:jndis)
                        
          {                
                            DataSource ds 
          = (DataSource)initCtx.lookup(jndi);
                             dsMap.put(jndi,ds);
                        }

                    }

                    
          else
                    
          {
                        
          for(String jndi:jndis)
                        
          {                
                            DataSource ds 
          = (DataSource)initCtx.lookup("java:comp/env/" + jndi);
                             dsMap.put(jndi,ds);
                        }
                        
                    }

                    initCtx.close();
                 }

                 
          catch(NamingException e)
                 
          {
                     SysLogger.error(
          "Can not connect database,may be jndi does not exist",e);
                 }

                 
          catch(Exception e)
                 
          {
                     SysLogger.error(
          "Can not connect database",e);
                 }

             }

             
             
          public static Connection getConnection()
             
          {
                 
          return getConnection(ModuleConfig.getDefaultJndi(),true);
             }

             
             
          public static Connection getConnection(final boolean autoCommit)
             
          {
                 
          return getConnection(ModuleConfig.getDefaultJndi(),autoCommit);
             }

             
             
          public static Connection getConnection(final String jndi,final boolean autoCommit)
             
          {
                 Connection conn 
          = null;
                 
          try
                 
          {           
                     DataSource ds 
          = dsMap.get(jndi);
                     
          if(ds==nullreturn null;
                     
                     conn 
          = ds.getConnection();           
                     conn.setAutoCommit(autoCommit);
                 }

                 
          catch(SQLException sqle)
                 
          {
                     SysLogger.error(
          "Database fail to get connection 1",sqle);
                 }

                 
          catch(Exception sqle)
                 
          {
                     SysLogger.error(
          "Database fail to get connection 2",sqle);
                 }

                 
          return conn;
             }

             
             
          public static void close(Connection conn)
             
          {
                 
          try
                 
          {
                     
          if(conn!=null && !conn.isClosed())
                        conn.close();
                 }

                 
          catch(SQLException se)
                 
          {
                     SysLogger.error(
          "Fail to close() connection",se);         
                 }

             }


             
          public static void close(Connection conn,Statement stmt,ResultSet rs)
             
          {
                 
          try
                 
          {
                     
          if(conn!=null && !conn.isClosed())
                        conn.close();
                     
          if(stmt!=null)
                        stmt.close();
                     
          if(rs!= null)
                        rs.close();           
                 }

                 
          catch(SQLException se)
                 
          {
                     SysLogger.error(
          "Fail to close() connection",se);         
                 }

             }

             
             
          public static void rollback(Connection conn)
             
          {
                 
          try
                 
          {
                     
          if(!conn.getAutoCommit())             
                        conn.rollback();
                 }

                 
          catch(SQLException se)
                 
          {
                     SysLogger.error(
          "Can not do rollback operation.",se);         
                 }

             }
             
          }
           

          posted on 2007-05-12 12:37 afunms 閱讀(130) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 蒲江县| 永吉县| 林口县| 罗定市| 安宁市| 玉田县| 通海县| 公主岭市| 名山县| 双柏县| 黎城县| 额济纳旗| 黑龙江省| 五家渠市| 石林| 介休市| 化德县| 景宁| 达州市| 来宾市| 仪征市| 吕梁市| 白水县| 舞阳县| 通榆县| 营山县| 龙江县| 石棉县| 四子王旗| 黔西县| 财经| 文成县| 华容县| 临西县| 金华市| 蕉岭县| 南溪县| 和静县| 顺义区| 正镶白旗| 南昌县|