Source code: org/media/mn8/conf/NamingConf.java
1 /*
2 * $COPYRIGHT$
3 * $Id: NamingConf.java,v 1.16 2002/09/16 23:06:30 borzy Exp $
4 *
5 * Date Author Changes
6 * May 04 2001 Remus Pereni Created
7 */
8 package org.media.mn8.conf;
9
10
11 import javax.xml.parsers.SAXParserFactory;
12 import javax.xml.parsers.SAXParser;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.DocumentBuilder;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17 import org.w3c.dom.Node;
18 import org.xml.sax.SAXParseException;
19 import java.util.Hashtable;
20 import java.util.Vector;
21 import java.util.Properties;
22 import java.util.Enumeration;
23 import java.io.InputStream;
24 import java.io.File;
25 import org.media.mn8.LogManager;
26 import org.media.mn8.util.SimpleExpression;
27 import org.media.mn8.util.SystemEnvReader;
28 import org.media.mn8.util.StreamResolver;
29 import org.media.mn8.concepts.StringConcept;
30 import java.net.URL;
31
32 /**
33 * Class who is responsible for loading and managing the
34 * configuration information stored in /conf/naning.mn8.xml
35 * @author <a href="mailto:remus@nolimits.ro">Remus Pereni</a>
36 * @version $Revision: 1.16 $ $Date: 2002/09/16 23:06:30 $
37 */
38
39 public class NamingConf {
40 /**
41 * Where will be stored in our JNDI Namespace the output stream
42 */
43 public static final String _SYSTEM_OUT_URI = "env://system/out";
44
45 /**
46 * Where will be stored in our JNDI Namespace the error stream
47 */
48 public static final String _SYSTEM_ERR_URI = "env://system/err";
49
50 /**
51 * Where will be stored in our JNDI Namespace the input stream
52 */
53 public static final String _SYSTEM_IN_URI = "env://system/err";
54
55 private static final String _NAMING_CONFIG_FILE = "/conf/mn8config.xml";
56 private static NamingHandlerBase _namingHandler = new NamingHandlerBase();
57 private static NamingConf _namingConf = new NamingConf();
58 private static Hashtable _envLoad = null;
59
60
61 /**
62 * Dafault constructor
63 */
64 private NamingConf () {
65 InputStream streamNaminXML = null;
66 SAXParserFactory factory = SAXParserFactory.newInstance();
67 URL resURL;
68
69 try {
70 Class _thisClass = Class.forName("org.media.mn8.conf.NamingConf");
71 resURL = _thisClass.getResource( _NAMING_CONFIG_FILE );
72 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
73 domFactory.setValidating( false );
74 DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
75
76 if ( resURL != null ) {
77 factory.setValidating(true);
78 SAXParser saxParser = factory.newSAXParser();
79 saxParser.parse (resURL.toString(), _namingHandler);
80
81 Document document = domBuilder.parse( resURL.toString());
82 Node node = document.getDocumentElement().getFirstChild();
83 while( node != null ) {
84 if( node.getNodeType() == Node.ELEMENT_NODE ) {
85 if( ((Element) node).getTagName().equalsIgnoreCase("concepts"))
86 ConceptsConf.getRef().initialize( (Element) node );
87 else if ( ((Element)node).getTagName().equalsIgnoreCase("java.properties") )
88 JavaPropertiesConf.getRef().initialize( (Element)node );
89 else if ( ((Element)node).getTagName().equalsIgnoreCase("context") ) {
90 ContextConf.getRef().initialize( ( Element)node );
91 }
92 }
93 node = node.getNextSibling();
94 }
95 } else
96 throw new RuntimeException();
97 } catch (ClassNotFoundException cex) {
98 LogManager.message( "warning",
99 "conf.file_missing",
100 _NAMING_CONFIG_FILE );
101 throw new RuntimeException();
102 } catch (SAXParseException saxex) {
103 LogManager.message( "warning",
104 "conf.parse_exception",
105 _NAMING_CONFIG_FILE +
106 "\n" + saxex.getMessage() );
107 saxex.printStackTrace();
108 throw new RuntimeException();
109 } catch (RuntimeException rex) {
110 LogManager.message( "warning",
111 "conf.file_missing",
112 _NAMING_CONFIG_FILE );
113 throw new RuntimeException();
114 } catch (Exception ex) {
115 ex.printStackTrace();
116 }
117
118 }
119
120
121 public static void cleanAll() {
122 _envLoad.clear();
123 }
124
125
126 /**
127 * Method for getting the NamingConf instance. Don't forger that it
128 * implements a Singleton pattern.
129 * @return The default instance of NamingConf
130 */
131 public static NamingConf getReference () {
132 return _namingConf;
133 }
134
135
136 /**
137 * Returns an hashtable loaded with all the data contained in
138 * the /conf/naming.mn8.xml file in the initial.context section.<br/>
139 * This hashtable should be enough to create an InitialContext.
140 * @return An hashtable containing the properties needed to
141 * successfully create an JNDI InitialContext.
142 */
143 public Hashtable getInitialContextProperties () {
144 return _namingHandler.getInitialContextProperties();
145 }
146
147
148 /**
149 * Returns an hashtable containing all the java system variables as
150 * the operating system environment variables, selected using the
151 * patterns from /conf/naming.mn8.xml the <load name="pattern*" /> and
152 * <except name="patter*" /> elements.
153 * @return A hastable containing the os and java environment variables.
154 */
155 public Hashtable getEnvProperties() {
156 Vector loadPatterns = _namingHandler.getEnvLoad();
157 Vector exceptPatterns = _namingHandler.getEnvLoadExcept();
158 Properties sysProp = System.getProperties();
159
160 _envLoad = new Hashtable();
161
162 if ( loadPatterns == null )
163 loadPatterns = new Vector();
164
165 if ( loadPatterns.size() < 1 ) // we load all the variables
166 loadPatterns.add("*");
167
168 if ( exceptPatterns == null )
169 exceptPatterns = new Vector();
170
171 _envLoad.put("env://system/out", new StringConcept( "System.out" ) );
172 _envLoad.put("env://system/err", new StringConcept( "System.err") );
173 _envLoad.put("env://system/in", new StringConcept( "System.in") );
174
175 // java system properties
176 applyEnvLoadConditions (sysProp,
177 loadPatterns,
178 exceptPatterns);
179
180 // operating system properties
181 applyEnvLoadConditions(getSystemEnvironment(),
182 loadPatterns,
183 exceptPatterns);
184
185 return _envLoad;
186 }
187
188
189 /**
190 * It is possible that mn8 will not use it's MemoryContext but rather it
191 * will use some external one. It is possible to map the memory context
192 * in any context desired in the external one providing a mapping which will
193 * be applied.
194 * @return The mapping into the external JNDI context of the env:// namespace.
195 */
196 public String getEnvMapping() {
197 return _namingHandler.getEnvMapping();
198 }
199
200
201 /**
202 * It is possible that mn8 will not use it's MemoryContext but rather it
203 * will use some external one. It is possible to map the memory context
204 * in any context desired in the external one providing a mapping which will
205 * be applied.
206 * @return The mapping into the external JNDI context of the mem:// namespace.
207 */
208 public String getMemMapping() {
209 return _namingHandler.getMemMapping();
210 }
211
212
213 private void applyEnvLoadConditions(Properties from,
214 Vector loadPatterns,
215 Vector exceptPatterns) {
216 Enumeration propEnum = from.propertyNames();
217 String propKey;
218 boolean isExcept;
219
220 while ( propEnum.hasMoreElements() ) {
221 propKey = (String) propEnum.nextElement();
222 isExcept = false;
223
224 for ( int cnt = 0 ; cnt < loadPatterns.size() && !isExcept; cnt++ ) {
225 if ( SimpleExpression.match ( propKey, (String)loadPatterns.elementAt(cnt) )) {
226 for (int i = 0; i < exceptPatterns.size() && !isExcept; i++) {
227 if ( SimpleExpression.match ( propKey, (String)exceptPatterns.elementAt(i) ) )
228 isExcept = true;
229 }
230 }
231 }
232
233 if ( !isExcept ) _envLoad.put( "env://system/properties/" + propKey,
234 new StringConcept( (String)from.getProperty(propKey) ) );
235 }
236 }
237
238
239 private Properties getSystemEnvironment () {
240 Hashtable osCmdEnvMap = _namingHandler.getEnvCommands();
241 String currentOs = System.getProperty("os.name");
242 String cmd="";
243 Enumeration keys;
244 Properties result = null;
245 String tmp_str;
246
247 if ( osCmdEnvMap == null ||
248 osCmdEnvMap.isEmpty() ) return null;
249
250 if ( osCmdEnvMap.containsKey(currentOs.toLowerCase()) ) {
251 cmd = (String)osCmdEnvMap.get(currentOs.toLowerCase() );
252 }else {
253 /* we try to match it */
254 keys = osCmdEnvMap.keys();
255 while ( keys.hasMoreElements() ){
256 tmp_str = (String) keys.nextElement();
257 if ( currentOs.toLowerCase().indexOf( tmp_str ) != -1 )
258 cmd = (String)osCmdEnvMap.get( tmp_str );
259 }
260 }
261
262 try {
263 result = SystemEnvReader.getSystemEnvironment(cmd);
264 } catch (Exception ex) {
265 ex.printStackTrace();
266 }
267 return result;
268 }
269 }