KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.web.context.ServletContextAware;
31
32 /**
33  * ConfigSource implementation that gets its data via files in a web
34  * application.
35  *
36  * TODO: Also deal with the source being specified as an init param i.e.
37  * param:config.files
38  *
39  * @author gavinc
40  */

41 public class WebAppConfigSource extends BaseConfigSource implements ServletContextAware
42 {
43     private static Log logger = LogFactory.getLog(FileConfigSource.class);
44     private ServletContext JavaDoc servletCtx;
45
46     /**
47      * Constructs a webapp configuration source that uses a single file
48      *
49      * @param filename the name of the file from which to get config
50      *
51      * @see WebAppConfigSource#WebAppConfigSource(List<String>)
52      */

53     public WebAppConfigSource(String JavaDoc filename)
54     {
55         this(Collections.singletonList(filename));
56     }
57     
58     /**
59      * @param sources
60      * List of paths to files in a web application
61      */

62     public WebAppConfigSource(List JavaDoc<String JavaDoc> sourceStrings)
63     {
64         super(sourceStrings);
65     }
66
67     /**
68      * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
69      */

70     public void setServletContext(ServletContext JavaDoc servletContext)
71     {
72         this.servletCtx = servletContext;
73     }
74
75     /**
76      * @see org.alfresco.config.source.BaseConfigSource#getInputStream(java.lang.String)
77      */

78     public InputStream JavaDoc getInputStream(String JavaDoc sourceString)
79     {
80         InputStream JavaDoc is = null;
81
82         try
83         {
84             String JavaDoc fullPath = this.servletCtx.getRealPath(sourceString);
85             is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fullPath));
86         }
87         catch (IOException JavaDoc ioe)
88         {
89             if (logger.isDebugEnabled())
90             {
91                 logger.debug("Failed to obtain input stream to file: " + sourceString, ioe);
92             }
93         }
94
95         return is;
96     }
97 }
98
Popular Tags