KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.alfresco.config.ConfigException;
25 import org.alfresco.config.ConfigSource;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Base class for ConfigSource implementations, provides support for parsing
31  * comma separated sources and iterating around them
32  *
33  * @author gavinc
34  */

35 public abstract class BaseConfigSource implements ConfigSource
36 {
37     private static final Log logger = LogFactory.getLog(BaseConfigSource.class);
38
39     private List JavaDoc<String JavaDoc> sourceStrings;
40
41     /**
42      * @param sourceStrings
43      * a list of implementation-specific sources. The meaning of the
44      * source is particular to the implementation, eg. for a file config
45      * source they would be file names.
46      */

47     protected BaseConfigSource(List JavaDoc<String JavaDoc> sourceStrings)
48     {
49         this.sourceStrings = new ArrayList JavaDoc<String JavaDoc>();
50         for (String JavaDoc sourceString : sourceStrings)
51         {
52             if (sourceString == null || sourceString.trim().length() == 0)
53             {
54                 throw new ConfigException("Invalid source value: " + sourceString);
55             }
56             addSourceString(sourceString);
57         }
58         // check that we have some kind of source
59
if (sourceStrings.size() == 0)
60         {
61             throw new ConfigException("No sources provided: " + sourceStrings);
62         }
63     }
64     
65     /**
66      * Conditionally adds the source to the set of source strings if its
67      * trimmed length is greater than 0.
68      */

69     private void addSourceString(String JavaDoc sourceString)
70     {
71         sourceString = sourceString.trim();
72         if (sourceString.length() > 0)
73         {
74             sourceStrings.add(sourceString);
75         }
76     }
77     
78     /**
79      * Converts all the sources given in the constructor into a list of
80      * input streams.
81      *
82      * @see #getInputStream(String)
83      */

84     public final Iterator JavaDoc<InputStream JavaDoc> iterator()
85     {
86         // build a list of input streams
87
List JavaDoc<InputStream JavaDoc> inputStreams = new ArrayList JavaDoc<InputStream JavaDoc>(sourceStrings.size());
88         for (String JavaDoc sourceString : sourceStrings)
89         {
90             if (logger.isDebugEnabled())
91             {
92                 logger.debug("Retrieving input stream for source: " + sourceString);
93             }
94             
95             InputStream JavaDoc is = getInputStream(sourceString);
96             if (is != null)
97             {
98                inputStreams.add(is);
99             }
100         }
101         // done
102
return inputStreams.iterator();
103     }
104
105     /**
106      * Retrieves an InputStream to the source represented by the given
107      * source location. The meaning of the source location will depend
108      * on the implementation.
109      *
110      * @param sourceString the source location
111      * @return Returns an InputStream to the named source location
112      */

113     protected abstract InputStream JavaDoc getInputStream(String JavaDoc sourceString);
114 }
115
Popular Tags