Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../../img/srcFileCovDistChart0.png 29% of files have more coverage
10   87   3   10
0   44   0,3   1
1     3  
1    
38,9% of code in this file is excluded from these metrics.
 
  JNDIConfigurationStrategy       Line # 46 10 3 0% 0.0
 
No Tests
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23    package net.smartlab.web.config;
24   
25    import java.net.URL;
26    import javax.naming.InitialContext;
27    import javax.naming.NamingException;
28    import javax.rmi.PortableRemoteObject;
29    import org.apache.commons.logging.Log;
30    import org.apache.commons.logging.LogFactory;
31    import org.hibernate.SessionFactory;
32    import org.hibernate.cfg.Configuration;
33    import net.smartlab.config.Element;
34    import net.smartlab.config.XMLConfiguration;
35    import net.smartlab.web.BusinessObjectFactory;
36    import net.smartlab.web.Domain;
37   
38    /**
39    * The Hibernate configuration file used must reside into the
40    * <code>META-INF</code> directory of the topmost packaging archive and should
41    * be named <code>smartweb.jar.hcf</code> or have the name of the JAR file
42    * containing the subclass followed by the <code>.hcf</code> suffix.
43    *
44    * @author rlogiacco
45    */
 
46    public class JNDIConfigurationStrategy implements FactoryConfigurationStrategy {
47   
48    /**
49    * Provides logging capabilities to the strategy.
50    */
51    protected final Log logger = LogFactory.getLog(JNDIConfigurationStrategy.class);
52   
53   
54    /**
55    * @see net.smartlab.web.config.FactoryConfigurationStrategy#getSessionFactory(net.smartlab.web.BusinessObjectFactory)
56    */
 
57  0 toggle public SessionFactory getSessionFactory(BusinessObjectFactory factory) {
58  0 synchronized (BusinessObjectFactory.class) {
59  0 String archive = Domain.getLastArchiveName(factory.getClass());
60  0 URL file = Domain.getResource(factory.getClass(), new String[] {"/META-INF/" + archive + ".hcf",
61    "/META-INF/smartweb.jar.hcf"});
62  0 try {
63  0 XMLConfiguration config = new XMLConfiguration(file);
64  0 Element sessionFactory = config.getElement("session-factory");
65  0 String jndi = sessionFactory.getAttribute("name");
66  0 try {
67  0 return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
68    SessionFactory.class);
69    } catch (NamingException ne) {
70    logger.info("getSessionFactory() - configure hibernate");
71    try {
72    // Session factory not yet configured
73    Configuration hibernate = new Configuration().configure(file);
74    hibernate.buildSessionFactory();
75    return (SessionFactory)PortableRemoteObject.narrow(new InitialContext().lookup(jndi),
76    SessionFactory.class);
77    } catch (Exception e) {
78    logger.error("getSessionFactory() - error", e);
79    }
80    }
81    } catch (Exception e) {
82    logger.error("getSessionFactory() - error", e);
83    }
84  0 return null;
85    }
86    }
87    }