KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > spi > ClassLoaderSPIConnectionFactory


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.tool.spi;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.jms.ConnectionFactory JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.io.File JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31
32 public abstract class ClassLoaderSPIConnectionFactory implements SPIConnectionFactory {
33     private static final Log log = LogFactory.getLog(ClassLoaderSPIConnectionFactory.class);
34
35     public static final String JavaDoc KEY_EXT_DIR = "extDir";
36
37     public final ConnectionFactory JavaDoc createConnectionFactory(Properties JavaDoc settings) throws Exception JavaDoc {
38
39         // Load new context class loader
40
ClassLoader JavaDoc newClassLoader = getContextClassLoader(settings);
41         Thread.currentThread().setContextClassLoader(newClassLoader);
42
43         return instantiateConnectionFactory(settings);
44     }
45
46     protected ClassLoader JavaDoc getContextClassLoader(Properties JavaDoc settings) {
47         String JavaDoc extDir = (String JavaDoc)settings.remove(KEY_EXT_DIR);
48         if (extDir != null) {
49             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(extDir, ";,");
50             List JavaDoc urls = new ArrayList JavaDoc();
51             while (tokens.hasMoreTokens()) {
52                 String JavaDoc dir = tokens.nextToken();
53                 try {
54                     File JavaDoc f = new File JavaDoc(dir);
55                     if (!f.exists()) {
56                         log.warn("Cannot find extension dir: " + f.getAbsolutePath());
57                     } else {
58                         log.info("Adding extension dir: " + f.getAbsolutePath());
59
60                         urls.add(f.toURL());
61
62                         File JavaDoc[] files = f.listFiles();
63                         if( files!=null ) {
64                             for (int j = 0; j < files.length; j++) {
65                                 if( files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar") ) {
66                                     log.info("Adding extension dir: " + files[j].getAbsolutePath());
67                                     urls.add(files[j].toURL());
68                                 }
69                             }
70                         }
71                     }
72                 } catch (Exception JavaDoc e) {
73                     log.warn("Failed to load ext dir: " + dir + ". Reason: " + e);
74                 }
75             }
76
77             URL JavaDoc u[] = new URL JavaDoc[urls.size()];
78             urls.toArray(u);
79             return new URLClassLoader JavaDoc(u, Thread.currentThread().getContextClassLoader());
80         }
81         return ClassLoaderSPIConnectionFactory.class.getClassLoader();
82     }
83
84     protected abstract ConnectionFactory JavaDoc instantiateConnectionFactory(Properties JavaDoc settings) throws Exception JavaDoc;
85 }
86
Popular Tags