KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > ClientFactoryBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.client;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.naming.InitialContext JavaDoc;
22
23 import org.apache.servicemix.jbi.container.JBIContainer;
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26
27 /**
28  * A factory bean which creates a ServiceMixClient.
29  * It first try to use the configured ClientFactory, the ComponentContext
30  * then JBIContainer and at last, it tries to locate the ClientFactory
31  * in JNDI under the provided name.
32  *
33  * @author <a HREF="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
34  * @org.apache.xbean.XBean element="client"
35  */

36 public class ClientFactoryBean implements FactoryBean, InitializingBean {
37
38     private String JavaDoc name = ClientFactory.DEFAULT_JNDI_NAME;
39     private JBIContainer container;
40     private ClientFactory factory;
41     private ComponentContext context;
42     
43     public ClientFactoryBean() {
44     }
45
46     /**
47      * @return the context
48      */

49     public ComponentContext getContext() {
50         return context;
51     }
52
53     /**
54      * @param context the context to set
55      */

56     public void setContext(ComponentContext context) {
57         this.context = context;
58     }
59
60     /**
61      * @return the container
62      */

63     public JBIContainer getContainer() {
64         return container;
65     }
66
67     /**
68      * @param container the container to set
69      */

70     public void setContainer(JBIContainer container) {
71         this.container = container;
72     }
73
74     /**
75      * @return the factory
76      */

77     public ClientFactory getFactory() {
78         return factory;
79     }
80
81     /**
82      * @param factory the factory to set
83      */

84     public void setFactory(ClientFactory factory) {
85         this.factory = factory;
86     }
87
88     /**
89      * @return the name
90      */

91     public String JavaDoc getName() {
92         return name;
93     }
94
95     /**
96      * @param name the name to set
97      */

98     public void setName(String JavaDoc name) {
99         this.name = name;
100     }
101
102     public Object JavaDoc getObject() throws Exception JavaDoc {
103         return factory.createClient();
104     }
105
106     public Class JavaDoc getObjectType() {
107         return ServiceMixClient.class;
108     }
109
110     public boolean isSingleton() {
111         return false;
112     }
113     
114     public void afterPropertiesSet() throws Exception JavaDoc {
115         if (factory == null) {
116             if (context != null) {
117                 factory = new ClientFactory() {
118                     public ServiceMixClient createClient() throws JBIException {
119                         return new ServiceMixClientFacade(context);
120                     }
121                 };
122             } else if (container != null) {
123                 factory = container.getClientFactory();
124             } else {
125                 factory = (ClientFactory) new InitialContext JavaDoc().lookup(name);
126             }
127         }
128     }
129     
130 }
131
Popular Tags