1 20 package org.apache.mina.integration.spring; 21 22 import java.net.SocketAddress ; 23 24 import org.apache.mina.common.IoAcceptor; 25 import org.apache.mina.common.IoHandler; 26 import org.apache.mina.common.IoServiceConfig; 27 import org.springframework.beans.factory.DisposableBean; 28 import org.springframework.beans.factory.FactoryBean; 29 import org.springframework.beans.factory.InitializingBean; 30 import org.springframework.util.Assert; 31 32 108 public class IoAcceptorFactoryBean implements FactoryBean, InitializingBean, 109 DisposableBean { 110 private Binding[] bindings = new Binding[0]; 111 112 private IoAcceptor target; 113 114 119 public void setTarget(IoAcceptor target) { 120 this.target = target; 121 } 122 123 134 public void setBindings(Binding[] bindings) { 135 Assert.notNull(bindings, "Property 'bindings' may not be null"); 136 this.bindings = bindings; 137 } 138 139 public Object getObject() throws Exception { 140 return target; 141 } 142 143 public Class getObjectType() { 144 return IoAcceptor.class; 145 } 146 147 public boolean isSingleton() { 148 return true; 149 } 150 151 public void afterPropertiesSet() throws Exception { 152 Assert.notNull(target, "Property 'target' may not be null"); 153 154 157 for (int i = 0; i < bindings.length; i++) { 158 Binding b = bindings[i]; 159 if (b.getServiceConfig() != null) { 160 target.bind(b.getAddress(), b.getHandler(), b 161 .getServiceConfig()); 162 } else { 163 target.bind(b.getAddress(), b.getHandler()); 164 } 165 } 166 } 167 168 public void destroy() throws Exception { 169 for (int i = 0; i < bindings.length; i++) { 170 Binding b = bindings[i]; 171 try { 172 target.unbind(b.getAddress()); 173 } catch (Exception ignored) { 174 } 175 } 176 } 177 } 178 | Popular Tags |