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.chat; 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; 025import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory; 026import org.apache.mina.filter.codec.ProtocolCodecFilter; 027import org.apache.mina.filter.codec.textline.TextLineCodecFactory; 028import org.apache.mina.filter.logging.LoggingFilter; 029import org.apache.mina.filter.logging.MdcInjectionFilter; 030import org.apache.mina.filter.ssl.SslFilter; 031import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 032 033/** 034 * (<b>Entry point</b>) Chat server 035 * 036 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 037 */ 038public class Main { 039 /** Choose your favorite port number. */ 040 private static final int PORT = 1234; 041 042 /** Set this to true if you want to make the server SSL */ 043 private static final boolean USE_SSL = false; 044 045 public static void main(String[] args) throws Exception { 046 NioSocketAcceptor acceptor = new NioSocketAcceptor(); 047 DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); 048 049 MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter(); 050 chain.addLast("mdc", mdcInjectionFilter); 051 052 // Add SSL filter if SSL is enabled. 053 if (USE_SSL) { 054 addSSLSupport(chain); 055 } 056 057 chain.addLast("codec", new ProtocolCodecFilter( 058 new TextLineCodecFactory())); 059 060 addLogger(chain); 061 062 // Bind 063 acceptor.setHandler(new ChatProtocolHandler()); 064 acceptor.bind(new InetSocketAddress(PORT)); 065 066 System.out.println("Listening on port " + PORT); 067 } 068 069 private static void addSSLSupport(DefaultIoFilterChainBuilder chain) 070 throws Exception { 071 SslFilter sslFilter = new SslFilter(BogusSslContextFactory 072 .getInstance(true)); 073 chain.addLast("sslFilter", sslFilter); 074 System.out.println("SSL ON"); 075 } 076 077 private static void addLogger(DefaultIoFilterChainBuilder chain) 078 throws Exception { 079 chain.addLast("logger", new LoggingFilter()); 080 System.out.println("Logging ON"); 081 } 082}