As you can see from the BundleHandler class I'm currently only comparing the
bundle state with the expected state after starting / stopping a bundle.
This evening I will add such a check to the methods, which install /
uninstall a bundle as well and report the results ...

2009/9/17 Karl Pauls <[email protected]>

> On Wed, Sep 16, 2009 at 9:23 PM, Markus Michel
> <[email protected]> wrote:
> > Hi Karl!
> >
> > After catching Throwable and Exception as well my output looks like this:
> >
> > trying to stop bundle 7 <- vehicle 1055293 left the scenario => reset of
> > framework 1
> > bundle 7 was stopped <- reset of application
> > try to uninstall bundle 7
> > bundle 7 was uninstalled
> > trying to install
> > file:/home/markus/bundles/application/VehicleBeaconingApp_1.0.0.jar
> > file:/home/markus/bundles/application/VehicleBeaconingApp_1.0.0.jar was
> > installed
> > vehicle id 1055293 successfully unbounded from framework 1
> > trying to start bundle 7 <- new vehicle tries to bind to framework 1 =>
> > start of application
> > exception java.lang.IllegalStateException: Cannot start an uninstalled
> > bundle.
> > bundle 7 was started
> > error: bundle 7 is not running
> >
> > Mhhh, the exception doesn't really make sense, because the application
> > (bundle 7) should be in the state INSTALLED, when the vehicle tries to
> start
> > it. Maybe the process of stopping / uninstalling and reinstalled the
> bundles
> > takes internally longer than I exspect from the output?!?
>
> Can you show us what it is you do exactly? It sounds like you are not
> waiting long enough ... how do you make sure the bundle has been
> uninstalled? One way might be to call refresh after uninstall and wait
> for the refreshed event...
>
> regards,
>
> Karl
>
> > BTW: Currently I'm not sure if the new vehicle contacts my framework
> handler
> > using the same thread. So maybe I have to synchronize same parts of my
> code,
> > too ... I will think about that more detailed tomorrow ...
> >
> > BR,
> >
> > Markus
> >
> > 2009/9/16 Karl Pauls <[email protected]>
> >
> >> Can't you catch Throwable as well and give us a stacktrace of whatever
> >> exception/error gets thrown by the start() method?
> >>
> >> regards,
> >>
> >> Karl
> >>
> >> On Wed, Sep 16, 2009 at 3:48 PM, Markus Michel
> >> <[email protected]> wrote:
> >> > Hi there!
> >> >
> >> > Here's a short explanation, how my program is working:
> >> >
> >> > Inside my program I using a FrameworkHandler class to control a pool
> of
> >> OSGi
> >> > instances, which represents vehicles of a traffic simulation, For
> every
> >> > instances I using three BundleHandler ( system, user, application) to
> >> > install / uninstall / start / stop a list of bundles. When the program
> is
> >> > started the pool is initialized and all system (OSGi services, Shell
> ..)
> >> and
> >> > user (my services) bundles are installed and started. Everytime (if
> there
> >> is
> >> > a free instance) there is a new vehicle, my program binds it to an
> >> instance
> >> > of the pool and installs / started an application, which should be
> >> running
> >> > on this vehicle using the application bundle handler. If the vehicles
> >> leaves
> >> > the scenario, the program resets the application by stopping,
> uninstalled
> >> > and reinstalling the corresponding bundles. Afterwards a new vehicle
> can
> >> be
> >> > bound the same instance.
> >> >
> >> > My problem is that currently my program seems to hang into a loop,
> when
> >> the
> >> > application bundle is started after it was reset. By doing some
> printouts
> >> I
> >> > know that the problem occurs into that code block:
> >> >
> >> >            // start bundle
> >> >            try
> >> >            {
> >> >                bundle.start();
> >> >            }
> >> >            catch (BundleException e)
> >> >            {
> >> >                System.out.println("error: could not start bundle " +
> >> > bundle.getBundleId());
> >> >                System.out.println(e);
> >> >                System.exit(1);
> >> >            }
> >> >
> >> > I'm not sure what really happens, because there is no bundle
> exception,
> >> > although the error occurs inside the call of the start method. Isn't
> it
> >> > possible to "reset" an OSGi bundle?
> >> >
> >> > Because I though that my problem might be a bug of the framework i
> >> already
> >> > upgraded my program to the new felix version 2.0.0, but the problem
> still
> >> > appears ...
> >> >
> >> > BR,
> >> >
> >> > Markus
> >> >
> >>
> >>
> >>
> >> --
> >> Karl Pauls
> >> [email protected]
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [email protected]
> >> For additional commands, e-mail: [email protected]
> >>
> >>
> >
>
>
>
> --
> Karl Pauls
> [email protected]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>


-- 
Mit freundlichen Grüßen,

Markus Michel
package slaveControl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

import util.PropertiesReader;

public class BundleHandler
{
	private String					bundlePath		= "";
	private final ArrayList<String>	fileList		= new ArrayList<String>();
	private ListIterator<String>	fileIter		= null;
	private final ArrayList<Bundle>	bundleList		= new ArrayList<Bundle>();
	private ListIterator<Bundle>	bundleIter		= null;
	private BundleContext			bundleContext	= null;
	private boolean					active			= false;
	private boolean					installed		= false;
	
	private int						mode			= 0;
	
	public BundleHandler(int mode, PropertiesReader propertiesReader)
	{
		this.mode = mode;
		
		// set bundle path
		if (mode == 0)
		{
			this.bundlePath = propertiesReader.getBundlePath() + "/system";
		}
		else if (mode == 1)
		{
			this.bundlePath = propertiesReader.getBundlePath() + "/user";
		}
		else if (mode == 2)
		{
			this.bundlePath = propertiesReader.getBundlePath() + "/application";
		}
		else
		{
			System.out.println("error: given bundle handler mode is not valid");
			System.exit(1);
		}
		
		// get bundle names and their order
		readBundleOrderFile();
	}
	
	public void setBundleContext(BundleContext bundleContext)
	{
		this.bundleContext = bundleContext;
	}
	
	public void installBundles()
	{
		// check if bundle context is available
		if (bundleContext == null)
		{
			System.out.println("error: could not install bundles because bundleContext is null");
			System.exit(1);
		}
		
		// install all bundles which are referenced with the bundleOrder file
		String file = "";
		Bundle bundle = null;
		
		fileIter = fileList.listIterator();
		
		while (fileIter.hasNext())
		{
			file = "file:" + bundlePath + "/" + fileIter.next();
			
			if (mode == 2)
			{
				System.out.println("trying to install " + file);
			}
			
			try
			{
				bundle = bundleContext.installBundle(file);
			}
			catch (BundleException e)
			{
				System.out.println("error: could not install bundle '" + file + "'");
				System.out.println(e);
				System.exit(1);
			}
			
			// add bundle to bundle list
			bundleList.add(bundle);
			
			if (mode == 2)
			{
				System.out.println(file + " was installed");
			}
		}
		
		// set installed flag
		installed = true;
	}
	
	public void uninstallBundles()
	{
		// check if bundle context is available
		if (bundleContext == null)
		{
			System.out.println("error: could not uninstall bundles because bundleContext is null");
			System.exit(1);
		}
		
		// uninstall all bundles which are referenced within the bundleList
		bundleIter = bundleList.listIterator();
		Bundle bundle = null;
		int bundleState = 0;
		
		while (bundleIter.hasNext())
		{
			bundle = bundleIter.next();
			
			if (mode == 2)
			{
				System.out.println("try to uninstall bundle " + bundle.getBundleId());
			}
			
			// get bundle state
			bundleState = bundle.getState();
			
			// check if bundle is running
			if (bundleState == Bundle.ACTIVE)
			{
				// stop bundle
				try
				{
					bundle.stop();
				}
				catch (BundleException e)
				{
					System.out.println("error: could not stop bundle " + bundle.getBundleId());
					System.out.println(e);
					System.exit(1);
				}
			}
			
			// uninstall bundle
			try
			{
				bundle.uninstall();
			}
			catch (BundleException e)
			{
				System.out.println("error: could not uninstall bundle " + bundle.getBundleId());
				System.out.println(e);
				System.exit(1);
			}
			
			if (mode == 2)
			{
				System.out.println("bundle " + bundle.getBundleId() + "was uninstalled");
			}
		}
		
		// unset installed flag
		installed = false;
	}
	
	public void startBundles()
	{
		// check if bundle context is available
		if (bundleContext == null)
		{
			System.out.println("error: could not start bundles because bundleContext is null");
			System.exit(1);
		}
		
		// start all bundles which are referenced within the bundleList
		bundleIter = bundleList.listIterator();
		Bundle bundle = null;
		int bundleState = 0;
		
		if (mode == 2)
		{
			System.out.println("before");
		}
		
		while (bundleIter.hasNext())
		{
			bundle = bundleIter.next();
			
			if (mode == 2)
			{
				System.out.println("trying to start bundle " + bundle.getBundleId());
			}
			
			// start bundle
			try
			{
				bundle.start();
			}
			catch (BundleException e)
			{
				System.out.println("error: could not start bundle " + bundle.getBundleId());
				System.out.println(e);
				System.exit(1);
			}
			catch (Exception e2)
			{
				System.out.println("exception " + e2);
			}
			catch (Throwable f)
			{
				System.out.println("throwable " + f);
			}
			
			if (mode == 2)
			{
				System.out.println("bundle " + bundle.getBundleId() + " was started");
			}
			
			// get bundle state
			bundleState = bundle.getState();
			
			// check if bundle is really running
			if (bundleState != Bundle.ACTIVE)
			{
				System.out.println("error: bundle " + bundle.getBundleId() + " is not running");
				System.exit(1);
			}
		}
		
		if (mode == 2)
		{
			System.out.println("after");
		}
		
		// set active flag
		active = true;
	}
	
	public void stopBundles()
	{
		// check if bundle context is available
		if (bundleContext == null)
		{
			System.out.println("error: could not stop bundles because bundleContext is null");
			System.exit(1);
		}
		
		// stop all bundles which are referenced within the bundleList in inversed order
		Bundle bundle = null;
		int bundleState = 0;
		
		for (int i = bundleList.size() - 1; i >= 0; i--)
		{
			bundle = bundleList.get(i);
			
			if (mode == 2)
			{
				System.out.println("trying to stop bundle " + bundle.getBundleId());
			}
			
			// stop bundle
			try
			{
				bundle.stop();
			}
			catch (BundleException e)
			{
				System.out.println("error: could not stop bundle " + bundle.getBundleId());
				System.out.println(e);
				System.exit(1);
			}
			
			// get bundle state
			bundleState = bundle.getState();
			
			// check if bundle is really stopped
			if (bundleState != Bundle.RESOLVED)
			{
				System.out.println("error: bundle " + bundle.getBundleId() + " was not stopped");
				System.exit(1);
			}
			
			if (mode == 2)
			{
				System.out.println("bundle " + bundle.getBundleId() + " was stopped");
			}
		}
		
		// unset active flag
		active = false;
	}
	
	public void resetBundles()
	{
		// stop bundles
		stopBundles();
		
		// uninstall bundles
		uninstallBundles();
		
		// install bundles
		installBundles();
	}
	
	public void printBundleNames()
	{
		fileIter = fileList.listIterator();
		
		while (fileIter.hasNext())
		{
			System.out.println("position: " + fileIter.nextIndex() + ", bundle name: " + fileIter.next());
		}
	}
	
	public int getBundleCount()
	{
		return fileList.size();
	}
	
	public ArrayList<Bundle> getBundleList()
	{
		return bundleList;
	}
	
	private void readBundleOrderFile()
	{
		File dir = new File(bundlePath);
		File file = new File(dir, "bundleOrder");
		
		// try to open bundleOrder file
		FileReader fileReader = null;
		try
		{
			fileReader = new FileReader(file);
		}
		catch (FileNotFoundException e)
		{
			System.out.println("error: bundleOrder file is not placed in the directory " + dir.getAbsolutePath());
			System.exit(0);
		}
		
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String line = "";
		
		try
		{
			while (bufferedReader.ready())
			{
				// read a line
				line = bufferedReader.readLine();
				
				// store bundle name inside the bundleList
				fileList.add(line);
			}
		}
		catch (IOException e)
		{
			System.out.println("error: could not read bundleOrder file");
			System.exit(0);
		}
	}
	
	public boolean isActive()
	{
		return active;
	}
	
	public boolean isInstalled()
	{
		return installed;
	}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to