001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.nhttp.util;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import java.nio.channels.Pipe;
026: import java.nio.channels.Channels;
027: import java.nio.channels.ReadableByteChannel;
028: import java.nio.channels.WritableByteChannel;
029: import java.nio.channels.spi.SelectorProvider;
030: import java.nio.ByteBuffer;
031: import java.io.PipedInputStream;
032: import java.io.PipedOutputStream;
033: import java.io.IOException;
034: import java.io.File;
035:
036: /**
037: * Create a Pipe suitable for the runtime platform. The java.nio.channels.Pipe implementation
038: * on Windows uses TCP ports bound to the loopback interface to implement a Pipe. In Linux and
039: * Solaris this is passed to a native method.
040: */
041: public class PipeImpl {
042:
043: private static final Log log = LogFactory.getLog(PipeImpl.class);
044:
045: private ReadableByteChannel source;
046: private WritableByteChannel sink;
047:
048: private PipedOutputStream pipedOut;
049: protected static boolean useNative;
050:
051: static {
052: // platfom default - Unix - native, Windows - Piped Streams
053: if ("/".equals(File.separator)) {
054: useNative = true;
055: }
056:
057: // has this been overridden?
058: String option = System.getProperty("native_pipes");
059: if (option != null) {
060: // if an option is specified, use it
061: if ("true".equals(option)) {
062: useNative = true;
063: } else if ("false".equals(option)) {
064: useNative = false;
065: }
066: }
067:
068: if (useNative) {
069: log
070: .info("Using native OS Pipes for event-driven to stream IO bridging");
071: } else {
072: log
073: .info("Using simulated buffered Pipes for event-driven to stream IO bridging");
074: }
075: }
076:
077: public PipeImpl() throws IOException {
078: if (useNative) {
079: Pipe pipe = Pipe.open();
080: source = pipe.source();
081: sink = pipe.sink();
082:
083: } else {
084: PipedInputStream pipedIn = new PipedInputStream();
085: try {
086: pipedOut = new PipedOutputStream(pipedIn);
087: } catch (IOException e) {
088: e.printStackTrace();
089: }
090:
091: source = Channels.newChannel(pipedIn);
092: sink = Channels.newChannel(pipedOut);
093: }
094: }
095:
096: public ReadableByteChannel source() {
097: return source;
098: }
099:
100: public WritableByteChannel sink() {
101: return sink;
102: }
103: }
|