1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.catalina.tribes.transport.nio;
18
19 import java.io.IOException;
20
21 import org.apache.catalina.tribes.ChannelException;
22 import org.apache.catalina.tribes.ChannelMessage;
23 import org.apache.catalina.tribes.Member;
24 import org.apache.catalina.tribes.transport.DataSender;
25 import org.apache.catalina.tribes.transport.MultiPointSender;
26 import org.apache.catalina.tribes.transport.PooledSender;
27
28 /**
29 * <p>Title: </p>
30 *
31 * <p>Description: </p>
32 *
33 * <p>Company: </p>
34 *
35 * @author not attributable
36 * @version 1.0
37 */
38 public class PooledParallelSender extends PooledSender implements MultiPointSender {
39 protected boolean connected = true;
40 public PooledParallelSender() {
41 super();
42 }
43
44 public void sendMessage(Member[] destination, ChannelMessage message) throws ChannelException {
45 if ( !connected ) throw new ChannelException("Sender not connected.");
46 ParallelNioSender sender = (ParallelNioSender)getSender();
47 if (sender == null) {
48 ChannelException cx = new ChannelException("Unable to retrieve a data sender, time out error.");
49 for (int i = 0; i < destination.length; i++) cx.addFaultyMember(destination[i], new NullPointerException("Unable to retrieve a sender from the sender pool"));
50 throw cx;
51 } else {
52 try {
53 sender.sendMessage(destination, message);
54 sender.keepalive();
55 } finally {
56 if (!connected) disconnect();
57 returnSender(sender);
58 }
59 }
60 }
61
62 public DataSender getNewDataSender() {
63 try {
64 ParallelNioSender sender = new ParallelNioSender();
65 sender.transferProperties(this,sender);
66 return sender;
67 } catch ( IOException x ) {
68 throw new RuntimeException("Unable to open NIO selector.",x);
69 }
70 }
71
72 public synchronized void disconnect() {
73 this.connected = false;
74 super.disconnect();
75 }
76
77 public synchronized void connect() throws IOException {
78 this.connected = true;
79 super.connect();
80 }
81
82 }