KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > xbean > XBeanBrokerFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.xbean;
19
20 import org.apache.activemq.broker.BrokerFactoryHandler;
21 import org.apache.activemq.broker.BrokerService;
22 import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
23 import org.apache.xbean.spring.context.impl.URIEditor;
24 import org.springframework.beans.BeansException;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.core.io.ClassPathResource;
27 import org.springframework.core.io.FileSystemResource;
28 import org.springframework.core.io.Resource;
29 import org.springframework.core.io.UrlResource;
30 import org.springframework.util.ResourceUtils;
31
32 import java.beans.PropertyEditorManager JavaDoc;
33 import java.io.File JavaDoc;
34 import java.net.URI JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36
37 /**
38  * @version $Revision$
39  */

40 public class XBeanBrokerFactory implements BrokerFactoryHandler {
41
42     static {
43         PropertyEditorManager.registerEditor(URI JavaDoc.class, URIEditor.class);
44     }
45
46     public BrokerService createBroker(URI JavaDoc config) throws Exception JavaDoc {
47
48         String JavaDoc uri = config.getSchemeSpecificPart();
49         ApplicationContext context = createApplicationContext(uri);
50
51         BrokerService broker = null;
52         try {
53             broker = (BrokerService) context.getBean("broker");
54         }
55         catch (BeansException e) {
56         }
57
58         if (broker == null) {
59             // lets try find by type
60
String JavaDoc[] names = context.getBeanNamesForType(BrokerService.class);
61             for (int i = 0; i < names.length; i++) {
62                 String JavaDoc name = names[i];
63                 broker = (BrokerService) context.getBean(name);
64                 if (broker != null) {
65                     break;
66                 }
67             }
68         }
69         if (broker == null) {
70             throw new IllegalArgumentException JavaDoc("The configuration has no BrokerService instance for resource: " + config);
71         }
72         
73         // TODO warning resources from the context may not be closed down!
74

75         return broker;
76     }
77
78     protected ApplicationContext createApplicationContext(String JavaDoc uri) throws MalformedURLException JavaDoc {
79         System.out.println("Now attempting to figure out the type of resource: " + uri);
80         Resource resource;
81         File JavaDoc file = new File JavaDoc(uri);
82         if (file.exists()) {
83             resource = new FileSystemResource(uri);
84         }
85         else if (ResourceUtils.isUrl(uri)) {
86             resource = new UrlResource(uri);
87         }
88         else {
89             resource = new ClassPathResource(uri);
90         }
91         return new ResourceXmlApplicationContext(resource);
92     }
93 }
94
Popular Tags