PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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