private void initService(DeployState deployState, HostResource hostResource, int userPort) {
        if (initialized) {
            throw new IllegalStateException("Service '" + getConfigId() + "' already initialized.");
        }
        if (hostResource == null) {
            throw new IllegalArgumentException("No host found for service '" + getServiceName() + "'. " +
                                               "The hostalias is probably missing from hosts.xml.");
        }
        id = getIndex(hostResource);
        ports = hostResource.allocateService(deployState.getDeployLogger(), this, getInstanceWantedPort(userPort));
        initialized = true;
        for(String envVar : deployState.getProperties().environmentVariables()) {
            addEnvironmentVariable(envVar);
        }
    }
Helper method to avoid replicating code.

@param hostResource The physical host on which this service should run.
@param userPort The wanted port given by the user.
config-model/src/main/java/com/yahoo/vespa/model/AbstractService.java