/** * Finds the references to unresolved services and replace them with the real service definitions. */ private void redefineUndefinedServices() { for (ServiceUsage usage : undefinedServiceUsages) { Name name = usage.getName(); Service service = model.getService(name); if (service == null) { System.err.println("Can't find service for name \"" + name + "\"."); continue; } ServiceSetter setter = usage.getSetter(); setter.accept(service); } }
/** * Finds the service corresponding to the given name and assigns it using the given setter. If there is no service * corresponding to the given name yet then a new undefined service will be created and assigned, and it will be * remembered so that it can later be replaced with the real service. * * @param serviceName the name of the service to find * @param serviceSetter the setter used to assign the service */ private void assignService(Name serviceName, ServiceSetter serviceSetter) { // First try to find a service that has already been defined: Service service = model.getService(serviceName); // If the service hasn't been defined then we create need to create a dummy service: if (service == null) { service = new UndefinedService(); service.setName(serviceName); } // If we are returning an undefined service then we need to to remember to replace it later, saving the name of // the service and the setter provided by the caller: if (service instanceof UndefinedService) { ServiceUsage serviceUsage = new ServiceUsage(); serviceUsage.setName(serviceName); serviceUsage.setSetter(serviceSetter); undefinedServiceUsages.add(serviceUsage); } // Assign the service: if (serviceSetter != null) { serviceSetter.accept(service); } }