/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.model.socket;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.jboss.as.model.AbstractModelElementUpdate;
import org.jboss.as.model.UpdateContext;
import org.jboss.as.model.UpdateFailedException;
import org.jboss.as.model.UpdateResultHandler;
import org.jboss.as.services.net.NetworkInterfaceBinding;
import org.jboss.as.services.net.NetworkInterfaceService;
import org.jboss.msc.service.BatchBuilder;
import org.jboss.msc.service.BatchServiceBuilder;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceRegistryException;

/**
 * A {@code InterfaceElement} update.
 *
 * @author Emanuel Muckenhuber
 */
public final class InterfaceAdd extends AbstractNetworkInterfaceUpdate {

    private static final long serialVersionUID = 2597565379666344003L;
    private final String name;
    private boolean anyLocalV4;
    private boolean anyLocalV6;
    private boolean anyLocal;
    private Collection<AbstractInterfaceCriteriaElement<?>> interfaceCriteria;

    public InterfaceAdd(String name, boolean anyLocalV4, boolean anyLocalV6, boolean anyLocal, Collection<AbstractInterfaceCriteriaElement<?>> interfaceCriteria) {
        this.name = name;
        this.anyLocalV4 = anyLocalV4;
        this.anyLocalV6 = anyLocalV6;
        this.anyLocal = anyLocal;
    }

    public InterfaceAdd(final InterfaceElement networkInterface) {
        this.name = networkInterface.getName();
        this.anyLocal = networkInterface.isAnyLocalAddress();
        this.anyLocalV4 = networkInterface.isAnyLocalV4Address();
        this.anyLocalV6 = networkInterface.isAnyLocalV6Address();
        this.interfaceCriteria = networkInterface.getCriteriaElements();
    }

    public String getName() {
        return name;
    }

    /** {@inheritDoc} */
    public void applyUpdate(InterfaceElement element) throws UpdateFailedException {
        if(interfaceCriteria != null && interfaceCriteria.size() > 0) {
            for(AbstractInterfaceCriteriaElement<?> criteria : interfaceCriteria) {
                element.addCriteria(criteria);
            }
        }
        if(anyLocal) element.setAnyLocal(anyLocal);
        if(anyLocalV4) element.setAnyLocalV4(anyLocalV4);
        if(anyLocalV6) element.setAnyLocalV6(anyLocalV6);
    }

    /** {@inheritDoc} */
    public AbstractModelElementUpdate<InterfaceElement> getCompensatingUpdate(InterfaceElement original) {
        return null;
    }


    public <P> void applyUpdate(UpdateContext updateContext, UpdateResultHandler<? super Void,P> resultHandler, P param) {
        final BatchBuilder batch = updateContext.getBatchBuilder();
        final BatchServiceBuilder<NetworkInterfaceBinding> builder = batch.addService(NetworkInterfaceService.JBOSS_NETWORK_INTERFACE.append(name),
                createInterfaceService())
                .setInitialMode(Mode.ON_DEMAND);
        builder.addListener(new UpdateResultHandler.ServiceStartListener<P>(resultHandler, param));
        try {
            batch.install();
        } catch (ServiceRegistryException e) {
            resultHandler.handleFailure(e, param);
        }
    }

    /**
     * Create a {@link NetworkInterfaceService}.
     *
     * @return the interface service
     */
    Service<NetworkInterfaceBinding> createInterfaceService() {
        Set<InterfaceCriteria> criteria = new HashSet<InterfaceCriteria>();
        for(final AbstractInterfaceCriteriaElement<?> element : interfaceCriteria) {
            criteria.add(element.getInterfaceCriteria());
        }
        return new NetworkInterfaceService(name, anyLocalV4, anyLocalV6, anyLocal, new OverallInterfaceCriteria(criteria));
    }

    /** Overall interface criteria. */
    static final class OverallInterfaceCriteria implements InterfaceCriteria {
        private static final long serialVersionUID = -5417786897309925997L;
        private final Set<InterfaceCriteria> interfaceCriteria;

        public OverallInterfaceCriteria(Set<InterfaceCriteria> criteria) {
            this.interfaceCriteria = criteria;
        }

        /** {@inheritDoc} */
        public boolean isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException {
            for (InterfaceCriteria criteria : interfaceCriteria) {
                if (! criteria.isAcceptable(networkInterface, address))
                    return false;
            }
            return true;
        }
    }

}

@@ -1174,24 +1174,54 @@
 Collection;%0A
+import java.util.Collections;%0A
 import java.
@@ -2227,26 +2227,51 @@
 faceCriteria
+ = Collections.emptySet()
 ;%0A
-
 %0A    public 
@@ -2555,24 +2555,76 @@
 = anyLocal;%0A
+        this.interfaceCriteria = interfaceCriteria;%0A
     %7D%0A%0A    p
@@ -2685,37 +2685,32 @@
 face) %7B%0A        
-this.
 name = networkIn
@@ -2728,37 +2728,32 @@
 Name();%0A        
-this.
 anyLocal = netwo
@@ -2785,37 +2785,32 @@
 ress();%0A        
-this.
 anyLocalV4 = net
@@ -2846,37 +2846,32 @@
 ress();%0A        
-this.
 anyLocalV6 = net
@@ -2907,37 +2907,32 @@
 ress();%0A        
-this.
 interfaceCriteri
@@ -5272,37 +5272,32 @@
 ) %7B%0A            
-this.
 interfaceCriteri

Fix interface add creation