KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > config > source > UrlConfigSource


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.config.source;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.config.ConfigException;
24
25 /**
26  * ConfigSource that looks for a prefix to determine where to look for the config.</br>
27  * Valid prefixes are:
28  * <ul>
29  * <li><b>file:</b> the location provided is a path to a physical file</li>
30  * <li><b>classpath:</b> the location provided is a resource on the classpath</li>
31  * <li><b>http:</b> the location provided is a HTTP address</li>
32  * </ul>
33  * The default, if none of the above is detected, is <b>classpath</b>. An example of
34  * a URL is <code>file:/home/root/settings/config.xml</code>.
35  *
36  * @author Derek Hulley
37  */

38 public class UrlConfigSource extends BaseConfigSource
39 {
40     public static final String JavaDoc PREFIX_FILE = "file:";
41     public static final String JavaDoc PREFIX_HTTP = "http:";
42     public static final String JavaDoc PREFIX_CLASSPATH = "classpath:";
43     
44     /**
45      * Constructs a config location that figures out where to look for the config
46      *
47      * @param sourceLocation
48      * the location from which to get config
49      *
50      * @see ClassPathConfigSource#ClassPathConfigSource(List<String>)
51      */

52     public UrlConfigSource(String JavaDoc sourceLocation)
53     {
54         this(Collections.singletonList(sourceLocation));
55     }
56
57     /**
58      * Constructs a config location that figures out where to look for the config
59      *
60      * @param source
61      * List of locations from which to get the config
62      */

63     public UrlConfigSource(List JavaDoc<String JavaDoc> sourceLocations)
64     {
65         super(sourceLocations);
66     }
67
68     public InputStream JavaDoc getInputStream(String JavaDoc sourceUrl)
69     {
70         // determine the config source
71
BaseConfigSource configSource = null;
72         String JavaDoc sourceString = null;
73         if (sourceUrl.startsWith(PREFIX_FILE))
74         {
75             sourceString = sourceUrl.substring(5);
76             configSource = new FileConfigSource(sourceString);
77         }
78         else if (sourceUrl.startsWith(PREFIX_HTTP))
79         {
80             sourceString = sourceUrl;
81             configSource = new HTTPConfigSource(sourceString);
82         }
83         else if (sourceUrl.startsWith(PREFIX_CLASSPATH))
84         {
85             sourceString = sourceUrl.substring(10);
86             configSource = new ClassPathConfigSource(sourceString);
87         }
88         else if (sourceUrl.indexOf(':') > -1)
89         {
90             throw new ConfigException("Config source cannot be determined: " + sourceString);
91         }
92         else
93         {
94             sourceString = sourceUrl;
95             configSource = new ClassPathConfigSource(sourceString);
96         }
97         
98         return configSource.getInputStream(sourceString);
99     }
100 }
101
Popular Tags