2 * Hibernate, Relational Persistence for Idiomatic Java
4 * JBoss, Home of Professional Open Source
5 * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
6 * as indicated by the @authors tag. All rights reserved.
7 * See the copyright.txt in the distribution for a
8 * full listing of individual contributors.
10 * This copyrighted material is made available to anyone wishing to use,
11 * modify, copy, or redistribute it subject to the terms and conditions
12 * of the GNU Lesser General Public License, v. 2.1.
13 * This program is distributed in the hope that it will be useful, but WITHOUT A
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License,
17 * v.2.1 along with this distribution; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 package org.hibernate.osgitest;
23 import org.hibernate.Session;
24 import org.hibernate.SessionFactory;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.FrameworkUtil;
28 import org.osgi.framework.ServiceReference;
34 public class HibernateUtil {
36 private SessionFactory sf;
38 public Session getSession() {
39 return getSessionFactory().openSession();
42 private SessionFactory getSessionFactory() {
44 Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
45 // Could get this by wiring up OsgiTestBundleActivator as well.
46 BundleContext context = thisBundle.getBundleContext();
48 ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() );
49 sf = (SessionFactory) context.getService( sr );