KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > discovery > DiscoveryAgentFactory


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.transport.discovery;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22
23 import org.apache.activemq.util.FactoryFinder;
24 import org.apache.activemq.util.IOExceptionSupport;
25
26 import java.util.concurrent.ConcurrentHashMap JavaDoc;
27
28 public abstract class DiscoveryAgentFactory {
29
30     static final private FactoryFinder discoveryAgentFinder = new FactoryFinder("META-INF/services/org/apache/activemq/transport/discoveryagent/");
31     static final private ConcurrentHashMap JavaDoc discoveryAgentFactorys = new ConcurrentHashMap JavaDoc();
32
33     /**
34      * @param uri
35      * @return
36      * @throws IOException
37      */

38     private static DiscoveryAgentFactory findDiscoveryAgentFactory(URI JavaDoc uri) throws IOException JavaDoc {
39         String JavaDoc scheme = uri.getScheme();
40         if( scheme == null )
41             throw new IOException JavaDoc("DiscoveryAgent scheme not specified: [" + uri + "]");
42         DiscoveryAgentFactory daf = (DiscoveryAgentFactory) discoveryAgentFactorys.get(scheme);
43         if (daf == null) {
44             // Try to load if from a META-INF property.
45
try {
46                 daf = (DiscoveryAgentFactory) discoveryAgentFinder.newInstance(scheme);
47                 discoveryAgentFactorys.put(scheme, daf);
48             }
49             catch (Throwable JavaDoc e) {
50                 throw IOExceptionSupport.create("DiscoveryAgent scheme NOT recognized: [" + scheme + "]", e);
51             }
52         }
53         return daf;
54     }
55     
56     public static DiscoveryAgent createDiscoveryAgent(URI JavaDoc uri) throws IOException JavaDoc {
57         DiscoveryAgentFactory tf = findDiscoveryAgentFactory(uri);
58         return tf.doCreateDiscoveryAgent(uri);
59
60     }
61
62     abstract protected DiscoveryAgent doCreateDiscoveryAgent(URI JavaDoc uri) throws IOException JavaDoc;
63 // {
64
// try {
65
// String type = ( uri.getScheme() == null ) ? uri.getPath() : uri.getScheme();
66
// DiscoveryAgent rc = (DiscoveryAgent) discoveryAgentFinder.newInstance(type);
67
// Map options = URISupport.parseParamters(uri);
68
// IntrospectionSupport.setProperties(rc, options);
69
// if( rc.getClass() == SimpleDiscoveryAgent.class ) {
70
// CompositeData data = URISupport.parseComposite(uri);
71
// ((SimpleDiscoveryAgent)rc).setServices(data.getComponents());
72
// }
73
// return rc;
74
// } catch (Throwable e) {
75
// throw IOExceptionSupport.create("Could not create discovery agent: "+uri, e);
76
// }
77
// }
78
}
79
Popular Tags