Author: edeoliveira
Date: Wed Jul 23 13:01:03 2008
New Revision: 679178

URL: http://svn.apache.org/viewvc?rev=679178&view=rev
Log:
DIRMINA-394 Backport of haiku example to 1.0 branch

Added:
    mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
   (with props)
    
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
   (with props)

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,59 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.util.Arrays;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class Haiku {
+    private final String[] phrases;
+
+    public Haiku(String[] lines) {
+        this.phrases = lines;
+        if (null == lines || lines.length != 3) {
+            throw new IllegalArgumentException("Must pass in 3 phrases of 
text");
+        }
+    }
+
+    public String[] getPhrases() {
+        return phrases;
+    }
+
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+
+        Haiku haiku = (Haiku) o;
+
+        return Arrays.equals(phrases, haiku.phrases);
+    }
+
+    public int hashCode() {
+        return Arrays.hashCode(phrases);
+    }
+
+    public String toString() {
+        return Arrays.toString(phrases);
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,57 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.net.InetSocketAddress;
+import java.nio.charset.Charset;
+
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
+import org.apache.mina.filter.executor.ExecutorFilter;
+import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
+import edu.emory.mathcs.backport.java.util.concurrent.Executors;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+
+public class HaikuValidationServer {
+    public static void main(String[] args) throws Exception {
+        ExecutorService executor = Executors.newCachedThreadPool();
+        SocketAcceptor acceptor = new SocketAcceptor(Runtime.getRuntime()
+                .availableProcessors(), executor);
+
+        SocketAcceptorConfig config = new SocketAcceptorConfig();
+
+        config.getFilterChain().addLast("executor",
+                new ExecutorFilter(executor));
+        config.getFilterChain().addLast(
+                "to-string",
+                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
+                        .forName("US-ASCII"))));
+        config.getFilterChain().addLast("to-haiki", new ToHaikuIoFilter());
+
+        acceptor.bind(new InetSocketAddress(42458),
+                new HaikuValidatorIoHandler(), config);
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class HaikuValidator {
+    private static final int[] SYLLABLE_COUNTS = { 5, 7, 5 };
+
+    public void validate(Haiku haiku) throws InvalidHaikuException {
+        String[] phrases = haiku.getPhrases();
+
+        for (int i = 0; i < phrases.length; i++) {
+            String phrase = phrases[i];
+            int count = PhraseUtilities.countSyllablesInPhrase(phrase);
+
+            if (count != SYLLABLE_COUNTS[i]) {
+                throw new InvalidHaikuException(i + 1, phrase, count,
+                        SYLLABLE_COUNTS[i]);
+            }
+        }
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,44 @@
+/*
+ * 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.mina.example.haiku;
+
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+
+public class HaikuValidatorIoHandler extends IoHandlerAdapter {
+
+    private final HaikuValidator validator = new HaikuValidator();
+
+    public void messageReceived(IoSession session, Object message)
+            throws Exception {
+        Haiku haiku = (Haiku) message;
+
+        try {
+            validator.validate(haiku);
+            session.write("HAIKU!");
+        } catch (InvalidHaikuException e) {
+            session.write("NOT A HAIKU: " + e.getMessage());
+        }
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,62 @@
+/*
+ * 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.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class InvalidHaikuException extends Exception {
+    private static final long serialVersionUID = 34877739006797894L;
+
+    private final int position;
+
+    private final String phrase;
+
+    private final int syllableCount;
+
+    private final int expectedSyllableCount;
+
+    public InvalidHaikuException(int position, String phrase,
+            int syllableCount, int expectedSyllableCount) {
+        super("phrase " + position + ", '" + phrase + "' had " + syllableCount
+                + " syllables, not " + expectedSyllableCount);
+
+        this.position = position;
+        this.phrase = phrase;
+        this.syllableCount = syllableCount;
+        this.expectedSyllableCount = expectedSyllableCount;
+    }
+
+    public int getExpectedSyllableCount() {
+        return expectedSyllableCount;
+    }
+
+    public String getPhrase() {
+        return phrase;
+    }
+
+    public int getSyllableCount() {
+        return syllableCount;
+    }
+
+    public int getPhrasePosition() {
+        return position;
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,66 @@
+package org.apache.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class PhraseUtilities {
+    static int countSyllablesInPhrase(String phrase) {
+        int syllables = 0;
+
+        String[] words = phrase.split("[^\\w-]+");
+        for (int i = 0, max = words.length; i < max; i++) {
+            String word = words[i];
+            if (word.length() > 0) {
+                syllables += countSyllablesInWord(word.toLowerCase());
+            }
+        }
+
+        return syllables;
+    }
+
+    static int countSyllablesInWord(String word) {
+        char[] chars = word.toCharArray();
+        int syllables = 0;
+        boolean lastWasVowel = false;
+
+        for (int i = 0; i < chars.length; i++) {
+            char c = chars[i];
+            if (isVowel(c)) {
+                if (!lastWasVowel
+                        || (i > 0 && isE(chars, i - 1) && isO(chars, i))) {
+                    ++syllables;
+                    lastWasVowel = true;
+                }
+            } else {
+                lastWasVowel = false;
+            }
+        }
+
+        if (word.endsWith("oned") || word.endsWith("ne")
+                || word.endsWith("ide") || word.endsWith("ve")
+                || word.endsWith("fe") || word.endsWith("nes")
+                || word.endsWith("mes")) {
+            --syllables;
+        }
+
+        return syllables;
+    }
+
+    static boolean isE(char[] chars, int position) {
+        return isCharacter(chars, position, 'e');
+    }
+
+    static boolean isCharacter(char[] chars, int position, char c) {
+        return chars[position] == c;
+    }
+
+    static boolean isO(char[] chars, int position) {
+        return isCharacter(chars, position, 'o');
+    }
+
+    static boolean isVowel(char c) {
+        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
+                || c == 'y';
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java?rev=679178&view=auto
==============================================================================
--- 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
 (added)
+++ 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
 Wed Jul 23 13:01:03 2008
@@ -0,0 +1,51 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.mina.common.IoFilterAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class ToHaikuIoFilter extends IoFilterAdapter {
+
+    public void messageReceived(NextFilter nextFilter, IoSession session,
+            Object message) throws Exception {
+        List phrases = (List) session.getAttribute("phrases");
+
+        if (null == phrases) {
+            phrases = new ArrayList();
+            session.setAttribute("phrases", phrases);
+        }
+
+        phrases.add((String) message);
+
+        if (phrases.size() == 3) {
+            session.removeAttribute("phrases");
+
+            super.messageReceived(nextFilter, session, new Haiku(
+                    (String[]) phrases.toArray(new String[3])));
+        }
+    }
+}

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/branches/1.0/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to