KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > configuration > ConfigurationManagerImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.container.configuration;
6
7 import java.io.InputStream JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.util.*;
10 import javax.servlet.ServletContext JavaDoc;
11
12 /**
13  * Jul 19, 2004
14  *
15  * @author: Tuan Nguyen
16  * @email: tuan08@users.sourceforge.net
17  * @version: $Id: ConfigurationServiceImpl.java,v 1.8 2004/10/30 02:29:51 tuan08
18  * Exp $
19  */

20 public class ConfigurationManagerImpl implements ConfigurationManager {
21   final static public String JavaDoc WAR_CONF_LOCATION = "/WEB-INF";
22
23   protected Configuration configurations_;
24
25   private ServletContext JavaDoc scontext_;
26
27   public ConfigurationManagerImpl(ServletContext JavaDoc context) {
28     scontext_ = context;
29   }
30
31   public void addConfiguration(String JavaDoc url) throws Exception JavaDoc {
32     addConfiguration(getURL(url));
33   }
34
35   public void addConfiguration(Collection urls) throws Exception JavaDoc {
36     Iterator i = urls.iterator();
37     while (i.hasNext()) {
38       URL JavaDoc url = (URL JavaDoc) i.next();
39       addConfiguration(url);
40     }
41   }
42
43   public void addConfiguration(URL JavaDoc url) throws Exception JavaDoc {
44     Configuration conf = XMLParser.parse(url.openStream());
45     if (configurations_ == null)
46       configurations_ = conf;
47     else
48       configurations_.mergeConfiguration(conf);
49     List urls = conf.getImportConfiguration();
50     for (int i = 0; i < urls.size(); i++) {
51       String JavaDoc uri = (String JavaDoc) urls.get(i);
52       try {
53         InputStream JavaDoc is = getURL(uri).openStream();
54         conf = XMLParser.parse(is);
55         configurations_.mergeConfiguration(conf);
56       } catch (Exception JavaDoc ex) {
57         System.err.println("Error: " + ex.getMessage());
58         System.err.println("Error: cannot process the configuration" + url);
59       }
60     }
61   }
62
63   public void processRemoveConfiguration() {
64     if(configurations_ == null) return;
65     List list = configurations_.getRemoveConfiguration();
66     for (int i = 0; i < list.size(); i++) {
67       String JavaDoc type = (String JavaDoc) list.get(i);
68       configurations_.removeServiceConfiguration(type);
69     }
70   }
71
72   public ValuesParam getGlobalInitParam(String JavaDoc name) throws Exception JavaDoc {
73     return configurations_.getInitParam(name);
74   }
75
76   public ServiceConfiguration getServiceConfiguration(String JavaDoc service)
77       throws Exception JavaDoc {
78     return configurations_.getServiceConfiguration(service);
79   }
80
81   public ServiceConfiguration getServiceConfiguration(Class JavaDoc clazz)
82       throws Exception JavaDoc {
83     return getServiceConfiguration(clazz.getName());
84   }
85
86   public Collection getServiceConfigurations() {
87     if(configurations_ == null) return null;
88     return configurations_.getServiceConfigurations();
89   }
90
91   public Collection getGroovyServiceConfigurations() {
92     return configurations_.getGroovyServiceConfigurations();
93   }
94
95   public URL JavaDoc getResource(String JavaDoc url, String JavaDoc defaultURL) throws Exception JavaDoc {
96     return null;
97   }
98
99   public URL JavaDoc getResource(String JavaDoc uri) throws Exception JavaDoc {
100     return getURL(uri);
101   }
102
103   public InputStream JavaDoc getInputStream(String JavaDoc url, String JavaDoc defaultURL)
104       throws Exception JavaDoc {
105     return null;
106   }
107
108   public InputStream JavaDoc getInputStream(String JavaDoc uri) throws Exception JavaDoc {
109     URL JavaDoc url = getURL(uri);
110     return url.openStream();
111   }
112
113   protected URL JavaDoc getURL(String JavaDoc url) throws Exception JavaDoc {
114     if (url.startsWith("jar:")) {
115       String JavaDoc path = removePrefix("jar:/", url);
116       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
117       return cl.getResource(path);
118     } else if (url.startsWith("war:")) {
119       if (scontext_ == null) {
120         throw new Exception JavaDoc("unsupport war uri in this configuration service");
121       }
122       String JavaDoc path = removePrefix("war:", url);
123       return scontext_.getResource(WAR_CONF_LOCATION + path);
124     } else if (url.startsWith("file:")) {
125       url = resolveSystemProperties(url);
126       return new URL JavaDoc(url);
127     }
128     return null;
129   }
130
131   private String JavaDoc resolveSystemProperties(String JavaDoc input) {
132     final int NORMAL = 0;
133     final int SEEN_DOLLAR = 1;
134     final int IN_BRACKET = 2;
135     if (input == null)
136       return input;
137     char[] chars = input.toCharArray();
138     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
139     boolean properties = false;
140     int state = NORMAL;
141     int start = 0;
142     for (int i = 0; i < chars.length; ++i) {
143       char c = chars[i];
144       if (c == '$' && state != IN_BRACKET)
145         state = SEEN_DOLLAR;
146       else if (c == '{' && state == SEEN_DOLLAR) {
147         buffer.append(input.substring(start, i - 1));
148         state = IN_BRACKET;
149         start = i - 1;
150       }
151       else if (state == SEEN_DOLLAR)
152         state = NORMAL;
153       else if (c == '}' && state == IN_BRACKET) {
154         if (start + 2 == i) {
155           buffer.append("${}");
156         }
157         else {
158           String JavaDoc value = null;
159           String JavaDoc key = input.substring(start + 2, i);
160           value = System.getProperty(key);
161           if (value != null) {
162             properties = true;
163             buffer.append(value);
164           }
165         }
166         start = i + 1;
167         state = NORMAL;
168       }
169     }
170     if (properties == false)
171       return input;
172     if (start != chars.length)
173       buffer.append(input.substring(start, chars.length));
174     return buffer.toString();
175
176   }
177
178   public boolean isDefault(String JavaDoc value) {
179      return value == null || value.length() == 0 || "default".equals(value) ;
180   }
181   
182   protected String JavaDoc removePrefix(String JavaDoc prefix, String JavaDoc url) {
183     return url.substring(prefix.length(), url.length());
184   }
185 }
Popular Tags