<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)

    隨筆檔案

    相冊

    搜索

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 成人a视频片在线观看免费| 99国产精品免费视频观看| 在线精品免费视频无码的| 亚洲三级在线视频| 亚洲AV无码精品色午夜在线观看| 免费很黄无遮挡的视频毛片| 国产激情免费视频在线观看 | 成年美女黄网站色大免费视频| 亚洲视频日韩视频| 亚洲无砖砖区免费| 亚洲国产片在线观看| 免费大片黄在线观看yw| 亚洲激情视频图片| 成年女人永久免费观看片| 自拍偷自拍亚洲精品偷一| 国产成人精品曰本亚洲79ren| 亚洲人成综合在线播放| 成人毛片免费观看视频大全| 亚洲一级免费毛片| 亚洲AV无码成人精品区大在线| 亚洲视频一区网站| 妞干网免费观看视频| 免费毛片毛片网址| 亚洲AV成人无码久久精品老人| 免费无码国产在线观国内自拍中文字幕 | 亚洲午夜福利717| 久久精品一本到99热免费| avtt天堂网手机版亚洲| 四虎影视永久免费视频观看| 亚洲AV成人无码天堂| 国产日产成人免费视频在线观看| 人禽伦免费交视频播放| 四虎国产精品免费视| 中文字幕在线观看免费| 亚洲国产高清在线精品一区| 国产免费观看青青草原网站| 久久久久免费精品国产小说| 国产亚洲av片在线观看播放| 国产精品成人观看视频免费| 亚洲AV天天做在线观看| 日韩特黄特色大片免费视频|