001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.sumup;
021
022import java.net.InetSocketAddress;
023
024import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;
025import org.apache.mina.filter.codec.ProtocolCodecFilter;
026import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
027import org.apache.mina.filter.logging.LoggingFilter;
028import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
029
030/**
031 * (<strong>Entry Point</strong>) Starts SumUp server.
032 *
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class Server {
036    private static final int SERVER_PORT = 8080;
037
038    // Set this to false to use object serialization instead of custom codec.
039    private static final boolean USE_CUSTOM_CODEC = true;
040
041    public static void main(String[] args) throws Throwable {
042        NioSocketAcceptor acceptor = new NioSocketAcceptor();
043
044        // Prepare the service configuration.
045        if (USE_CUSTOM_CODEC) {
046            acceptor.getFilterChain()
047                    .addLast(
048                            "codec",
049                            new ProtocolCodecFilter(
050                                    new SumUpProtocolCodecFactory(true)));
051        } else {
052            acceptor.getFilterChain().addLast(
053                    "codec",
054                    new ProtocolCodecFilter(
055                            new ObjectSerializationCodecFactory()));
056        }
057        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
058
059        acceptor.setHandler(new ServerSessionHandler());
060        acceptor.bind(new InetSocketAddress(SERVER_PORT));
061
062        System.out.println("Listening on port " + SERVER_PORT);
063    }
064}