<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--
  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.
-->

<beans>

  <!-- This makes it possible to specify java.net.SocketAddress values 
    (e.g. :1234 below) as Strings.
    They will be converted into java.net.InetSocketAddress objects by Spring.  -->
  <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
      <map>
        <entry key="java.net.SocketAddress">
          <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
        </entry>
      </map>
    </property>
  </bean>

  <!-- The IoHandler implementation -->
  <bean id="chatHandler" class="org.apache.mina.example.chat.ChatProtocolHandler" />

  <!-- The SSL configuration -->
  <bean id="keystoreFactory" class="org.apache.mina.filter.ssl.KeyStoreFactory">
    <property name="password" value="boguspw"/>
    <property name="dataUrl" value="classpath:org/apache/mina/example/echoserver/ssl/bogus.cert"/>
  </bean>

  <bean id="keyStore" factory-bean="keystoreFactory" factory-method="newInstance"/>

  <bean id="bogusTrustManagerFactory" class="org.apache.mina.filter.ssl.BogusTrustManagerFactory"/>
  
  <!-- SSLContext to be used -->
  <bean id="sslContextFactory"  class="org.apache.mina.filter.ssl.SslContextFactory">
    <property name="protocol" value="TLS"/>
    <property name="keyManagerFactoryAlgorithm" value="SunX509"/>
    <property name="keyManagerFactoryKeyStore"><ref local="keyStore"/></property>
    <property name="keyManagerFactoryKeyStorePassword" value="boguspw"/>
    <property name="trustManagerFactory"><ref local="bogusTrustManagerFactory"/></property>
  </bean>

  <bean id="sslContext" factory-bean="sslContextFactory" factory-method="newInstance"/>

  <!-- the IoFilters -->

  <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter"/>

  <bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
      <constructor-arg value="remoteAddress"/>
  </bean>

  <bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
    <constructor-arg>
      <bean class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />
    </constructor-arg>
  </bean>

  <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />

  <bean id="sslFilter" class="org.apache.mina.filter.ssl.SslFilter">
    <constructor-arg ref="sslContext"/>
  </bean>

  <!-- The non-SSL filter chain. -->
  <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
    <property name="filters">
      <map>
        <entry key="executor" value-ref="executorFilter"/>
        <entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter"/>
        <entry key="codecFilter" value-ref="codecFilter"/>
        <entry key="loggingFilter" value-ref="loggingFilter"/>
      </map>
    </property>
  </bean>

  <!-- The SSL filter chain. -->
  <bean id="sslFilterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
    <property name="filters">
      <map>
        <entry key="executor" value-ref="executorFilter"/>
        <entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter"/>
        <entry key="sslFilter" value-ref="sslFilter"/>
        <entry key="codecFilter" value-ref="codecFilter"/>
        <entry key="loggingFilter" value-ref="loggingFilter"/>
      </map>
    </property>
  </bean>

  <!-- The IoAcceptor which binds to port 1234 -->
  <bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
    <property name="defaultLocalAddress" value=":1234" />
    <property name="handler" ref="chatHandler" />
    <property name="reuseAddress" value="true" />
    <property name="filterChainBuilder" ref="filterChainBuilder" />
  </bean>
  
  <!-- The SSL enabled IoAcceptor which binds to port 1235 -->
  <bean id="ioAcceptorWithSSL" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" destroy-method="unbind">
    <property name="defaultLocalAddress" value=":1235" />
    <property name="handler" ref="chatHandler" />
    <property name="reuseAddress" value="true" />
    <property name="filterChainBuilder" ref="sslFilterChainBuilder" />
  </bean>

  <!-- JMX stuff. Creates an MBeanServer. -->
  <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible" value="true"/>
  </bean>

  <!-- JMX stuff. Creates an MBeanServer. -->
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
      <map>
        <entry key="chatServer:name=ioHandler">
          <ref local="chatHandler"/>
        </entry>
      </map>
    </property>
    <property name="assembler">
      <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
        <property name="managedMethods" value="isChatUser,getNumberOfUsers,kick"/>
      </bean>
    </property>
    <property name="server">
      <ref local="mbeanServer"/>
    </property>
  </bean>

</beans>
