001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2012-2015 ForgeRock AS. 026 */ 027 028package org.opends.server.extensions; 029 030 031 032import java.io.IOException; 033import java.nio.ByteBuffer; 034import java.nio.channels.ByteChannel; 035 036 037 038/** 039 * This class redirects read and write requests either to a child byte channel, 040 * or a byte channel to be redirected to. 041 */ 042public class RedirectingByteChannel implements ByteChannel 043{ 044 /** 045 * Create an instance of a redirecting byte channel using the specified byte 046 * channel as the child. 047 * 048 * @param bc 049 * A byte channel to use as the child. 050 * @return A redirecting byte channel. 051 */ 052 public static RedirectingByteChannel getRedirectingByteChannel( 053 final ByteChannel bc) 054 { 055 return new RedirectingByteChannel(bc); 056 } 057 058 059 060 private final ByteChannel child; 061 private volatile ByteChannel redirect; 062 063 064 065 private RedirectingByteChannel(final ByteChannel child) 066 { 067 this.child = child; 068 } 069 070 071 072 /** {@inheritDoc} */ 073 public void close() throws IOException 074 { 075 final ByteChannel tmp = redirect; 076 if (tmp != null) 077 { 078 tmp.close(); 079 } 080 else 081 { 082 child.close(); 083 } 084 } 085 086 087 088 /** 089 * Disable redirection. 090 */ 091 public final void disable() 092 { 093 redirect = null; 094 } 095 096 097 098 /** {@inheritDoc} */ 099 public boolean isOpen() 100 { 101 final ByteChannel tmp = redirect; 102 if (tmp != null) 103 { 104 return tmp.isOpen(); 105 } 106 else 107 { 108 return child.isOpen(); 109 } 110 } 111 112 113 114 /** {@inheritDoc} */ 115 public int read(final ByteBuffer buffer) throws IOException 116 { 117 final ByteChannel tmp = redirect; 118 if (tmp != null) 119 { 120 return tmp.read(buffer); 121 } 122 else 123 { 124 return child.read(buffer); 125 } 126 } 127 128 129 130 /** 131 * Redirects a byte channel to a byte channel associated with the specified 132 * provider. 133 * 134 * @param provider 135 * The provider to redirect to. 136 */ 137 public final void redirect(final ConnectionSecurityProvider provider) 138 { 139 redirect = provider.getChannel(); 140 } 141 142 143 144 /** {@inheritDoc} */ 145 public int write(final ByteBuffer buffer) throws IOException 146 { 147 final ByteChannel tmp = redirect; 148 if (tmp != null) 149 { 150 return tmp.write(buffer); 151 } 152 else 153 { 154 return child.write(buffer); 155 } 156 } 157}