protected Service getServiceInstance()

in src/java/org/apache/turbine/services/BaseServiceBroker.java [607:685]


    protected Service getServiceInstance(String name)
            throws InstantiationException
    {
        Service service = services.get(name);

        if (service == null)
        {
            serviceLock.lock();

            try
            {
                // Double check
                service = services.get(name);

                if (service == null)
                {
                    if (!this.isLocalService(name))
                    {
                        throw new InstantiationException(
                                "ServiceBroker: unknown service " + name
                                + " requested");
                    }

                    try
                    {
                        Class<?> clazz = mapping.get(name);

                        try
                        {
                            service = (Service) clazz.getDeclaredConstructor().newInstance();

                            // check if the newly created service is also a
                            // service provider - if so then remember it
                            if (service instanceof TurbineServiceProvider)
                            {
                                Service _service = this.serviceProviderInstanceMap.putIfAbsent(name,service);
                                if (_service != null)
                                {
                                    service = _service;
                                }
                            }
                        }
                        // those two errors must be passed to the VM
                        catch (ClassCastException e)
                        {
                            throw new InstantiationException("Class " + clazz +
                                    " doesn't implement the Service interface", e);
                        }
                        catch (ThreadDeath | OutOfMemoryError t)
                        {
                            throw t;
                        }
                        catch (Throwable t)
                        {
                            throw new InstantiationException("Failed to instantiate " + clazz, t);
                        }
                    }
                    catch (InstantiationException e)
                    {
                        throw new InstantiationException(
                                "Failed to instantiate service " + name, e);
                    }
                    service.setServiceBroker(this);
                    service.setName(name);
                    Service _service = services.putIfAbsent(name, service);
                    if (_service != null) // Unlikely
                    {
                        service = _service;
                    }
                }
            }
            finally
            {
                serviceLock.unlock();
            }
        }

        return service;
    }