Exemplo n.º 1
0
            testFacets(Ice.Communicator com, bool builtInFacets)
            {
                if (builtInFacets)
                {
                    test(com.findAdminFacet("Properties") != null);
                    test(com.findAdminFacet("Process") != null);
                    test(com.findAdminFacet("Logger") != null);
                    test(com.findAdminFacet("Metrics") != null);
                }

                Test.TestFacet f1 = new TestFacetI();
                Test.TestFacet f2 = new TestFacetI();
                Test.TestFacet f3 = new TestFacetI();

                com.addAdminFacet(f1, "Facet1");
                com.addAdminFacet(f2, "Facet2");
                com.addAdminFacet(f3, "Facet3");

                test(com.findAdminFacet("Facet1") == f1);
                test(com.findAdminFacet("Facet2") == f2);
                test(com.findAdminFacet("Facet3") == f3);
                test(com.findAdminFacet("Bogus") == null);

                Dictionary <string, Ice.Object> facetMap = com.findAllAdminFacets();

                if (builtInFacets)
                {
                    test(facetMap.Count == 7);
                    test(facetMap.ContainsKey("Properties"));
                    test(facetMap.ContainsKey("Process"));
                    test(facetMap.ContainsKey("Logger"));
                    test(facetMap.ContainsKey("Metrics"));
                }
                else
                {
                    test(facetMap.Count >= 3);
                }
                test(facetMap.ContainsKey("Facet1"));
                test(facetMap.ContainsKey("Facet2"));
                test(facetMap.ContainsKey("Facet3"));

                try
                {
                    com.addAdminFacet(f1, "Facet1");
                    test(false);
                }
                catch (Ice.AlreadyRegisteredException)
                {
                    // Expected
                }

                try
                {
                    com.removeAdminFacet("Bogus");
                    test(false);
                }
                catch (Ice.NotRegisteredException)
                {
                    // Expected
                }

                com.removeAdminFacet("Facet1");
                com.removeAdminFacet("Facet2");
                com.removeAdminFacet("Facet3");

                try
                {
                    com.removeAdminFacet("Facet1");
                    test(false);
                }
                catch (Ice.NotRegisteredException)
                {
                    // Expected
                }
            }
Exemplo n.º 2
0
        public int run()
        {
            try
            {
            Ice.Properties properties = _communicator.getProperties();

            //
            // Create an object adapter. Services probably should NOT share
            // this object adapter, as the endpoint(s) for this object adapter
            // will most likely need to be firewalled for security reasons.
            //
            Ice.ObjectAdapter adapter = null;
            if(properties.getProperty("IceBox.ServiceManager.Endpoints").Length != 0)
            {
                adapter = _communicator.createObjectAdapter("IceBox.ServiceManager");

                Ice.Identity identity = new Ice.Identity();
                identity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox");
                identity.name = "ServiceManager";
                adapter.add(this, identity);
            }

            //
            // Parse the property set with the prefix "IceBox.Service.". These
            // properties should have the following format:
            //
            // IceBox.Service.Foo=<assembly>:Package.Foo [args]
            //
            // We parse the service properties specified in IceBox.LoadOrder
            // first, then the ones from remaining services.
            //
            string prefix = "IceBox.Service.";
            Dictionary<string, string> services = properties.getPropertiesForPrefix(prefix);
            string[] loadOrder = properties.getPropertyAsList("IceBox.LoadOrder");
            List<StartServiceInfo> servicesInfo = new List<StartServiceInfo>();
            for(int i = 0; i < loadOrder.Length; ++i)
            {
                if(loadOrder[i].Length > 0)
                {
                    string key = prefix + loadOrder[i];
                    string value = services[key];
                    if(value == null)
                    {
                        FailureException ex = new FailureException();
                        ex.reason = "ServiceManager: no service definition for `" + loadOrder[i] + "'";
                        throw ex;
                    }
                    servicesInfo.Add(new StartServiceInfo(loadOrder[i], value, _argv));
                    services.Remove(key);
                }
            }
            foreach(KeyValuePair<string, string> entry in services)
            {
                string name = entry.Key.Substring(prefix.Length);
                string value = entry.Value;
                servicesInfo.Add(new StartServiceInfo(name, value, _argv));
            }

            //
            // Check if some services are using the shared communicator in which
            // case we create the shared communicator now with a property set that
            // is the union of all the service properties (from services that use
            // the shared communicator).
            //
            if(properties.getPropertiesForPrefix("IceBox.UseSharedCommunicator.").Count > 0)
            {
                Ice.InitializationData initData = new Ice.InitializationData();
                initData.properties = createServiceProperties("SharedCommunicator");
                foreach(StartServiceInfo service in servicesInfo)
                {
                    if(properties.getPropertyAsInt("IceBox.UseSharedCommunicator." + service.name) <= 0)
                    {
                        continue;
                    }

                    //
                    // Load the service properties using the shared communicator properties as
                    // the default properties.
                    //
                    Ice.Properties svcProperties = Ice.Util.createProperties(ref service.args, initData.properties);

                    //
                    // Remove properties from the shared property set that a service explicitly clears.
                    //
                    Dictionary<string, string> allProps = initData.properties.getPropertiesForPrefix("");
                    foreach(string key in allProps.Keys)
                    {
                        if(svcProperties.getProperty(key).Length == 0)
                        {
                            initData.properties.setProperty(key, "");
                        }
                    }

                    //
                    // Add the service properties to the shared communicator properties.
                    //
                    foreach(KeyValuePair<string, string> entry in svcProperties.getPropertiesForPrefix(""))
                    {
                        initData.properties.setProperty(entry.Key, entry.Value);
                    }

                    //
                    // Parse <service>.* command line options (the Ice command line options
                    // were parsed by the call to createProperties above).
                    //
                    service.args = initData.properties.parseCommandLineOptions(service.name, service.args);
                }

                string facetNamePrefix = "IceBox.SharedCommunicator.";
                bool addFacets = configureAdmin(initData.properties, facetNamePrefix);

                _sharedCommunicator = Ice.Util.initialize(initData);

                if(addFacets)
                {
                    // Add all facets created on shared communicator to the IceBox communicator
                    // but renamed <prefix>.<facet-name>, except for the Process facet which is
                    // never added.
                    foreach(KeyValuePair<string, Ice.Object> p in _sharedCommunicator.findAllAdminFacets())
                    {
                        if(!p.Key.Equals("Process"))
                        {
                            _communicator.addAdminFacet(p.Value, facetNamePrefix + p.Key);
                        }
                    }
                }
            }

            foreach(StartServiceInfo s in servicesInfo)
            {
                startService(s.name, s.entryPoint, s.args);
            }

            //
            // We may want to notify external scripts that the services
            // have started. This is done by defining the property:
            //
            // PrintServicesReady=bundleName
            //
            // Where bundleName is whatever you choose to call this set of
            // services. It will be echoed back as "bundleName ready".
            //
            // This must be done after start() has been invoked on the
            // services.
            //
            string bundleName = properties.getProperty("IceBox.PrintServicesReady");
            if(bundleName.Length > 0)
            {
                Console.Out.WriteLine(bundleName + " ready");
            }

            //
            // Don't move after the adapter activation. This allows
            // applications to wait for the service manager to be
            // reachable before sending a signal to shutdown the
            //
            //
            Ice.Application.shutdownOnInterrupt();

            //
            // Register "this" as a facet to the Admin object and create Admin object
            //
            try
            {
                _communicator.addAdminFacet(this, "IceBox.ServiceManager");
                _communicator.getAdmin();
            }
            catch(Ice.ObjectAdapterDeactivatedException)
            {
                //
                // Expected if the communicator has been shutdown.
                //
            }

            //
            // Start request dispatching after we've started the services.
            //
            if(adapter != null)
            {
                try
                {
                    adapter.activate();
                }
                catch(Ice.ObjectAdapterDeactivatedException)
                {
                    //
                    // Expected if the communicator has been shutdown.
                    //
                }
            }

            _communicator.waitForShutdown();
            }
            catch(FailureException ex)
            {
            _logger.error(ex.ToString());
            return 1;
            }
            catch(Exception ex)
            {
            _logger.error("ServiceManager: caught exception:\n" + ex.ToString());
            return 1;
            }
            finally
            {
            //
            // Invoke stop() on the services.
            //
            stopAll();
            }

            return 0;
        }