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.io.InvalidObjectException;
23 import java.io.ObjectInputStream;
24 import java.io.Serializable;
25 import net.jini.core.constraint.InvocationConstraint;
26 import org.apache.river.api.io.AtomicSerial;
27 import org.apache.river.api.io.AtomicSerial.GetArg;
28
29 /**
30 * Represents a constraint on the timeout set on sockets used for unicast
31 * discovery. Lookup services and discovery clients can use this constraint to
32 * specify the maximum length of time that reads of unicast discovery data will
33 * block.
34 *
35 * @author Sun Microsystems, Inc.
36 * @since 2.0
37 */
38 @AtomicSerial
39 public final class UnicastSocketTimeout
40 implements InvocationConstraint, Serializable
41 {
42 private static final long serialVersionUID = 6500477426762925657L;
43
44 /**
45 * The socket timeout.
46 *
47 * @serial
48 */
49 private final int timeout;
50
51 /**
52 * Creates a <code>UnicastSocketTimeout</code> constraint for the given
53 * timeout.
54 *
55 * @param timeout the socket timeout
56 * @throws IllegalArgumentException if the given timeout is negative
57 */
58 public UnicastSocketTimeout(int timeout) {
59 this(timeout, check(timeout));
60 }
61
62 UnicastSocketTimeout(GetArg arg) throws IOException {
63 this(arg.get("timeout", -1));
64 }
65
66 private UnicastSocketTimeout(int timeout, boolean check){
67 this.timeout = timeout;
68 }
69
70 private static boolean check(int timeout){
71 if (timeout < 0) {
72 throw new IllegalArgumentException("invalid timeout");
73 }
74 return true;
75 }
76
77 /**
78 * Returns the socket timeout.
79 *
80 * @return the socket timeout
81 */
82 public int getTimeout() {
83 return timeout;
84 }
85
86 public int hashCode() {
87 return UnicastSocketTimeout.class.hashCode() + timeout;
88 }
89
90 public boolean equals(Object obj) {
91 return obj instanceof UnicastSocketTimeout &&
92 timeout == ((UnicastSocketTimeout) obj).timeout;
93 }
94
95 public String toString() {
96 return "UnicastSocketTimeout[" + timeout + "]";
97 }
98
99 private void readObject(ObjectInputStream in)
100 throws IOException, ClassNotFoundException
101 {
102 in.defaultReadObject();
103 if (timeout < 0) {
104 throw new InvalidObjectException("invalid timeout");
105 }
106 }
107 }