├── LICENSE ├── NOTICE ├── activeio-core ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── apache │ │ │ └── activeio │ │ │ ├── adapter │ │ │ ├── PacketByteArrayOutputStream.java │ │ │ ├── PacketInputStream.java │ │ │ ├── PacketOutputStream.java │ │ │ ├── PacketToInputStream.java │ │ │ └── package.html │ │ │ ├── journal │ │ │ ├── InvalidRecordLocationException.java │ │ │ ├── Journal.java │ │ │ ├── JournalEventListener.java │ │ │ ├── RecordLocation.java │ │ │ ├── active │ │ │ │ ├── BatchedWrite.java │ │ │ │ ├── ControlFile.java │ │ │ │ ├── JournalImpl.java │ │ │ │ ├── JournalLockedException.java │ │ │ │ ├── Location.java │ │ │ │ ├── LogFile.java │ │ │ │ ├── LogFileManager.java │ │ │ │ ├── LogFileNode.java │ │ │ │ ├── Record.java │ │ │ │ ├── RecordInfo.java │ │ │ │ └── package.html │ │ │ ├── howl │ │ │ │ ├── HowlJournal.java │ │ │ │ ├── LongRecordLocation.java │ │ │ │ └── package.html │ │ │ └── package.html │ │ │ ├── oneport │ │ │ ├── HttpRecognizer.java │ │ │ ├── IIOPRecognizer.java │ │ │ ├── ProtocolRecognizer.java │ │ │ ├── UnknownRecognizer.java │ │ │ └── package.html │ │ │ ├── packet │ │ │ ├── AppendedPacket.java │ │ │ ├── ByteArrayPacket.java │ │ │ ├── ByteBufferPacket.java │ │ │ ├── ByteBufferPacketPool.java │ │ │ ├── BytePacket.java │ │ │ ├── ByteSequence.java │ │ │ ├── EOSPacket.java │ │ │ ├── EmptyPacket.java │ │ │ ├── FilterPacket.java │ │ │ ├── Packet.java │ │ │ ├── PacketData.java │ │ │ ├── PacketPool.java │ │ │ └── package.html │ │ │ └── xnet │ │ │ ├── ServerService.java │ │ │ ├── ServiceDaemon.java │ │ │ ├── ServiceException.java │ │ │ ├── ServiceLogger.java │ │ │ ├── ServicePool.java │ │ │ ├── SocketService.java │ │ │ ├── StandardServiceStack.java │ │ │ └── hba │ │ │ ├── ExactIPAddressPermission.java │ │ │ ├── ExactIPv6AddressPermission.java │ │ │ ├── FactorizedIPAddressPermission.java │ │ │ ├── IPAddressPermission.java │ │ │ ├── IPAddressPermissionEditor.java │ │ │ ├── IPAddressPermissionFactory.java │ │ │ ├── NetmaskIPAddressPermission.java │ │ │ ├── NetmaskIPv6AddressPermission.java │ │ │ ├── ServiceAccessController.java │ │ │ └── StartWithIPAddressPermission.java │ └── resources │ │ └── META-INF │ │ └── services │ │ └── org │ │ └── apache │ │ └── activeio │ │ └── channel │ │ ├── datagram │ │ ├── multicast │ │ ├── nio │ │ ├── nio-async │ │ ├── nio-sync │ │ ├── socket │ │ ├── ssl │ │ └── vmpipe │ └── test │ ├── java │ └── org │ │ └── apache │ │ └── activeio │ │ ├── PacketDataTest.java │ │ ├── journal │ │ ├── JournalPerfToolSupport.java │ │ ├── JournalRWPerfToolSupport.java │ │ ├── JournalStatsFilter.java │ │ ├── active │ │ │ ├── DataStruturesTest.java │ │ │ ├── JournalImplTest.java │ │ │ ├── JournalPerfTool.java │ │ │ ├── JournalRWPerfTool.java │ │ │ └── LogFileManagerTest.java │ │ └── howl │ │ │ └── JournalPerfTool.java │ │ ├── packet │ │ ├── AppendedPacketTest.java │ │ ├── ByteArrayPacketTest.java │ │ ├── ByteBufferPacketTest.java │ │ └── PacketTestSupport.java │ │ ├── stats │ │ ├── CountStatisticImpl.java │ │ ├── IndentPrinter.java │ │ ├── StatisticImpl.java │ │ ├── TimeStatisticImpl.java │ │ └── package.html │ │ └── xnet │ │ └── hba │ │ └── ServiceAccessControllerTest.java │ └── resources │ ├── client.keystore │ ├── log4j.properties │ └── server.keystore └── pom.xml /NOTICE: -------------------------------------------------------------------------------- 1 | Apache ActiveMQ ActiveIO 2 | Copyright 2005-2009 Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | 7 | -------------------------------------------------------------------------------- /activeio-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 4.0.0 21 | 22 | org.apache.activemq 23 | activeio-parent 24 | 3.2-SNAPSHOT 25 | 26 | 27 | activeio-core 28 | bundle 29 | ActiveIO :: Core 30 | A high performance IO abstraction framework 31 | 32 | 33 | org.objectweb.howl.log*;resolution:=optional, 34 | * 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | maven-jar-plugin 44 | 45 | 46 | 47 | test-jar 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | maven-surefire-plugin 56 | 57 | 58 | **/*Test.* 59 | 60 | 61 | 62 | **/ChannelFactoryTest.* 63 | 64 | 65 | **/NIOAsyncChannelTest.* 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.geronimo.specs 77 | geronimo-j2ee-management_1.1_spec 78 | 79 | 80 | commons-logging 81 | commons-logging 82 | 83 | 84 | 85 | 86 | howl 87 | howl-logger 88 | true 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/adapter/PacketByteArrayOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.adapter; 19 | 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | 23 | import org.apache.activeio.packet.AppendedPacket; 24 | import org.apache.activeio.packet.ByteArrayPacket; 25 | import org.apache.activeio.packet.Packet; 26 | 27 | /** 28 | * 29 | */ 30 | final public class PacketByteArrayOutputStream extends OutputStream { 31 | 32 | private Packet result; 33 | private Packet current; 34 | int nextAllocationSize=0; 35 | 36 | public PacketByteArrayOutputStream() { 37 | this( 1024 ); 38 | } 39 | 40 | public PacketByteArrayOutputStream(int initialSize) { 41 | nextAllocationSize = initialSize; 42 | current = allocate(); 43 | } 44 | 45 | protected Packet allocate() { 46 | ByteArrayPacket packet = new ByteArrayPacket(new byte[nextAllocationSize]); 47 | nextAllocationSize <<= 3; // x by 8 48 | return packet; 49 | } 50 | 51 | public void skip(int size) { 52 | while( size > 0 ) { 53 | if( !current.hasRemaining() ) { 54 | allocatedNext(); 55 | } 56 | 57 | int skip = ((size <= current.remaining()) ? size : current.remaining()); 58 | current.position(current.position()+skip); 59 | size -= skip; 60 | } 61 | } 62 | 63 | public void write(int b) throws IOException { 64 | if( !current.hasRemaining() ) { 65 | allocatedNext(); 66 | } 67 | current.write(b); 68 | } 69 | 70 | public void write(byte[] b, int off, int len) throws IOException { 71 | while( len > 0 ) { 72 | if( !current.hasRemaining() ) { 73 | allocatedNext(); 74 | } 75 | int wrote = current.write(b,off,len); 76 | off+=wrote; 77 | len-=wrote; 78 | } 79 | } 80 | 81 | private void allocatedNext() { 82 | if( result == null ) { 83 | current.flip(); 84 | result = current; 85 | } else { 86 | current.flip(); 87 | result = AppendedPacket.join(result, current); 88 | } 89 | current = allocate(); 90 | } 91 | 92 | public Packet getPacket() { 93 | if( result == null ) { 94 | current.flip(); 95 | return current.slice(); 96 | } else { 97 | current.flip(); 98 | return AppendedPacket.join(result, current); 99 | } 100 | } 101 | 102 | public void reset() { 103 | result = null; 104 | current.clear(); 105 | } 106 | 107 | public int position() { 108 | return current.position() + (result==null ? 0 : result.remaining()); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/adapter/PacketInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.adapter; 19 | 20 | import org.apache.activeio.packet.Packet; 21 | 22 | /** 23 | * @deprecated Use PacketToInputStream instead. This class will be removed very soon. 24 | */ 25 | public class PacketInputStream extends PacketToInputStream { 26 | public PacketInputStream(Packet packet) { 27 | super(packet); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/adapter/PacketOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.adapter; 19 | 20 | import java.io.IOException; 21 | import java.io.OutputStream; 22 | 23 | import org.apache.activeio.packet.Packet; 24 | 25 | /** 26 | * Provides an OutputStream for a given Packet. 27 | * 28 | * @version $Revision$ 29 | */ 30 | public class PacketOutputStream extends OutputStream { 31 | 32 | final Packet packet; 33 | 34 | public PacketOutputStream(Packet packet) { 35 | this.packet = packet; 36 | } 37 | 38 | public void write(int b) throws IOException { 39 | if( !packet.write(b) ) 40 | throw new IOException("Packet does not have any remaining space to write to."); 41 | } 42 | 43 | public void write(byte[] b, int off, int len) throws IOException { 44 | if( packet.write(b, off, len)!=len ) 45 | throw new IOException("Packet does not have "+len+" byte(s) left to write to."); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/adapter/PacketToInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.adapter; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | 23 | import org.apache.activeio.packet.Packet; 24 | 25 | /** 26 | * Provides an InputStream for a given Packet. 27 | * 28 | * @version $Revision$ 29 | */ 30 | public class PacketToInputStream extends InputStream { 31 | 32 | final Packet packet; 33 | 34 | /** 35 | * @param packet 36 | */ 37 | public PacketToInputStream(Packet packet) { 38 | this.packet = packet; 39 | } 40 | 41 | /** 42 | * @see java.io.InputStream#read() 43 | */ 44 | public int read() throws IOException { 45 | return packet.read(); 46 | } 47 | 48 | /** 49 | * @see java.io.InputStream#read(byte[], int, int) 50 | */ 51 | public int read(byte[] b, int off, int len) throws IOException { 52 | return packet.read(b, off, len); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/adapter/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | The Adapter package provides classes that make it easy ot bridge between the the 24 | SynchChannel, AsyncChannel, InputStream, OutputStream, Socket, and ServerSocket domains. 25 |

26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/InvalidRecordLocationException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal; 19 | 20 | /** 21 | * Exception thrown by a Journal to indicate that an invalid RecordLocation was detected. 22 | * 23 | * @version $Revision: 1.1 $ 24 | */ 25 | public class InvalidRecordLocationException extends Exception { 26 | 27 | /** 28 | * Comment for serialVersionUID 29 | */ 30 | private static final long serialVersionUID = 3618414947307239475L; 31 | 32 | /** 33 | * 34 | */ 35 | public InvalidRecordLocationException() { 36 | super(); 37 | } 38 | 39 | /** 40 | * @param msg 41 | */ 42 | public InvalidRecordLocationException(String msg) { 43 | super(msg); 44 | } 45 | 46 | /** 47 | * @param msg 48 | * @param rootCause 49 | */ 50 | public InvalidRecordLocationException(String msg, Throwable rootCause) { 51 | super(msg, rootCause); 52 | } 53 | 54 | /** 55 | * @param rootCause 56 | */ 57 | public InvalidRecordLocationException(Throwable rootCause) { 58 | super(rootCause); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/Journal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal; 19 | 20 | import java.io.IOException; 21 | 22 | import org.apache.activeio.packet.Packet; 23 | 24 | /** 25 | * A Journal is a record logging Interface that can be used to implement 26 | * a transaction log. 27 | * 28 | * 29 | * This interface was largely extracted out of the HOWL project to allow 30 | * ActiveMQ to switch between different Journal implementations verry easily. 31 | * 32 | * @version $Revision: 1.1 $ 33 | */ 34 | public interface Journal { 35 | 36 | /** 37 | * Writes a {@see Packet} of data to the journal. If sync 38 | * is true, then this call blocks until the data has landed on the physical 39 | * disk. Otherwise, this enqueues the write request and returns. 40 | * 41 | * @param record - the data to be written to disk. 42 | * @param sync - If this call should block until the data lands on disk. 43 | * 44 | * @return RecordLocation the location where the data will be written to on disk. 45 | * 46 | * @throws IOException if the write failed. 47 | * @throws IllegalStateException if the journal is closed. 48 | */ 49 | public RecordLocation write(Packet packet, boolean sync) throws IOException, IllegalStateException; 50 | 51 | /** 52 | * Reads a previously written record from the journal. 53 | * 54 | * @param location is where to read the record from. 55 | * 56 | * @return the data previously written at the location. 57 | * 58 | * @throws InvalidRecordLocationException if location parameter is out of range. 59 | * It cannot be a location that is before the current mark. 60 | * @throws IOException if the record could not be read. 61 | * @throws IllegalStateException if the journal is closed. 62 | */ 63 | public Packet read(RecordLocation location) throws InvalidRecordLocationException, IOException, IllegalStateException; 64 | 65 | /** 66 | * Informs the journal that all the journal space up to the location is no longer 67 | * needed and can be reclaimed for reuse. 68 | * 69 | * @param location the location of the record to mark. All record locations before the marked 70 | * location will no longger be vaild. 71 | * 72 | * @param sync if this call should block until the mark is set on the journal. 73 | * 74 | * @throws InvalidRecordLocationException if location parameter is out of range. 75 | * It cannot be a location that is before the current mark. 76 | * @throws IOException if the record could not be read. 77 | * @throws IllegalStateException if the journal is closed. 78 | */ 79 | public abstract void setMark(RecordLocation location, boolean sync) 80 | throws InvalidRecordLocationException, IOException, IllegalStateException; 81 | 82 | /** 83 | * Obtains the mark that was set in the Journal. 84 | * 85 | * @see read(RecordLocation location); 86 | * @return the mark that was set in the Journal. 87 | * @throws IllegalStateException if the journal is closed. 88 | */ 89 | public RecordLocation getMark() throws IllegalStateException; 90 | 91 | 92 | /** 93 | * Close the Journal. 94 | * This is blocking operation that waits for any pending put opperations to be forced to disk. 95 | * Once the Journal is closed, all other methods of the journal should throw IllegalStateException. 96 | * 97 | * @throws IOException if an error occurs while the journal is being closed. 98 | */ 99 | public abstract void close() throws IOException; 100 | 101 | /** 102 | * Allows you to get the next RecordLocation after the location that 103 | * is in the journal. 104 | * 105 | * @param location the reference location the is used to find the next location. 106 | * To get the oldest location available in the journal, location 107 | * should be set to null. 108 | * 109 | * 110 | * @return the next record location 111 | * 112 | * @throws InvalidRecordLocationException if location parameter is out of range. 113 | * It cannot be a location that is before the current mark. 114 | * @throws IllegalStateException if the journal is closed. 115 | */ 116 | public abstract RecordLocation getNextRecordLocation(RecordLocation location) 117 | throws InvalidRecordLocationException, IOException, IllegalStateException; 118 | 119 | 120 | /** 121 | * Registers a JournalEventListener that will receive notifications from the Journal. 122 | * 123 | * @param listener object that will receive journal events. 124 | * @throws IllegalStateException if the journal is closed. 125 | */ 126 | public abstract void setJournalEventListener(JournalEventListener listener) throws IllegalStateException; 127 | 128 | } 129 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/JournalEventListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal; 19 | 20 | /** 21 | * Defines an object which listens for Journal Events. 22 | * 23 | * @version $Revision: 1.1 $ 24 | */ 25 | public interface JournalEventListener { 26 | 27 | /** 28 | * This event is issues when a Journal implementations wants to recover 29 | * disk space used by old records. If journal space is not reliquised 30 | * by setting the Journal's mark at or past the safeLocation 31 | * further write opperations against the Journal may casuse IOExceptions 32 | * to occur due to a log overflow condition. 33 | * 34 | * @param safeLocation the oldest location that the journal recomends the mark to be set. 35 | */ 36 | void overflowNotification(RecordLocation safeLocation); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/RecordLocation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal; 19 | 20 | /** 21 | * A RecordLocation is used to locate data records that have been 22 | * logged to a Journal via the Journal.put() method call. 23 | * 24 | * RecordLocation are comparable on the position in the Journal 25 | * where they reside. 26 | * 27 | * @version $Revision: 1.1 $ 28 | */ 29 | public interface RecordLocation extends Comparable { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/BatchedWrite.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import org.apache.activeio.packet.Packet; 21 | 22 | import java.util.concurrent.CountDownLatch; 23 | 24 | /** 25 | * This contains all the data needed to write and force a list of records to a 26 | * LogFile. The more records that can be cramed into a single BatchedWrite, the 27 | * higher throughput that can be achived by a write and force operation. 28 | * 29 | * @version $Revision: 1.1 $ 30 | */ 31 | final public class BatchedWrite { 32 | 33 | private final Packet packet; 34 | public Throwable error; 35 | private Location mark; 36 | private boolean appendDisabled = false; 37 | private boolean appendInProgress = false; 38 | private CountDownLatch writeDoneCountDownLatch; 39 | 40 | /** 41 | * @param packet 42 | */ 43 | public BatchedWrite(Packet packet) { 44 | this.packet = packet; 45 | } 46 | 47 | /** 48 | * @throws InterruptedException 49 | * 50 | */ 51 | synchronized private void disableAppend() throws InterruptedException { 52 | appendDisabled = true; 53 | while (appendInProgress) { 54 | wait(); 55 | } 56 | } 57 | 58 | /** 59 | * @param packet2 60 | * @param mark2 61 | * @return 62 | */ 63 | public boolean append(Record record, Location recordMark, boolean force) { 64 | 65 | synchronized (this) { 66 | if (appendDisabled) 67 | return false; 68 | appendInProgress = true; 69 | } 70 | 71 | 72 | if( force && writeDoneCountDownLatch==null) 73 | writeDoneCountDownLatch = new CountDownLatch(1); 74 | 75 | record.read(packet); 76 | 77 | // if we fit the record in this batch 78 | if ( !record.hasRemaining() ) { 79 | if (recordMark != null) 80 | mark = recordMark; 81 | } 82 | 83 | synchronized (this) { 84 | appendInProgress = false; 85 | this.notify(); 86 | 87 | if (appendDisabled) 88 | return false; 89 | else 90 | return packet.remaining() > 0; 91 | } 92 | } 93 | 94 | public void waitForForce() throws Throwable { 95 | if( writeDoneCountDownLatch!=null ) { 96 | writeDoneCountDownLatch.await(); 97 | synchronized (this) { 98 | if (error != null) 99 | throw error; 100 | } 101 | } 102 | } 103 | 104 | public void forced() { 105 | if( writeDoneCountDownLatch!=null ) { 106 | writeDoneCountDownLatch.countDown(); 107 | } 108 | } 109 | 110 | public void writeFailed(Throwable error) { 111 | if( writeDoneCountDownLatch!=null ) { 112 | synchronized (this) { 113 | this.error = error; 114 | } 115 | writeDoneCountDownLatch.countDown(); 116 | } 117 | } 118 | 119 | public Packet getPacket() { 120 | return packet; 121 | } 122 | 123 | /** 124 | * @return 125 | */ 126 | public Location getMark() { 127 | return mark; 128 | } 129 | 130 | /** 131 | * @throws InterruptedException 132 | * 133 | */ 134 | public void flip() throws InterruptedException { 135 | disableAppend(); 136 | packet.flip(); 137 | } 138 | 139 | public boolean getForce() { 140 | return writeDoneCountDownLatch!=null; 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/ControlFile.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.io.RandomAccessFile; 23 | import java.nio.ByteBuffer; 24 | import java.nio.channels.FileChannel; 25 | import java.nio.channels.FileLock; 26 | import java.util.HashSet; 27 | import java.util.Properties; 28 | import java.util.Set; 29 | 30 | import org.apache.activeio.packet.ByteBufferPacket; 31 | import org.apache.activeio.packet.Packet; 32 | 33 | /** 34 | * Control file holds the last known good state of the journal. It stores the state in 35 | * record that is versioned and repeated twice in the file so that a failure in the 36 | * middle of the write of the first or second record do not not result in an unknown 37 | * state. 38 | * 39 | * @version $Revision: 1.1 $ 40 | */ 41 | final public class ControlFile { 42 | 43 | /** The File that holds the control data. */ 44 | private final RandomAccessFile file; 45 | private final FileChannel channel; 46 | private final ByteBufferPacket controlData; 47 | 48 | private final static boolean brokenFileLock = "true".equals(System.getProperty("java.nio.channels.FileLock.broken", "false")); 49 | 50 | private long controlDataVersion=0; 51 | private FileLock lock; 52 | private boolean disposed; 53 | private static Set lockSet; 54 | private String canonicalPath; 55 | 56 | public ControlFile(File fileName, int controlDataSize) throws IOException { 57 | canonicalPath = fileName.getCanonicalPath(); 58 | boolean existed = fileName.exists(); 59 | file = new RandomAccessFile(fileName, "rw"); 60 | channel = file.getChannel(); 61 | controlData = new ByteBufferPacket(ByteBuffer.allocateDirect(controlDataSize)); 62 | 63 | } 64 | 65 | /** 66 | * Locks the control file. 67 | * @throws IOException 68 | */ 69 | public void lock() throws IOException { 70 | 71 | Properties properties = System.getProperties(); 72 | synchronized(properties) { 73 | String lockKey = "org.apache.activeio.journal.active.lockMap:"+canonicalPath; 74 | if( properties.setProperty(lockKey, "true")!=null ) { 75 | throw new JournalLockedException("Journal is already opened by this application."); 76 | } 77 | 78 | if( !brokenFileLock ) { 79 | lock = channel.tryLock(); 80 | if (lock == null) { 81 | properties.remove(lockKey); 82 | throw new JournalLockedException("Journal is already opened by another application"); 83 | } 84 | } 85 | } 86 | } 87 | 88 | /** 89 | * Un locks the control file. 90 | * 91 | * @throws IOException 92 | */ 93 | public void unlock() throws IOException { 94 | 95 | Properties properties = System.getProperties(); 96 | synchronized(properties) { 97 | if (lock != null) { 98 | String lockKey = "org.apache.activeio.journal.active.lockMap:"+canonicalPath; 99 | properties.remove(lockKey); 100 | lock.release(); 101 | lock = null; 102 | } 103 | } 104 | } 105 | 106 | public boolean load() throws IOException { 107 | long l = file.length(); 108 | if( l < controlData.capacity() ) { 109 | controlDataVersion=0; 110 | controlData.position(0); 111 | controlData.limit(0); 112 | return false; 113 | } else { 114 | file.seek(0); 115 | long v1 = file.readLong(); 116 | file.seek(controlData.capacity()+8); 117 | long v1check = file.readLong(); 118 | 119 | file.seek(controlData.capacity()+16); 120 | long v2 = file.readLong(); 121 | file.seek((controlData.capacity()*2)+24); 122 | long v2check = file.readLong(); 123 | 124 | if( v2 == v2check ) { 125 | controlDataVersion = v2; 126 | file.seek(controlData.capacity()+24); 127 | controlData.clear(); 128 | channel.read(controlData.getByteBuffer()); 129 | } else if ( v1 == v1check ){ 130 | controlDataVersion = v1; 131 | file.seek(controlData.capacity()+8); 132 | controlData.clear(); 133 | channel.read(controlData.getByteBuffer()); 134 | } else { 135 | // Bummer.. Both checks are screwed. we don't know 136 | // if any of the two buffer are ok. This should 137 | // only happen is data got corrupted. 138 | throw new IOException("Control data corrupted."); 139 | } 140 | return true; 141 | } 142 | } 143 | 144 | public void store() throws IOException { 145 | controlDataVersion++; 146 | file.setLength((controlData.capacity()*2)+32); 147 | file.seek(0); 148 | 149 | // Write the first copy of the control data. 150 | file.writeLong(controlDataVersion); 151 | controlData.clear(); 152 | channel.write(controlData.getByteBuffer()); 153 | file.writeLong(controlDataVersion); 154 | 155 | // Write the second copy of the control data. 156 | file.writeLong(controlDataVersion); 157 | controlData.clear(); 158 | channel.write(controlData.getByteBuffer()); 159 | file.writeLong(controlDataVersion); 160 | 161 | channel.force(false); 162 | } 163 | 164 | public Packet getControlData() { 165 | controlData.clear(); 166 | return controlData; 167 | } 168 | 169 | public void dispose() { 170 | if( disposed ) 171 | return; 172 | disposed=true; 173 | try { 174 | unlock(); 175 | } catch (IOException e) { 176 | } 177 | try { 178 | file.close(); 179 | } catch (IOException e) { 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/JournalLockedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * @version $Revision$ 24 | */ 25 | public class JournalLockedException extends IOException { 26 | 27 | private static final long serialVersionUID = -3696987774575855799L; 28 | 29 | public JournalLockedException() { 30 | super(); 31 | } 32 | 33 | /** 34 | * @param arg0 35 | */ 36 | public JournalLockedException(String arg0) { 37 | super(arg0); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/Location.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.DataInput; 21 | import java.io.DataOutput; 22 | import java.io.IOException; 23 | 24 | import org.apache.activeio.journal.RecordLocation; 25 | import org.apache.activeio.packet.Packet; 26 | import org.apache.activeio.packet.PacketData; 27 | 28 | /** 29 | * Defines a where a record can be located in the Journal. 30 | * 31 | * @version $Revision: 1.1 $ 32 | */ 33 | final public class Location implements RecordLocation { 34 | 35 | static final public int SERIALIZED_SIZE=8; 36 | 37 | final private int logFileId; 38 | final private int logFileOffset; 39 | 40 | public Location(int logFileId, int fileOffset) { 41 | this.logFileId = logFileId; 42 | this.logFileOffset = fileOffset; 43 | } 44 | 45 | public int compareTo(Object o) { 46 | int rc = logFileId - ((Location) o).logFileId; 47 | if (rc != 0) 48 | return rc; 49 | 50 | return logFileOffset - ((Location) o).logFileOffset; 51 | } 52 | 53 | public int hashCode() { 54 | return logFileOffset ^ logFileId; 55 | } 56 | 57 | public boolean equals(Object o) { 58 | if (o == null || o.getClass() != Location.class) 59 | return false; 60 | Location rl = (Location) o; 61 | return rl.logFileId == this.logFileId && rl.logFileOffset == this.logFileOffset; 62 | } 63 | 64 | public String toString() { 65 | return "" + logFileId + ":" + logFileOffset; 66 | } 67 | 68 | public int getLogFileId() { 69 | return logFileId; 70 | } 71 | 72 | public int getLogFileOffset() { 73 | return logFileOffset; 74 | } 75 | 76 | public void writeToPacket(Packet packet) throws IOException { 77 | PacketData data = new PacketData(packet); 78 | data.writeInt(logFileId); 79 | data.writeInt(logFileOffset); 80 | } 81 | 82 | public void writeToDataOutput(DataOutput data) throws IOException { 83 | data.writeInt(logFileId); 84 | data.writeInt(logFileOffset); 85 | } 86 | 87 | static public Location readFromPacket(Packet packet) throws IOException { 88 | PacketData data = new PacketData(packet); 89 | return new Location(data.readInt(), data.readInt()); 90 | } 91 | 92 | public static Location readFromDataInput(DataInput data) throws IOException { 93 | return new Location(data.readInt(), data.readInt()); 94 | } 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/LogFile.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.File; 21 | import java.io.FileOutputStream; 22 | import java.io.IOException; 23 | import java.io.RandomAccessFile; 24 | import java.nio.ByteBuffer; 25 | import java.nio.channels.FileChannel; 26 | 27 | /** 28 | * Allows read/append access to a LogFile. 29 | * 30 | * @version $Revision: 1.1 $ 31 | */ 32 | final public class LogFile { 33 | 34 | private final RandomAccessFile file; 35 | private final FileChannel channel; 36 | 37 | /** Prefered size. The size that the log file is set to when initilaized. */ 38 | private final int initialSize; 39 | 40 | /** Where the we are in the file right now */ 41 | private int currentOffset; 42 | private boolean disposed; 43 | 44 | public LogFile(File file, int initialSize) throws IOException { 45 | this.initialSize = initialSize; 46 | boolean initializationNeeeded = !file.exists(); 47 | this.file = new RandomAccessFile(file, "rw"); 48 | channel = this.file.getChannel(); 49 | if( initializationNeeeded ) 50 | resize(); 51 | channel.position(0); 52 | reloadCurrentOffset(); 53 | } 54 | 55 | /** 56 | * To avoid doing un-needed seeks. 57 | */ 58 | private void seek(int offset) throws IOException { 59 | if( offset == currentOffset ) { 60 | if( currentOffset != channel.position() ) 61 | throw new RuntimeException(" "+currentOffset+", "+channel.position() ); 62 | return; 63 | } 64 | channel.position(offset); 65 | currentOffset = offset; 66 | } 67 | private void reloadCurrentOffset() throws IOException { 68 | currentOffset= (int) channel.position(); 69 | } 70 | private void addToCurrentOffset(int rc) { 71 | currentOffset+=rc; 72 | } 73 | 74 | public boolean loadAndCheckRecord(int offset, Record record) throws IOException { 75 | 76 | try { 77 | // Read the next header 78 | seek(offset); 79 | record.readHeader(file); 80 | 81 | if (Record.isChecksumingEnabled()) { 82 | record.checksum(file); 83 | } 84 | // Load the footer. 85 | seek(offset+record.getPayloadLength()+Record.RECORD_HEADER_SIZE); 86 | record.readFooter(file); 87 | 88 | addToCurrentOffset(record.getRecordLength()); 89 | return true; 90 | 91 | } catch (IOException e) { 92 | reloadCurrentOffset(); 93 | return false; 94 | } 95 | } 96 | 97 | public void resize() throws IOException { 98 | file.setLength(initialSize); 99 | } 100 | 101 | public void force() throws IOException { 102 | channel.force(false); 103 | } 104 | 105 | public void dispose() { 106 | if( disposed ) 107 | return; 108 | disposed=true; 109 | try { 110 | this.file.close(); 111 | } catch (IOException e) { 112 | } 113 | } 114 | 115 | public void write(int offset, ByteBuffer buffer) throws IOException { 116 | 117 | try { 118 | 119 | int size = buffer.remaining(); 120 | seek(offset); 121 | while (buffer.hasRemaining()) { 122 | channel.write(buffer); 123 | } 124 | addToCurrentOffset(size); 125 | 126 | } catch (IOException e) { 127 | reloadCurrentOffset(); 128 | } 129 | } 130 | 131 | public void readRecordHeader(int offset, Record record) throws IOException { 132 | seek(offset); 133 | try { 134 | record.readHeader(file); 135 | } catch ( IOException e ) { 136 | reloadCurrentOffset(); 137 | throw e; 138 | } 139 | addToCurrentOffset(Record.RECORD_HEADER_SIZE); 140 | } 141 | 142 | public void read(int offset, byte[] answer) throws IOException { 143 | seek(offset); 144 | file.readFully(answer); 145 | addToCurrentOffset(answer.length); 146 | } 147 | 148 | public void copyTo(File location) throws IOException { 149 | FileOutputStream fos = new FileOutputStream(location); 150 | channel.transferTo(0, channel.size(), fos.getChannel()); 151 | fos.getChannel().force(false); 152 | fos.close(); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/LogFileNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.DataInput; 21 | import java.io.DataOutput; 22 | import java.io.IOException; 23 | 24 | /** 25 | * @version $Revision: 1.1 $ 26 | */ 27 | final class LogFileNode { 28 | 29 | static final public int SERIALIZED_SIZE = 10; 30 | 31 | private final LogFile logFile; 32 | private LogFileNode next; 33 | 34 | /** The id of the log file. */ 35 | private int id; 36 | /** Does it have live records in it? */ 37 | private boolean active = false; 38 | /** Is the log file in readonly mode */ 39 | private boolean readOnly; 40 | /** The location of the next append offset */ 41 | private int appendOffset = 0; 42 | 43 | public LogFileNode(LogFile logFile) { 44 | this.logFile = logFile; 45 | } 46 | 47 | public LogFile getLogFile() { 48 | return logFile; 49 | } 50 | 51 | ///////////////////////////////////////////////////////////// 52 | // 53 | // Method used to mange the state of the log file. 54 | // 55 | ///////////////////////////////////////////////////////////// 56 | 57 | public void activate(int id) { 58 | if (active) 59 | throw new IllegalStateException("Log already active."); 60 | this.id = id; 61 | this.readOnly = false; 62 | this.active = true; 63 | this.appendOffset = 0; 64 | } 65 | 66 | public int getId() { 67 | return id; 68 | } 69 | 70 | public void setReadOnly(boolean enable) { 71 | if (!active) 72 | throw new IllegalStateException("Log not active."); 73 | this.readOnly = enable; 74 | } 75 | 76 | public void deactivate() throws IOException { 77 | if (!active) 78 | throw new IllegalStateException("Log already inactive."); 79 | this.active=false; 80 | this.id = -1; 81 | this.readOnly = true; 82 | this.appendOffset = 0; 83 | getLogFile().resize(); 84 | } 85 | 86 | public boolean isActive() { 87 | return active; 88 | } 89 | 90 | public int getAppendOffset() { 91 | return appendOffset; 92 | } 93 | 94 | public Location getFirstRecordLocation() { 95 | if (isActive() && appendOffset > 0) 96 | return new Location(getId(), 0); 97 | return null; 98 | } 99 | 100 | public boolean isReadOnly() { 101 | return readOnly; 102 | } 103 | 104 | public void appended(int i) { 105 | appendOffset += i; 106 | } 107 | 108 | ///////////////////////////////////////////////////////////// 109 | // 110 | // Method used to maintain the list of LogFileNodes used by 111 | // the LogFileManager 112 | // 113 | ///////////////////////////////////////////////////////////// 114 | 115 | public LogFileNode getNext() { 116 | return next; 117 | } 118 | 119 | public void setNext(LogFileNode state) { 120 | next = state; 121 | } 122 | 123 | public LogFileNode getNextActive() { 124 | if (getNext().isActive()) 125 | return getNext(); 126 | return null; 127 | } 128 | 129 | public LogFileNode getNextInactive() { 130 | if (!getNext().isActive()) 131 | return getNext(); 132 | return null; 133 | } 134 | 135 | /** 136 | * @param data 137 | * @throws IOException 138 | */ 139 | public void writeExternal(DataOutput data) throws IOException { 140 | data.writeInt(id); 141 | data.writeBoolean(active); 142 | data.writeBoolean(readOnly); 143 | data.writeInt(appendOffset); 144 | } 145 | 146 | /** 147 | * @param data 148 | * @throws IOException 149 | */ 150 | public void readExternal(DataInput data) throws IOException { 151 | id = data.readInt(); 152 | active = data.readBoolean(); 153 | readOnly = data.readBoolean(); 154 | appendOffset = data.readInt(); 155 | } 156 | 157 | public void setAppendOffset(int offset) { 158 | appendOffset = offset; 159 | } 160 | 161 | } 162 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/RecordInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | /** 21 | * @version $Revision: 1.1 $ 22 | */ 23 | final public class RecordInfo { 24 | 25 | private final Location location; 26 | private final Record header; 27 | private final LogFileNode logFileState; 28 | private final LogFile logFile; 29 | 30 | public RecordInfo(Location location, Record header, LogFileNode logFileState, LogFile logFile) { 31 | this.location = location; 32 | this.header = header; 33 | this.logFileState = logFileState; 34 | this.logFile = logFile; 35 | } 36 | 37 | int getNextLocation() { 38 | return location.getLogFileOffset() + header.getPayloadLength() + Record.RECORD_BASE_SIZE; 39 | } 40 | 41 | public Record getHeader() { 42 | return header; 43 | } 44 | 45 | public Location getLocation() { 46 | return location; 47 | } 48 | 49 | public LogFileNode getLogFileState() { 50 | return logFileState; 51 | } 52 | 53 | public LogFile getLogFile() { 54 | return logFile; 55 | } 56 | 57 | public int getDataOffset() { 58 | return location.getLogFileOffset() + Record.RECORD_HEADER_SIZE; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/active/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | The Active Journal is a high performance Journal implemenation which does not 24 | place limits on how big the data being logged can be. 25 |

26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/howl/LongRecordLocation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.howl; 19 | 20 | import org.apache.activeio.journal.RecordLocation; 21 | 22 | /** 23 | * Provides a RecordLocation implementation for the long based 24 | * location pointers that HOWL uses. 25 | * 26 | * @version $Revision: 1.1 $ 27 | */ 28 | public class LongRecordLocation implements RecordLocation { 29 | 30 | final private long location; 31 | 32 | public LongRecordLocation(long l) { 33 | this.location = l; 34 | } 35 | 36 | /** 37 | * @see java.lang.Comparable#compareTo(java.lang.Object) 38 | */ 39 | public int compareTo(Object o) { 40 | return (int) (location - ((LongRecordLocation) o).location); 41 | } 42 | 43 | /** 44 | * @return the original long location provided by HOWL 45 | */ 46 | public long getLongLocation() { 47 | return location; 48 | } 49 | 50 | /** 51 | * @see java.lang.Object#hashCode() 52 | */ 53 | public int hashCode() { 54 | int lowPart = (int) (0xFFFFFFFF & location); 55 | int highPart = (int) (0xFFFFFFFF & (location >> 4)); 56 | return lowPart ^ highPart; 57 | } 58 | 59 | /** 60 | * @see java.lang.Object#equals(java.lang.Object) 61 | */ 62 | public boolean equals(Object o) { 63 | if (o == null || o.getClass() != LongRecordLocation.class) 64 | return false; 65 | return ((LongRecordLocation) o).location == location; 66 | } 67 | 68 | /** 69 | * @see java.lang.Object#toString() 70 | */ 71 | public String toString() { 72 | return "0x" + Long.toHexString(location); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/howl/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

A Journal implemenation using using a high performance transaction log 23 | implemented using Howl

24 | 25 | 26 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/journal/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | Provides the API for storing and accessing record based binary data in sequential log files. 24 |

25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/oneport/HttpRecognizer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.oneport; 19 | 20 | import java.util.HashSet; 21 | 22 | import org.apache.activeio.packet.Packet; 23 | 24 | 25 | public class HttpRecognizer implements ProtocolRecognizer { 26 | 27 | static private HashSet methods = new HashSet(); 28 | static { 29 | // This list built using: http://www.w3.org/Protocols/HTTP/Methods.html 30 | methods.add("GET "); 31 | methods.add("PUT "); 32 | methods.add("POST "); 33 | methods.add("HEAD "); 34 | methods.add("LINK "); 35 | methods.add("TRACE "); 36 | methods.add("UNLINK "); 37 | methods.add("SEARCH "); 38 | methods.add("DELETE "); 39 | methods.add("CHECKIN "); 40 | methods.add("OPTIONS "); 41 | methods.add("CONNECT "); 42 | methods.add("CHECKOUT "); 43 | methods.add("SPACEJUMP "); 44 | methods.add("SHOWMETHOD "); 45 | methods.add("TEXTSEARCH "); 46 | } 47 | 48 | static final public HttpRecognizer HTTP_RECOGNIZER = new HttpRecognizer(); 49 | 50 | private HttpRecognizer() {} 51 | 52 | public boolean recognizes(Packet packet) { 53 | 54 | StringBuffer b = new StringBuffer(12); 55 | for (int i = 0; i < 11; i++) { 56 | int c = (char)packet.read(); 57 | if( c == -1) 58 | return false; 59 | 60 | b.append((char)c); 61 | if(((char)c)==' ') 62 | break; 63 | } 64 | 65 | return methods.contains(b.toString()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/oneport/IIOPRecognizer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.oneport; 19 | 20 | import org.apache.activeio.packet.Packet; 21 | 22 | 23 | public class IIOPRecognizer implements ProtocolRecognizer { 24 | 25 | static final public IIOPRecognizer IIOP_RECOGNIZER = new IIOPRecognizer(); 26 | 27 | private IIOPRecognizer() {} 28 | 29 | public boolean recognizes(Packet packet) { 30 | return ( 31 | packet.read()=='G' && 32 | packet.read()=='I' && 33 | packet.read()=='O' && 34 | packet.read()=='P' 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/oneport/ProtocolRecognizer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.oneport; 19 | 20 | import org.apache.activeio.packet.Packet; 21 | 22 | /** 23 | * 24 | */ 25 | public interface ProtocolRecognizer { 26 | boolean recognizes(Packet packet); 27 | } 28 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/oneport/UnknownRecognizer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.oneport; 19 | 20 | import org.apache.activeio.packet.Packet; 21 | 22 | 23 | class UnknownRecognizer implements ProtocolRecognizer { 24 | 25 | static public final ProtocolRecognizer UNKNOWN_RECOGNIZER = new UnknownRecognizer(); 26 | 27 | private UnknownRecognizer() { 28 | } 29 | 30 | public boolean recognizes(Packet packet) { 31 | if( packet.limit() > 15 ) 32 | return true; 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/oneport/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | This package provides the implementing classes that allow you to run multiple protocols 24 | from a sigle port. 25 | 26 | The only restriction is that all the protococols sharing the port must provide a magic 27 | number in the intial request message to the server. 28 |

29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/ByteBufferPacketPool.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * Provides a simple pool of ByteBuffer objects. 25 | * 26 | * @version $Revision: 1.1 $ 27 | */ 28 | final public class ByteBufferPacketPool extends PacketPool { 29 | 30 | private final int packetSize; 31 | 32 | /** 33 | * Creates a pool of bufferCount ByteBuffers that are 34 | * directly allocated being bufferSize big. 35 | * 36 | * @param packetCount the number of buffers that will be in the pool. 37 | * @param packetSize the size of the buffers that are in the pool. 38 | */ 39 | public ByteBufferPacketPool(int packetCount,int packetSize) { 40 | super(packetCount); 41 | this.packetSize = packetSize; 42 | } 43 | 44 | protected Packet allocateNewPacket() { 45 | return new ByteBufferPacket(ByteBuffer.allocateDirect(packetSize)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/BytePacket.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | import java.lang.reflect.Constructor; 24 | 25 | 26 | /** 27 | * Provides a Packet implementation that is directly backed by a byte. 28 | * 29 | * @version $Revision$ 30 | */ 31 | final public class BytePacket implements Packet { 32 | 33 | private byte data; 34 | private byte position; 35 | private byte limit; 36 | 37 | public BytePacket(byte data) { 38 | this.data = data; 39 | clear(); 40 | } 41 | 42 | public int position() { 43 | return position; 44 | } 45 | 46 | public void position(int position) { 47 | this.position = (byte) position; 48 | } 49 | 50 | public int limit() { 51 | return limit; 52 | } 53 | 54 | public void limit(int limit) { 55 | this.limit = (byte) limit; 56 | } 57 | 58 | public void flip() { 59 | limit(position()); 60 | position(0); 61 | } 62 | 63 | public int remaining() { 64 | return limit() - position(); 65 | } 66 | 67 | public void rewind() { 68 | position(0); 69 | } 70 | 71 | public boolean hasRemaining() { 72 | return remaining() > 0; 73 | } 74 | 75 | public void clear() { 76 | position(0); 77 | limit(capacity()); 78 | } 79 | 80 | public int capacity() { 81 | return 1; 82 | } 83 | 84 | public Packet slice() { 85 | if( hasRemaining() ) 86 | return new BytePacket(data); 87 | return EmptyPacket.EMPTY_PACKET; 88 | } 89 | 90 | public Packet duplicate() { 91 | BytePacket packet = new BytePacket(data); 92 | packet.limit(limit()); 93 | packet.position(position()); 94 | return packet; 95 | } 96 | 97 | public Object duplicate(ClassLoader cl) throws IOException { 98 | try { 99 | Class clazz = cl.loadClass(BytePacket.class.getName()); 100 | Constructor constructor = clazz.getConstructor(new Class[]{byte.class}); 101 | return constructor.newInstance(new Object[]{new Byte(data)}); 102 | } catch (Throwable e) { 103 | throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e); 104 | } 105 | } 106 | 107 | public void writeTo(OutputStream out) throws IOException { 108 | if( hasRemaining() ) { 109 | out.write(data); 110 | position(1); 111 | } 112 | } 113 | 114 | public void writeTo(DataOutput out) throws IOException { 115 | if( hasRemaining() ) { 116 | out.write(data); 117 | position(1); 118 | } 119 | } 120 | 121 | /** 122 | * @see org.apache.activeio.packet.Packet#read() 123 | */ 124 | public int read() { 125 | if( !hasRemaining() ) 126 | return -1; 127 | position(1); 128 | return data & 0xff; 129 | } 130 | 131 | /** 132 | * @see org.apache.activeio.packet.Packet#read(byte[], int, int) 133 | */ 134 | public int read(byte[] data, int offset, int length) { 135 | if( !hasRemaining() ) 136 | return -1; 137 | 138 | if( length > 0 ) { 139 | data[offset] = this.data; 140 | position(1); 141 | return 1; 142 | } 143 | return 0; 144 | } 145 | 146 | /** 147 | * @see org.apache.activeio.packet.Packet#write(int) 148 | */ 149 | public boolean write(int data) { 150 | if( !hasRemaining() ) 151 | return false; 152 | 153 | this.data = (byte) data; 154 | position(1); 155 | return true; 156 | } 157 | 158 | /** 159 | * @see org.apache.activeio.packet.Packet#write(byte[], int, int) 160 | */ 161 | public int write(byte[] data, int offset, int length) { 162 | if( !hasRemaining() ) 163 | return -1; 164 | 165 | if( length > 0 ) { 166 | this.data = data[offset] ; 167 | position(1); 168 | return 1; 169 | } 170 | return 0; 171 | } 172 | 173 | public ByteSequence asByteSequence() { 174 | return null; 175 | } 176 | 177 | /** 178 | * @see org.apache.activeio.packet.Packet#sliceAsBytes() 179 | */ 180 | public byte[] sliceAsBytes() { 181 | return null; 182 | } 183 | 184 | /** 185 | * @param dest 186 | * @return the number of bytes read into the dest. 187 | */ 188 | public int read(Packet dest) { 189 | if( hasRemaining() ) { 190 | dest.write(data); 191 | position(1); 192 | return 1; 193 | } 194 | return 0; 195 | } 196 | 197 | public String toString() { 198 | return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}"; 199 | } 200 | 201 | public Object getAdapter(Class target) { 202 | if( target.isAssignableFrom(getClass()) ) { 203 | return this; 204 | } 205 | return null; 206 | } 207 | 208 | public void dispose() { 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/ByteSequence.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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.activeio.packet; 20 | 21 | public class ByteSequence { 22 | final byte[] data; 23 | final int offset; 24 | final int length; 25 | 26 | public ByteSequence(byte data[], int offset, int length) { 27 | this.data = data; 28 | this.offset = offset; 29 | this.length = length; 30 | } 31 | public byte[] getData() { 32 | return data; 33 | } 34 | public int getLength() { 35 | return length; 36 | } 37 | public int getOffset() { 38 | return offset; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/EOSPacket.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | 24 | 25 | /** 26 | * Provides a Packet implementation that is used to represent the end of a stream. 27 | * 28 | * @version $Revision$ 29 | */ 30 | final public class EOSPacket implements Packet { 31 | 32 | static final public EOSPacket EOS_PACKET = new EOSPacket(); 33 | 34 | private EOSPacket() { 35 | } 36 | 37 | public void writeTo(OutputStream out) throws IOException { 38 | } 39 | public void writeTo(DataOutput out) throws IOException { 40 | } 41 | 42 | public int position() { 43 | return 1; 44 | } 45 | 46 | public void position(int position) { 47 | } 48 | 49 | public int limit() { 50 | return 0; 51 | } 52 | 53 | public void limit(int limit) { 54 | } 55 | 56 | public void flip() { 57 | } 58 | 59 | public int remaining() { 60 | return -1; 61 | } 62 | 63 | public void rewind() { 64 | } 65 | 66 | public boolean hasRemaining() { 67 | return false; 68 | } 69 | 70 | public void clear() { 71 | } 72 | 73 | public int capacity() { 74 | return 0; 75 | } 76 | 77 | public Packet slice() { 78 | return this; 79 | } 80 | 81 | public Packet duplicate() { 82 | return this; 83 | } 84 | 85 | public Object duplicate(ClassLoader cl) throws IOException { 86 | try { 87 | Class clazz = cl.loadClass(EOSPacket.class.getName()); 88 | return clazz.getField("EOS_PACKET").get(null); 89 | } catch (Throwable e) { 90 | throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e); 91 | } 92 | } 93 | 94 | /** 95 | * @see org.apache.activeio.packet.Packet#read() 96 | */ 97 | public int read() { 98 | return -1; 99 | } 100 | 101 | /** 102 | * @see org.apache.activeio.packet.Packet#read(byte[], int, int) 103 | */ 104 | public int read(byte[] data, int offset, int length) { 105 | return -1; 106 | } 107 | 108 | /** 109 | * @see org.apache.activeio.packet.Packet#write(int) 110 | */ 111 | public boolean write(int data) { 112 | return false; 113 | } 114 | 115 | /** 116 | * @see org.apache.activeio.packet.Packet#write(byte[], int, int) 117 | */ 118 | public int write(byte[] data, int offset, int length) { 119 | return -1; 120 | } 121 | 122 | public ByteSequence asByteSequence() { 123 | return EmptyPacket.EMPTY_BYTE_SEQUENCE; 124 | } 125 | 126 | public byte[] sliceAsBytes() { 127 | return EmptyPacket.EMPTY_BYTE_ARRAY; 128 | } 129 | 130 | /** 131 | * @param dest 132 | * @return the number of bytes read into the dest. 133 | */ 134 | public int read(Packet dest) { 135 | return 0; 136 | } 137 | 138 | public String toString() { 139 | return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}"; 140 | } 141 | 142 | public Object getAdapter(Class target) { 143 | if( target.isAssignableFrom(getClass()) ) { 144 | return this; 145 | } 146 | return null; 147 | } 148 | 149 | public void dispose() { 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/EmptyPacket.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | 24 | 25 | /** 26 | * Provides a Packet implementation that is directly backed by a byte[0]. 27 | * 28 | * @version $Revision$ 29 | */ 30 | final public class EmptyPacket implements Packet { 31 | 32 | static final public EmptyPacket EMPTY_PACKET = new EmptyPacket(); 33 | static final byte EMPTY_BYTE_ARRAY[] = new byte[]{}; 34 | static final ByteSequence EMPTY_BYTE_SEQUENCE = new ByteSequence(EMPTY_BYTE_ARRAY,0,0); 35 | 36 | private EmptyPacket() { 37 | } 38 | 39 | public void writeTo(OutputStream out) throws IOException { 40 | } 41 | public void writeTo(DataOutput out) throws IOException { 42 | } 43 | 44 | public int position() { 45 | return 0; 46 | } 47 | 48 | public void position(int position) { 49 | } 50 | 51 | public int limit() { 52 | return 0; 53 | } 54 | 55 | public void limit(int limit) { 56 | } 57 | 58 | public void flip() { 59 | } 60 | 61 | public int remaining() { 62 | return 0; 63 | } 64 | 65 | public void rewind() { 66 | } 67 | 68 | public boolean hasRemaining() { 69 | return false; 70 | } 71 | 72 | public void clear() { 73 | } 74 | 75 | public int capacity() { 76 | return 0; 77 | } 78 | 79 | public Packet slice() { 80 | return this; 81 | } 82 | 83 | public Packet duplicate() { 84 | return this; 85 | } 86 | 87 | public Object duplicate(ClassLoader cl) throws IOException { 88 | try { 89 | Class clazz = cl.loadClass(EmptyPacket.class.getName()); 90 | return clazz.getField("EMPTY_PACKET").get(null); 91 | } catch (Throwable e) { 92 | throw (IOException)new IOException("Could not duplicate packet in a different classloader: "+e).initCause(e); 93 | } 94 | } 95 | 96 | /** 97 | * @see org.apache.activeio.packet.Packet#read() 98 | */ 99 | public int read() { 100 | return -1; 101 | } 102 | 103 | /** 104 | * @see org.apache.activeio.packet.Packet#read(byte[], int, int) 105 | */ 106 | public int read(byte[] data, int offset, int length) { 107 | return -1; 108 | } 109 | 110 | /** 111 | * @see org.apache.activeio.packet.Packet#write(int) 112 | */ 113 | public boolean write(int data) { 114 | return false; 115 | } 116 | 117 | /** 118 | * @see org.apache.activeio.packet.Packet#write(byte[], int, int) 119 | */ 120 | public int write(byte[] data, int offset, int length) { 121 | return -1; 122 | } 123 | 124 | public ByteSequence asByteSequence() { 125 | return EMPTY_BYTE_SEQUENCE; 126 | } 127 | 128 | public byte[] sliceAsBytes() { 129 | return EMPTY_BYTE_ARRAY; 130 | } 131 | 132 | /** 133 | * @param dest 134 | * @return the number of bytes read into the dest. 135 | */ 136 | public int read(Packet dest) { 137 | return -1; 138 | } 139 | 140 | public String toString() { 141 | return "{position="+position()+",limit="+limit()+",capacity="+capacity()+"}"; 142 | } 143 | 144 | public Object getAdapter(Class target) { 145 | if( target.isAssignableFrom(getClass()) ) { 146 | return this; 147 | } 148 | return null; 149 | } 150 | 151 | public void dispose() { 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/FilterPacket.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | 24 | 25 | /** 26 | * Provides a Packet implementation that filters operations to another packet. 27 | * 28 | * Used to make it easier to augment the {@see #narrow(Class)}method. 29 | * 30 | * @version $Revision$ 31 | */ 32 | public abstract class FilterPacket implements Packet { 33 | final protected Packet next; 34 | 35 | public FilterPacket(Packet next) { 36 | this.next = next; 37 | } 38 | 39 | public ByteSequence asByteSequence() { 40 | return next.asByteSequence(); 41 | } 42 | 43 | public int capacity() { 44 | return next.capacity(); 45 | } 46 | 47 | public void clear() { 48 | next.clear(); 49 | } 50 | 51 | public void flip() { 52 | next.flip(); 53 | } 54 | 55 | public boolean hasRemaining() { 56 | return next.hasRemaining(); 57 | } 58 | 59 | public int limit() { 60 | return next.limit(); 61 | } 62 | 63 | public void limit(int limit) { 64 | next.limit(limit); 65 | } 66 | 67 | public Object getAdapter(Class target) { 68 | if( target.isAssignableFrom(getClass()) ) { 69 | return this; 70 | } 71 | return next.getAdapter(target); 72 | } 73 | 74 | public int position() { 75 | return next.position(); 76 | } 77 | 78 | public void position(int position) { 79 | next.position(position); 80 | } 81 | 82 | public int read() { 83 | return next.read(); 84 | } 85 | 86 | public int read(byte[] data, int offset, int length) { 87 | return next.read(data, offset, length); 88 | } 89 | 90 | public int read(Packet dest) { 91 | return next.read(dest); 92 | } 93 | 94 | public int remaining() { 95 | return next.remaining(); 96 | } 97 | 98 | public void rewind() { 99 | next.rewind(); 100 | } 101 | 102 | public byte[] sliceAsBytes() { 103 | return next.sliceAsBytes(); 104 | } 105 | 106 | public int write(byte[] data, int offset, int length) { 107 | return next.write(data, offset, length); 108 | } 109 | 110 | public boolean write(int data) { 111 | return next.write(data); 112 | } 113 | 114 | public void writeTo(OutputStream out) throws IOException { 115 | next.writeTo(out); 116 | } 117 | public void writeTo(DataOutput out) throws IOException { 118 | next.writeTo(out); 119 | } 120 | 121 | public Object duplicate(ClassLoader cl) throws IOException { 122 | return next.duplicate(cl); 123 | } 124 | 125 | public Packet duplicate() { 126 | return filter(next.duplicate()); 127 | } 128 | 129 | public Packet slice() { 130 | return filter(next.slice()); 131 | } 132 | 133 | public void dispose() { 134 | next.dispose(); 135 | } 136 | 137 | abstract public Packet filter(Packet packet); 138 | } 139 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/Packet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.io.DataOutput; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | 24 | /** 25 | * Provides a ByteBuffer like interface to work with IO channel packets of data. 26 | * 27 | * @version $Revision$ 28 | */ 29 | public interface Packet { 30 | 31 | public int position(); 32 | public void position(int position); 33 | public int limit(); 34 | public void limit(int limit); 35 | public void flip(); 36 | public int remaining(); 37 | public void rewind(); 38 | public boolean hasRemaining(); 39 | public void clear(); 40 | public Packet slice(); 41 | public Packet duplicate(); 42 | public Object duplicate(ClassLoader cl) throws IOException; 43 | public int capacity(); 44 | public void dispose(); 45 | 46 | public ByteSequence asByteSequence(); 47 | public byte[] sliceAsBytes(); 48 | 49 | /** 50 | * @Return object that is an instance of requested type and is associated this this object. May return null if no 51 | * object of that type is associated. 52 | */ 53 | Object getAdapter(Class target); 54 | 55 | 56 | /** 57 | * Writes the remaing bytes in the packet to the output stream. 58 | * 59 | * @param out 60 | * @return 61 | */ 62 | void writeTo(OutputStream out) throws IOException; 63 | void writeTo(DataOutput out) throws IOException; 64 | 65 | // To read data out of the packet. 66 | public int read(); 67 | public int read(byte data[], int offset, int length); 68 | 69 | // To write data into the packet. 70 | public boolean write( int data ); 71 | public int write( byte data[], int offset, int length ); 72 | public int read(Packet dest); 73 | 74 | } 75 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/PacketPool.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.util.ArrayList; 21 | 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | 24 | /** 25 | * Provides a simple pool of Packet objects. When the packets that this pool produces are disposed, 26 | * they are returned to the pool. 27 | * 28 | * @version $Revision: 1.1 $ 29 | */ 30 | abstract public class PacketPool { 31 | 32 | public static final int DEFAULT_POOL_SIZE = Integer.parseInt(System.getProperty("org.apache.activeio.journal.active.DefaultPoolSize", ""+(5))); 33 | public static final int DEFAULT_PACKET_SIZE = Integer.parseInt(System.getProperty("org.apache.activeio.journal.active.DefaultPacketSize", ""+(1024*1024*4))); 34 | 35 | private final ArrayList pool = new ArrayList(); 36 | private final int maxPackets; 37 | private int currentPoolSize; 38 | private boolean disposed; 39 | 40 | public class PooledPacket extends FilterPacket { 41 | private final AtomicInteger referenceCounter; 42 | 43 | public PooledPacket(Packet next) { 44 | this(next, new AtomicInteger(0)); 45 | } 46 | 47 | private PooledPacket(Packet next, AtomicInteger referenceCounter) { 48 | super(next); 49 | this.referenceCounter=referenceCounter; 50 | this.referenceCounter.incrementAndGet(); 51 | } 52 | 53 | public Packet filter(Packet packet) { 54 | return new PooledPacket(next, referenceCounter); 55 | } 56 | 57 | int getReferenceCounter() { 58 | return referenceCounter.get(); 59 | } 60 | 61 | public void dispose() { 62 | if( referenceCounter.decrementAndGet()==0 ) { 63 | returnPacket(next); 64 | } 65 | } 66 | } 67 | 68 | /** 69 | * @param maxPackets the number of buffers that will be in the pool. 70 | */ 71 | public PacketPool(int maxPackets) { 72 | this.maxPackets = maxPackets; 73 | } 74 | 75 | /** 76 | * Blocks until a ByteBuffer can be retreived from the pool. 77 | * 78 | * @return 79 | * @throws InterruptedException 80 | */ 81 | public Packet getPacket() throws InterruptedException { 82 | Packet answer=null; 83 | synchronized(this) { 84 | while(answer==null) { 85 | if( disposed ) 86 | return null; 87 | if( pool.size()>0) { 88 | answer = (Packet) pool.remove(pool.size()-1); 89 | } else if( currentPoolSize < maxPackets ) { 90 | answer = allocateNewPacket(); 91 | currentPoolSize++; 92 | } 93 | if( answer==null ) { 94 | this.wait(); 95 | } 96 | } 97 | } 98 | return new PooledPacket(answer); 99 | } 100 | 101 | /** 102 | * Returns a ByteBuffer to the pool. 103 | * 104 | * @param packet 105 | */ 106 | private void returnPacket(Packet packet) { 107 | packet.clear(); 108 | synchronized(this) { 109 | pool.add(packet); 110 | this.notify(); 111 | } 112 | } 113 | 114 | synchronized public void dispose() { 115 | disposed = true; 116 | while( currentPoolSize>0 ) { 117 | if( pool.size()>0) { 118 | currentPoolSize -= pool.size(); 119 | pool.clear(); 120 | } else { 121 | try { 122 | this.wait(); 123 | } catch (InterruptedException e) { 124 | return; 125 | } 126 | } 127 | } 128 | } 129 | 130 | synchronized public void waitForPacketsToReturn() { 131 | while( currentPoolSize!=pool.size() ) { 132 | try { 133 | this.wait(); 134 | } catch (InterruptedException e) { 135 | return; 136 | } 137 | } 138 | } 139 | 140 | /** 141 | * @return 142 | */ 143 | abstract protected Packet allocateNewPacket(); 144 | 145 | } 146 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/packet/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | Implementations of the Packet class. 24 |

25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/ServerService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import java.util.Properties; 21 | 22 | 23 | /** 24 | * The Server will call the following methods. 25 | *

26 | * newInstance() 27 | * init( port, properties) 28 | * start() 29 | * stop() 30 | *

31 | * All ServerService implementations must have a no argument 32 | * constructor. 33 | */ 34 | public interface ServerService extends SocketService { 35 | 36 | public void init(Properties props) throws Exception; 37 | 38 | public void start() throws ServiceException; 39 | 40 | public void stop() throws ServiceException; 41 | 42 | 43 | /** 44 | * Gets the ip number that the 45 | * daemon is listening on. 46 | */ 47 | public String getIP(); 48 | 49 | /** 50 | * Gets the port number that the 51 | * daemon is listening on. 52 | */ 53 | public int getPort(); 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/ServiceDaemon.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import org.apache.commons.logging.Log; 21 | import org.apache.commons.logging.LogFactory; 22 | 23 | import java.io.IOException; 24 | import java.net.InetAddress; 25 | import java.net.ServerSocket; 26 | import java.net.Socket; 27 | import java.net.SocketException; 28 | import java.net.SocketTimeoutException; 29 | import java.util.Properties; 30 | 31 | public class ServiceDaemon implements ServerService { 32 | private static final Log log = LogFactory.getLog(ServiceDaemon.class); 33 | 34 | private final SocketService socketService; 35 | private final InetAddress address; 36 | private int port; 37 | 38 | private SocketListener socketListener; 39 | private int timeout; 40 | private String name; 41 | 42 | public ServiceDaemon(SocketService socketService, InetAddress address, int port) { 43 | this(null, socketService, address, port); 44 | } 45 | 46 | public ServiceDaemon(String name, SocketService socketService, InetAddress address, int port) { 47 | this.name = name; 48 | if (socketService == null) { 49 | throw new IllegalArgumentException("socketService is null"); 50 | } 51 | this.socketService = socketService; 52 | this.address = address; 53 | this.port = port; 54 | } 55 | 56 | public void setSoTimeout(int timeout) throws SocketException { 57 | this.timeout = timeout; 58 | if (socketListener != null) { 59 | socketListener.setSoTimeout(timeout); 60 | } 61 | } 62 | 63 | public int getSoTimeout() throws IOException { 64 | if (socketListener == null) return 0; 65 | return socketListener.getSoTimeout(); 66 | } 67 | 68 | public String getServiceName() { 69 | return socketService.getName(); 70 | } 71 | 72 | /** 73 | * Gets the inetAddress number that the 74 | * daemon is listening on. 75 | */ 76 | public InetAddress getAddress() { 77 | return address; 78 | } 79 | 80 | public void init(Properties props) throws Exception { 81 | } 82 | 83 | public void start() throws ServiceException { 84 | synchronized (this) { 85 | // Don't bother if we are already started/starting 86 | if (socketListener != null) { 87 | return; 88 | } 89 | 90 | ServerSocket serverSocket; 91 | try { 92 | serverSocket = new ServerSocket(port, 20, address); 93 | port = serverSocket.getLocalPort(); 94 | serverSocket.setSoTimeout(timeout); 95 | } catch (Exception e) { 96 | throw new ServiceException("Service failed to open socket", e); 97 | } 98 | 99 | socketListener = new SocketListener(socketService, serverSocket); 100 | Thread thread = new Thread(socketListener); 101 | thread.setName("service." + name + "@" + socketListener.hashCode()); 102 | thread.setDaemon(true); 103 | thread.start(); 104 | } 105 | } 106 | 107 | public void stop() throws ServiceException { 108 | synchronized (this) { 109 | if (socketListener != null) { 110 | socketListener.stop(); 111 | socketListener = null; 112 | } 113 | } 114 | } 115 | 116 | public String getIP() { 117 | return null; 118 | } 119 | 120 | /** 121 | * Gets the port number that the 122 | * daemon is listening on. 123 | */ 124 | public int getPort() { 125 | return port; 126 | } 127 | 128 | public void service(Socket socket) throws ServiceException, IOException { 129 | } 130 | 131 | public String getName() { 132 | return null; 133 | } 134 | 135 | private static class SocketListener implements Runnable { 136 | private SocketService serverService; 137 | private ServerSocket serverSocket; 138 | private boolean stopped; 139 | 140 | public SocketListener(SocketService serverService, ServerSocket serverSocket) { 141 | this.serverService = serverService; 142 | this.serverSocket = serverSocket; 143 | stopped = false; 144 | } 145 | 146 | public synchronized void stop() { 147 | stopped = true; 148 | } 149 | 150 | private synchronized boolean shouldStop() { 151 | return stopped; 152 | } 153 | 154 | public void run() { 155 | while (!shouldStop()) { 156 | Socket socket = null; 157 | try { 158 | socket = serverSocket.accept(); 159 | socket.setTcpNoDelay(true); 160 | if (!shouldStop()) { 161 | // the server service is responsible 162 | // for closing the socket. 163 | serverService.service(socket); 164 | } 165 | } catch (SocketTimeoutException e) { 166 | // we don't really care 167 | // log.debug("Socket timed-out",e); 168 | } catch (Throwable e) { 169 | log.error("Unexpected error", e); 170 | } 171 | } 172 | 173 | if (serverSocket != null) { 174 | try { 175 | serverSocket.close(); 176 | } catch (IOException ioException) { 177 | log.debug("Error cleaning up socked", ioException); 178 | } 179 | serverSocket = null; 180 | } 181 | serverService = null; 182 | } 183 | 184 | public void setSoTimeout(int timeout) throws SocketException { 185 | serverSocket.setSoTimeout(timeout); 186 | } 187 | 188 | public int getSoTimeout() throws IOException { 189 | return serverSocket.getSoTimeout(); 190 | } 191 | } 192 | 193 | 194 | } 195 | 196 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/ServiceException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | /** 21 | * 22 | */ 23 | public class ServiceException extends Exception { 24 | 25 | /** 26 | *

27 | * Default constructor, which simply delegates exception 28 | * handling up the inheritance chain to Exception. 29 | *

30 | */ 31 | public ServiceException() { 32 | super(); 33 | } 34 | 35 | /** 36 | *

37 | * This constructor allows a message to be supplied indicating the source 38 | * of the problem that occurred. 39 | *

40 | * 41 | * @param message String identifying the cause of the problem. 42 | */ 43 | public ServiceException(String message) { 44 | super(message); 45 | } 46 | 47 | /** 48 | *

49 | * This constructor allows a "root cause" exception to be supplied, 50 | * which may later be used by the wrapping application. 51 | *

52 | * 53 | * @param rootCause Throwable that triggered the problem. 54 | */ 55 | public ServiceException(Throwable rootCause) { 56 | super(rootCause); 57 | } 58 | 59 | /** 60 | * This constructor allows both a message identifying the 61 | * problem that occurred as well as a "root cause" exception 62 | * to be supplied, which may later be used by the wrapping 63 | * application. 64 | * 65 | * @param message String identifying the cause of the problem. 66 | * @param rootCause Throwable that triggered this problem. 67 | */ 68 | public ServiceException(String message, Throwable rootCause) { 69 | super(message, rootCause); 70 | } 71 | 72 | } 73 | 74 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/ServiceLogger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import org.apache.commons.logging.Log; 21 | import org.apache.commons.logging.LogFactory; 22 | 23 | import java.io.IOException; 24 | import java.net.InetAddress; 25 | import java.net.Socket; 26 | import java.util.Properties; 27 | 28 | public class ServiceLogger implements ServerService { 29 | private final Log log; 30 | private final ServerService next; 31 | private final String[] logOnSuccess; 32 | private final String[] logOnFailure; 33 | private final String name; 34 | 35 | 36 | public ServiceLogger(String name, ServerService next, String[] logOnSuccess, String[] logOnFailure) { 37 | this.log = LogFactory.getLog("OpenEJB.server.service." + name); 38 | this.next = next; 39 | this.logOnSuccess = logOnSuccess; 40 | this.logOnFailure = logOnFailure; 41 | this.name = name; 42 | } 43 | 44 | /** 45 | * log_on_success 46 | * ----------------- 47 | * Different information can be logged when a server starts: 48 | *

49 | * PID : the server's PID (if it's an internal xinetd service, the PID has then a value of 0) ; 50 | * HOST : the client address ; 51 | * USERID : the identity of the remote user, according to RFC1413 defining identification protocol; 52 | * EXIT : the process exit status; 53 | * DURATION : the session duration. 54 | *

55 | * log_on_failure 56 | * ------------------ 57 | * Here again, xinetd can log a lot of information when a server can't start, either by lack of resources or because of access rules: 58 | * HOST, USERID : like above mentioned ; 59 | * ATTEMPT : logs an access attempt. This an automatic option as soon as another value is provided; 60 | * RECORD : logs every information available on the client. 61 | * 62 | * @param socket 63 | * @throws org.apache.activeio.xnet.ServiceException 64 | * 65 | * @throws IOException 66 | */ 67 | public void service(Socket socket) throws ServiceException, IOException { 68 | // Fill this in more deeply later. 69 | InetAddress client = socket.getInetAddress(); 70 | // MDC.put("HOST", client.getHostName()); 71 | // MDC.put("SERVER", getName()); 72 | 73 | try { 74 | logIncoming(); 75 | next.service(socket); 76 | logSuccess(); 77 | } catch (Exception e) { 78 | logFailure(e); 79 | e.printStackTrace(); 80 | } 81 | } 82 | 83 | public String[] getLogOnSuccess() { 84 | return logOnSuccess; 85 | } 86 | 87 | public String[] getLogOnFailure() { 88 | return logOnFailure; 89 | } 90 | 91 | private void logIncoming() { 92 | log.trace("incomming request"); 93 | } 94 | 95 | private void logSuccess() { 96 | log.trace("successful request"); 97 | } 98 | 99 | private void logFailure(Exception e) { 100 | log.error(e.getMessage()); 101 | } 102 | 103 | 104 | public void init(Properties props) throws Exception { 105 | next.init(props); 106 | } 107 | 108 | public void start() throws ServiceException { 109 | next.start(); 110 | } 111 | 112 | public void stop() throws ServiceException { 113 | next.stop(); 114 | } 115 | 116 | public String getName() { 117 | return next.getName(); 118 | } 119 | 120 | public String getIP() { 121 | return next.getIP(); 122 | } 123 | 124 | public int getPort() { 125 | return next.getPort(); 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/ServicePool.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import java.util.concurrent.Executor; 21 | import java.util.concurrent.LinkedBlockingQueue; 22 | import java.util.concurrent.ThreadPoolExecutor; 23 | import java.util.concurrent.ThreadFactory; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | import org.apache.commons.logging.Log; 27 | import org.apache.commons.logging.LogFactory; 28 | 29 | import java.io.IOException; 30 | import java.net.Socket; 31 | import java.util.Properties; 32 | 33 | public class ServicePool implements ServerService { 34 | private static final Log log = LogFactory.getLog(ServicePool.class); 35 | 36 | private final ServerService next; 37 | private final Executor executor; 38 | 39 | public ServicePool(ServerService next, final String name, final int threads, final long keepAliveTime) { 40 | this.next = next; 41 | 42 | ThreadPoolExecutor p = new ThreadPoolExecutor(threads, threads, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); 43 | p.setThreadFactory(new ThreadFactory() { 44 | private volatile int id = 0; 45 | 46 | public Thread newThread(Runnable arg0) { 47 | Thread thread = new Thread(arg0, name + " " + getNextID()); 48 | return thread; 49 | } 50 | 51 | private int getNextID() { 52 | return id++; 53 | } 54 | 55 | }); 56 | executor = p; 57 | } 58 | 59 | public ServicePool(ServerService next, Executor executor) { 60 | this.next = next; 61 | this.executor = executor; 62 | } 63 | 64 | public void service(final Socket socket) throws ServiceException, IOException { 65 | final Runnable service = new Runnable() { 66 | public void run() { 67 | try { 68 | next.service(socket); 69 | } catch (SecurityException e) { 70 | log.error("Security error: " + e.getMessage(), e); 71 | } catch (Throwable e) { 72 | log.error("Unexpected error", e); 73 | } finally { 74 | try { 75 | if (socket != null) { 76 | socket.close(); 77 | } 78 | } catch (Throwable t) { 79 | log.warn("Error while closing connection with client", t); 80 | } 81 | } 82 | } 83 | }; 84 | 85 | final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); 86 | Runnable ctxCL = new Runnable() { 87 | public void run() { 88 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 89 | Thread.currentThread().setContextClassLoader(tccl); 90 | try { 91 | service.run(); 92 | } finally { 93 | Thread.currentThread().setContextClassLoader(cl); 94 | } 95 | } 96 | }; 97 | 98 | executor.execute(ctxCL); 99 | } 100 | 101 | /** 102 | * Pulls out the access log information 103 | * 104 | * @param props 105 | * @throws ServiceException 106 | */ 107 | public void init(Properties props) throws Exception { 108 | // Do our stuff 109 | 110 | // Then call the next guy 111 | next.init(props); 112 | } 113 | 114 | public void start() throws ServiceException { 115 | // Do our stuff 116 | 117 | // Then call the next guy 118 | next.start(); 119 | } 120 | 121 | public void stop() throws ServiceException { 122 | // Do our stuff 123 | 124 | // Then call the next guy 125 | next.stop(); 126 | } 127 | 128 | 129 | /** 130 | * Gets the name of the service. 131 | * Used for display purposes only 132 | */ 133 | public String getName() { 134 | return next.getName(); 135 | } 136 | 137 | /** 138 | * Gets the ip number that the 139 | * daemon is listening on. 140 | */ 141 | public String getIP() { 142 | return next.getIP(); 143 | } 144 | 145 | /** 146 | * Gets the port number that the 147 | * daemon is listening on. 148 | */ 149 | public int getPort() { 150 | return next.getPort(); 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/SocketService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import java.io.IOException; 21 | import java.net.Socket; 22 | 23 | /** 24 | * @version $Revision: 1.1 $ $Date: 2004/04/09 19:04:01 $ 25 | */ 26 | public interface SocketService { 27 | void service(Socket socket) throws ServiceException, IOException; 28 | 29 | /** 30 | * Gets the name of the service. 31 | * Used for display purposes only 32 | */ 33 | String getName(); 34 | } 35 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/StandardServiceStack.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet; 19 | 20 | import java.util.concurrent.Executor; 21 | 22 | import org.apache.activeio.xnet.hba.IPAddressPermission; 23 | import org.apache.activeio.xnet.hba.ServiceAccessController; 24 | 25 | import java.io.IOException; 26 | import java.net.InetAddress; 27 | import java.net.InetSocketAddress; 28 | import java.net.SocketException; 29 | import java.net.UnknownHostException; 30 | 31 | public class StandardServiceStack { 32 | 33 | private String name; 34 | 35 | private ServiceDaemon daemon; 36 | private ServiceLogger logger; 37 | private ServiceAccessController hba; 38 | private ServicePool pool; 39 | private ServerService server; 40 | private String host; 41 | 42 | public StandardServiceStack(String name, int port, String host, IPAddressPermission[] allowHosts, String[] logOnSuccess, String[] logOnFailure, Executor executor, ServerService server) throws UnknownHostException { 43 | this.server = server; 44 | this.name = name; 45 | this.host = host; 46 | InetAddress address = InetAddress.getByName(host); 47 | this.pool = new ServicePool(server, executor); 48 | this.hba = new ServiceAccessController(name, pool, allowHosts); 49 | this.logger = new ServiceLogger(name, hba, logOnSuccess, logOnFailure); 50 | this.daemon = new ServiceDaemon(name, logger, address, port); 51 | 52 | } 53 | 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | public InetAddress getAddress() { 59 | return daemon.getAddress(); 60 | } 61 | 62 | public InetSocketAddress getFullAddress() { 63 | return new InetSocketAddress(getAddress(), getPort()); 64 | } 65 | 66 | public String getHost() { 67 | return host; 68 | } 69 | 70 | public int getPort() { 71 | return daemon.getPort(); 72 | } 73 | 74 | public int getSoTimeout() throws IOException { 75 | return daemon.getSoTimeout(); 76 | } 77 | 78 | public void setSoTimeout(int timeout) throws SocketException { 79 | daemon.setSoTimeout(timeout); 80 | } 81 | 82 | public String[] getLogOnSuccess() { 83 | return logger.getLogOnSuccess(); 84 | } 85 | 86 | public String[] getLogOnFailure() { 87 | return logger.getLogOnFailure(); 88 | } 89 | 90 | public IPAddressPermission[] getAllowHosts() { 91 | return hba.getAllowHosts(); 92 | } 93 | 94 | public void setAllowHosts(IPAddressPermission[] allowHosts) { 95 | hba.setAllowHosts(allowHosts); 96 | } 97 | 98 | public void doStart() throws Exception { 99 | daemon.start(); 100 | } 101 | 102 | public void doStop() throws Exception { 103 | daemon.stop(); 104 | } 105 | 106 | public void doFail() { 107 | try { 108 | daemon.stop(); 109 | } catch (ServiceException dontCare) { 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/ExactIPAddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.net.InetAddress; 23 | import java.net.Inet4Address; 24 | 25 | /** 26 | * @version $Revision$ $Date$ 27 | */ 28 | public class ExactIPAddressPermission implements IPAddressPermission { 29 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"); 30 | 31 | public static boolean canSupport(String mask) { 32 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 33 | return matcher.matches(); 34 | } 35 | 36 | private final byte[] bytes; 37 | 38 | public ExactIPAddressPermission(byte[] bytes) { 39 | this.bytes = bytes; 40 | } 41 | 42 | public ExactIPAddressPermission(String mask) { 43 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 44 | if (false == matcher.matches()) { 45 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 46 | } 47 | 48 | bytes = new byte[4]; 49 | for (int i = 0; i < 4; i++) { 50 | String group = matcher.group(i + 1); 51 | int value = Integer.parseInt(group); 52 | if (value < 0 || 255 < value) { 53 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 54 | } 55 | bytes[i] = (byte) value; 56 | } 57 | } 58 | 59 | public boolean implies(InetAddress address) { 60 | if (false == address instanceof Inet4Address) { 61 | return false; 62 | } 63 | 64 | byte[] byteAddress = address.getAddress(); 65 | for (int i = 0; i < 4; i++) { 66 | if (byteAddress[i] != bytes[i]) { 67 | return false; 68 | } 69 | } 70 | return true; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/ExactIPv6AddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.util.StringTokenizer; 23 | import java.net.InetAddress; 24 | import java.net.Inet6Address; 25 | 26 | /** 27 | * @version $Revision$ $Date$ 28 | */ 29 | public class ExactIPv6AddressPermission implements IPAddressPermission { 30 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^(([a-fA-F0-9]{1,4}:){7})([a-fA-F0-9]{1,4})$"); 31 | 32 | public static boolean canSupport(String mask) { 33 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 34 | return matcher.matches(); 35 | } 36 | 37 | private final byte[] bytes; 38 | 39 | public ExactIPv6AddressPermission(byte[] bytes) { 40 | this.bytes = bytes; 41 | } 42 | 43 | public ExactIPv6AddressPermission(String mask) { 44 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 45 | if (false == matcher.matches()) { 46 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 47 | } 48 | 49 | bytes = new byte[16]; 50 | int pos = 0; 51 | StringTokenizer tokenizer = new StringTokenizer(mask, ":"); 52 | while (tokenizer.hasMoreTokens()) { 53 | String token = tokenizer.nextToken(); 54 | int value = Integer.parseInt(token, 16); 55 | bytes[pos++] = (byte) ((value & 0xff00) >> 8); 56 | bytes[pos++] = (byte) value; 57 | } 58 | } 59 | 60 | public boolean implies(InetAddress address) { 61 | if (false == address instanceof Inet6Address) { 62 | return false; 63 | } 64 | 65 | byte[] byteAddress = address.getAddress(); 66 | for (int i = 0; i < 16; i++) { 67 | if (byteAddress[i] != bytes[i]) { 68 | return false; 69 | } 70 | } 71 | return true; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/FactorizedIPAddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.util.StringTokenizer; 23 | import java.net.InetAddress; 24 | import java.net.Inet4Address; 25 | 26 | /** 27 | * @version $Revision$ $Date$ 28 | */ 29 | public class FactorizedIPAddressPermission implements IPAddressPermission { 30 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^((\\d{1,3}){1}(\\.\\d{1,3}){0,2}\\.)?\\{(\\d{1,3}){1}((,\\d{1,3})*)\\}$"); 31 | 32 | public static boolean canSupport(String mask) { 33 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 34 | return matcher.matches(); 35 | } 36 | 37 | private final byte[] prefixBytes; 38 | private final byte[] suffixBytes; 39 | 40 | public FactorizedIPAddressPermission(String mask) { 41 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 42 | if (false == matcher.matches()) { 43 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 44 | } 45 | 46 | // group 1 is the factorized IP part. 47 | // e.g. group 1 in "1.2.3.{4,5,6}" is "1.2.3." 48 | String prefix = matcher.group(1); 49 | StringTokenizer tokenizer = new StringTokenizer(prefix, "."); 50 | prefixBytes = new byte[tokenizer.countTokens()]; 51 | for (int i = 0; i < prefixBytes.length; i++) { 52 | String token = tokenizer.nextToken(); 53 | int value = Integer.parseInt(token); 54 | if (value < 0 || 255 < value) { 55 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 56 | } 57 | prefixBytes[i] = (byte) value; 58 | } 59 | 60 | // group 5 is a comma separated list of optional suffixes. 61 | // e.g. group 5 in "1.2.3.{4,5,6}" is ",5,6" 62 | String suffix = matcher.group(5); 63 | tokenizer = new StringTokenizer(suffix, ","); 64 | suffixBytes = new byte[1 + tokenizer.countTokens()]; 65 | 66 | // group 4 is the compulsory and first suffix. 67 | // e.g. group 4 in "1.2.3.{4,5,6}" is "4" 68 | int value = Integer.parseInt(matcher.group(4)); 69 | int i = 0; 70 | if (value < 0 || 255 < value) { 71 | throw new IllegalArgumentException("suffix " + i + " is not valid."); 72 | } 73 | suffixBytes[i++] = (byte) value; 74 | 75 | for (; i < suffixBytes.length; i++) { 76 | String token = tokenizer.nextToken(); 77 | value = Integer.parseInt(token); 78 | if (value < 0 || 255 < value) { 79 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 80 | } 81 | suffixBytes[i] = (byte) value; 82 | } 83 | } 84 | 85 | public boolean implies(InetAddress address) { 86 | if (false == address instanceof Inet4Address) { 87 | return false; 88 | } 89 | 90 | byte[] byteAddress = address.getAddress(); 91 | for (int i = 0; i < prefixBytes.length; i++) { 92 | if (byteAddress[i] != prefixBytes[i]) { 93 | return false; 94 | } 95 | } 96 | byte lastByte = byteAddress[prefixBytes.length]; 97 | for (int i = 0; i < suffixBytes.length; i++) { 98 | if (lastByte == suffixBytes[i]) { 99 | return true; 100 | } 101 | } 102 | return false; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/IPAddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.io.Serializable; 21 | import java.net.InetAddress; 22 | 23 | /** 24 | * @version $Revision$ $Date$ 25 | */ 26 | public interface IPAddressPermission extends Serializable { 27 | public boolean implies(InetAddress address); 28 | } 29 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/IPAddressPermissionEditor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.beans.PropertyEditorSupport; 21 | 22 | /** 23 | * @version $Revision$ $Date$ 24 | */ 25 | public class IPAddressPermissionEditor extends PropertyEditorSupport { 26 | private IPAddressPermission addressMask; 27 | 28 | public void setAsText(String text) throws IllegalArgumentException { 29 | addressMask = IPAddressPermissionFactory.getIPAddressMask(text); 30 | } 31 | 32 | public Object getValue() { 33 | return addressMask; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/IPAddressPermissionFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | /** 21 | * @version $Revision$ $Date$ 22 | */ 23 | public class IPAddressPermissionFactory { 24 | 25 | public static IPAddressPermission getIPAddressMask(String mask) { 26 | if (StartWithIPAddressPermission.canSupport(mask)) { 27 | return new StartWithIPAddressPermission(mask); 28 | } else if (ExactIPAddressPermission.canSupport(mask)) { 29 | return new ExactIPAddressPermission(mask); 30 | } else if (FactorizedIPAddressPermission.canSupport(mask)) { 31 | return new FactorizedIPAddressPermission(mask); 32 | } else if (NetmaskIPAddressPermission.canSupport(mask)) { 33 | return new NetmaskIPAddressPermission(mask); 34 | } else if (ExactIPv6AddressPermission.canSupport(mask)) { 35 | return new ExactIPv6AddressPermission(mask); 36 | } else if (NetmaskIPv6AddressPermission.canSupport(mask)) { 37 | return new NetmaskIPv6AddressPermission(mask); 38 | } 39 | throw new IllegalArgumentException("Mask " + mask + " is not supported."); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/NetmaskIPAddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.net.InetAddress; 23 | import java.net.Inet4Address; 24 | 25 | /** 26 | * @version $Revision$ $Date$ 27 | */ 28 | public class NetmaskIPAddressPermission implements IPAddressPermission { 29 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/((\\d{1,2})|(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}))$"); 30 | 31 | public static boolean canSupport(String mask) { 32 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 33 | return matcher.matches(); 34 | } 35 | 36 | private final byte[] networkAddressBytes; 37 | private final byte[] netmaskBytes; 38 | 39 | public NetmaskIPAddressPermission(String mask) { 40 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 41 | if (false == matcher.matches()) { 42 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 43 | } 44 | 45 | networkAddressBytes = new byte[4]; 46 | for (int i = 0; i < 4; i++) { 47 | String group = matcher.group(i + 1); 48 | int value = Integer.parseInt(group); 49 | if (value < 0 || 255 < value) { 50 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 51 | } 52 | networkAddressBytes[i] = (byte) value; 53 | } 54 | 55 | netmaskBytes = new byte[4]; 56 | String netmask = matcher.group(6); 57 | if (null != netmask) { 58 | int value = Integer.parseInt(netmask); 59 | int pos = value / 8; 60 | int shift = 8 - value % 8; 61 | for (int i = 0; i < pos; i++) { 62 | netmaskBytes[i] = (byte) 0xff; 63 | } 64 | netmaskBytes[pos] = (byte) (0xff << shift); 65 | } else { 66 | for (int i = 0; i < 4; i++) { 67 | String group = matcher.group(i + 7); 68 | int value = Integer.parseInt(group); 69 | if (value < 0 || 255 < value) { 70 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 71 | } 72 | netmaskBytes[i] = (byte) value; 73 | } 74 | } 75 | } 76 | 77 | public boolean implies(InetAddress address) { 78 | if (false == address instanceof Inet4Address) { 79 | return false; 80 | } 81 | 82 | byte[] byteAddress = address.getAddress(); 83 | for (int i = 0; i < 4; i++) { 84 | if ((netmaskBytes[i] & byteAddress[i]) != networkAddressBytes[i]) { 85 | return false; 86 | } 87 | } 88 | return true; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/NetmaskIPv6AddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.util.StringTokenizer; 23 | import java.net.InetAddress; 24 | import java.net.Inet6Address; 25 | 26 | /** 27 | * @version $Revision$ $Date$ 28 | */ 29 | public class NetmaskIPv6AddressPermission implements IPAddressPermission { 30 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^(([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4})/((\\d{1,3})|(([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}))$"); 31 | 32 | public static boolean canSupport(String mask) { 33 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 34 | return matcher.matches(); 35 | } 36 | 37 | private final byte[] networkAddressBytes; 38 | private final byte[] netmaskBytes; 39 | 40 | public NetmaskIPv6AddressPermission(String mask) { 41 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 42 | if (false == matcher.matches()) { 43 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 44 | } 45 | 46 | networkAddressBytes = new byte[16]; 47 | int pos = 0; 48 | StringTokenizer tokenizer = new StringTokenizer(matcher.group(1), ":"); 49 | while (tokenizer.hasMoreTokens()) { 50 | String token = tokenizer.nextToken(); 51 | int value = Integer.parseInt(token, 16); 52 | networkAddressBytes[pos++] = (byte) ((value & 0xff00) >> 8); 53 | networkAddressBytes[pos++] = (byte) value; 54 | } 55 | 56 | netmaskBytes = new byte[16]; 57 | String netmask = matcher.group(4); 58 | if (null != netmask) { 59 | int value = Integer.parseInt(netmask); 60 | pos = value / 8; 61 | int shift = 8 - value % 8; 62 | for (int i = 0; i < pos; i++) { 63 | netmaskBytes[i] = (byte) 0xff; 64 | } 65 | netmaskBytes[pos] = (byte) (0xff << shift); 66 | } else { 67 | pos = 0; 68 | tokenizer = new StringTokenizer(matcher.group(5), ":"); 69 | while (tokenizer.hasMoreTokens()) { 70 | String token = tokenizer.nextToken(); 71 | int value = Integer.parseInt(token, 16); 72 | netmaskBytes[pos++] = (byte) ((value & 0xff00) >> 8); 73 | netmaskBytes[pos++] = (byte) value; 74 | } 75 | } 76 | } 77 | 78 | public boolean implies(InetAddress address) { 79 | if (false == address instanceof Inet6Address) { 80 | return false; 81 | } 82 | 83 | byte[] byteAddress = address.getAddress(); 84 | for (int i = 0; i < 16; i++) { 85 | if ((netmaskBytes[i] & byteAddress[i]) != networkAddressBytes[i]) { 86 | return false; 87 | } 88 | } 89 | return true; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/ServiceAccessController.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import org.apache.activeio.xnet.ServerService; 21 | import org.apache.activeio.xnet.ServiceException; 22 | 23 | import java.io.IOException; 24 | import java.net.Inet4Address; 25 | import java.net.InetAddress; 26 | import java.net.Socket; 27 | import java.net.UnknownHostException; 28 | import java.util.LinkedList; 29 | import java.util.Properties; 30 | import java.util.StringTokenizer; 31 | 32 | public class ServiceAccessController implements ServerService { 33 | private final ServerService next; 34 | private IPAddressPermission[] allowHosts; 35 | 36 | public ServiceAccessController(ServerService next) { 37 | this.next = next; 38 | } 39 | 40 | public ServiceAccessController(String name, ServerService next, IPAddressPermission[] ipAddressMasks) { 41 | this.next = next; 42 | this.allowHosts = ipAddressMasks; 43 | } 44 | 45 | public void service(Socket socket) throws ServiceException, IOException { 46 | // Check authorization 47 | checkHostsAuthorization(socket.getInetAddress(), socket.getLocalAddress()); 48 | 49 | next.service(socket); 50 | } 51 | 52 | public IPAddressPermission[] getAllowHosts() { 53 | return allowHosts; 54 | } 55 | 56 | public void setAllowHosts(IPAddressPermission[] ipAddressMasks) { 57 | this.allowHosts = ipAddressMasks; 58 | } 59 | 60 | public void checkHostsAuthorization(InetAddress clientAddress, InetAddress serverAddress) throws SecurityException { 61 | // Check the client ip against the server ip. Hosts are 62 | // allowed to access themselves, so if these ips 63 | // match, the following for loop will be skipped. 64 | if (clientAddress.equals(serverAddress)) { 65 | return; 66 | } 67 | 68 | for (int i = 0; i < allowHosts.length; i++) { 69 | if (allowHosts[i].implies(clientAddress)) { 70 | return; 71 | } 72 | } 73 | 74 | throw new SecurityException("Host " + clientAddress.getHostAddress() + " is not authorized to access this service."); 75 | } 76 | 77 | private void parseAdminIPs(Properties props) throws ServiceException { 78 | LinkedList ipAddressMasksList = new LinkedList(); 79 | 80 | try { 81 | InetAddress[] localIps = InetAddress.getAllByName("localhost"); 82 | for (int i = 0; i < localIps.length; i++) { 83 | if (localIps[i] instanceof Inet4Address) { 84 | ipAddressMasksList.add(new ExactIPAddressPermission(localIps[i].getAddress())); 85 | } else { 86 | ipAddressMasksList.add(new ExactIPv6AddressPermission(localIps[i].getAddress())); 87 | } 88 | } 89 | } catch (UnknownHostException e) { 90 | throw new ServiceException("Could not get localhost inet address", e); 91 | } 92 | 93 | String ipString = props.getProperty("only_from"); 94 | if (ipString != null) { 95 | StringTokenizer st = new StringTokenizer(ipString, " "); 96 | while (st.hasMoreTokens()) { 97 | String mask = st.nextToken(); 98 | ipAddressMasksList.add(IPAddressPermissionFactory.getIPAddressMask(mask)); 99 | } 100 | } 101 | 102 | allowHosts = (IPAddressPermission[]) ipAddressMasksList.toArray(new IPAddressPermission[ipAddressMasksList.size()]); 103 | } 104 | 105 | public void init(Properties props) throws Exception { 106 | parseAdminIPs(props); 107 | next.init(props); 108 | } 109 | 110 | public void start() throws ServiceException { 111 | next.start(); 112 | } 113 | 114 | public void stop() throws ServiceException { 115 | next.stop(); 116 | } 117 | 118 | public String getName() { 119 | return next.getName(); 120 | } 121 | 122 | public String getIP() { 123 | return next.getIP(); 124 | } 125 | 126 | public int getPort() { 127 | return next.getPort(); 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /activeio-core/src/main/java/org/apache/activeio/xnet/hba/StartWithIPAddressPermission.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.xnet.hba; 19 | 20 | import java.util.regex.Pattern; 21 | import java.util.regex.Matcher; 22 | import java.net.InetAddress; 23 | import java.net.Inet4Address; 24 | 25 | /** 26 | * @version $Revision$ $Date$ 27 | */ 28 | public class StartWithIPAddressPermission implements IPAddressPermission { 29 | private static final Pattern MASK_VALIDATOR = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.0$"); 30 | 31 | public static boolean canSupport(String mask) { 32 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 33 | return matcher.matches(); 34 | } 35 | 36 | private final byte[] bytes; 37 | 38 | public StartWithIPAddressPermission(String mask) { 39 | Matcher matcher = MASK_VALIDATOR.matcher(mask); 40 | if (false == matcher.matches()) { 41 | throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern()); 42 | } 43 | 44 | Byte[] tmpBytes = new Byte[4]; 45 | boolean isWildCard = false; 46 | int size = 0; 47 | for (int i = 0; i < 3; i++) { 48 | String group = matcher.group(i + 1); 49 | if (group.equals("0")) { 50 | isWildCard = true; 51 | } else if (isWildCard) { 52 | throw new IllegalArgumentException("0 at position " + size + " in mask"); 53 | } else { 54 | int value = Integer.parseInt(group); 55 | if (value < 0 || 255 < value) { 56 | throw new IllegalArgumentException("byte #" + i + " is not valid."); 57 | } 58 | tmpBytes[i] = new Byte((byte) value); 59 | size++; 60 | } 61 | } 62 | 63 | bytes = new byte[size]; 64 | for (int i = 0; i < bytes.length; i++) { 65 | bytes[i] = tmpBytes[i].byteValue(); 66 | } 67 | } 68 | 69 | public boolean implies(InetAddress address) { 70 | if (false == address instanceof Inet4Address) { 71 | return false; 72 | } 73 | 74 | byte[] byteAddress = address.getAddress(); 75 | for (int i = 0; i < bytes.length; i++) { 76 | if (byteAddress[i] != bytes[i]) { 77 | return false; 78 | } 79 | } 80 | return true; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/datagram: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.datagram.DatagramSocketSyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/multicast: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.multicast.MulticastSocketSyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/nio: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.nio.NIOSyncChannelFactory 2 | AsyncChannelFactory.class=org.apache.activeio.packet.async.nio.NIOAsyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/nio-async: -------------------------------------------------------------------------------- 1 | AsyncChannelFactory.class=org.apache.activeio.packet.async.nio.NIOAsyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/nio-sync: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.nio.NIOSyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/socket: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.socket.SocketSyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/ssl: -------------------------------------------------------------------------------- 1 | SyncChannelFactory.class=org.apache.activeio.packet.sync.ssl.SslSocketSyncChannelFactory -------------------------------------------------------------------------------- /activeio-core/src/main/resources/META-INF/services/org/apache/activeio/channel/vmpipe: -------------------------------------------------------------------------------- 1 | AsyncChannelFactory.class=org.apache.activeio.packet.async.vmpipe.VMPipeAsyncChannelFactory 2 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/PacketDataTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio; 19 | 20 | import java.io.IOException; 21 | 22 | import org.apache.activeio.packet.ByteArrayPacket; 23 | import org.apache.activeio.packet.PacketData; 24 | 25 | import junit.framework.TestCase; 26 | 27 | /** 28 | */ 29 | public class PacketDataTest extends TestCase { 30 | 31 | ByteArrayPacket packet = new ByteArrayPacket(new byte[200]); 32 | PacketData data = new PacketData(packet); 33 | 34 | public void testInteger() throws IOException { 35 | data.writeInt(Integer.MAX_VALUE); 36 | data.writeInt(Integer.MIN_VALUE); 37 | data.writeInt(551); 38 | 39 | packet.flip(); 40 | assertEquals(Integer.MAX_VALUE, data.readInt()); 41 | assertEquals(Integer.MIN_VALUE, data.readInt()); 42 | assertEquals(551, data.readInt()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/journal/JournalPerfToolSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal; 19 | 20 | import java.io.File; 21 | import java.io.FileOutputStream; 22 | import java.io.IOException; 23 | import java.io.PrintWriter; 24 | import java.util.Random; 25 | 26 | import org.apache.activeio.journal.InvalidRecordLocationException; 27 | import org.apache.activeio.journal.Journal; 28 | import org.apache.activeio.journal.JournalEventListener; 29 | import org.apache.activeio.journal.RecordLocation; 30 | import org.apache.activeio.packet.ByteArrayPacket; 31 | 32 | /** 33 | * Provides the base class uses to run performance tests against a Journal. 34 | * Should be subclassed to customize for specific journal implementation. 35 | * 36 | * @version $Revision: 1.1 $ 37 | */ 38 | abstract public class JournalPerfToolSupport implements JournalEventListener { 39 | 40 | private JournalStatsFilter journal; 41 | private Random random = new Random(); 42 | private byte data[]; 43 | private int workerCount=0; 44 | private PrintWriter statWriter; 45 | // Performance test Options 46 | 47 | // The output goes here: 48 | protected File journalDirectory = new File("journal-logs"); 49 | protected File statCSVFile = new File("stats.csv");; 50 | 51 | // Controls how often we start a new batch of workers. 52 | protected int workerIncrement=5; 53 | protected long incrementDelay=1000*20; 54 | protected boolean verbose=true; 55 | protected int initialWorkers=10; 56 | 57 | // Worker configuration. 58 | protected int recordSize=1024; 59 | protected int syncFrequency=15; 60 | protected int workerThinkTime=100; 61 | 62 | private final class Worker implements Runnable { 63 | public void run() { 64 | int i=random.nextInt()%syncFrequency; 65 | while(true) { 66 | boolean sync=false; 67 | 68 | if( syncFrequency>=0 && (i%syncFrequency)==0 ) { 69 | sync=true; 70 | } 71 | try { 72 | journal.write(new ByteArrayPacket(data), sync); 73 | Thread.sleep(workerThinkTime); 74 | } catch (Exception e) { 75 | e.printStackTrace(); 76 | return; 77 | } 78 | i++; 79 | } 80 | } 81 | } 82 | 83 | /** 84 | * @throws IOException 85 | * 86 | */ 87 | protected void exec() throws Exception { 88 | 89 | System.out.println("Client threads write records using: Record Size: "+recordSize+", Sync Frequency: "+syncFrequency+", Worker Think Time: "+workerThinkTime); 90 | 91 | // Create the record and fill it with some values. 92 | data = new byte[recordSize]; 93 | for (int i = 0; i < data.length; i++) { 94 | data[i] = (byte)i; 95 | } 96 | 97 | if( statCSVFile!=null ) { 98 | statWriter = new PrintWriter(new FileOutputStream(statCSVFile)); 99 | statWriter.println("Threads,Throughput (k/s),Forcd write latency (ms),Throughput (records/s)"); 100 | } 101 | 102 | if( journalDirectory.exists() ) { 103 | deleteDir(journalDirectory); 104 | } 105 | journal = new JournalStatsFilter(createJournal()).enableDetailedStats(verbose); 106 | journal.setJournalEventListener(this); 107 | 108 | try { 109 | 110 | System.out.println("Starting "+initialWorkers+" Workers..."); 111 | for(int i=0;i =0 && (i%syncFrequency)==0 ) { 77 | sync=true; 78 | } 79 | try { 80 | journal.write(new ByteArrayPacket(data), sync); 81 | Thread.sleep(writeWorkerThinkTime); 82 | } catch (Exception e) { 83 | e.printStackTrace(); 84 | return; 85 | } 86 | i++; 87 | } 88 | } 89 | } 90 | 91 | 92 | private final class ReadWorker implements Runnable { 93 | 94 | AtomicLong counter=new AtomicLong(); 95 | public void run() { 96 | while(true) { 97 | try { 98 | RecordLocation pos = null; 99 | while( (pos=journal.getNextRecordLocation(pos))!=null ) { 100 | Packet packet = journal.read(pos); 101 | counter.addAndGet(packet.limit()); 102 | Thread.sleep(readWorkerThinkTime); 103 | } 104 | } catch (Throwable e) { 105 | e.printStackTrace(); 106 | } 107 | } 108 | } 109 | } 110 | 111 | /** 112 | * @throws IOException 113 | * 114 | */ 115 | protected void exec() throws Exception { 116 | 117 | System.out.println("Client threads write records using: Record Size: "+recordSize+", Sync Frequency: "+syncFrequency+", Worker Think Time: "+readWorkerThinkTime); 118 | 119 | // Create the record and fill it with some values. 120 | data = new byte[recordSize]; 121 | for (int i = 0; i < data.length; i++) { 122 | data[i] = (byte)i; 123 | } 124 | 125 | if( statCSVFile!=null ) { 126 | statWriter = new PrintWriter(new FileOutputStream(statCSVFile)); 127 | statWriter.println("Threads,Write Throughput (k/s),Forced Write latency (ms), Write Throughput (records/s),Read Throughput (k/s),Read latency (ms),Read Throughput (records/s)"); 128 | } 129 | 130 | if( journalDirectory.exists() ) { 131 | deleteDir(journalDirectory); 132 | } 133 | journal = new JournalStatsFilter(createJournal()).enableDetailedStats(verbose); 134 | journal.setJournalEventListener(this); 135 | 136 | try { 137 | 138 | System.out.println("Starting "+initialWriteWorkers+" Write Workers..."); 139 | for(int i=0;i >>"); 132 | pos = (Location) journal.write( p, false); 133 | journal.setMark(pos, false); 134 | 135 | } while( pos.getLogFileId() < 5 ); 136 | 137 | Packet p = createPacket("<<>>"); 138 | pos = (Location) journal.write( p, true); 139 | 140 | // Now see if we can read that first packet. 141 | Packet data; 142 | data = journal.read(location1); 143 | assertEquals( data1, data); 144 | 145 | } 146 | 147 | /** 148 | * @param string 149 | * @return 150 | */ 151 | private Packet createPacket(String string) { 152 | return new ByteArrayPacket(string.getBytes()); 153 | } 154 | 155 | public static void assertEquals(Packet arg0, Packet arg1) { 156 | assertEquals(arg0.sliceAsBytes(), arg1.sliceAsBytes()); 157 | } 158 | 159 | public static void assertEquals(byte[] arg0, byte[] arg1) { 160 | if( arg0==null ^ arg1==null ) 161 | fail("Not equal: "+arg0+" != "+arg1); 162 | if( arg0==null ) 163 | return; 164 | if( arg0.length!=arg1.length) 165 | fail("Array lenght not equal: "+arg0.length+" != "+arg1.length); 166 | for( int i=0; i 0 ) { 47 | tool.journalDirectory = new File(args[0]); 48 | } 49 | if( args.length > 1 ) { 50 | tool.workerIncrement = Integer.parseInt(args[1]); 51 | } 52 | if( args.length > 2 ) { 53 | tool.incrementDelay = Long.parseLong(args[2]); 54 | } 55 | if( args.length > 3 ) { 56 | tool.verbose = Boolean.getBoolean(args[3]); 57 | } 58 | if( args.length > 4 ) { 59 | tool.recordSize = Integer.parseInt(args[4]); 60 | } 61 | if( args.length > 5 ) { 62 | tool.syncFrequency = Integer.parseInt(args[5]); 63 | } 64 | if( args.length > 6 ) { 65 | tool.workerThinkTime = Integer.parseInt(args[6]); 66 | } 67 | if( args.length > 7 ) { 68 | tool.logFileCount = Integer.parseInt(args[7]); 69 | } 70 | if( args.length > 8 ) { 71 | tool.logFileSize = Integer.parseInt(args[8]); 72 | } 73 | tool.exec(); 74 | } 75 | 76 | /** 77 | * @throws IOException 78 | * @see org.apache.activeio.journal.JournalPerfToolSupport#createJournal() 79 | */ 80 | public Journal createJournal() throws IOException { 81 | return new JournalImpl( this.journalDirectory, logFileCount, logFileSize); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/journal/active/JournalRWPerfTool.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | 23 | import org.apache.activeio.journal.Journal; 24 | import org.apache.activeio.journal.JournalRWPerfToolSupport; 25 | 26 | /** 27 | * A Performance statistics gathering tool for the JournalImpl based Journal. 28 | * 29 | * @version $Revision: 1.1 $ 30 | */ 31 | public class JournalRWPerfTool extends JournalRWPerfToolSupport { 32 | 33 | private int logFileSize = 1024*1024*50; 34 | private int logFileCount = 4; 35 | 36 | public static void main(String[] args) throws Exception { 37 | JournalRWPerfTool tool = new JournalRWPerfTool(); 38 | tool.initialWriteWorkers=10; 39 | tool.syncFrequency=15; 40 | tool.writeWorkerIncrement=0; 41 | tool.writeWorkerThinkTime=0; 42 | tool.verbose=false; 43 | tool.incrementDelay=5*1000; 44 | 45 | if( args.length > 0 ) { 46 | tool.journalDirectory = new File(args[0]); 47 | } 48 | if( args.length > 1 ) { 49 | tool.writeWorkerIncrement = Integer.parseInt(args[1]); 50 | } 51 | if( args.length > 2 ) { 52 | tool.incrementDelay = Long.parseLong(args[2]); 53 | } 54 | if( args.length > 3 ) { 55 | tool.verbose = Boolean.getBoolean(args[3]); 56 | } 57 | if( args.length > 4 ) { 58 | tool.recordSize = Integer.parseInt(args[4]); 59 | } 60 | if( args.length > 5 ) { 61 | tool.syncFrequency = Integer.parseInt(args[5]); 62 | } 63 | if( args.length > 6 ) { 64 | tool.writeWorkerThinkTime = Integer.parseInt(args[6]); 65 | } 66 | if( args.length > 7 ) { 67 | tool.logFileCount = Integer.parseInt(args[7]); 68 | } 69 | if( args.length > 8 ) { 70 | tool.logFileSize = Integer.parseInt(args[8]); 71 | } 72 | tool.exec(); 73 | } 74 | 75 | /** 76 | * @throws IOException 77 | * @see org.apache.activeio.journal.JournalPerfToolSupport#createJournal() 78 | */ 79 | public Journal createJournal() throws IOException { 80 | return new JournalImpl( this.journalDirectory, logFileCount, logFileSize); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/journal/active/LogFileManagerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.active; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.nio.ByteBuffer; 23 | 24 | import junit.framework.TestCase; 25 | 26 | import org.apache.activeio.journal.InvalidRecordLocationException; 27 | import org.apache.activeio.journal.active.BatchedWrite; 28 | import org.apache.activeio.journal.active.Location; 29 | import org.apache.activeio.journal.active.LogFileManager; 30 | import org.apache.activeio.journal.active.Record; 31 | import org.apache.activeio.packet.ByteArrayPacket; 32 | import org.apache.activeio.packet.ByteBufferPacket; 33 | 34 | /** 35 | * Tests the LogFile used by JournalImpl 36 | * 37 | * @version $Revision: 1.1 $ 38 | */ 39 | public class LogFileManagerTest extends TestCase { 40 | 41 | int size = 1024 * 512; 42 | 43 | int logFileCount = 4; 44 | 45 | File logDirectory = new File("test-logfile"); 46 | 47 | private LogFileManager logFile; 48 | 49 | /** 50 | * @see junit.framework.TestCase#setUp() 51 | */ 52 | protected void setUp() throws Exception { 53 | if (logDirectory.exists()) { 54 | deleteDir(logDirectory); 55 | } 56 | assertTrue(!logDirectory.exists()); 57 | logFile = new LogFileManager(logDirectory, logFileCount, size, null); 58 | } 59 | 60 | /** 61 | */ 62 | private void deleteDir(File f) { 63 | File[] files = f.listFiles(); 64 | for (int i = 0; i < files.length; i++) { 65 | File file = files[i]; 66 | file.delete(); 67 | } 68 | f.delete(); 69 | } 70 | 71 | protected void tearDown() throws Exception { 72 | logFile.dispose(); 73 | if (logDirectory.exists()) 74 | deleteDir(logDirectory); 75 | assertTrue(!logDirectory.exists()); 76 | } 77 | 78 | public void testLogFileCreation() throws IOException { 79 | assertTrue(logFile.canActivateNextLogFile()); 80 | assertEquals(null,logFile.getFirstActiveLogLocation()); 81 | assertNull(logFile.getLastMarkedRecordLocation()); 82 | assertEquals(new Location(0, 0),logFile.getNextAppendLocation()); 83 | } 84 | 85 | public void testAppendAndRead() throws IOException, InvalidRecordLocationException, InterruptedException { 86 | 87 | System.out.println("Initial:"+logFile.getNextAppendLocation()); 88 | appendHelloRecord(1001); 89 | Location loc2 = logFile.getNextAppendLocation(); 90 | appendHelloRecord(2002); 91 | appendHelloRecord(3003); 92 | appendHelloRecord(3004); 93 | 94 | Location loc3 = logFile.getNextDataRecordLocation(loc2); 95 | assertTrue(loc3.getLogFileOffset() > loc2.getLogFileOffset()); 96 | Location loc4 = logFile.getNextDataRecordLocation(loc3); 97 | assertTrue(loc4.getLogFileOffset() > loc3.getLogFileOffset()); 98 | 99 | } 100 | 101 | public void testRollOver() throws IOException, InvalidRecordLocationException, InterruptedException { 102 | 103 | int lastId = logFile.getNextAppendLocation().getLogFileId(); 104 | int counter = 0; 105 | for (int i = 0; i < logFileCount; i++) { 106 | counter += 500; 107 | appendHelloRecord(counter); 108 | if (i + 1 == logFileCount) { 109 | assertFalse(logFile.canActivateNextLogFile()); 110 | } else { 111 | assertTrue(logFile.canActivateNextLogFile()); 112 | logFile.activateNextLogFile(); 113 | assertEquals(lastId + 1, logFile.getNextAppendLocation().getLogFileId()); 114 | lastId = logFile.getNextAppendLocation().getLogFileId(); 115 | } 116 | } 117 | 118 | } 119 | 120 | /** 121 | * @param i 122 | * @throws IOException 123 | * @throws InterruptedException 124 | */ 125 | private void appendHelloRecord(int i) throws IOException, InterruptedException { 126 | byte data[] = ("Hello World: " + i).getBytes(); 127 | Record batchedRecord = new Record(LogFileManager.DATA_RECORD_TYPE, new ByteArrayPacket(data), null); 128 | batchedRecord.setLocation(logFile.getNextAppendLocation()); 129 | 130 | BatchedWrite write = new BatchedWrite(new ByteBufferPacket(ByteBuffer.allocate(1024))); 131 | write.append(batchedRecord,null, true); 132 | write.flip(); 133 | logFile.append(write); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/journal/howl/JournalPerfTool.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.journal.howl; 19 | 20 | import java.io.File; 21 | 22 | import org.apache.activeio.journal.Journal; 23 | import org.apache.activeio.journal.JournalPerfToolSupport; 24 | import org.apache.activeio.journal.howl.HowlJournal; 25 | import org.objectweb.howl.log.Configuration; 26 | 27 | /** 28 | * A Performance statistics gathering tool for the HOWL based Journal. 29 | * 30 | * @version $Revision: 1.1 $ 31 | */ 32 | public class JournalPerfTool extends JournalPerfToolSupport { 33 | 34 | private int maxLogFiles= 2; 35 | private int bufferSize = 1024*4; 36 | private int maxBuffers = 20; 37 | private int maxBlocksPerFile = 100; 38 | 39 | public static void main(String[] args) throws Exception { 40 | 41 | try { 42 | JournalPerfTool tool = new JournalPerfTool(); 43 | if( args.length > 0 ) { 44 | tool.journalDirectory = new File(args[0]); 45 | } 46 | if( args.length > 1 ) { 47 | tool.workerIncrement = Integer.parseInt(args[1]); 48 | } 49 | if( args.length > 2 ) { 50 | tool.incrementDelay = Long.parseLong(args[2]); 51 | } 52 | if( args.length > 3 ) { 53 | tool.verbose = Boolean.getBoolean(args[3]); 54 | } 55 | if( args.length > 4 ) { 56 | tool.recordSize = Integer.parseInt(args[4]); 57 | } 58 | if( args.length > 5 ) { 59 | tool.syncFrequency = Integer.parseInt(args[5]); 60 | } 61 | if( args.length > 6 ) { 62 | tool.workerThinkTime = Integer.parseInt(args[6]); 63 | } 64 | 65 | if( args.length > 7 ) { 66 | tool.maxLogFiles = Integer.parseInt(args[7]); 67 | } 68 | if( args.length > 8 ) { 69 | tool.bufferSize = Integer.parseInt(args[8]); 70 | } 71 | if( args.length > 9 ) { 72 | tool.maxBuffers = Integer.parseInt(args[9]); 73 | } 74 | if( args.length > 10 ) { 75 | tool.maxBlocksPerFile = Integer.parseInt(args[10]); 76 | } 77 | tool.exec(); 78 | } catch (Throwable e) { 79 | e.printStackTrace(); 80 | } 81 | } 82 | 83 | public Journal createJournal() throws Exception { 84 | Configuration c = new Configuration(); 85 | c.setLogFileDir(journalDirectory.getPath()); 86 | c.setMaxLogFiles(maxLogFiles); 87 | c.setBufferSize(bufferSize); 88 | c.setMaxBuffers(maxBuffers); 89 | c.setMaxBlocksPerFile(maxBlocksPerFile); 90 | return new HowlJournal( c ); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/packet/AppendedPacketTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import org.apache.activeio.packet.AppendedPacket; 21 | import org.apache.activeio.packet.ByteArrayPacket; 22 | import org.apache.activeio.packet.Packet; 23 | 24 | 25 | /** 26 | */ 27 | public class AppendedPacketTest extends PacketTestSupport { 28 | 29 | Packet createTestPacket(int capacity) { 30 | int c1 = capacity/2; 31 | int c2 = capacity-c1; 32 | 33 | return AppendedPacket.join( 34 | new ByteArrayPacket(new byte[c1]), 35 | new ByteArrayPacket(new byte[c2])); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/packet/ByteArrayPacketTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import org.apache.activeio.packet.ByteArrayPacket; 21 | import org.apache.activeio.packet.Packet; 22 | 23 | 24 | /** 25 | */ 26 | public class ByteArrayPacketTest extends PacketTestSupport { 27 | 28 | Packet createTestPacket(int capacity) { 29 | return new ByteArrayPacket(new byte[capacity]); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/packet/ByteBufferPacketTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | import org.apache.activeio.packet.ByteBufferPacket; 23 | import org.apache.activeio.packet.Packet; 24 | 25 | 26 | /** 27 | */ 28 | public class ByteBufferPacketTest extends PacketTestSupport { 29 | 30 | Packet createTestPacket(int capacity) { 31 | return new ByteBufferPacket(ByteBuffer.allocate(capacity)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/packet/PacketTestSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.packet; 19 | 20 | import java.util.Arrays; 21 | 22 | import org.apache.activeio.packet.Packet; 23 | 24 | import junit.framework.TestCase; 25 | 26 | 27 | /** 28 | */ 29 | abstract public class PacketTestSupport extends TestCase { 30 | abstract Packet createTestPacket(int capacity); 31 | 32 | public void testInit() { 33 | Packet packet = createTestPacket(100); 34 | assertEquals( 100, packet.capacity() ); 35 | assertEquals( 0, packet.position()); 36 | assertEquals( 100, packet.limit() ); 37 | assertEquals( 100, packet.remaining() ); 38 | assertTrue( packet.hasRemaining() ); 39 | } 40 | 41 | public void testPosition() { 42 | Packet packet = createTestPacket(100); 43 | packet.position(10); 44 | assertEquals( 10, packet.position() ); 45 | } 46 | 47 | public void testLimit() { 48 | Packet packet = createTestPacket(100); 49 | packet.limit(10); 50 | assertEquals( 10, packet.limit() ); 51 | } 52 | 53 | public void testRemaining() { 54 | Packet packet = createTestPacket(100); 55 | packet.position(5); 56 | packet.limit(95); 57 | assertEquals(90, packet.remaining()); 58 | assertTrue(packet.hasRemaining()); 59 | 60 | packet.position(5); 61 | packet.limit(5); 62 | assertEquals(0, packet.remaining()); 63 | assertFalse(packet.hasRemaining()); 64 | } 65 | 66 | public void testFlip() { 67 | Packet packet = createTestPacket(100); 68 | packet.position(95); 69 | packet.flip(); 70 | assertEquals(0, packet.position()); 71 | assertEquals(95, packet.limit()); 72 | } 73 | 74 | public void testClear() { 75 | Packet packet = createTestPacket(100); 76 | packet.position(5); 77 | packet.limit(95); 78 | packet.clear(); 79 | assertEquals(0, packet.position()); 80 | assertEquals(100, packet.limit()); 81 | } 82 | 83 | public void testDuplicate() { 84 | Packet packet = createTestPacket(100); 85 | packet.position(5); 86 | packet.limit(95); 87 | Packet packet2 = packet.duplicate(); 88 | packet2.position(10); 89 | packet2.limit(20); 90 | 91 | assertEquals(5, packet.position()); 92 | assertEquals(95, packet.limit()); 93 | assertEquals(10, packet2.position()); 94 | assertEquals(20, packet2.limit()); 95 | } 96 | 97 | public void testRewind() { 98 | Packet packet = createTestPacket(100); 99 | packet.position(5); 100 | packet.limit(95); 101 | packet.rewind(); 102 | 103 | assertEquals(0, packet.position()); 104 | assertEquals(95, packet.limit()); 105 | } 106 | 107 | public void testSlice() { 108 | Packet packet = createTestPacket(100); 109 | packet.position(5); 110 | packet.limit(95); 111 | Packet packet2 = packet.slice(); 112 | 113 | assertEquals(0, packet2.position()); 114 | assertEquals(90, packet2.capacity()); 115 | assertEquals(90, packet2.limit()); 116 | } 117 | 118 | public void testWriteAndReadByte() { 119 | 120 | Packet packet = createTestPacket(256); 121 | for(int i=0; i < 256; i++) { 122 | assertTrue(packet.write(i)); 123 | } 124 | assertFalse(packet.write(0)); 125 | 126 | packet.flip(); 127 | for(int i=0; i < 256; i++) { 128 | assertEquals(i, packet.read()); 129 | } 130 | assertEquals(-1, packet.read()); 131 | } 132 | 133 | public void testWriteAndReadBulkByte() { 134 | 135 | byte data[] = new byte[10]; 136 | Packet packet = createTestPacket(data.length*10); 137 | for(int i=0; i < 10; i++) { 138 | Arrays.fill(data,(byte)i); 139 | assertEquals(data.length, packet.write(data,0,data.length)); 140 | } 141 | assertEquals(-1, packet.write(data,0,data.length)); 142 | 143 | byte buffer[] = new byte[data.length]; 144 | packet.flip(); 145 | for(int i=0; i < 10; i++) { 146 | assertEquals(buffer.length, packet.read(buffer,0,buffer.length)); 147 | Arrays.fill(data,(byte)i); 148 | assertEquals(buffer, data); 149 | } 150 | assertEquals(-1, packet.read(buffer,0,buffer.length)); 151 | } 152 | 153 | public void assertEquals(byte buffer[], byte data[]) { 154 | assertEquals(buffer.length, data.length); 155 | for (int i = 0; i < data.length; i++) { 156 | assertEquals(buffer[i], data[i]); 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/stats/CountStatisticImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.stats; 19 | 20 | import java.util.concurrent.atomic.AtomicLong; 21 | 22 | import javax.management.j2ee.statistics.CountStatistic; 23 | 24 | /** 25 | * Shamelessly taken from the ActiveMQ project ( http://activemq.com ) 26 | * A count statistic implementation 27 | * 28 | * @version $Revision: 1.1 $ 29 | */ 30 | public class CountStatisticImpl extends StatisticImpl implements CountStatistic { 31 | 32 | private final AtomicLong counter = new AtomicLong(0); 33 | private CountStatisticImpl parent; 34 | 35 | public CountStatisticImpl(CountStatisticImpl parent, String name, String description) { 36 | this(name, description); 37 | this.parent = parent; 38 | } 39 | 40 | public CountStatisticImpl(String name, String description) { 41 | this(name, "count", description); 42 | } 43 | 44 | public CountStatisticImpl(String name, String unit, String description) { 45 | super(name, unit, description); 46 | } 47 | 48 | public void reset() { 49 | super.reset(); 50 | counter.set(0); 51 | } 52 | 53 | public long getCount() { 54 | return counter.get(); 55 | } 56 | 57 | public void setCount(long count) { 58 | counter.set(count); 59 | } 60 | 61 | public void add(long amount) { 62 | counter.addAndGet(amount); 63 | updateSampleTime(); 64 | if (parent != null) { 65 | parent.add(amount); 66 | } 67 | } 68 | 69 | public void increment() { 70 | counter.incrementAndGet(); 71 | updateSampleTime(); 72 | if (parent != null) { 73 | parent.increment(); 74 | } 75 | } 76 | 77 | public void subtract(long amount) { 78 | counter.addAndGet(-amount); 79 | updateSampleTime(); 80 | if (parent != null) { 81 | parent.subtract(amount); 82 | } 83 | } 84 | 85 | public void decrement() { 86 | counter.decrementAndGet(); 87 | updateSampleTime(); 88 | if (parent != null) { 89 | parent.decrement(); 90 | } 91 | } 92 | 93 | public CountStatisticImpl getParent() { 94 | return parent; 95 | } 96 | 97 | public void setParent(CountStatisticImpl parent) { 98 | this.parent = parent; 99 | } 100 | 101 | protected void appendFieldDescription(StringBuffer buffer) { 102 | buffer.append(" count: "); 103 | buffer.append(Long.toString(counter.get())); 104 | super.appendFieldDescription(buffer); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/stats/IndentPrinter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.stats; 19 | 20 | import java.io.PrintWriter; 21 | 22 | /** 23 | * A helper class for printing indented text 24 | * 25 | * @version $Revision: 1.1 $ 26 | */ 27 | public class IndentPrinter { 28 | 29 | private int indentLevel; 30 | private String indent; 31 | private PrintWriter out; 32 | 33 | public IndentPrinter() { 34 | this(new PrintWriter(System.out), " "); 35 | } 36 | 37 | public IndentPrinter(PrintWriter out) { 38 | this(out, " "); 39 | } 40 | 41 | public IndentPrinter(PrintWriter out, String indent) { 42 | this.out = out; 43 | this.indent = indent; 44 | } 45 | 46 | public void println(Object value) { 47 | out.print(value.toString()); 48 | out.println(); 49 | } 50 | 51 | public void println(String text) { 52 | out.print(text); 53 | out.println(); 54 | } 55 | 56 | public void print(String text) { 57 | out.print(text); 58 | } 59 | 60 | public void printIndent() { 61 | for (int i = 0; i < indentLevel; i++) { 62 | out.print(indent); 63 | } 64 | } 65 | 66 | public void println() { 67 | out.println(); 68 | } 69 | 70 | public void incrementIndent() { 71 | ++indentLevel; 72 | } 73 | 74 | public void decrementIndent() { 75 | --indentLevel; 76 | } 77 | 78 | public int getIndentLevel() { 79 | return indentLevel; 80 | } 81 | 82 | public void setIndentLevel(int indentLevel) { 83 | this.indentLevel = indentLevel; 84 | } 85 | 86 | public void flush() { 87 | out.flush(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/stats/StatisticImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.stats; 19 | 20 | import javax.management.j2ee.statistics.Statistic; 21 | 22 | /** 23 | * Shamelessly taken from the ActiveMQ project ( http://activemq.com ) 24 | * 25 | * Base class for a Statistic implementation 26 | * @version $Revision: 1.1 $ 27 | */ 28 | public class StatisticImpl implements Statistic { 29 | private String name; 30 | private String unit; 31 | private String description; 32 | private long startTime; 33 | private long lastSampleTime; 34 | 35 | public StatisticImpl(String name, String unit, String description) { 36 | this.name = name; 37 | this.unit = unit; 38 | this.description = description; 39 | startTime = System.currentTimeMillis(); 40 | lastSampleTime = startTime; 41 | } 42 | 43 | public synchronized void reset() { 44 | startTime = System.currentTimeMillis(); 45 | lastSampleTime = startTime; 46 | } 47 | 48 | protected synchronized void updateSampleTime() { 49 | lastSampleTime = System.currentTimeMillis(); 50 | } 51 | 52 | public synchronized String toString() { 53 | StringBuffer buffer = new StringBuffer(); 54 | buffer.append(name); 55 | buffer.append("{"); 56 | appendFieldDescription(buffer); 57 | buffer.append(" }"); 58 | return buffer.toString(); 59 | } 60 | 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | public String getUnit() { 66 | return unit; 67 | } 68 | 69 | public String getDescription() { 70 | return description; 71 | } 72 | 73 | public synchronized long getStartTime() { 74 | return startTime; 75 | } 76 | 77 | public synchronized long getLastSampleTime() { 78 | return lastSampleTime; 79 | } 80 | 81 | protected synchronized void appendFieldDescription(StringBuffer buffer) { 82 | buffer.append(" unit: "); 83 | buffer.append(unit); 84 | buffer.append(" startTime: "); 85 | //buffer.append(new Date(startTime)); 86 | buffer.append(startTime); 87 | buffer.append(" lastSampleTime: "); 88 | //buffer.append(new Date(lastSampleTime)); 89 | buffer.append(lastSampleTime); 90 | buffer.append(" description: "); 91 | buffer.append(description); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/stats/TimeStatisticImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * 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 | package org.apache.activeio.stats; 19 | 20 | import javax.management.j2ee.statistics.TimeStatistic; 21 | 22 | /** 23 | * Shamelessly taken from the ActiveMQ project ( http://activemq.com ) 24 | * A time statistic implementation 25 | * 26 | * @version $Revision: 1.1 $ 27 | */ 28 | public class TimeStatisticImpl extends StatisticImpl implements TimeStatistic { 29 | private long count; 30 | private long maxTime; 31 | private long minTime; 32 | private long totalTime; 33 | private TimeStatisticImpl parent; 34 | 35 | public TimeStatisticImpl(String name, String description) { 36 | this(name, "millis", description); 37 | } 38 | 39 | public TimeStatisticImpl(TimeStatisticImpl parent, String name, String description) { 40 | this(name, description); 41 | this.parent = parent; 42 | } 43 | 44 | public TimeStatisticImpl(String name, String unit, String description) { 45 | super(name, unit, description); 46 | } 47 | 48 | public synchronized void reset() { 49 | super.reset(); 50 | count = 0; 51 | maxTime = 0; 52 | minTime = 0; 53 | totalTime = 0; 54 | } 55 | 56 | public synchronized long getCount() { 57 | return count; 58 | } 59 | 60 | public synchronized void addTime(long time) { 61 | count++; 62 | totalTime += time; 63 | if (time > maxTime) { 64 | maxTime = time; 65 | } 66 | if (time < minTime || minTime == 0) { 67 | minTime = time; 68 | } 69 | updateSampleTime(); 70 | if (parent != null) { 71 | parent.addTime(time); 72 | } 73 | } 74 | 75 | /** 76 | * @return the maximum time of any step 77 | */ 78 | public long getMaxTime() { 79 | return maxTime; 80 | } 81 | 82 | /** 83 | * @return the minimum time of any step 84 | */ 85 | public synchronized long getMinTime() { 86 | return minTime; 87 | } 88 | 89 | /** 90 | * @return the total time of all the steps added together 91 | */ 92 | public synchronized long getTotalTime() { 93 | return totalTime; 94 | } 95 | 96 | /** 97 | * @return the average time calculated by dividing the 98 | * total time by the number of counts 99 | */ 100 | public synchronized double getAverageTime() { 101 | if (count == 0) { 102 | return 0; 103 | } 104 | double d = totalTime; 105 | return d / count; 106 | } 107 | 108 | 109 | /** 110 | * @return the average time calculated by dividing the 111 | * total time by the number of counts but excluding the 112 | * minimum and maximum times. 113 | */ 114 | public synchronized double getAverageTimeExcludingMinMax() { 115 | if (count <= 2) { 116 | return 0; 117 | } 118 | double d = totalTime - minTime - maxTime; 119 | return d / (count - 2); 120 | } 121 | 122 | 123 | /** 124 | * @return the average number of steps per second 125 | */ 126 | public double getAveragePerSecond() { 127 | double d = 1000; 128 | double averageTime = getAverageTime(); 129 | if (averageTime == 0) { 130 | return 0; 131 | } 132 | return d / averageTime; 133 | } 134 | 135 | /** 136 | * @return the average number of steps per second excluding the min & max values 137 | */ 138 | public double getAveragePerSecondExcludingMinMax() { 139 | double d = 1000; 140 | double average = getAverageTimeExcludingMinMax(); 141 | if (average == 0) { 142 | return 0; 143 | } 144 | return d / average; 145 | } 146 | 147 | public TimeStatisticImpl getParent() { 148 | return parent; 149 | } 150 | 151 | public void setParent(TimeStatisticImpl parent) { 152 | this.parent = parent; 153 | } 154 | 155 | protected synchronized void appendFieldDescription(StringBuffer buffer) { 156 | buffer.append(" count: "); 157 | buffer.append(Long.toString(count)); 158 | buffer.append(" maxTime: "); 159 | buffer.append(Long.toString(maxTime)); 160 | buffer.append(" minTime: "); 161 | buffer.append(Long.toString(minTime)); 162 | buffer.append(" totalTime: "); 163 | buffer.append(Long.toString(totalTime)); 164 | buffer.append(" averageTime: "); 165 | buffer.append(Double.toString(getAverageTime())); 166 | buffer.append(" averageTimeExMinMax: "); 167 | buffer.append(Double.toString(getAverageTimeExcludingMinMax())); 168 | buffer.append(" averagePerSecond: "); 169 | buffer.append(Double.toString(getAveragePerSecond())); 170 | buffer.append(" averagePerSecondExMinMax: "); 171 | buffer.append(Double.toString(getAveragePerSecondExcludingMinMax())); 172 | super.appendFieldDescription(buffer); 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /activeio-core/src/test/java/org/apache/activeio/stats/package.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | Some Statistic helper classes. 24 |

25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /activeio-core/src/test/resources/client.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/activemq-activeio/981f3bb0d7e0837ed2edb5d9debc5600acf2211a/activeio-core/src/test/resources/client.keystore -------------------------------------------------------------------------------- /activeio-core/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 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 | 18 | # 19 | # The logging properties used during tests.. 20 | # 21 | log4j.rootLogger=INFO, out 22 | 23 | # CONSOLE appender not used by default 24 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 25 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 26 | log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n 27 | 28 | # File appender 29 | log4j.appender.out=org.apache.log4j.FileAppender 30 | log4j.appender.out.layout=org.apache.log4j.PatternLayout 31 | log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n 32 | log4j.appender.out.file=${basedir}/target/test.log 33 | log4j.appender.out.append=true 34 | -------------------------------------------------------------------------------- /activeio-core/src/test/resources/server.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apache/activemq-activeio/981f3bb0d7e0837ed2edb5d9debc5600acf2211a/activeio-core/src/test/resources/server.keystore --------------------------------------------------------------------------------