<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 閱讀(128) 評論(0)  編輯  收藏


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


    網站導航:
     

    My Links

    News

    留言簿(18)

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 日韩精品无码区免费专区| 5g影院5g天天爽永久免费影院| 男人的天堂亚洲一区二区三区| 亚洲毛片基地4455ww| 亚洲女同成av人片在线观看| a级午夜毛片免费一区二区| 亚洲国产成人久久精品软件| 亚洲天堂电影在线观看| 在线免费视频一区二区| 99久9在线|免费| 亚洲不卡视频在线观看| 夫妻免费无码V看片| 老汉精品免费AV在线播放| 国产99久久久国产精免费| 免费国产va在线观看| 亚洲乱码日产精品一二三| 亚洲&#228;v永久无码精品天堂久久| 精品无码免费专区毛片| 日韩电影免费在线观看中文字幕| 亚洲精品456人成在线| 亚洲av日韩片在线观看| 日韩中文无码有码免费视频| 国产自国产自愉自愉免费24区 | 亚洲日本国产乱码va在线观看| 久久国产亚洲精品麻豆| 91九色视频无限观看免费| 久久综合九色综合97免费下载| 岛国精品一区免费视频在线观看| 黄页网址大全免费观看12网站| 337p欧洲亚洲大胆艺术| 亚洲AV无码乱码在线观看性色扶| 国产激情免费视频在线观看| a级日本高清免费看| 免费福利电影在线观看| 久久久久免费看黄a级试看| 日韩亚洲人成网站| 亚洲AV无码专区国产乱码不卡| 亚洲欧美日韩久久精品| 亚洲精品天堂成人片AV在线播放| 亚洲国产美女精品久久久| 亚洲精品成a人在线观看夫|