private Map createServiceMap()

in tapestry-framework/src/org/apache/tapestry/engine/AbstractEngine.java [1761:1845]


    private Map createServiceMap()
    {
        if (LOG.isDebugEnabled())
            LOG.debug("Creating service map.");

        ISpecificationSource source = getSpecificationSource();

// Build the initial version of the result map,
// where each value is the *name* of a class.

        Map result = new HashMap();

// Do the framework first.

        addServices(source.getFrameworkNamespace(), result);

// And allow the application to override the framework.

        addServices(source.getApplicationNamespace(), result);

        IResourceResolver resolver = getResourceResolver();

        Iterator i = result.entrySet().iterator();

        while (i.hasNext())
        {
            Map.Entry entry = (Map.Entry) i.next();

            String name = (String) entry.getKey();
            String className = (String) entry.getValue();

            if (LOG.isDebugEnabled())
                LOG.debug("Creating service " + name + " as instance of " + className);

            Class serviceClass = resolver.findClass(className);

            try
            {
                IEngineService service = (IEngineService) serviceClass.newInstance();
                String serviceName = service.getName();

                if (!service.getName().equals(name))
                    throw new ApplicationRuntimeException(
                            Tapestry.format(
                                    "AbstractEngine.service-name-mismatch",
                                    name,
                                    className,
                                    serviceName));

// Replace the class name with an instance
// of the named class.

                entry.setValue(service);
            }
            catch (InstantiationException ex)
            {
                String message =
                        Tapestry.format(
                                "AbstractEngine.unable-to-instantiate-service",
                                name,
                                className);

                LOG.error(message, ex);

                throw new ApplicationRuntimeException(message, ex);
            }
            catch (IllegalAccessException ex)
            {
                String message =
                        Tapestry.format(
                                "AbstractEngine.unable-to-instantiate-service",
                                name,
                                className);

                LOG.error(message, ex);

                throw new ApplicationRuntimeException(message, ex);
            }
        }

        // Result should not be modified after this point, for threadsafety issues.
        // We could wrap it in an unmodifiable, but for efficiency we don't.

        return result;
    }