Project

Profile

Help

Revision 29c3f254

Added by Michael Kay over 11 years ago

Fix bug 3043783 Saxon fails to initialize in some class loading environments

View differences:

latest9.2/hej/net/sf/saxon/Configuration.java
33 33
import javax.xml.transform.stream.StreamSource;
34 34
import java.io.InputStream;
35 35
import java.io.Serializable;
36
import java.io.IOException;
36 37
import java.util.*;
37 38
import java.util.Collection;
39
import java.net.URL;
38 40

  
39 41

  
40 42
/**
......
228 230
        try {
229 231
            String sConfigFile = "edition.properties";
230 232

  
231
            InputStream in = Configuration.class.getClassLoader().getResourceAsStream(sConfigFile);
232
            if (in == null) {
233
                throw new RuntimeException("Cannot read edition.properties file");
234
            }
235
            Properties props = new Properties();
236
            props.load(in);
237

  
238
            String platformClassName = props.getProperty("platform");
239
            if (platformClassName == null) {
240
                platformClassName = "net.sf.saxon.java.JavaPlatform";
241
            }
242 233
            ClassLoader loader = null;
243 234
            try {
244 235
                loader = Thread.currentThread().getContextClassLoader();
245 236
            } catch (Exception err) {
246 237
                System.err.println("Failed to getContextClassLoader() - continuing");
247 238
            }
239

  
240
            if (loader == null) {
241
                loader = Configuration.class.getClassLoader();
242
            }
243

  
244
            InputStream in = null;
245

  
246
            if (loader != null) {
247
                in = loader.getResourceAsStream(sConfigFile);
248
                if (in == null) {
249
                    System.err.println("Cannot read edition.properties file located using ClassLoader " +
250
                            loader + ": using defaults");
251
                }
252
            } else {
253
                // Means we're in a very strange class-loading environment, things are getting desparate
254
                URL url = ClassLoader.getSystemResource(sConfigFile);
255
                if (url != null) {
256
                    try {
257
                        in = url.openStream();
258
                    } catch (IOException ioe) {
259
                        System.err.println(
260
                                "IO error " + ioe.getMessage() +
261
                                " reading edition.properties located using getSystemResource(): using defaults");
262
                        in = null;
263
                    }
264
                }
265
            }
266

  
267
            String platformClassName;
268
            String configClassName;
269

  
270
            if (in == null) {
271
                platformClassName = "net.sf.saxon.java.JavaPlatform";
272
                configClassName = "net.sf.saxon.Configuration";
273
            } else {
274
                Properties props = new Properties();
275
                props.load(in);
276
                platformClassName = props.getProperty("platform");
277
                if (platformClassName == null) {
278
                    platformClassName = "net.sf.saxon.java.JavaPlatform";
279
                }
280
                configClassName = props.getProperty("config");
281
                if (configClassName == null) {
282
                    configClassName = "net.sf.saxon.Configuration";
283
                }
284
            }
285

  
248 286
            Class platformClass;
249 287
            if (loader != null) {
250 288
                try {
......
257 295
            }
258 296
            platform = (Platform)platformClass.newInstance();
259 297

  
260
            String configClassName = props.getProperty("config");
261
            if (configClassName == null) {
262
                configClassName = "net.sf.saxon.Configuration";
263
            }
264 298
            Class configClass;
265 299
            if (loader != null) {
266 300
                try {
......
276 310
            e.printStackTrace();
277 311
            throw new RuntimeException("Failed to load configuration defined in edition.properties", e);
278 312
        }
279
    }    
280

  
313
    }
314
    
281 315
    /**
282 316
     * Factory method to construct a Configuration object by reading a configuration file.
283 317
     * Requires Saxon-PE or Saxon-EE

Also available in: Unified diff