View Javadoc
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.io.IOException;
22  import java.util.Arrays;
23  import net.jini.core.lookup.ServiceID;
24  import net.jini.io.UnsupportedConstraintException;
25  
26  /**
27   * Class representing the values in a multicast announcement.
28   *
29   * @author Sun Microsystems, Inc.
30   * @since 2.0
31   */
32  public class MulticastAnnouncement {
33  
34      /** The announcement sequence number. */
35      private final long sequenceNumber;
36      /** The lookup service host. */
37      private final String host;
38      /** The lookup service listen port. */
39      private final int port;
40      /** The lookup service member groups. */
41      private final String[] groups;
42      /** The lookup service ID. */
43      private final ServiceID serviceID;
44  
45      /**
46       * Creates a new <code>MulticastAnnouncement</code> instance containing the
47       * given values.  The <code>groups</code> array is copied; a
48       * <code>null</code> value is considered equivalent to an empty array.
49       *
50       * @param sequenceNumber the announcement sequence number
51       * @param host the lookup service host
52       * @param port the lookup service listen port
53       * @param groups the lookup service member groups
54       * @param serviceID the lookup service ID
55       * @throws NullPointerException if <code>host</code> or
56       * <code>serviceID</code> is <code>null</code>, or if <code>groups</code>
57       * contains <code>null</code>
58       * @throws IllegalArgumentException if <code>port</code> is out of range
59       */
60      public MulticastAnnouncement(   long sequenceNumber,
61  				    String host,
62  				    int port,
63  				    String[] groups,
64  				    ServiceID serviceID)
65      {
66  	this(sequenceNumber, host, port, groups, serviceID,
67  		check(host, port, groups, serviceID));
68      }
69      
70      private static boolean check(String host,
71  				 int port,
72  				 String[] groups,
73  				 ServiceID serviceID)
74      {
75  	groups = (groups != null) ? groups : new String[0];
76  	if (host == null || 
77  	    serviceID == null ||
78  	    Arrays.asList(groups).contains(null))
79  	{
80  	    throw new NullPointerException();
81  	}
82  	if (port < 0 || port > 0xFFFF) {
83  	    throw new IllegalArgumentException("port out of range: " + port);
84  	}
85  	return true;
86      }
87      
88      private MulticastAnnouncement(  long sequenceNumber,
89  				    String host,
90  				    int port,
91  				    String[] groups,
92  				    ServiceID serviceID,
93  				    boolean check)
94      {
95  	this.sequenceNumber = sequenceNumber;
96  	this.host = host;
97  	this.port = port;
98  	this.groups = groups.clone();
99  	this.serviceID = serviceID;
100     }
101 
102     /**
103      * Returns the announcement sequence number.
104      *
105      * @return the announcement sequence number
106      */
107     public long getSequenceNumber() {
108 	return sequenceNumber;
109     }
110 
111     /**
112      * Returns the lookup service host name.
113      *
114      * @return the lookup service host name
115      */
116     public String getHost() {
117 	return host;
118     }
119 
120     /**
121      * Returns the lookup service listen port.
122      *
123      * @return the lookup service listen port
124      */
125     public int getPort() {
126 	return port;
127     }
128 
129     /**
130      * Returns the member groups of the lookup service.
131      *
132      * @return the member groups of the lookup service
133      */
134     public String[] getGroups() {
135 	return groups.clone();
136     }
137 
138     /**
139      * Returns the service ID of the lookup service.
140      *
141      * @return the service ID of the lookup service
142      */
143     public ServiceID getServiceID() {
144 	return serviceID;
145     }
146     
147     /**
148      * Checks if the constraints whose checking was delayed when this instance
149      * was decoded, if any, are satisfied. If the instance was not decoded,
150      * but directly constructed, this method does nothing.
151      * @throws UnsupportedConstraintException if unable to satisfy the specified
152      * constraints
153      * @throws SecurityException if the given constraints cannot be satisfied
154      * due to insufficient caller permissions
155      * @throws IOException if an error occurs in interpreting the data
156      */
157     public void checkConstraints() throws IOException {
158         // do nothing by default
159     }
160     
161     /**
162      * Returns a string representation of this announcement.
163      *
164      * @return a string representation of this announcement
165      */
166     @Override
167     public String toString() {
168 	StringBuilder sb = new StringBuilder(120);
169 	sb.append("MulticastAnnouncement[").append(sequenceNumber).append(",")
170 	    .append(host).append(":").append(port).append(",")
171 	    .append(Arrays.asList(groups)).append(",")
172 	    .append(serviceID).append("]");
173 	return sb.toString();
174     }
175 }