<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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)

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 成在线人永久免费视频播放| 亚洲成人黄色在线| 麻豆成人精品国产免费| 国产男女猛烈无遮挡免费网站| 亚洲A∨精品一区二区三区| 亚洲色中文字幕无码AV| 亚洲福利电影一区二区?| 亚洲av无码日韩av无码网站冲| 最新亚洲精品国偷自产在线| 激情无码亚洲一区二区三区| 国产成人AV免费观看| 日本阿v免费费视频完整版| 国产在线观看免费完整版中文版 | 亚洲乱码国产乱码精品精| 亚洲精品美女久久久久9999| 精品无码无人网站免费视频| 日本不卡免费新一二三区| 国产精品久久久久久亚洲影视| 国产乱子伦精品免费女| 久久亚洲精品无码aⅴ大香| 蜜臀亚洲AV无码精品国产午夜.| 日本免费电影一区| yellow视频免费在线观看| 久久国产精品成人片免费| 亚洲福利电影一区二区?| 免费高清在线爱做视频| 免费一区二区三区在线视频| 无码国产精品一区二区免费虚拟VR| 亚洲最大成人网色香蕉| 男的把j放进女人下面视频免费| 啦啦啦手机完整免费高清观看| 亚洲精品国偷自产在线| 1000部拍拍拍18勿入免费凤凰福利 | 久久精品国产免费观看三人同眠| 亚洲av产在线精品亚洲第一站| 国产一级片免费看| 亚洲性久久久影院| 亚洲爆乳少妇无码激情| 精品女同一区二区三区免费站| 亚洲人成人伊人成综合网无码 | 久久精品国产亚洲αv忘忧草|