View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.util;
22  
23  import java.io.InputStream;
24  import java.net.URL;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  /***
30   * TODO Add docs
31   * Need this Class so runtime would not depend on JAXB.
32   * 
33   * Created on 04.12.2004
34   *
35   * Contributing Author(s):
36   *
37   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
38   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
39   *
40   * @author vlads
41   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/09 21:35:18 $
42   */
43  
44  public class ConfigFileUtil extends FileUtil {
45  
46      publicong> static Object unmarshalConfigFile(String fileName, String packageName) {
47      	try {
48      		InputStream is = ConfigFileUtil.loadConfig(fileName);
49      		if (is == null) {
50      			return null;
51      		}
52      		JAXBContext jc = JAXBContext.newInstance(packageName);
53      		javax.xml.bind.Unmarshaller unmarshaller;
54      		unmarshaller = jc.createUnmarshaller();
55      		unmarshaller.setValidating(false);
56      
57      		return unmarshaller.unmarshal(is);
58      
59      	} catch (JAXBException jaxbe) {
60      		FileUtil.log.error("Configuration error", jaxbe);
61      		return null;
62      	}
63      }
64  
65      public static InputStream loadConfig(String fileName) {
66          URL location = FileUtil.getFile(fileName);
67          if (location != null) {
68              try {
69                  FileUtil.log.info("Using config file " + location);
70                  return location.openStream(); 
71              } catch (Exception e) {
72                  FileUtil.log.error("Error reading " + fileName, e);
73                  return null;
74              }
75          } else {
76              FileUtil.log.error("Config file not found: " + fileName);
77              return null;
78          }
79      }
80  }