001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 *****************************************************************************/
008
009 package org.picocontainer.persistence.hibernate;
010
011 import java.io.Serializable;
012 import java.sql.Connection;
013 import java.util.Map;
014 import java.util.Set;
015
016 import javax.naming.NamingException;
017 import javax.naming.Reference;
018
019 import org.hibernate.HibernateException;
020 import org.hibernate.Interceptor;
021 import org.hibernate.SessionFactory;
022 import org.hibernate.StatelessSession;
023 import org.hibernate.cfg.Configuration;
024 import org.hibernate.classic.Session;
025 import org.hibernate.engine.FilterDefinition;
026 import org.hibernate.metadata.ClassMetadata;
027 import org.hibernate.metadata.CollectionMetadata;
028 import org.hibernate.stat.Statistics;
029 import org.picocontainer.Disposable;
030 import org.picocontainer.PicoCompositionException;
031
032 /**
033 * Session factory implementation that uses a delegate session factory
034 * created from configuration.
035 *
036 * @author Jose Peleteiro
037 * @author Mauro Talevi
038 */
039 @SuppressWarnings("serial")
040 public final class ConfigurableSessionFactory implements SessionFactory, Disposable {
041
042 private final SessionFactory delegate;
043
044 public ConfigurableSessionFactory(Configuration configuration) {
045 try {
046 delegate = configuration.buildSessionFactory();
047 } catch (HibernateException e) {
048 throw new PicoCompositionException(e);
049 }
050 }
051
052 public SessionFactory getDelegate() {
053 return delegate;
054 }
055
056 /** {@inheritDoc} **/
057 public void close() {
058 delegate.close();
059 }
060
061 /** {@inheritDoc} **/
062 @SuppressWarnings("unchecked")
063 public void evict(Class persistentClass) {
064 delegate.evict(persistentClass);
065 }
066
067 /** {@inheritDoc} **/
068 @SuppressWarnings("unchecked")
069 public void evict(Class persistentClass, Serializable id) {
070 delegate.evict(persistentClass, id);
071 }
072
073 /** {@inheritDoc} **/
074 public void evictCollection(String roleName) {
075 delegate.evictCollection(roleName);
076 }
077
078 /** {@inheritDoc} **/
079 public void evictCollection(String roleName, Serializable id) {
080 delegate.evictCollection(roleName, id);
081 }
082
083 /** {@inheritDoc} **/
084 public void evictEntity(String entityName) {
085 delegate.evictEntity(entityName);
086 }
087
088 /** {@inheritDoc} **/
089 public void evictEntity(String entityName, Serializable id) {
090 delegate.evictEntity(entityName, id);
091 }
092
093 /** {@inheritDoc} **/
094 public void evictQueries() {
095 delegate.evictQueries();
096 }
097
098 /** {@inheritDoc} **/
099 public void evictQueries(String cacheRegion) {
100 delegate.evictQueries(cacheRegion);
101 }
102
103 /** {@inheritDoc} **/
104 @SuppressWarnings("unchecked")
105 public Map getAllClassMetadata() {
106 return delegate.getAllClassMetadata();
107 }
108
109 /** {@inheritDoc} **/
110 @SuppressWarnings("unchecked")
111 public Map getAllCollectionMetadata() {
112 return delegate.getAllCollectionMetadata();
113 }
114
115 /** {@inheritDoc} **/
116 @SuppressWarnings("unchecked")
117 public ClassMetadata getClassMetadata(Class persistentClass) {
118 return delegate.getClassMetadata(persistentClass);
119 }
120
121 /** {@inheritDoc} **/
122 public ClassMetadata getClassMetadata(String entityName) {
123 return delegate.getClassMetadata(entityName);
124 }
125
126 /** {@inheritDoc} **/
127 public CollectionMetadata getCollectionMetadata(String roleName) {
128 return delegate.getCollectionMetadata(roleName);
129 }
130
131 /** {@inheritDoc} **/
132 public Session getCurrentSession() {
133 return delegate.getCurrentSession();
134 }
135
136 /** {@inheritDoc} **/
137 @SuppressWarnings("unchecked")
138 public Set getDefinedFilterNames() {
139 return delegate.getDefinedFilterNames();
140 }
141
142 /** {@inheritDoc} **/
143 public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {
144 return delegate.getFilterDefinition(filterName);
145 }
146
147 /** {@inheritDoc} **/
148 public Reference getReference() throws NamingException {
149 return delegate.getReference();
150 }
151
152 /** {@inheritDoc} **/
153 public Statistics getStatistics() {
154 return delegate.getStatistics();
155 }
156
157 /** {@inheritDoc} **/
158 public boolean isClosed() {
159 return delegate.isClosed();
160 }
161
162 /** {@inheritDoc} **/
163 public Session openSession() {
164 return delegate.openSession();
165 }
166
167 /** {@inheritDoc} **/
168 public Session openSession(Connection connection) {
169 return delegate.openSession(connection);
170 }
171
172 /** {@inheritDoc} **/
173 public Session openSession(Connection connection, Interceptor interceptor) {
174 return delegate.openSession(connection, interceptor);
175 }
176
177 /** {@inheritDoc} **/
178 public Session openSession(Interceptor interceptor) {
179 return delegate.openSession(interceptor);
180 }
181
182 /** {@inheritDoc} **/
183 public StatelessSession openStatelessSession() {
184 return delegate.openStatelessSession();
185 }
186
187 /** {@inheritDoc} **/
188 public StatelessSession openStatelessSession(Connection connection) {
189 return delegate.openStatelessSession(connection);
190 }
191
192 /**
193 * Clears the session factory when the container is disposed.
194 */
195 public void dispose() {
196 close();
197 }
198
199 }