directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject [3/3] directory-kerby git commit: Add kerby-event library and the event-network support back in the branch
Date Sat, 13 Jun 2015 10:02:35 GMT
Add kerby-event library and the event-network support back in the branch


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/f3645ba1
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/f3645ba1
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/f3645ba1

Branch: refs/heads/event-network-support
Commit: f3645ba1d0e924243ef18037ea9a6b29d344febe
Parents: 6b4d1b4
Author: Drankye <drankye@gmail.com>
Authored: Sat Jun 13 18:01:32 2015 +0800
Committer: Drankye <drankye@gmail.com>
Committed: Sat Jun 13 18:01:32 2015 +0800

----------------------------------------------------------------------
 kerby-kerb/kerb-client/pom.xml                  |   5 +
 .../kerby/kerberos/kerb/client/KrbClient.java   |  14 +-
 .../kerby/kerberos/kerb/client/KrbOption.java   |   1 +
 .../client/impl/event/EventBasedKrbClient.java  | 118 ++++++++
 .../kerb/client/impl/event/EventKrbHandler.java |  87 ++++++
 .../kerb/client/impl/event/KrbClientEvent.java  |  43 +++
 .../client/impl/event/KrbClientEventType.java   |  29 ++
 kerby-kerb/kerb-common/pom.xml                  |   5 +
 .../kerb/common/KrbStreamingDecoder.java        |  42 +++
 .../kerby/kerberos/kerb/server/KdcTestBase.java |  16 +-
 .../kerb/server/KdcTestUsingEventClient.java    |  35 +++
 .../kerb/server/KdcTestUsingEventKdc.java       |  35 +++
 kerby-kerb/kerb-server/pom.xml                  |   5 +
 .../kerby/kerberos/kerb/server/KdcServer.java   |  10 +
 .../kerberos/kerb/server/KdcServerOption.java   |   1 +
 .../server/impl/event/EventBasedKdcServer.java  |  76 +++++
 .../kerb/server/impl/event/EventKdcHandler.java |  61 ++++
 lib/kerby-event/README                          |   1 +
 lib/kerby-event/pom.xml                         |  28 ++
 .../kerby/event/AbstractEventHandler.java       |  55 ++++
 .../event/AbstractInternalEventHandler.java     |  66 +++++
 .../kerby/event/BufferedEventHandler.java       |  49 +++
 .../java/org/apache/kerby/event/Dispatcher.java |  29 ++
 .../main/java/org/apache/kerby/event/Event.java |  43 +++
 .../org/apache/kerby/event/EventHandler.java    |  31 ++
 .../java/org/apache/kerby/event/EventHub.java   | 193 ++++++++++++
 .../java/org/apache/kerby/event/EventType.java  |  24 ++
 .../org/apache/kerby/event/EventWaiter.java     |  37 +++
 .../kerby/event/ExecutedEventHandler.java       |  76 +++++
 .../kerby/event/InternalEventHandler.java       |  34 +++
 .../kerby/event/LongRunningEventHandler.java    |  77 +++++
 .../apache/kerby/event/WaitEventHandler.java    | 126 ++++++++
 .../org/apache/kerby/transport/Acceptor.java    |  36 +++
 .../org/apache/kerby/transport/Connector.java   |  36 +++
 .../apache/kerby/transport/MessageHandler.java  |  42 +++
 .../org/apache/kerby/transport/Network.java     | 297 +++++++++++++++++++
 .../org/apache/kerby/transport/Transport.java   |  84 ++++++
 .../kerby/transport/TransportHandler.java       |  34 +++
 .../kerby/transport/TransportSelector.java      | 100 +++++++
 .../kerby/transport/buffer/BufferPool.java      |  33 +++
 .../kerby/transport/buffer/BufferUtil.java      |  42 +++
 .../kerby/transport/buffer/RecvBuffer.java      | 155 ++++++++++
 .../kerby/transport/buffer/TransBuffer.java     |  49 +++
 .../kerby/transport/event/AddressEvent.java     |  39 +++
 .../kerby/transport/event/MessageEvent.java     |  41 +++
 .../kerby/transport/event/TransportEvent.java   |  56 ++++
 .../transport/event/TransportEventType.java     |  29 ++
 .../kerby/transport/tcp/DecodingCallback.java   |  38 +++
 .../kerby/transport/tcp/StreamingDecoder.java   |  26 ++
 .../apache/kerby/transport/tcp/TcpAcceptor.java | 112 +++++++
 .../kerby/transport/tcp/TcpAddressEvent.java    |  36 +++
 .../kerby/transport/tcp/TcpConnector.java       |  94 ++++++
 .../kerby/transport/tcp/TcpEventType.java       |  27 ++
 .../kerby/transport/tcp/TcpTransport.java       | 116 ++++++++
 .../transport/tcp/TcpTransportHandler.java      |  77 +++++
 .../apache/kerby/transport/udp/UdpAcceptor.java |  84 ++++++
 .../kerby/transport/udp/UdpAddressEvent.java    |  36 +++
 .../kerby/transport/udp/UdpChannelEvent.java    |  47 +++
 .../kerby/transport/udp/UdpConnector.java       |  76 +++++
 .../kerby/transport/udp/UdpEventType.java       |  29 ++
 .../kerby/transport/udp/UdpTransport.java       |  65 ++++
 .../transport/udp/UdpTransportHandler.java      | 109 +++++++
 .../org/apache/kerby/event/NetworkUtil.java     |  47 +++
 .../java/org/apache/kerby/event/TestBuffer.java |  51 ++++
 .../kerby/event/network/TestNetworkBase.java    |  64 ++++
 .../kerby/event/network/TestNetworkClient.java  | 213 +++++++++++++
 .../kerby/event/network/TestNetworkServer.java  | 115 +++++++
 .../org/apache/kerby/event/tcp/TestTcpBase.java |  62 ++++
 .../apache/kerby/event/tcp/TestTcpClient.java   | 162 ++++++++++
 .../apache/kerby/event/tcp/TestTcpServer.java   |  94 ++++++
 .../org/apache/kerby/event/udp/TestUdpBase.java |  46 +++
 .../apache/kerby/event/udp/TestUdpClient.java   | 151 ++++++++++
 .../apache/kerby/event/udp/TestUdpServer.java   |  93 ++++++
 lib/pom.xml                                     |   1 +
 74 files changed, 4594 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/pom.xml b/kerby-kerb/kerb-client/pom.xml
index 2dca9a4..5d01fe3 100644
--- a/kerby-kerb/kerb-client/pom.xml
+++ b/kerby-kerb/kerb-client/pom.xml
@@ -43,6 +43,11 @@
     </dependency>
     <dependency>
       <groupId>org.apache.kerby</groupId>
+      <artifactId>kerby-event</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.kerby</groupId>
       <artifactId>kerb-util</artifactId>
       <version>${project.version}</version>
     </dependency>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
index adf89c3..7cbb3d0 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbClient.java
@@ -22,6 +22,7 @@ package org.apache.kerby.kerberos.kerb.client;
 import org.apache.kerby.KOptions;
 import org.apache.kerby.kerberos.kerb.KrbException;
 import org.apache.kerby.kerberos.kerb.client.impl.DefaultInternalKrbClient;
+import org.apache.kerby.kerberos.kerb.client.impl.event.EventBasedKrbClient;
 import org.apache.kerby.kerberos.kerb.spec.base.AuthToken;
 import org.apache.kerby.kerberos.kerb.spec.ticket.ServiceTicket;
 import org.apache.kerby.kerberos.kerb.spec.ticket.TgtTicket;
@@ -119,11 +120,22 @@ public class KrbClient {
     }
 
     /**
+     * Use event model. By default blocking model is used.
+     */
+    public void useEventModel() {
+        commonOptions.add(KrbOption.USE_EVENT_MODEL);
+    }
+
+    /**
      * Init the client.
      * @throws KrbException
      */
     public void init() throws KrbException {
-        innerClient = new DefaultInternalKrbClient();
+        if (commonOptions.contains(KrbOption.USE_EVENT_MODEL)) {
+            innerClient = new EventBasedKrbClient();
+        } else {
+            innerClient = new DefaultInternalKrbClient();
+        }
         innerClient.init(commonOptions);
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
index caea76d..f1afbfd 100644
--- a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/KrbOption.java
@@ -24,6 +24,7 @@ import org.apache.kerby.KOptionType;
 
 public enum KrbOption implements KOption {
     NONE("NONE"),
+    USE_EVENT_MODEL("use event model", KOptionType.NOV),
     CLIENT_PRINCIPAL("client-principal", "Client principal", KOptionType.STR),
     KRB_CONFIG("krb config", KOptionType.OBJ),
     CONF_DIR("conf dir", KOptionType.DIR),

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventBasedKrbClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventBasedKrbClient.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventBasedKrbClient.java
new file mode 100644
index 0000000..525dfb6
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventBasedKrbClient.java
@@ -0,0 +1,118 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.client.impl.event;
+
+import org.apache.kerby.KOptions;
+import org.apache.kerby.event.Event;
+import org.apache.kerby.event.EventHub;
+import org.apache.kerby.event.EventWaiter;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.client.impl.AbstractInternalKrbClient;
+import org.apache.kerby.kerberos.kerb.client.request.AsRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+import org.apache.kerby.kerberos.kerb.common.KrbStreamingDecoder;
+import org.apache.kerby.kerberos.kerb.spec.ticket.ServiceTicket;
+import org.apache.kerby.kerberos.kerb.spec.ticket.TgtTicket;
+import org.apache.kerby.transport.Network;
+import org.apache.kerby.transport.Transport;
+import org.apache.kerby.transport.event.TransportEvent;
+import org.apache.kerby.transport.event.TransportEventType;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * An event based krb client implementation.
+ */
+public class EventBasedKrbClient extends AbstractInternalKrbClient {
+
+    private EventKrbHandler krbHandler;
+    private EventHub eventHub;
+    private EventWaiter eventWaiter;
+    private Transport transport;
+
+    @Override
+    public void init(KOptions commonOptions) throws KrbException {
+        super.init(commonOptions);
+
+        this.krbHandler = new EventKrbHandler();
+        krbHandler.init(getContext());
+
+        this.eventHub = new EventHub();
+        eventHub.register(krbHandler);
+
+        Network network = new Network();
+        network.setStreamingDecoder(new KrbStreamingDecoder());
+        eventHub.register(network);
+
+        eventWaiter = eventHub.waitEvent(
+                TransportEventType.NEW_TRANSPORT,
+                KrbClientEventType.TGT_RESULT,
+                KrbClientEventType.TKT_RESULT
+        );
+
+        eventHub.start();
+
+        network.tcpConnect(getSetting().getKdcHost(),
+                getSetting().getKdcTcpPort());
+        if (getSetting().allowUdp()) {
+            network.udpConnect(getSetting().getKdcHost(),
+                    getSetting().getKdcUdpPort());
+        }
+        final Event event = eventWaiter.waitEvent(TransportEventType.NEW_TRANSPORT);
+        transport = ((TransportEvent) event).getTransport();
+    }
+
+    @Override
+    protected TgtTicket doRequestTgtTicket(AsRequest tgtTktReq) throws KrbException {
+        tgtTktReq.setSessionData(transport);
+        transport.setAttachment(tgtTktReq);
+
+        eventHub.dispatch(KrbClientEvent.createTgtIntentEvent(tgtTktReq));
+        Event resultEvent;
+        try {
+            resultEvent = eventWaiter.waitEvent(KrbClientEventType.TGT_RESULT,
+                    getSetting().getTimeout(), TimeUnit.SECONDS);
+        } catch (TimeoutException e) {
+            throw new KrbException("Network timeout", e);
+        }
+        AsRequest asResponse = (AsRequest) resultEvent.getEventData();
+
+        return asResponse.getTicket();
+    }
+
+    @Override
+    protected ServiceTicket doRequestServiceTicket(TgsRequest ticketReq) throws KrbException {
+        ticketReq.setSessionData(transport);
+        transport.setAttachment(ticketReq);
+
+        eventHub.dispatch(KrbClientEvent.createTktIntentEvent(ticketReq));
+        Event resultEvent;
+        try {
+            resultEvent = eventWaiter.waitEvent(KrbClientEventType.TKT_RESULT,
+                    getSetting().getTimeout(), TimeUnit.SECONDS);
+        } catch (TimeoutException e) {
+            throw new KrbException("Network timeout", e);
+        }
+        TgsRequest tgsResponse = (TgsRequest) resultEvent.getEventData();
+
+        return tgsResponse.getServiceTicket();
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventKrbHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventKrbHandler.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventKrbHandler.java
new file mode 100644
index 0000000..e2a6dbd
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/EventKrbHandler.java
@@ -0,0 +1,87 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.client.impl.event;
+
+import org.apache.kerby.event.AbstractEventHandler;
+import org.apache.kerby.event.Event;
+import org.apache.kerby.event.EventType;
+import org.apache.kerby.kerberos.kerb.client.KrbContext;
+import org.apache.kerby.kerberos.kerb.client.KrbHandler;
+import org.apache.kerby.kerberos.kerb.client.request.AsRequest;
+import org.apache.kerby.kerberos.kerb.client.request.KdcRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+import org.apache.kerby.transport.Transport;
+import org.apache.kerby.transport.event.MessageEvent;
+import org.apache.kerby.transport.event.TransportEventType;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class EventKrbHandler extends AbstractEventHandler {
+
+    private KrbHandler innerHandler;
+
+
+    public void init(KrbContext context) {
+        this.innerHandler = new KrbHandler() {
+            @Override
+            protected void sendMessage(KdcRequest kdcRequest,
+                                       ByteBuffer requestMessage) throws IOException {
+                Transport transport = (Transport) kdcRequest.getSessionData();
+                transport.sendMessage(requestMessage);
+            }
+        };
+        innerHandler.init(context);
+    }
+
+    @Override
+    public EventType[] getInterestedEvents() {
+        return new EventType[] {
+                TransportEventType.INBOUND_MESSAGE,
+                KrbClientEventType.TGT_INTENT,
+                KrbClientEventType.TKT_INTENT
+        };
+    }
+
+    @Override
+    protected void doHandle(Event event) throws Exception {
+        EventType eventType = event.getEventType();
+
+        if (eventType == KrbClientEventType.TGT_INTENT ||
+                eventType == KrbClientEventType.TKT_INTENT) {
+            KdcRequest kdcRequest = (KdcRequest) event.getEventData();
+            innerHandler.handleRequest(kdcRequest);
+        } else if (event.getEventType() == TransportEventType.INBOUND_MESSAGE) {
+            handleMessage((MessageEvent) event);
+        }
+    }
+
+    protected void handleMessage(MessageEvent event) throws Exception {
+        ByteBuffer receivedMessage = event.getMessage();
+
+        KdcRequest kdcRequest = (KdcRequest) event.getTransport().getAttachment();
+        innerHandler.onResponseMessage(kdcRequest, receivedMessage);
+        if (AsRequest.class.isAssignableFrom(kdcRequest.getClass())) {
+            dispatch(KrbClientEvent.createTgtResultEvent((AsRequest) kdcRequest));
+        } else if (TgsRequest.class.isAssignableFrom(kdcRequest.getClass())) {
+            dispatch(KrbClientEvent.createTktResultEvent((TgsRequest) kdcRequest));
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEvent.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEvent.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEvent.java
new file mode 100644
index 0000000..574e2d0
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEvent.java
@@ -0,0 +1,43 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.client.impl.event;
+
+import org.apache.kerby.event.Event;
+import org.apache.kerby.kerberos.kerb.client.request.AsRequest;
+import org.apache.kerby.kerberos.kerb.client.request.TgsRequest;
+
+public class KrbClientEvent {
+
+    public static Event createTgtIntentEvent(AsRequest asRequest) {
+        return new Event(KrbClientEventType.TGT_INTENT, asRequest);
+    }
+
+    public static Event createTktIntentEvent(TgsRequest tgsRequest) {
+        return new Event(KrbClientEventType.TKT_INTENT, tgsRequest);
+    }
+
+    public static Event createTgtResultEvent(AsRequest asRequest) {
+        return new Event(KrbClientEventType.TGT_RESULT, asRequest);
+    }
+
+    public static Event createTktResultEvent(TgsRequest tgsRequest) {
+        return new Event(KrbClientEventType.TKT_RESULT, tgsRequest);
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEventType.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEventType.java b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEventType.java
new file mode 100644
index 0000000..8f0e981
--- /dev/null
+++ b/kerby-kerb/kerb-client/src/main/java/org/apache/kerby/kerberos/kerb/client/impl/event/KrbClientEventType.java
@@ -0,0 +1,29 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.client.impl.event;
+
+import org.apache.kerby.event.EventType;
+
+public enum KrbClientEventType implements EventType {
+    TGT_INTENT,
+    TGT_RESULT,
+    TKT_INTENT,
+    TKT_RESULT
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-common/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/pom.xml b/kerby-kerb/kerb-common/pom.xml
index c745928..d21d320 100644
--- a/kerby-kerb/kerb-common/pom.xml
+++ b/kerby-kerb/kerb-common/pom.xml
@@ -33,6 +33,11 @@
     </dependency>
     <dependency>
       <groupId>org.apache.kerby</groupId>
+      <artifactId>kerby-event</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.kerby</groupId>
       <artifactId>kerb-core</artifactId>
       <version>${project.version}</version>
     </dependency>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbStreamingDecoder.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbStreamingDecoder.java b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbStreamingDecoder.java
new file mode 100644
index 0000000..a647bf0
--- /dev/null
+++ b/kerby-kerb/kerb-common/src/main/java/org/apache/kerby/kerberos/kerb/common/KrbStreamingDecoder.java
@@ -0,0 +1,42 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.common;
+
+import org.apache.kerby.transport.tcp.DecodingCallback;
+import org.apache.kerby.transport.tcp.StreamingDecoder;
+
+import java.nio.ByteBuffer;
+
+public class KrbStreamingDecoder implements StreamingDecoder {
+
+    @Override
+    public void decode(ByteBuffer streamingBuffer, DecodingCallback callback) {
+        if (streamingBuffer.remaining() >= 4) {
+            int len = streamingBuffer.getInt();
+            if (streamingBuffer.remaining() >= len) {
+                callback.onMessageComplete(len + 4, 4); //Remove the prefix length
+            } else {
+                callback.onMoreDataNeeded(len + 4);
+            }
+        } else {
+            callback.onMoreDataNeeded();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-kdc-test/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcTestBase.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcTestBase.java b/kerby-kerb/kerb-kdc-test/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcTestBase.java
index 0543568..f750d31 100644
--- a/kerby-kerb/kerb-kdc-test/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcTestBase.java
+++ b/kerby-kerb/kerb-kdc-test/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcTestBase.java
@@ -152,7 +152,9 @@ public abstract class KdcTestBase {
      * @throws Exception
      */
     protected void prepareKrbClient() throws Exception {
-
+        if (useEventModelClient()) {
+            krbClnt.useEventModel();
+        }
     }
 
     /**
@@ -170,6 +172,18 @@ public abstract class KdcTestBase {
         if (udpPort > 0) {
             kdcServer.setKdcUdpPort(udpPort);
         }
+
+        if (useEventModelKdc()) {
+            kdcServer.useEventModel();
+        }
+    }
+
+    protected boolean useEventModelKdc() {
+        return false;
+    }
+
+    protected boolean useEventModelClient() {
+        return false;
     }
 
     protected void setUpKdcServer() throws Exception {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventClient.java b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventClient.java
new file mode 100644
index 0000000..5e906de
--- /dev/null
+++ b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventClient.java
@@ -0,0 +1,35 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.server;
+
+import org.junit.Test;
+
+public class KdcTestUsingEventClient extends KdcTest {
+
+    @Override
+    protected boolean useEventModelClient() {
+        return false;
+    }
+
+    @Test
+    public void testKdc() throws Exception {
+        performKdcTest();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventKdc.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventKdc.java b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventKdc.java
new file mode 100644
index 0000000..99a1aad
--- /dev/null
+++ b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/KdcTestUsingEventKdc.java
@@ -0,0 +1,35 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.server;
+
+import org.junit.Test;
+
+public class KdcTestUsingEventKdc extends KdcTest {
+
+    @Override
+    protected boolean useEventModelKdc() {
+        return false;
+    }
+
+    @Test
+    public void testKdc() throws Exception {
+        performKdcTest();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-server/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/pom.xml b/kerby-kerb/kerb-server/pom.xml
index b0ab417..138d123 100644
--- a/kerby-kerb/kerb-server/pom.xml
+++ b/kerby-kerb/kerb-server/pom.xml
@@ -47,6 +47,11 @@
       <artifactId>kerb-identity</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.kerby</groupId>
+      <artifactId>kerby-event</artifactId>
+      <version>${project.version}</version>
+    </dependency>
   </dependencies>
 
   <profiles>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServer.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServer.java b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServer.java
index a2f6c46..7cbd6eb 100644
--- a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServer.java
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServer.java
@@ -22,6 +22,7 @@ package org.apache.kerby.kerberos.kerb.server;
 import org.apache.kerby.KOptions;
 import org.apache.kerby.kerberos.kerb.identity.IdentityService;
 import org.apache.kerby.kerberos.kerb.server.impl.DefaultInternalKdcServerImpl;
+import org.apache.kerby.kerberos.kerb.server.impl.event.EventBasedKdcServer;
 
 import java.io.File;
 
@@ -113,6 +114,13 @@ public class KdcServer {
     }
 
     /**
+     * Use event model. By default blocking model is used.
+     */
+    public void useEventModel() {
+        commonOptions.add(KdcServerOption.USE_EVENT_MODEL);
+    }
+
+    /**
      * Set runtime folder.
      * @param workDir
      */
@@ -165,6 +173,8 @@ public class KdcServer {
         if (commonOptions.contains(KdcServerOption.INNER_KDC_IMPL)) {
             innerKdc = (InternalKdcServer) commonOptions.getOptionValue(
                     KdcServerOption.INNER_KDC_IMPL);
+        } else if (commonOptions.contains(KdcServerOption.USE_EVENT_MODEL)) {
+            innerKdc = new EventBasedKdcServer();
         } else {
             innerKdc = new DefaultInternalKdcServerImpl();
         }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServerOption.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServerOption.java b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServerOption.java
index a6bad7d..61017d2 100644
--- a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServerOption.java
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/KdcServerOption.java
@@ -27,6 +27,7 @@ import org.apache.kerby.KOptionType;
  */
 public enum KdcServerOption implements KOption {
     NONE("NONE"),
+    USE_EVENT_MODEL("use event model", KOptionType.NOV),
     INNER_KDC_IMPL("inner KDC impl", KOptionType.OBJ),
     KDC_CONFIG("kdc config", KOptionType.OBJ),
     BACKEND_CONFIG("backend config", KOptionType.OBJ),

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventBasedKdcServer.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventBasedKdcServer.java b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventBasedKdcServer.java
new file mode 100644
index 0000000..ec076cf
--- /dev/null
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventBasedKdcServer.java
@@ -0,0 +1,76 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *
+ */
+package org.apache.kerby.kerberos.kerb.server.impl.event;
+
+import org.apache.kerby.event.EventHub;
+import org.apache.kerby.kerberos.kerb.common.KrbStreamingDecoder;
+import org.apache.kerby.kerberos.kerb.server.KdcContext;
+import org.apache.kerby.kerberos.kerb.server.impl.AbstractInternalKdcServer;
+import org.apache.kerby.kerberos.kerb.server.preauth.PreauthHandler;
+import org.apache.kerby.transport.Network;
+
+/**
+ * Event based KDC server.
+ */
+public class EventBasedKdcServer extends AbstractInternalKdcServer {
+
+    private EventKdcHandler kdcHandler;
+    private EventHub eventHub;
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        prepareHandler();
+
+        this.eventHub = new EventHub();
+
+        eventHub.register(kdcHandler);
+
+        Network network = new Network();
+        network.setStreamingDecoder(new KrbStreamingDecoder());
+        eventHub.register(network);
+
+        eventHub.start();
+        network.tcpListen(getSetting().getKdcHost(),
+                getSetting().getKdcTcpPort());
+        if (getSetting().allowUdp()) {
+            network.udpListen(getSetting().getKdcHost(),
+                    getSetting().getKdcUdpPort());
+        }
+    }
+
+    private void prepareHandler() {
+        KdcContext kdcContext = new KdcContext(getSetting());
+        kdcContext.setIdentityService(getBackend());
+        PreauthHandler preauthHandler = new PreauthHandler();
+        preauthHandler.init();
+        kdcContext.setPreauthHandler(preauthHandler);
+
+        this.kdcHandler = new EventKdcHandler(kdcContext);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+
+        eventHub.stop();
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventKdcHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventKdcHandler.java b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventKdcHandler.java
new file mode 100644
index 0000000..5c6ab7b
--- /dev/null
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/impl/event/EventKdcHandler.java
@@ -0,0 +1,61 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.kerberos.kerb.server.impl.event;
+
+import org.apache.kerby.kerberos.kerb.server.KdcHandler;
+import org.apache.kerby.kerberos.kerb.server.KdcContext;
+import org.apache.kerby.transport.MessageHandler;
+import org.apache.kerby.transport.Transport;
+import org.apache.kerby.transport.event.MessageEvent;
+import org.apache.kerby.transport.tcp.TcpTransport;
+
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+
+/**
+ * KDC handler to process client requests. Currently only one realm is supported.
+ */
+public class EventKdcHandler extends MessageHandler {
+
+    private final KdcHandler myKdcHandler;
+
+    public EventKdcHandler(KdcContext kdcContext) {
+        this.myKdcHandler = new KdcHandler(kdcContext);
+    }
+
+    @Override
+    protected void handleMessage(MessageEvent event) throws Exception {
+        ByteBuffer message = event.getMessage();
+        Transport transport = event.getTransport();
+
+        InetSocketAddress clientAddress = transport.getRemoteAddress();
+        boolean isTcp = transport instanceof TcpTransport;
+
+        try {
+            ByteBuffer krbResponse = myKdcHandler.handleMessage(message, isTcp,
+                    clientAddress.getAddress());
+            transport.sendMessage(krbResponse);
+        } catch (Exception e) {
+            //TODO: log the error
+            System.out.println("Error occured while processing request:"
+                    + e.getMessage());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/README
----------------------------------------------------------------------
diff --git a/lib/kerby-event/README b/lib/kerby-event/README
new file mode 100644
index 0000000..cb3b88a
--- /dev/null
+++ b/lib/kerby-event/README
@@ -0,0 +1 @@
+An event driven application framework with mixed (TCP, UDP) x (connector, acceptor) supported.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/pom.xml
----------------------------------------------------------------------
diff --git a/lib/kerby-event/pom.xml b/lib/kerby-event/pom.xml
new file mode 100644
index 0000000..af1e11a
--- /dev/null
+++ b/lib/kerby-event/pom.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License. See accompanying LICENSE file.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>lib</artifactId>
+    <groupId>org.apache.kerby</groupId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>kerby-event</artifactId>
+  <name>Kerby Event</name>
+  <description>Kerby Event and Transport facilities for both client and server</description>
+
+</project>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractEventHandler.java
new file mode 100644
index 0000000..59a0a82
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractEventHandler.java
@@ -0,0 +1,55 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public abstract class AbstractEventHandler implements EventHandler {
+
+    private Dispatcher dispatcher;
+
+    public AbstractEventHandler() {
+
+    }
+
+    protected void dispatch(Event event) {
+        dispatcher.dispatch(event);
+    }
+
+    @Override
+    public Dispatcher getDispatcher() {
+        return dispatcher;
+    }
+
+    @Override
+    public void setDispatcher(Dispatcher dispatcher) {
+        this.dispatcher = dispatcher;
+    }
+
+    @Override
+    public void handle(Event event) {
+        try {
+            doHandle(event);
+        } catch (Exception e) {
+            throw new RuntimeException(event.toString(), e);
+        }
+    }
+
+    protected abstract void doHandle(Event event) throws Exception;
+}
+

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractInternalEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractInternalEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractInternalEventHandler.java
new file mode 100644
index 0000000..bfed126
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/AbstractInternalEventHandler.java
@@ -0,0 +1,66 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public abstract class AbstractInternalEventHandler extends AbstractEventHandler
+        implements InternalEventHandler {
+
+    private int id = -1;
+    protected EventHandler handler;
+
+    private static AtomicInteger idGen = new AtomicInteger(1);
+
+    public AbstractInternalEventHandler() {
+        super();
+
+        this.id = idGen.getAndIncrement();
+
+        init();
+    }
+
+    public AbstractInternalEventHandler(EventHandler handler) {
+        this();
+
+        this.handler = handler;
+    }
+
+    protected void setEventHandler(EventHandler handler) {
+        this.handler = handler;
+    }
+
+    @Override
+    public int id() {
+        return id;
+    }
+
+    public abstract void init();
+
+    protected void process(Event event) {
+        handler.handle(event);
+    }
+
+    @Override
+    public EventType[] getInterestedEvents() {
+        return handler.getInterestedEvents();
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/BufferedEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/BufferedEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/BufferedEventHandler.java
new file mode 100644
index 0000000..d3c1401
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/BufferedEventHandler.java
@@ -0,0 +1,49 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+
+/**
+ * An EventHandler wrapper buffering events and processing them later
+ */
+public abstract class BufferedEventHandler extends AbstractInternalEventHandler {
+
+    protected BlockingQueue<Event> eventQueue;
+
+    public BufferedEventHandler(EventHandler handler) {
+        super(handler);
+    }
+
+    public BufferedEventHandler() {
+        super();
+    }
+
+    @Override
+    public void init() {
+        this.eventQueue = new ArrayBlockingQueue<Event>(2);
+    }
+
+    @Override
+    protected void doHandle(Event event) throws Exception {
+        eventQueue.put(event);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/Dispatcher.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/Dispatcher.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/Dispatcher.java
new file mode 100644
index 0000000..f730745
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/Dispatcher.java
@@ -0,0 +1,29 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public interface Dispatcher {
+
+    void dispatch(Event event);
+
+    void register(EventHandler handler);
+
+    void register(InternalEventHandler internalHandler);
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/Event.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/Event.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/Event.java
new file mode 100644
index 0000000..332ee0d
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/Event.java
@@ -0,0 +1,43 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public class Event {
+
+    private EventType eventType;
+    private Object eventData;
+
+    public Event(EventType eventType) {
+        this.eventType = eventType;
+    }
+
+    public Event(EventType eventType, Object eventData) {
+        this.eventType = eventType;
+        this.eventData = eventData;
+    }
+
+    public EventType getEventType() {
+        return eventType;
+    }
+
+    public Object getEventData() {
+        return eventData;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHandler.java
new file mode 100644
index 0000000..27c4a44
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHandler.java
@@ -0,0 +1,31 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public interface EventHandler {
+
+    void handle(Event event);
+
+    EventType[] getInterestedEvents();
+
+    Dispatcher getDispatcher();
+
+    void setDispatcher(Dispatcher dispatcher);
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHub.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHub.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHub.java
new file mode 100644
index 0000000..9f10c88
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventHub.java
@@ -0,0 +1,193 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class EventHub implements Dispatcher {
+
+    private enum BuiltInEventType implements EventType {
+        STOP,
+        ALL
+    }
+
+    private boolean started = false;
+
+    private Map<Integer, InternalEventHandler> handlers =
+            new ConcurrentHashMap<Integer, InternalEventHandler>();
+
+    private Map<EventType, Set<Integer>> eventHandlersMap =
+        new ConcurrentHashMap<EventType, Set<Integer>>();
+
+    private InternalEventHandler builtInHandler;
+
+    class BuiltInEventHandler extends AbstractEventHandler {
+        public BuiltInEventHandler() {
+            super();
+        }
+
+        @Override
+        protected void doHandle(Event event) {
+
+        }
+
+        @Override
+        public EventType[] getInterestedEvents() {
+            return BuiltInEventType.values();
+        }
+    }
+
+    public EventHub() {
+        init();
+    }
+
+    private void init() {
+        EventHandler eh = new BuiltInEventHandler();
+        builtInHandler = new ExecutedEventHandler(eh);
+        register(builtInHandler);
+    }
+
+    @Override
+    public void dispatch(Event event) {
+        process(event);
+    }
+
+    @Override
+    public void register(EventHandler handler) {
+        handler.setDispatcher(this);
+        InternalEventHandler ieh = new ExecutedEventHandler(handler);
+        register(ieh);
+    }
+
+    @Override
+    public void register(InternalEventHandler handler) {
+        handler.setDispatcher(this);
+        handler.init();
+        handlers.put(handler.id(), handler);
+
+        if (started) {
+            handler.start();
+        }
+
+        EventType[] interestedEvents = handler.getInterestedEvents();
+        Set<Integer> tmpHandlers;
+        for (EventType eventType : interestedEvents) {
+            if (eventHandlersMap.containsKey(eventType)) {
+                tmpHandlers = eventHandlersMap.get(eventType);
+            } else {
+                tmpHandlers = new HashSet<Integer>();
+                eventHandlersMap.put(eventType, tmpHandlers);
+            }
+            tmpHandlers.add(handler.id());
+        }
+    }
+
+    public EventWaiter waitEvent(final EventType event) {
+        return waitEvent(new EventType[] { event } );
+    }
+
+    public EventWaiter waitEvent(final EventType... events) {
+        EventHandler handler = new AbstractEventHandler() {
+            @Override
+            protected void doHandle(Event event) throws Exception {
+                // no op;
+            }
+
+            @Override
+            public EventType[] getInterestedEvents() {
+                return events;
+            }
+        };
+
+        handler.setDispatcher(this);
+        final WaitEventHandler waitEventHandler = new WaitEventHandler(handler);
+        register(waitEventHandler);
+        EventWaiter waiter = new EventWaiter() {
+            @Override
+            public Event waitEvent(EventType event) {
+                return waitEventHandler.waitEvent(event);
+            }
+
+            @Override
+            public Event waitEvent() {
+                return waitEventHandler.waitEvent();
+            }
+
+            @Override
+            public Event waitEvent(EventType event, long timeout,
+                                   TimeUnit timeUnit) throws TimeoutException {
+                return waitEventHandler.waitEvent(event, timeout, timeUnit);
+            }
+
+            @Override
+            public Event waitEvent(long timeout,
+                                   TimeUnit timeUnit) throws TimeoutException {
+                return waitEventHandler.waitEvent(timeout, timeUnit);
+            }
+        };
+
+        return waiter;
+    }
+
+    private void process(Event event) {
+        EventType eventType = event.getEventType();
+        InternalEventHandler handler;
+        Set<Integer> handlerIds;
+
+        if (eventHandlersMap.containsKey(eventType)) {
+            handlerIds = eventHandlersMap.get(eventType);
+            for (Integer hid : handlerIds) {
+                handler = handlers.get(hid);
+                handler.handle(event);
+            }
+        }
+
+        if (eventHandlersMap.containsKey(BuiltInEventType.ALL)) {
+            handlerIds = eventHandlersMap.get(BuiltInEventType.ALL);
+            for (Integer hid : handlerIds) {
+                handler = handlers.get(hid);
+                handler.handle(event);
+            }
+        }
+    }
+
+    public void start() {
+        if (!started) {
+            for (InternalEventHandler handler : handlers.values()) {
+                handler.start();
+            }
+            started = true;
+        }
+    }
+
+    public void stop() {
+        if (started) {
+            for (InternalEventHandler handler : handlers.values()) {
+                handler.stop();
+            }
+            started = false;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/EventType.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/EventType.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventType.java
new file mode 100644
index 0000000..6a4a453
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventType.java
@@ -0,0 +1,24 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public interface EventType {
+    // no op
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/EventWaiter.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/EventWaiter.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventWaiter.java
new file mode 100644
index 0000000..dacc5eb
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/EventWaiter.java
@@ -0,0 +1,37 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public interface EventWaiter {
+
+    Event waitEvent(EventType event);
+
+    Event waitEvent();
+
+    Event waitEvent(EventType event, long timeout,
+                                    TimeUnit timeUnit) throws TimeoutException;
+
+    Event waitEvent(long timeout,
+                                    TimeUnit timeUnit) throws TimeoutException;
+
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/ExecutedEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/ExecutedEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/ExecutedEventHandler.java
new file mode 100644
index 0000000..d094711
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/ExecutedEventHandler.java
@@ -0,0 +1,76 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * An EventHandler wrapper processing events using an ExecutorService
+ */
+public class ExecutedEventHandler extends AbstractInternalEventHandler {
+
+    private ExecutorService executorService;
+
+    public ExecutedEventHandler(EventHandler handler) {
+        super(handler);
+    }
+
+    @Override
+    protected void doHandle(final Event event) throws Exception {
+        if (executorService.isTerminated()) {
+            return;
+        }
+
+        executorService.execute(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    process(event);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+    }
+
+    @Override
+    public void start() {
+        executorService = Executors.newFixedThreadPool(2);
+    }
+
+    @Override
+    public void stop() {
+        if (executorService.isShutdown()) {
+            return;
+        }
+        executorService.shutdownNow();
+    }
+
+    @Override
+    public boolean isStopped() {
+        return executorService.isShutdown();
+    }
+
+    @Override
+    public void init() {
+
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/InternalEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/InternalEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/InternalEventHandler.java
new file mode 100644
index 0000000..e7fafad
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/InternalEventHandler.java
@@ -0,0 +1,34 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+public interface InternalEventHandler extends EventHandler {
+
+    int id();
+
+    void init();
+
+    void start();
+
+    void stop();
+
+    boolean isStopped();
+}
+

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/LongRunningEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/LongRunningEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/LongRunningEventHandler.java
new file mode 100644
index 0000000..10c1f0b
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/LongRunningEventHandler.java
@@ -0,0 +1,77 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public abstract class LongRunningEventHandler extends BufferedEventHandler {
+
+    private ExecutorService executorService;
+
+    public LongRunningEventHandler(EventHandler handler) {
+        super(handler);
+    }
+
+    public LongRunningEventHandler() {
+        super();
+    }
+
+    protected abstract void loopOnce();
+
+    @Override
+    public void start() {
+        executorService = Executors.newFixedThreadPool(1);
+        executorService.execute(new Runnable() {
+            @Override
+            public void run() {
+                while (true) {
+
+                    processEvents();
+
+                    loopOnce();
+                }
+            }
+        });
+    }
+
+    @Override
+    public void stop() {
+        if (executorService.isShutdown()) {
+            return;
+        }
+        executorService.shutdownNow();
+    }
+
+    @Override
+    public boolean isStopped() {
+        return executorService.isShutdown();
+    }
+
+    protected void processEvents() {
+        while (! eventQueue.isEmpty()) {
+            try {
+                process(eventQueue.take());
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/event/WaitEventHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/event/WaitEventHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/event/WaitEventHandler.java
new file mode 100644
index 0000000..da9c561
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/event/WaitEventHandler.java
@@ -0,0 +1,126 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.event;
+
+import java.util.concurrent.*;
+
+public class WaitEventHandler extends BufferedEventHandler {
+
+    private ExecutorService executorService;
+
+    public WaitEventHandler(EventHandler handler) {
+        super(handler);
+    }
+
+    public Event waitEvent() {
+        return waitEvent(null);
+    }
+
+    public Event waitEvent(final EventType eventType) {
+        Future<Event> future = doWaitEvent(eventType);
+
+        try {
+            return future.get();
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Event waitEvent(final EventType eventType,
+                           long timeout, TimeUnit timeUnit) throws TimeoutException {
+        Future<Event> future = doWaitEvent(eventType);
+
+        try {
+            return future.get(timeout, timeUnit);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Event waitEvent(long timeout, TimeUnit timeUnit) throws TimeoutException {
+        Future<Event> future = doWaitEvent(null);
+
+        try {
+            return future.get(timeout, timeUnit);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private Future<Event> doWaitEvent(final EventType eventType) {
+        Future<Event> future = executorService.submit(new Callable<Event>() {
+            @Override
+            public Event call() throws Exception {
+                if (eventType != null) {
+                    return checkEvent(eventType);
+                } else {
+                    return checkEvent();
+                }
+            }
+        });
+
+        return future;
+    }
+
+    private Event checkEvent() throws Exception {
+        return eventQueue.take();
+    }
+
+    private Event checkEvent(EventType eventType) throws Exception {
+        while (true) {
+            if (eventQueue.size() == 1) {
+                if (eventQueue.peek().getEventType() == eventType) {
+                    return eventQueue.take();
+                }
+            } else {
+                Event event = eventQueue.take();
+                if (event.getEventType() == eventType) {
+                    return event;
+                } else {
+                    eventQueue.put(event); // put back since not wanted
+                }
+            }
+        }
+    }
+
+    @Override
+    public void start() {
+        executorService = Executors.newFixedThreadPool(1);
+    }
+
+    @Override
+    public void stop() {
+        if (executorService.isShutdown()) {
+            return;
+        }
+        executorService.shutdown();
+    }
+
+    @Override
+    public boolean isStopped() {
+        return executorService.isShutdown();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/transport/Acceptor.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/transport/Acceptor.java b/lib/kerby-event/src/main/java/org/apache/kerby/transport/Acceptor.java
new file mode 100644
index 0000000..efa827a
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/transport/Acceptor.java
@@ -0,0 +1,36 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.transport;
+
+import java.net.InetSocketAddress;
+
+public abstract class Acceptor extends TransportSelector {
+
+    public Acceptor(TransportHandler transportHandler) {
+        super(transportHandler);
+    }
+
+    public void listen(String address, int listenPort) {
+        InetSocketAddress socketAddress = new InetSocketAddress(address, listenPort);
+        doListen(socketAddress);
+    }
+
+    protected abstract void doListen(InetSocketAddress socketAddress);
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/transport/Connector.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/transport/Connector.java b/lib/kerby-event/src/main/java/org/apache/kerby/transport/Connector.java
new file mode 100644
index 0000000..720f481
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/transport/Connector.java
@@ -0,0 +1,36 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.transport;
+
+import java.net.InetSocketAddress;
+
+public abstract class Connector extends TransportSelector {
+
+    public Connector(TransportHandler transportHandler) {
+        super(transportHandler);
+    }
+
+    public void connect(String serverAddress, int serverPort) {
+        InetSocketAddress sa = new InetSocketAddress(serverAddress, serverPort);
+        doConnect(sa);
+    }
+
+    protected abstract void doConnect(InetSocketAddress sa);
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/f3645ba1/lib/kerby-event/src/main/java/org/apache/kerby/transport/MessageHandler.java
----------------------------------------------------------------------
diff --git a/lib/kerby-event/src/main/java/org/apache/kerby/transport/MessageHandler.java b/lib/kerby-event/src/main/java/org/apache/kerby/transport/MessageHandler.java
new file mode 100644
index 0000000..d6ad01e
--- /dev/null
+++ b/lib/kerby-event/src/main/java/org/apache/kerby/transport/MessageHandler.java
@@ -0,0 +1,42 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.kerby.transport;
+
+import org.apache.kerby.event.AbstractEventHandler;
+import org.apache.kerby.event.Event;
+import org.apache.kerby.event.EventType;
+import org.apache.kerby.transport.event.MessageEvent;
+import org.apache.kerby.transport.event.TransportEventType;
+
+public abstract class MessageHandler extends AbstractEventHandler {
+
+    @Override
+    protected void doHandle(Event event) throws Exception {
+        handleMessage((MessageEvent) event);
+    }
+
+    protected abstract void handleMessage(MessageEvent event) throws Exception;
+
+    @Override
+    public EventType[] getInterestedEvents() {
+        return new EventType[] { TransportEventType.INBOUND_MESSAGE };
+    }
+
+}


Mime
View raw message