| /** |
| * Copyright (c) 2012, 2015 Lunifera GmbH (Gross Enzersdorf), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - initial API and implementation |
| */ |
| package org.eclipse.osbp.runtime.tests; |
| |
| import javax.persistence.EntityManagerFactory; |
| |
| import org.knowhowlab.osgi.testing.assertions.BundleAssert; |
| import org.knowhowlab.osgi.testing.assertions.ServiceAssert; |
| import org.osgi.framework.Bundle; |
| import org.osgi.framework.BundleActivator; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| import org.osgi.framework.InvalidSyntaxException; |
| import org.osgi.framework.ServiceReference; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class Activator implements BundleActivator { |
| |
| @SuppressWarnings("unused") |
| private static final Logger logger = LoggerFactory |
| .getLogger(Activator.class); |
| |
| private static Activator instance; |
| public static BundleContext context; |
| |
| /** |
| * @return the instance |
| */ |
| public static Activator getInstance() { |
| return instance; |
| } |
| |
| @Override |
| public void start(BundleContext context) throws Exception { |
| Activator.context = context; |
| instance = this; |
| |
| BundleAssert.setDefaultBundleContext(context); |
| ServiceAssert.setDefaultBundleContext(context); |
| |
| // BundleHelper.ensureSetup(); |
| } |
| |
| /** |
| * Returns the bundle with the given id. |
| * |
| * @param id |
| * @return |
| */ |
| public static Bundle findBundle(String id) { |
| for (Bundle bundle : context.getBundles()) { |
| if (bundle.getSymbolicName().equals(id)) { |
| return bundle; |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Returns the entity manager factory or <code>null</code>. |
| * |
| * @return |
| */ |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| public static EntityManagerFactory getEMF() { |
| BundleContext context = FrameworkUtil.getBundle(Activator.class) |
| .getBundleContext(); |
| ServiceReference[] refs; |
| try { |
| refs = context.getServiceReferences( |
| EntityManagerFactory.class.getName(), |
| "(osgi.unit.name=dbDerby)"); |
| if (refs != null) { |
| return (EntityManagerFactory) context.getService(refs[0]); |
| } |
| } catch (InvalidSyntaxException e) { |
| throw new RuntimeException(e); |
| } |
| |
| return null; |
| } |
| |
| @Override |
| public void stop(BundleContext context) throws Exception { |
| Activator.context = null; |
| instance = null; |
| } |
| } |