All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anrisoftware.sscontrol.hosts.service.HostsServiceImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012-2015 Erwin Müller 
 *
 * This file is part of sscontrol-hosts.
 *
 * sscontrol-hosts is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * sscontrol-hosts is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with sscontrol-hosts. If not, see .
 */
package com.anrisoftware.sscontrol.hosts.service;

import static com.anrisoftware.sscontrol.hosts.service.HostsServiceFactory.NAME;
import groovy.lang.Script;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.anrisoftware.sscontrol.core.api.ServiceException;
import com.anrisoftware.sscontrol.core.api.ServiceScriptFactory;
import com.anrisoftware.sscontrol.core.service.AbstractService;
import com.anrisoftware.sscontrol.hosts.api.HostsService;
import com.anrisoftware.sscontrol.hosts.host.Host;
import com.anrisoftware.sscontrol.hosts.host.HostFactory;

/**
 * Hosts service.
 * 
 * @author Erwin Mueller, [email protected]
 * @since 1.0
 */
@SuppressWarnings("serial")
public class HostsServiceImpl extends AbstractService implements HostsService {

    private static final String HOST_ADDRESS = "address";

    private static final String HOSTS = "hosts";

    private final HostsServiceImplLogger log;

    private final List hosts;

    @Inject
    private HostFactory hostFactory;

    @Inject
    HostsServiceImpl(HostsServiceImplLogger logger) {
        this.log = logger;
        this.hosts = new ArrayList();
    }

    @Override
    protected Script getScript(String profileName) throws ServiceException {
        ServiceScriptFactory scriptFactory = findScriptFactory(NAME);
        return (Script) scriptFactory.getScript();
    }

    /**
     * Because we load the script from a script service the dependencies are
     * already injected.
     */
    @Override
    protected void injectScript(Script script) {
    }

    @Override
    public String getName() {
        return NAME;
    }

    /**
     * Starts the hosts configuration.
     * 
     * @return this {@link HostsServiceImpl}.
     */
    public Object hosts(Object closure) {
        return this;
    }

    /**
     * Adds a new host entry with the specified IP address.
     */
    public void ip(Map args, String address) {
        args.put(HOST_ADDRESS, address);
        Host host = hostFactory.create(this, args);
        hosts.add(host);
        log.hostAdded(this, host);
    }

    @Override
    public List getHosts() {
        return hosts;
    }

    @Override
    public void addHostsHead(Collection hosts) {
        int index = 0;
        for (Host host : hosts) {
            this.hosts.add(index++, host);
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).appendSuper(super.toString())
                .append(HOSTS, hosts).toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy