modules/host-android/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java [126:387]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        MessageFactory messageFactory = factories.getFactory(MessageFactory.class);

        // Get proxy factory
        ProxyFactoryExtensionPoint proxyFactories = registry.getExtensionPoint(ProxyFactoryExtensionPoint.class);  
        proxyFactory = new ExtensibleProxyFactory(proxyFactories); 

        // Create model factories
        assemblyFactory = new RuntimeAssemblyFactory();
        factories.addFactory(assemblyFactory);
        PolicyFactory policyFactory = new DefaultPolicyFactory();
        factories.addFactory(policyFactory);
        
        // Load the runtime modules
        modules = loadModules(registry);
        
        // Start the runtime modules
        startModules(registry, modules);
        
        SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class);
        IntentAttachPointTypeFactory intentAttachPointTypeFactory = new DefaultIntentAttachPointTypeFactory();
        factories.addFactory(intentAttachPointTypeFactory);
        ContributionFactory contributionFactory = factories.getFactory(ContributionFactory.class);
        
        // Create a monitor
        MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class);
        
        if (monitorFactory != null){
            monitor = monitorFactory.createMonitor();
        } else {
            monitorFactory = new DefaultMonitorFactoryImpl();
            monitor = monitorFactory.createMonitor();
            utilities.addUtility(monitorFactory);
            //logger.fine("No MonitorFactory is found on the classpath.");
        }
        
        // Create a contribution service
        policyDefinitions = new ArrayList<SCADefinitions>();
        policyDefinitionsResolver = new DefaultModelResolver();
        contributionService = ReallySmallRuntimeBuilder.createContributionService(classLoader,
                                                                                  registry,
                                                                                  contributionFactory,
                                                                                  assemblyFactory,
                                                                                  policyFactory,
                                                                                  mapper,
                                                                                  policyDefinitions,
                                                                                  policyDefinitionsResolver,
                                                                                  monitor);
        
        // Create the ScopeRegistry
        scopeRegistry = ReallySmallRuntimeBuilder.createScopeRegistry(registry); 
        
        // Create a composite activator
        compositeActivator = ReallySmallRuntimeBuilder.createCompositeActivator(registry,
                                                                                assemblyFactory,
                                                                                messageFactory,
                                                                                scaBindingFactory,
                                                                                mapper,
                                                                                proxyFactory,
                                                                                scopeRegistry,
                                                                                workScheduler);

        // Load the definitions.xml
        loadSCADefinitions();
        
        if (logger.isLoggable(Level.FINE)) {
            long end = System.currentTimeMillis();
            logger.fine("The tuscany runtime is started in " + (end - start) + " ms.");
        }
    }
    
    public void stop() throws ActivationException {
    	long start = System.currentTimeMillis();

        // Stop the runtime modules
        stopModules(registry, modules);

        // Stop and destroy the work manager
        workScheduler.destroy(); 

        // Cleanup
        modules = null;
        registry = null;
        assemblyFactory = null;
        contributionService = null;
        compositeActivator = null;
        workScheduler = null;
        scopeRegistry = null;
        
        if (logger.isLoggable(Level.FINE)) {
            long end = System.currentTimeMillis();
            logger.fine("The tuscany runtime is stopped in " + (end - start) + " ms.");
        }
    }
    
    public void buildComposite(Composite composite) throws CompositeBuilderException {
        //Get factory extension point
        ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class);
        SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class);
        IntentAttachPointTypeFactory intentAttachPointTypeFactory = factories.getFactory(IntentAttachPointTypeFactory.class);
        EndpointFactory endpointFactory = factories.getFactory(EndpointFactory.class);        
        UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class);
        InterfaceContractMapper mapper = utilities.getUtility(InterfaceContractMapper.class);
        DocumentBuilderFactory documentBuilderFactory = factories.getFactory(DocumentBuilderFactory.class);
        TransformerFactory transformerFactory = factories.getFactory(TransformerFactory.class);
        
        //Create a composite builder
        SCADefinitions aggregatedDefinitions = new SCADefinitionsImpl();
        for ( SCADefinitions definition : ((List<SCADefinitions>)policyDefinitions) ) {
            SCADefinitionsUtil.aggregateSCADefinitions(definition, aggregatedDefinitions);
        }
        compositeBuilder = ReallySmallRuntimeBuilder.createCompositeBuilder(monitor,
                                                                            assemblyFactory,
                                                                            scaBindingFactory,
                                                                            endpointFactory,
                                                                            intentAttachPointTypeFactory,
                                                                            documentBuilderFactory,
                                                                            transformerFactory,
                                                                            mapper, 
                                                                            aggregatedDefinitions);
        compositeBuilder.build(composite);
        
    }
    
    public ContributionService getContributionService() {
        return contributionService;
    }

    public CompositeActivator getCompositeActivator() {
        return compositeActivator;
    }
    
    public CompositeBuilder getCompositeBuilder() {
        return compositeBuilder;
    }

    public AssemblyFactory getAssemblyFactory() {
        return assemblyFactory;
    }
   
    private void  loadSCADefinitions() throws ActivationException {
        try {
            URLArtifactProcessorExtensionPoint documentProcessors = registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class);
            URLArtifactProcessor<SCADefinitions> definitionsProcessor = documentProcessors.getProcessor(SCADefinitions.class);
            SCADefinitionsProviderExtensionPoint scaDefnProviders = registry.getExtensionPoint(SCADefinitionsProviderExtensionPoint.class);
            
            SCADefinitions systemSCADefinitions = new SCADefinitionsImpl();
            SCADefinitions aSCADefn = null;
            for ( SCADefinitionsProvider aProvider : scaDefnProviders.getSCADefinitionsProviders() ) {
               aSCADefn = aProvider.getSCADefinition(); 
               SCADefinitionsUtil.aggregateSCADefinitions(aSCADefn, systemSCADefinitions);
            }
            
            policyDefinitions.add(systemSCADefinitions);
            
            //we cannot expect that providers will add the intents and policysets into the resolver
            //so we do this here explicitly
            for ( Intent intent : systemSCADefinitions.getPolicyIntents() ) {
                policyDefinitionsResolver.addModel(intent);
            }
            
            for ( PolicySet policySet : systemSCADefinitions.getPolicySets() ) {
                policyDefinitionsResolver.addModel(policySet);
            }
            
            for ( IntentAttachPointType attachPoinType : systemSCADefinitions.getBindingTypes() ) {
                policyDefinitionsResolver.addModel(attachPoinType);
            }
            
            for ( IntentAttachPointType attachPoinType : systemSCADefinitions.getImplementationTypes() ) {
                policyDefinitionsResolver.addModel(attachPoinType);
            }
            
            //now that all system sca definitions have been read, lets resolve them right away
            definitionsProcessor.resolve(systemSCADefinitions, 
                                         policyDefinitionsResolver);
        } catch ( Exception e ) {
            throw new ActivationException(e);
        }
    }
    
    @SuppressWarnings("unchecked")
    private List<ModuleActivator> loadModules(ExtensionPointRegistry registry) throws ActivationException {

        // Load and instantiate the modules found on the classpath (or any registered ClassLoaders)
        modules = new ArrayList<ModuleActivator>();
        try {
            Set<ServiceDeclaration> moduleActivators =
                ServiceDiscovery.getInstance().getServiceDeclarations(ModuleActivator.class);
            Set<String> moduleClasses = new HashSet<String>();
            for (ServiceDeclaration moduleDeclarator : moduleActivators) {
                if (moduleClasses.contains(moduleDeclarator.getClassName())) {
                    continue;
                }
                moduleClasses.add(moduleDeclarator.getClassName());
                Class<?> moduleClass = moduleDeclarator.loadClass();
                ModuleActivator module = (ModuleActivator)moduleClass.newInstance();
                modules.add(module);
            }
        } catch (IOException e) {
            throw new ActivationException(e);
        } catch (ClassNotFoundException e) {
            throw new ActivationException(e);
        } catch (InstantiationException e) {
            throw new ActivationException(e);
        } catch (IllegalAccessException e) {
            throw new ActivationException(e);
        }

        return modules;
    }
    
    private void startModules(ExtensionPointRegistry registry, List<ModuleActivator> modules)
        throws ActivationException {
        boolean debug = logger.isLoggable(Level.FINE);
        // Start all the extension modules
        for (ModuleActivator module : modules) {
            long start = 0L;
            if (debug) {
                logger.fine(module.getClass().getName() + " is starting.");
                start = System.currentTimeMillis();
            }
            try {
                module.start(registry);
                if (debug) {
                    long end = System.currentTimeMillis();
                    logger.fine(module.getClass().getName() + " is started in " + (end - start) + " ms.");
                }
            } catch (Throwable e) {
            	logger.log(Level.WARNING, "Exception starting module " + module.getClass().getName() + " :" + e.getMessage());
            	logger.log(Level.FINE, "Exception starting module " + module.getClass().getName(), e);
            }
        }
    }

    private void stopModules(final ExtensionPointRegistry registry, List<ModuleActivator> modules) {
        boolean debug = logger.isLoggable(Level.FINE);
        for (ModuleActivator module : modules) {
            long start = 0L;
            if (debug) {
                logger.fine(module.getClass().getName() + " is stopping.");
                start = System.currentTimeMillis();
            }
            module.stop(registry);
            if (debug) {
                long end = System.currentTimeMillis();
                logger.fine(module.getClass().getName() + " is stopped in " + (end - start) + " ms.");
            }
        }
    }

    /**
     * @return the proxyFactory
     */
    public ProxyFactory getProxyFactory() {
        return proxyFactory;
    }

    /**
     * @return the registry
     */
    public ExtensionPointRegistry getExtensionPointRegistry() {
        return registry;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



modules/host-embedded/src/main/java/org/apache/tuscany/sca/host/embedded/impl/ReallySmallRuntime.java [123:384]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        MessageFactory messageFactory = factories.getFactory(MessageFactory.class);

        // Get proxy factory
        ProxyFactoryExtensionPoint proxyFactories = registry.getExtensionPoint(ProxyFactoryExtensionPoint.class);  
        proxyFactory = new ExtensibleProxyFactory(proxyFactories); 

        // Create model factories
        assemblyFactory = new RuntimeAssemblyFactory();
        factories.addFactory(assemblyFactory);
        PolicyFactory policyFactory = new DefaultPolicyFactory();
        factories.addFactory(policyFactory);
        
        // Load the runtime modules
        modules = loadModules(registry);
        
        // Start the runtime modules
        startModules(registry, modules);
        
        SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class);
        IntentAttachPointTypeFactory intentAttachPointTypeFactory = new DefaultIntentAttachPointTypeFactory();
        factories.addFactory(intentAttachPointTypeFactory);
        ContributionFactory contributionFactory = factories.getFactory(ContributionFactory.class);
        
        // Create a monitor
        MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class);
        
        if (monitorFactory != null){
            monitor = monitorFactory.createMonitor();
        } else {
            monitorFactory = new DefaultMonitorFactoryImpl();
            monitor = monitorFactory.createMonitor();
            utilities.addUtility(monitorFactory);
            //logger.fine("No MonitorFactory is found on the classpath.");
        }
        
        // Create a contribution service
        policyDefinitions = new ArrayList<SCADefinitions>();
        policyDefinitionsResolver = new DefaultModelResolver();
        contributionService = ReallySmallRuntimeBuilder.createContributionService(classLoader,
                                                                                  registry,
                                                                                  contributionFactory,
                                                                                  assemblyFactory,
                                                                                  policyFactory,
                                                                                  mapper,
                                                                                  policyDefinitions,
                                                                                  policyDefinitionsResolver,
                                                                                  monitor);
        
        // Create the ScopeRegistry
        scopeRegistry = ReallySmallRuntimeBuilder.createScopeRegistry(registry); 
        
        // Create a composite activator
        compositeActivator = ReallySmallRuntimeBuilder.createCompositeActivator(registry,
                                                                                assemblyFactory,
                                                                                messageFactory,
                                                                                scaBindingFactory,
                                                                                mapper,
                                                                                proxyFactory,
                                                                                scopeRegistry,
                                                                                workScheduler);

        // Load the definitions.xml
        loadSCADefinitions();
        
        if (logger.isLoggable(Level.FINE)) {
            long end = System.currentTimeMillis();
            logger.fine("The tuscany runtime is started in " + (end - start) + " ms.");
        }
    }
    
    public void stop() throws ActivationException {
    	long start = System.currentTimeMillis();

        // Stop the runtime modules
        stopModules(registry, modules);

        // Stop and destroy the work manager
        workScheduler.destroy(); 

        // Cleanup
        modules = null;
        registry = null;
        assemblyFactory = null;
        contributionService = null;
        compositeActivator = null;
        workScheduler = null;
        scopeRegistry = null;
        
        if (logger.isLoggable(Level.FINE)) {
            long end = System.currentTimeMillis();
            logger.fine("The tuscany runtime is stopped in " + (end - start) + " ms.");
        }
    }
    
    public void buildComposite(Composite composite) throws CompositeBuilderException {
        //Get factory extension point
        ModelFactoryExtensionPoint factories = registry.getExtensionPoint(ModelFactoryExtensionPoint.class);
        SCABindingFactory scaBindingFactory = factories.getFactory(SCABindingFactory.class);
        IntentAttachPointTypeFactory intentAttachPointTypeFactory = factories.getFactory(IntentAttachPointTypeFactory.class);
        EndpointFactory endpointFactory = factories.getFactory(EndpointFactory.class);        
        UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class);
        InterfaceContractMapper mapper = utilities.getUtility(InterfaceContractMapper.class);
        DocumentBuilderFactory documentBuilderFactory = factories.getFactory(DocumentBuilderFactory.class);
        TransformerFactory transformerFactory = factories.getFactory(TransformerFactory.class);
        
        //Create a composite builder
        SCADefinitions aggregatedDefinitions = new SCADefinitionsImpl();
        for ( SCADefinitions definition : ((List<SCADefinitions>)policyDefinitions) ) {
            SCADefinitionsUtil.aggregateSCADefinitions(definition, aggregatedDefinitions);
        }
        compositeBuilder = ReallySmallRuntimeBuilder.createCompositeBuilder(monitor,
                                                                            assemblyFactory,
                                                                            scaBindingFactory,
                                                                            endpointFactory,
                                                                            intentAttachPointTypeFactory,
                                                                            documentBuilderFactory,
                                                                            transformerFactory,
                                                                            mapper, 
                                                                            aggregatedDefinitions);
        compositeBuilder.build(composite);
        
    }
    
    public ContributionService getContributionService() {
        return contributionService;
    }

    public CompositeActivator getCompositeActivator() {
        return compositeActivator;
    }
    
    public CompositeBuilder getCompositeBuilder() {
        return compositeBuilder;
    }

    public AssemblyFactory getAssemblyFactory() {
        return assemblyFactory;
    }
   
    private void  loadSCADefinitions() throws ActivationException {
        try {
            URLArtifactProcessorExtensionPoint documentProcessors = registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class);
            URLArtifactProcessor<SCADefinitions> definitionsProcessor = documentProcessors.getProcessor(SCADefinitions.class);
            SCADefinitionsProviderExtensionPoint scaDefnProviders = registry.getExtensionPoint(SCADefinitionsProviderExtensionPoint.class);
            
            SCADefinitions systemSCADefinitions = new SCADefinitionsImpl();
            SCADefinitions aSCADefn = null;
            for ( SCADefinitionsProvider aProvider : scaDefnProviders.getSCADefinitionsProviders() ) {
               aSCADefn = aProvider.getSCADefinition(); 
               SCADefinitionsUtil.aggregateSCADefinitions(aSCADefn, systemSCADefinitions);
            }
            
            policyDefinitions.add(systemSCADefinitions);
            
            //we cannot expect that providers will add the intents and policysets into the resolver
            //so we do this here explicitly
            for ( Intent intent : systemSCADefinitions.getPolicyIntents() ) {
                policyDefinitionsResolver.addModel(intent);
            }
            
            for ( PolicySet policySet : systemSCADefinitions.getPolicySets() ) {
                policyDefinitionsResolver.addModel(policySet);
            }
            
            for ( IntentAttachPointType attachPoinType : systemSCADefinitions.getBindingTypes() ) {
                policyDefinitionsResolver.addModel(attachPoinType);
            }
            
            for ( IntentAttachPointType attachPoinType : systemSCADefinitions.getImplementationTypes() ) {
                policyDefinitionsResolver.addModel(attachPoinType);
            }
            
            //now that all system sca definitions have been read, lets resolve them right away
            definitionsProcessor.resolve(systemSCADefinitions, 
                                         policyDefinitionsResolver);
        } catch ( Exception e ) {
            throw new ActivationException(e);
        }
    }
    
    @SuppressWarnings("unchecked")
    private List<ModuleActivator> loadModules(ExtensionPointRegistry registry) throws ActivationException {

        // Load and instantiate the modules found on the classpath (or any registered ClassLoaders)
        modules = new ArrayList<ModuleActivator>();
        try {
            Set<ServiceDeclaration> moduleActivators =
                ServiceDiscovery.getInstance().getServiceDeclarations(ModuleActivator.class);
            Set<String> moduleClasses = new HashSet<String>();
            for (ServiceDeclaration moduleDeclarator : moduleActivators) {
                if (moduleClasses.contains(moduleDeclarator.getClassName())) {
                    continue;
                }
                moduleClasses.add(moduleDeclarator.getClassName());
                Class<?> moduleClass = moduleDeclarator.loadClass();
                ModuleActivator module = (ModuleActivator)moduleClass.newInstance();
                modules.add(module);
            }
        } catch (IOException e) {
            throw new ActivationException(e);
        } catch (ClassNotFoundException e) {
            throw new ActivationException(e);
        } catch (InstantiationException e) {
            throw new ActivationException(e);
        } catch (IllegalAccessException e) {
            throw new ActivationException(e);
        }

        return modules;
    }
    
    private void startModules(ExtensionPointRegistry registry, List<ModuleActivator> modules)
        throws ActivationException {
        boolean debug = logger.isLoggable(Level.FINE);
        // Start all the extension modules
        for (ModuleActivator module : modules) {
            long start = 0L;
            if (debug) {
                logger.fine(module.getClass().getName() + " is starting.");
                start = System.currentTimeMillis();
            }
            try {
                module.start(registry);
                if (debug) {
                    long end = System.currentTimeMillis();
                    logger.fine(module.getClass().getName() + " is started in " + (end - start) + " ms.");
                }
            } catch (Throwable e) {
            	logger.log(Level.WARNING, "Exception starting module " + module.getClass().getName() + " :" + e.getMessage());
            	logger.log(Level.FINE, "Exception starting module " + module.getClass().getName(), e);
            }
        }
    }

    private void stopModules(final ExtensionPointRegistry registry, List<ModuleActivator> modules) {
        boolean debug = logger.isLoggable(Level.FINE);
        for (ModuleActivator module : modules) {
            long start = 0L;
            if (debug) {
                logger.fine(module.getClass().getName() + " is stopping.");
                start = System.currentTimeMillis();
            }
            module.stop(registry);
            if (debug) {
                long end = System.currentTimeMillis();
                logger.fine(module.getClass().getName() + " is stopped in " + (end - start) + " ms.");
            }
        }
    }

    /**
     * @return the proxyFactory
     */
    public ProxyFactory getProxyFactory() {
        return proxyFactory;
    }

    /**
     * @return the registry
     */
    public ExtensionPointRegistry getExtensionPointRegistry() {
        return registry;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



