Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2010 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 
 package org.eclipse.osgi.framework.internal.core;
 
 import java.net.URL;
 
 public class BundleHost extends AbstractBundle {
 	public static final int LAZY_TRIGGER = 0x40000000;
The BundleLoader proxy; a lightweight object that acts as a proxy to the BundleLoader and allows lazy creation of the BundleLoader object
 
 	private BundleLoaderProxy proxy;

The BundleContext that represents this Bundle and all of its fragments
 
 	protected BundleContextImpl context;

The List of BundleFragments
 
 	protected BundleFragment[] fragments;
 
 	public BundleHost(BundleData bundledataFramework frameworkthrows BundleException {
 		super(bundledataframework);
 		 = null;
 		 = null;
 	}

Load the bundle.
 
 	protected void load() {
 		if (. && .) {
 			if (( & ()) == 0) {
 				Debug.println("Bundle.load called when state != INSTALLED: " + this); //$NON-NLS-1$
 				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
 			}
 			if ( != null) {
 				Debug.println("Bundle.load called when proxy != null: " + this); //$NON-NLS-1$
 				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
 			}
 		}
 
 		if (.isActive()) {
 			if (sm != null && . != null) {
 			}
 		}
 		 = null;
 	}

Reload from a new bundle. This method must be called while holding the bundles lock.

Parameters:
newBundle Dummy Bundle which contains new data.
Returns:
true if an exported package is "in use". i.e. it has been imported by a bundle
 
 	protected boolean reload(AbstractBundle newBundle) {
 		if (. && .) {
 			if (( & ( | )) == 0) {
 				Debug.println("Bundle.reload called when state != INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
 				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
 			}
 		}
 
 		boolean exporting = false;
 
 		if (.isActive()) {
 			if ( == ) {
 				exporting = curProxy.inUse();
 				if (exporting) {
 					// add the bundle data to the list of removals
 					// make sure the BundleLoader is created.
 				} else
 					BundleLoader.closeBundleLoader();
 				 = null;
				 = null;
			}
else {
			/* close the outgoing jarfile */
			try {
catch (IOException e) {
				// Do Nothing
			}
		}
		this. = newBundle.bundledata;
		// create a new domain for the bundle because its signers/symbolic-name may have changed
		if (.isActive() && System.getSecurityManager() != null && . != null)
		return (exporting);
	}

Refresh the bundle. This is called by Framework.refreshPackages. This method must be called while holding the bundles lock.
	protected void refresh() {
			if (( & ( |  | )) == 0) {
				Debug.println("Bundle.reload called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
			}
		}
		if ( == ) {
			BundleLoader.closeBundleLoader();
			 = null;
			 = null;
			// Do not publish UNRESOLVED event here.  This is done by caller 
			// to resolve if appropriate.
		}
	}

Unload the bundle. This method must be called while holding the bundles lock.

Returns:
true if an exported package is "in use". i.e. it has been imported by a bundle
	protected boolean unload() {
			if (( & ( |  | )) == 0) {
				Debug.println("Bundle.unload called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); //$NON-NLS-1$
				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
			}
		}
		boolean exporting = false;
			if ( == ) {
				exporting = curProxy.inUse();
				if (exporting) {
					// add the bundle data to the list of removals
					// make sure the BundleLoader is created.
else
					BundleLoader.closeBundleLoader();
				 = null;
				 = null;
				 = null;
			}
		}
		if (!exporting) {
			try {
catch (IOException e) { // Do Nothing.
			}
		}
		return (exporting);
	}
		// check to see if the bundle is resolved
		if (!isResolved()) {
			if (!..resolveBundles(new Bundle[] {this})) {
				return null;
			}
		}
			if (( & ( |  |  | )) == 0) {
				Debug.println("Bundle.checkLoader() called when state != STARTING | ACTIVE | STOPPING | RESOLVED: " + this); //$NON-NLS-1$ 
				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
			}
		}
		if (loader == null) {
				Debug.println("Bundle.checkLoader() called when loader == null: " + this); //$NON-NLS-1$ 
				Debug.printStackTrace(new Exception("Stack trace")); //$NON-NLS-1$
			}
			return null;
		}
		return loader;
	}

This method loads a class from the bundle.

Parameters:
name the name of the desired Class.
checkPermission indicates whether a permission check should be done.
Returns:
the resulting Class
Throws:
java.lang.ClassNotFoundException if the class definition was not found.
	protected Class loadClass(String nameboolean checkPermissionthrows ClassNotFoundException {
		if (checkPermission) {
			try {
catch (SecurityException e) {
				throw new ClassNotFoundException(namee);
			}
		}
		if (loader == null)
		try {
			return (loader.loadClass(name));
catch (ClassNotFoundException e) {
			// this is to support backward compatibility in eclipse
			// we always attempted to start a bundle even if the class was not found
			if (!(e instanceof StatusException) && (.getStatus() & .) != 0 && !testStateChanging(Thread.currentThread()))
				try {
					// only start the bundle if this is a simple CNFE
catch (BundleException be) {
				}
			throw e;
		}
	}

Find the specified resource in this bundle. This bundle's class loader is called to search for the named resource. If this bundle's state is INSTALLED, then only this bundle will be searched for the specified resource. Imported packages cannot be searched when a bundle has not been resolved.

Parameters:
name The name of the resource. See java.lang.ClassLoader.getResource for a description of the format of a resource name.
Returns:
a URL to the named resource, or null if the resource could not be found or if the caller does not have the AdminPermission, and the Java Runtime Environment supports permissions.
Throws:
java.lang.IllegalStateException If this bundle has been uninstalled.
	public URL getResource(String name) {
		BundleLoader loader = null;
		try {
catch (SecurityException ee) {
			return null;
		}
		loader = checkLoader();
		if (loader == null)
			return null;
		return (loader.findResource(name));
	}
	public Enumeration getResources(String namethrows IOException {
		BundleLoader loader = null;
		try {
catch (SecurityException ee) {
			return null;
		}
		loader = checkLoader();
		if (loader == null)
			return null;
		Enumeration result = loader.getResources(name);
		if (result != null && result.hasMoreElements())
			return result;
		return null;
	}

Internal worker to start a bundle.

Parameters:
options the start options
	protected void startWorker(int optionsthrows BundleException {
		if ((options & ) == 0) {
				new Exception("A persistent start has been called on bundle: " + getBundleData()).printStackTrace(); //$NON-NLS-1$
		}
		if (!. || ( & ) != 0)
			return;
			if ((options & ) == 0 && (options & ) != 0) {
				// throw exception if this is a transient start
				// Use a StatusException to indicate to the lazy starter that this should result in a warning
			}
			return;
		}
		if ( == ) {
		}
		if ((options & ) != 0 && (.getStatus() & .) != 0) {
			// the bundle must use the activation policy here.
			if (( & ) != 0) {
				// now we must publish the LAZY_ACTIVATION event and return
				// release the state change lock before sending lazy activation event (bug 258659)
			}
			return;
		}
			Debug.println("Bundle: Active sl = " + ..getStartLevel() + "; Bundle " + getBundleId() + " sl = " + getStartLevel()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
		if ((options & ) != 0) {
			if (( & ) != 0) {
				// Should publish the lazy activation event here before the starting event
				// This can happen if another bundle in the same start-level causes a class load from the lazy start bundle.
				// release the state change lock before sending lazy activation event (bug 258659)
				if ( != ) {
					// while firing the LAZY_ACTIVATION event some one else caused the bundle to transition
					// out of STARTING.  This could have happened because some listener called start on the bundle
					// or another class load could have caused the start trigger to get fired again.
					return;
				}
			}
		}
		//STARTUP TIMING Start here		
		long start = 0;
		if (.) {
			if (bundleStats != null)
				start = System.currentTimeMillis();
				..println("Starting " + getSymbolicName()); //$NON-NLS-1$
			}
		}
		try {
					Debug.println("->started " + this); //$NON-NLS-1$
				}
				// release the state change lock before sending lazy activation event (bug 258659)
			}
catch (BundleException e) {
			// we must fire the stopping event
			 = null;
			// if this is a lazy start bundle that fails to start then
			// we must fire the stopped event
			throw e;
finally {
			if (.) {
				if (bundleStats != null)
					..println("End starting " + getSymbolicName() + " " + (System.currentTimeMillis() - start)); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		if ( == ) {
			 = null;
		}
	}

	protected void startHook() throws BundleException {
		// do nothing by default
	}
	protected boolean readyToResume() {
		// Return false if the bundle is not at the correct start-level
			return false;
		int status = .getStatus();
		// Return false if the bundle is not persistently marked for start
		if ((status & .) == 0)
			return false;
		if ((status & .) == 0 || (status & .) == 0 || isLazyTriggerSet())
			return true;
		if (!isResolved()) {
				// should never transition from UNRESOLVED -> STARTING
				return false;
		}
		// now we can publish the LAZY_ACTIVATION event
		// release the state change lock before sending lazy activation event (bug 258659)
		return false;
	}
	private synchronized boolean isLazyTriggerSet() {
		if ( == null)
			return false;
		return loader != null ? loader.isLazyTriggerSet() : false;
	}

Create a BundleContext for this bundle.

Returns:
BundleContext for this bundle.
		return (new BundleContextImpl(this));
	}

Return the current context for this bundle.

Returns:
BundleContext for this bundle.
	protected synchronized BundleContextImpl getContext() {
		if ( == null) {
			// only create the context if we are starting, active or stopping
			// this is so that SCR can get the context for lazy-start bundles
			if (( & ( |  | )) != 0)
		}
		return ();
	}

Internal worker to stop a bundle.

Parameters:
options the stop options
	protected void stopWorker(int optionsthrows BundleException {
		if ((options & ) == 0) {
				new Exception("A persistent start has been called on bundle: " + getBundleData()).printStackTrace(); //$NON-NLS-1$
		}
			if (( & ( |  | )) != 0) {
				return;
			}
			if (.) {
				if (bundleStats != null)
			}
			try {
				// context may be null if a lazy-start bundle is STARTING
				if ( != null)
finally {
				if ( != null) {
					 = null;
				}
					Debug.println("->stopped " + this); //$NON-NLS-1$
				}
				if (.) {
					if (bundleStats != null)
				}
			}
		}
	}

	protected void stopHook() throws BundleException {
		// do nothing
	}

Provides a list of org.osgi.framework.ServiceReferences for the services registered by this bundle or null if the bundle has no registered services.

The list is valid at the time of the call to this method, but the framework is a very dynamic environment and services can be modified or unregistered at anytime.

		if ( == null) {
			return null;
		}
	}

Provides a list of org.osgi.framework.ServiceReferences for the services this bundle is using, or null if the bundle is not using any services. A bundle is considered to be using a service if the bundle's use count for the service is greater than zero.

The list is valid at the time of the call to this method, but the framework is a very dynamic environment and services can be modified or unregistered at anytime.

Returns:
An array of org.osgi.framework.ServiceReference or null.
Throws:
java.lang.IllegalStateException If the bundle has been uninstalled.
See also:
org.osgi.framework.ServiceReference
		if ( == null) {
			return null;
		}
	}
		synchronized (.) {
			if ( == null)
				return null;
			System.arraycopy(, 0, result, 0, result.length);
			return result;
		}
	}

Attaches a fragment to this BundleHost. Fragments must be attached to the host by ID order. If the ClassLoader of the host is already created then the fragment must be attached to the host ClassLoader

Parameters:
fragment The fragment bundle to attach return true if the fragment successfully attached; false if the fragment could not be logically inserted at the end of the fragment chain.
	protected void attachFragment(BundleFragment fragmentthrows BundleException {
		// do not force the creation of the bundle loader here
		// If the Host ClassLoader exists then we must attach
		// the fragment to the ClassLoader.
		if (loader != null)
			loader.attachFragment(fragment);
		if ( == null) {
			 = new BundleFragment[] {fragment};
else {
			boolean inserted = false;
			// We must keep our fragments ordered by bundle ID; or 
			// install order.
			BundleFragment[] newFragments = new BundleFragment[. + 1];
			for (int i = 0; i < .i++) {
				if (fragment == [i])
					return// this fragment is already attached
				if (!inserted && fragment.getBundleId() < [i].getBundleId()) {
					// if the loader has already been created
					// then we cannot attach a fragment into the middle
					// of the fragment chain.
					if (loader != null) {
					}
					newFragments[i] = fragment;
					inserted = true;
				}
				newFragments[inserted ? i + 1 : i] = [i];
			}
			if (!inserted)
				newFragments[newFragments.length - 1] = fragment;
			 = newFragments;
		}
	}
		return curProxy == null ? null : curProxy.getBundleLoader();
	}
	public synchronized BundleLoaderProxy getLoaderProxy() {
		if ( != null)
			return ;
		BundleDescription bundleDescription = getBundleDescription();
		if (bundleDescription == null)
			return null;
		 = new BundleLoaderProxy(thisbundleDescription);
		bundleDescription.setUserObject();
		return ;
	}

Gets the class loader for the host bundle. This may end up creating the bundle class loader if it was not already created. A null value may be returned if the bundle is not resolved.

Returns:
the bundle class loader or null if the bundle is not resolved.
		if (sm != null)
			sm.checkPermission(new RuntimePermission("getClassLoader")); //$NON-NLS-1$
		BundleLoader loader = curProxy == null ? null : curProxy.getBundleLoader();
		BundleClassLoader bcl = loader == null ? null : loader.createClassLoader();
		return (bcl instanceof ClassLoader) ? (ClassLoaderbcl : null;
	}
New to GrepCode? Check out our FAQ X