KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > util > ORComponentManager


1 /*
2  * Copyright (C) 2004 Erik Swenson - eswenson@opensourcesoft.net
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.util;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import com.opensymphony.xwork.ActionContext;
26 import com.opensymphony.xwork.interceptor.component.*;
27
28 import org.apache.log4j.Logger;
29 import org.xml.sax.SAXException JavaDoc;
30
31 public class ORComponentManager
32 {
33     protected static Logger log = Logger.getLogger(ORComponentManager.class);
34
35     /*
36      * creates a new ComponentManager for objects that do not share an
37      * ActionContext with the OpenReports web application such as the
38      * ScheduleReportJob and the SampleDataGenerator
39      */

40     public static void initializeObject(Object JavaDoc object)
41     {
42         ComponentManager container = (ComponentManager) ActionContext.getContext().get(
43                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
44
45         if (container == null)
46         {
47             log.info("Creating ComponentManager for: " + object.getClass().getName());
48
49             container = new DefaultComponentManager();
50
51             ComponentConfiguration config = loadConfiguration();
52             config.configure(container, "application");
53
54             ActionContext.getContext().put(
55                     "com.opensymphony.xwork.interceptor.component.ComponentManager", container);
56         }
57
58         container.initializeObject(object);
59     }
60
61     public static ComponentConfiguration loadConfiguration()
62     {
63         ComponentConfiguration config = new ComponentConfiguration();
64
65         InputStream JavaDoc configXml = Thread.currentThread().getContextClassLoader()
66                 .getResourceAsStream("components.xml");
67
68         if (configXml == null)
69         {
70             final String JavaDoc message = "Unable to find the file components.xml in the classpath.";
71             log.error(message);
72             throw new RuntimeException JavaDoc(message);
73         }
74
75         try
76         {
77             config.loadFromXml(configXml);
78         }
79         catch (IOException JavaDoc ioe)
80         {
81             log.error(ioe);
82             throw new RuntimeException JavaDoc("Unable to load component configuration");
83         }
84         catch (SAXException JavaDoc sae)
85         {
86             log.error(sae);
87             throw new RuntimeException JavaDoc("Unable to load component configuration");
88         }
89
90         return config;
91     }
92
93 }
Popular Tags