public void StartActivator(IBundleContext context)
        {
            var path = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
            var name = AssemblyName.GetAssemblyName(path);

            context.ChangeBundleState(name.Name, BundleState.Starting);

            // load the assembly in the AppDomain
            var assembly = AppDomain.CurrentDomain.Load(name);

            // get the meta-data of the bundle
            var activatorAttribute = (BundleActivatorAttribute)assembly.GetCustomAttributes(typeof(BundleActivatorAttribute), false).FirstOrDefault();
            var dependencyAttributes = assembly.GetCustomAttributes(typeof(BundleDependencyAttribute), false).Cast<BundleDependencyAttribute>();

            // if there's some dependencies, load them
            if (dependencyAttributes != null)
            {
                foreach (var bundle in dependencyAttributes)
                {
                    var existingBundle = context.GetBundle(bundle.Name);
                    if (existingBundle == null)
                    {
                        throw new BundleNotFoundException(bundle.Name, bundle.Version);
                    }
                    else
                    {
                        if (existingBundle.Version != bundle.Version)
                        {
                            throw new InvalidBundleVersionException(bundle.Name, bundle.Version, existingBundle.Version);
                        }
                        else if (existingBundle.State == BundleState.Installed)
                        {
                            BundleController.Start(context, bundle.Name);
                        }
                        else if (existingBundle.State == BundleState.Resolved)
                        {
                            BundleController.Start(context, bundle.Name);
                        }
                        else if (existingBundle.State == BundleState.Starting)
                        {
                            BundleController.WaitFor(context, bundle.Name, BundleState.Active);
                        }
                        else if (existingBundle.State == BundleState.Stopping)
                        {
                            BundleController.WaitFor(context, bundle.Name, BundleState.Resolved);
                            BundleController.Start(context, bundle.Name);
                        }
                        else if (existingBundle.State == BundleState.Uninstalled)
                            throw new BundleIsBeingUninstalledException(existingBundle.Name, existingBundle.Version);

                        logger.InfoFormat("Loading dependency: {0}", bundle);
                    }
                }
            }

            // create a new instance of the activator
            var activatorType = Type.GetType(activatorAttribute.Activator.AssemblyQualifiedName);
            var ctor = activatorType.GetConstructor(new Type[0]);
            this.instance = (IBundleActivator)ctor.Invoke(new object[0]);

            // start the bundle
            ThreadPool.QueueUserWorkItem(new WaitCallback((o) => ((IBundleActivator)o).Start()), this.instance);

            context.ChangeBundleState(name.Name, BundleState.Active);
        }
 public static void Stop(IBundleContext context, string bundleName)
 {
     var bundle = context.GetBundle(bundleName);
     if (bundle == null)
     {
         logger.InfoFormat("Application {0} not loaded", bundleName);
         return;
     }
     context.ChangeBundleState(bundle.Name, BundleState.Stopping);
     bundle.Boostrap.StopActivator(context);
     context.ChangeBundleState(bundle.Name, BundleState.Resolved);
     AppDomain.Unload(bundle.AppDomain);
 }