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 java.util.Map;
27 import java.util.TreeMap;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.cfg.Configuration;
32 import net.smartlab.web.BusinessObjectFactory;
33 import net.smartlab.web.Domain;
34
35 /**
36 * The Hibernate configuration file used must reside into the
37 * <code>META-INF</code> directory of the topmost packaging archive and should
38 * be named <code>smartweb.jar.hcf</code> or have the name of the JAR file
39 * containing the subclass followed by the <code>.hcf</code> suffix.
40 *
41 * @author rlogiacco
42 */
43 public class TomcatConfigurationStrategy implements FactoryConfigurationStrategy {
44
45 /**
46 * TODO documentation
47 */
48 private Map factories = new TreeMap();
49
50 /**
51 * Provides logging capabilities to the strategy.
52 */
53 protected final Log logger = LogFactory.getLog(TomcatConfigurationStrategy.class);
54
55
56 /**
57 * @see net.smartlab.web.config.FactoryConfigurationStrategy#getSessionFactory(net.smartlab.web.BusinessObjectFactory)
58 */
59 public SessionFactory getSessionFactory(BusinessObjectFactory bof) {
60 synchronized (BusinessObjectFactory.class) {
61 String archive = Domain.getLastArchiveName(bof.getClass());
62 URL file = Domain.getResource(bof.getClass(), new String[] {"/META-INF/" + archive + ".hcf",
63 "/META-INF/smartweb.jar.hcf", "/META-INF/hibernate.conf.xml"});
64 try {
65 if (!factories.containsKey(file.toString())) {
66 // Session factory not found in cache
67 logger.warn("getSessionFactory() - configure hibernate without JNDI");
68 SessionFactory factory = new Configuration().configure(file).buildSessionFactory();
69 factories.put(file.toString(), factory);
70 }
71 return (SessionFactory)factories.get(file.toString());
72 } catch (Exception e) {
73 logger.error("getSessionFactory() - error", e);
74 return null;
75 }
76 }
77 }
78 }