PageRenderTime 40ms CodeModel.GetById 28ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/interfaces/NicMatchInterfaceCriteria.java

#
Java | 59 lines | 24 code | 12 blank | 23 comment | 3 complexity | a221daf3bd38d10f943bfe09a63897ec MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
 1/**
 2 *
 3 */
 4package org.jboss.as.controller.interfaces;
 5
 6import java.net.InetAddress;
 7import java.net.NetworkInterface;
 8import java.net.SocketException;
 9import java.util.regex.Pattern;
10
11import static org.jboss.as.controller.ControllerMessages.MESSAGES;
12
13/**
14 * {@link InterfaceCriteria} that tests whether a given {@link Pattern regex pattern}
15 * matches the network interface's {@link NetworkInterface#getName() name}.
16 *
17 * @author Brian Stansberry
18 */
19public class NicMatchInterfaceCriteria implements InterfaceCriteria {
20
21    private static final long serialVersionUID = 6456168020697683203L;
22
23    private final Pattern pattern;
24
25    /**
26     * Creates a new AnyInterfaceCriteria
27     *
28     * @param pattern the criteria to check to see if any are satisfied.
29     *                 Cannot be <code>null</code>
30     *
31     * @throws IllegalArgumentException if <code>criteria</code> is <code>null</code>
32     */
33    public NicMatchInterfaceCriteria(Pattern pattern) {
34        if (pattern == null)
35            throw MESSAGES.nullVar("pattern");
36        this.pattern = pattern;
37    }
38
39    public Pattern getAcceptablePattern() {
40        return pattern;
41    }
42
43    /**
44     * {@inheritDoc}
45     *
46     * @return <code>address</code> if the {@link #getAcceptablePattern() acceptable pattern}
47     *          matches <code>networkInterface</code>'s {@link NetworkInterface#getName() name}.
48     */
49    @Override
50    public InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException {
51
52        if( pattern.matcher(networkInterface.getName()).matches() )
53            return address;
54        return null;
55    }
56
57
58
59}