1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.integration.spring;
21
22 import org.apache.mina.common.ExecutorThreadModel;
23 import org.springframework.beans.factory.FactoryBean;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.util.Assert;
26
27 import java.util.concurrent.Executor;
28
29 /**
30 * Spring {@link FactoryBean} which makes it possible to set up a MINA
31 * {@link ExecutorThreadModel} using Spring. The <code>serviceName</code>
32 * property must be set using {@link #setServiceName(String)}.
33 *
34 * @author The Apache Directory Project (mina-dev@directory.apache.org)
35 * @version $Rev$, $Date$
36 */
37 public class ExecutorThreadModelFactoryBean implements FactoryBean,
38 InitializingBean {
39 private String serviceName = null;
40
41 private Executor executor = null;
42
43 /**
44 * Sets the {@link Executor} to use. If not set a default {@link Executor}
45 * will be used by the {@link ExecutorThreadModel} created by this
46 * factory bean.
47 *
48 * @param executor the executor.
49 * @throws IllegalArgumentException if the specified value is
50 * <code>null</code>.
51 */
52 public void setExecutor(Executor executor) {
53 Assert.notNull(executor, "Property 'executor' may not be null");
54 this.executor = executor;
55 }
56
57 /**
58 * Sets the name of the service as used in the call to
59 * {@link ExecutorThreadModel#getInstance(String)}. This property is
60 * required.
61 *
62 * @param executor the executor.
63 * @throws IllegalArgumentException if the specified value is
64 * <code>null</code>.
65 */
66 public void setServiceName(String serviceName) {
67 Assert.notNull(serviceName, "Property 'serviceName' may not be null");
68 this.serviceName = serviceName;
69 }
70
71 public Class getObjectType() {
72 return ExecutorThreadModel.class;
73 }
74
75 public Object getObject() throws Exception {
76 ExecutorThreadModel model = ExecutorThreadModel
77 .getInstance(serviceName);
78 if (executor != null) {
79 model.setExecutor(executor);
80 }
81 return model;
82 }
83
84 public boolean isSingleton() {
85 return true;
86 }
87
88 public void afterPropertiesSet() throws Exception {
89 Assert.notNull(serviceName, "Property 'serviceName' may not be null");
90 }
91
92 }