/****************************************************************************/
/*									    */
/*  Copyright 2009, by Pocomatic Software, LLC.	All Rights Reserved.        */
/*									    */
/****************************************************************************/
#include "pocoapp.h"
#include <stdio.h>

#include "MovieLister.h"

int main(int argc, char** argv)
{
	POCO_AppEnv* env = POCO_AppContext::initDefaultAppEnv(argc, argv);

	//
	// setup the application (i.e. Movie lister) context
	//
	POCO_AppContext* ctx = POCO_AppContext::create("setup.xml", "file");
	if( ctx == NULL ) {
		printf("failure: %s\n", env->get_message());
		return -1;
	}

	//
	// retrieve the movie lister from it. We will use this
	// instance in this example.
	//
	MovieLister* lister = (MovieLister*)ctx->getBean(
				"movie-lister", "MovieLister");

	printf("[1] The movie lister component is deployed\n");

	//
	// calling an method on the movie lister without loading any 
	// movie finder bundle. the expect result will be a message 
	// saying movie finder isn't available.
	//
	printf("\n[2] Listing movies directed by Ron Howard\n");
	lister->listMoviesDirectedBy("Ron Howard");

	//
	// loading a bundle and initialize it. this bundle contains the
	// the movie finder 1. As the result of initialize singletons of
	// this bundle, this movie finder service will be registered to 
	// the service registry. 
	//
	POCO_AppContext* ctx1 = POCO_AppContext::create("bundle-1.xml", "file");
	if( ctx1 == NULL ) {
		printf("failure: %s\n", env->get_message());
		return -1;
	}
	printf("\n[3] Installing the MovieFinder service bundle-1\n");
	ctx1->initSingletons();

	//
	// refresh our application context. This will have its dependent
	// service(s) been non-invasively sync'ed with the service registry.
	// Namely, the just registered movie finder 1 from bundle 1 
	// will be injected to the movie lister.
	//
	printf("\n[4] Rewiring services to components\n");
	ctx->getBean("poco-dynamic-resync");

	//
	// calling the movie lister again. Now, it will use the service
	// movie finder in the bundle 1 (i.e. movie finder 1).
	//
	printf("\n[5] Listing movies directed by Ron Howard\n");
	lister->listMoviesDirectedBy("Ron Howard");

	//
	// Now we load and initialize bundle 2. this will have the
	// movie finder in the service registry been replaced by the
	// new movie finder 2 in the new bundle.
	//
	POCO_AppContext* ctx2 = POCO_AppContext::create("bundle-2.xml", "file");
	if( ctx2 == NULL ) {
		printf("failure: %s\n", env->get_message());
		return -1;
	}
	printf("\n[6] Installing the MovieFinder service bundle-2\n");
	ctx2->initSingletons();

	//
	// refresh our application context. This will have its dependet
	// service been non-invasively sync'ed with the latest service
	// registration. Because we have just loaded bundle 2, the movie
	// finder 2 in that bundle will be injected to our existing
	// movie lister
	//
	printf("\n[7] Rewiring services to components\n");
	ctx->getBean("poco-dynamic-resync");

	//
	// calling the movie lister again. Now, it will use the movie finder
	// in bundle 2 (i.e. movie finder 2).
	//
	printf("\n[8] Listing movies directed by Ron Howard\n");
	lister->listMoviesDirectedBy("Ron Howard");
	return 0;
}

