KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > config > ConfigTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.framework.config;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24
25 import sync4j.framework.config.ConfigurationException;
26 import sync4j.framework.tools.beans.BeanFactory;
27
28 import org.apache.commons.lang.StringUtils;
29
30 /**
31  * Utility class for configuration server beans handling
32  *
33  * @author Stefano Fornari @ Funambol
34  * @version $Id: ConfigTools.java,v 1.4 2005/03/02 20:57:37 harrie Exp $
35  */

36 public class ConfigTools {
37     /**
38      * It is supposed that the value of the parameter is a bean name. It returns
39      * that bean, created with <i>BeanFactory.getBeanInstance()</i>.
40      *
41      * @param configLoader class loader to be used to load classes and
42      * configuration objects
43      * @param name the name of the bean
44      *
45      * @return an instance of the class specified by the parameter
46      *
47      * @throws ConfigurationException in case of errors
48      */

49     public static Object JavaDoc getBeanInstance(ClassLoader JavaDoc configLoader, String JavaDoc name)
50     throws ConfigurationException {
51        try {
52            return BeanFactory.getBeanInstance(configLoader, name);
53        } catch (Exception JavaDoc e) {
54            throw new ConfigurationException( "Error in creating an instance of "
55                                            + name
56                                            , e );
57        }
58     }
59
60     /**
61      * Create and return an array of bean as specified by a list of bean names.
62      *
63      * @param configLoader class loader to be used to load classes and
64      * configuration objects
65      * @param names the name of the anme
66      * @param separator the separator character
67      *
68      */

69     public static Object JavaDoc[] getBeanArray(ClassLoader JavaDoc configLoader, String JavaDoc names, char separator)
70     throws ConfigurationException {
71         String JavaDoc[] values = StringUtils.split(names, String.valueOf(separator));
72
73         Object JavaDoc[] ret = new Object JavaDoc[values.length];
74
75         int i = 0;
76         try {
77             for (; i<ret.length; ++i) {
78                 ret[i] = BeanFactory.getBeanInstance(configLoader, values[i]);
79             }
80         } catch (Exception JavaDoc e) {
81             throw new ConfigurationException ( "Error in creating the bean "
82                                              + values[i]
83                                              , e );
84         }
85
86         return ret;
87     }
88     
89     /**
90      * Checks if the given string is an accepted URL (starting with http://,
91      * https:// or file://). If yes, a new URL object representing the given
92      * url is returned; otherwise, the given string is considered a file name
93      * and a new URL is obtained calling File.toURL().
94      *
95      * @param s the string to check
96      *
97      * @return the corresponding URL if the string represents a URL or the
98      * fixed URL if the string is a pathname/filename
99      *
100      * @throws MalformedURLException
101      */

102     public static URL JavaDoc fixURL(final String JavaDoc s)
103     throws MalformedURLException JavaDoc {
104
105         try {
106             return new URL JavaDoc(s);
107         } catch (MalformedURLException JavaDoc e) {
108             //
109
// This is not a URL, let's consider it just a file
110
//
111
}
112
113         try {
114             return new File JavaDoc(new File JavaDoc(s).getCanonicalPath()).toURL();
115         } catch (IOException JavaDoc e) {
116             throw new MalformedURLException JavaDoc("Unable to convert" + s + " to a URL");
117         }
118     }
119
120 }
121
Popular Tags