1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.river.discovery; 20 21 import java.util.Arrays; 22 import net.jini.core.lookup.ServiceRegistrar; 23 24 /** 25 * Class representing the values obtained as the result of unicast discovery. 26 * 27 * @author Sun Microsystems, Inc. 28 * @since 2.0 29 */ 30 public class UnicastResponse { 31 32 /** The lookup service host. */ 33 private final String host; 34 /** The lookup service listen port. */ 35 private final int port; 36 /** The lookup service member groups. */ 37 private final String[] groups; 38 /** The lookup service proxy. */ 39 private final ServiceRegistrar registrar; 40 41 /** 42 * Creates new <code>UnicastResponse</code> instance containing the given 43 * values. The <code>groups</code> array is copied; a <code>null</code> 44 * value is considered equivalent to an empty array. 45 * 46 * @param host the lookup service host 47 * @param port the lookup service listen port 48 * @param groups the lookup service member groups 49 * @param registrar the lookup service proxy 50 * @throws NullPointerException if <code>host</code> or 51 * <code>registrar</code> is <code>null</code>, or if <code>groups</code> 52 * contains <code>null</code> 53 * @throws IllegalArgumentException if <code>port</code> is out of range 54 */ 55 public UnicastResponse(String host, 56 int port, 57 String[] groups, 58 ServiceRegistrar registrar) 59 { 60 groups = (groups != null) ? (String[]) groups.clone() : new String[0]; 61 if (host == null || 62 registrar == null || 63 Arrays.asList(groups).contains(null)) 64 { 65 throw new NullPointerException(); 66 } 67 if (port < 0 || port > 0xFFFF) { 68 throw new IllegalArgumentException("port out of range: " + port); 69 } 70 this.host = host; 71 this.port = port; 72 this.groups = groups; 73 this.registrar = registrar; 74 } 75 76 /** 77 * Returns the lookup service host name. 78 * 79 * @return the lookup service host name 80 */ 81 public String getHost() { 82 return host; 83 } 84 85 /** 86 * Returns the lookup service listen port. 87 * 88 * @return the lookup service listen port 89 */ 90 public int getPort() { 91 return port; 92 } 93 94 /** 95 * Returns the member groups of the lookup service. 96 * 97 * @return the member groups of the lookup service 98 */ 99 public String[] getGroups() { 100 return (String[]) groups.clone(); 101 } 102 103 /** 104 * Returns the lookup service proxy. 105 * 106 * @return the lookup service proxy 107 */ 108 public ServiceRegistrar getRegistrar() { 109 return registrar; 110 } 111 112 /** 113 * Returns a string representation of this response. 114 * 115 * @return a string representation of this response 116 */ 117 public String toString() { 118 StringBuilder sb = new StringBuilder(256); 119 sb.append("UnicastResponse[") 120 .append(host) 121 .append(":") 122 .append(port) 123 .append(", ") 124 .append(Arrays.asList(groups)) 125 .append(", ") 126 .append(registrar) 127 .append("]"); 128 return sb.toString(); 129 } 130 }