1 /******************************************************************************* 2 * Copyright (c) 2011 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.equinox.http.servlet.tests.bundle; 15 16 import org.osgi.framework.Bundle; 17 import org.osgi.framework.BundleContext; 18 import org.osgi.framework.BundleException; 19 20 /* 21 * The BundleAdvisor is responsible for starting and stopping bundles, given 22 * a Bundle-SymbolicName. It is an OSGi dependent class. 23 */ 24 public class BundleAdvisor extends Object { 25 private final BundleContext bundleContext; 26 BundleAdvisor(BundleContext bundleContext)27 public BundleAdvisor(BundleContext bundleContext) { 28 super(); 29 if (bundleContext == null) 30 throw new IllegalArgumentException("bundleContext must not be null"); //$NON-NLS-1$ 31 this.bundleContext = bundleContext; 32 } 33 getBundle(String symbolicName)34 private Bundle getBundle(String symbolicName) { 35 if (symbolicName == null) 36 throw new IllegalArgumentException("symbolicName must not be null"); //$NON-NLS-1$ 37 BundleContext context = getBundleContext(); 38 Bundle[] bundles = context.getBundles(); 39 for (Bundle bundle : bundles) { 40 String bsn = bundle.getSymbolicName(); 41 boolean match = symbolicName.equals(bsn); 42 if (match) { 43 return bundle; 44 } 45 } 46 throw new IllegalArgumentException("Failed to find bundle: " + symbolicName); //$NON-NLS-1$ 47 } 48 getBundleContext()49 private BundleContext getBundleContext() { 50 return bundleContext; 51 } 52 startBundle(String symbolicName)53 public void startBundle(String symbolicName) throws BundleException { 54 Bundle bundle = getBundle(symbolicName); 55 int state = bundle.getState(); 56 if (state == Bundle.ACTIVE) { 57 return; 58 } 59 bundle.start(Bundle.START_TRANSIENT); 60 } 61 stopBundle(String symbolicName)62 public void stopBundle(String symbolicName) throws BundleException { 63 Bundle bundle = getBundle(symbolicName); 64 bundle.stop(); 65 } 66 } 67