KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > SetupServiceImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.impl;
10
11 import org.jboss.system.ServiceMBeanSupport;
12 import org.jboss.portal.setup.dl.DataLoaderConfig;
13 import org.jboss.portal.setup.dl.DataLoader;
14 import org.jboss.portal.setup.sl.SchemaLoaderConfig;
15 import org.jboss.portal.setup.sl.SchemaLoader;
16 import org.jboss.portal.setup.PortalSetupException;
17 import org.jboss.portal.setup.SetupService;
18 import org.jboss.portal.common.util.XML;
19 import org.apache.log4j.Logger;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
34  * Date: Apr 29, 2005; Time: 9:49:10 PM
35  * @jmx.mbean
36  * @jboss.xmbean
37  * @since JBoss portal 2.0
38  * Interface org.jboss.portal.setup.impl.SetupServiceImpl
39  */

40 public class SetupServiceImpl extends ServiceMBeanSupport implements SetupService
41 {
42    protected String JavaDoc DEFAULT_DATA_URI = null;
43    protected final Logger m_log;
44    private boolean m_performLoad = true;
45    private Element JavaDoc m_dataFileLocation = null;
46
47    private DataLoaderConfig m_dataLoaderConfig = null;
48    private SchemaLoaderConfig m_schemaLoaderConfig = null;
49
50    protected SchemaLoader m_schemaLoader = null;
51    protected DataLoader m_dataLoader = null;
52    protected List JavaDoc m_dataUriList = new ArrayList JavaDoc();
53
54
55    public SetupServiceImpl()
56    {
57       super();
58       m_log = Logger.getLogger(getClass().getName());
59    }
60
61    /**
62     * @jmx.managed-attribute access="read-only"
63     */

64    public SetupService getSetupService()
65    {
66       return this;
67    }
68
69
70    /**
71     * @jmx.managed-attribute access="read-only"
72     */

73    public int getState()
74    {
75       return super.getState();
76    }
77
78    /**
79     * @jmx.managed-attribute access="read-only"
80     */

81    public String JavaDoc getStateString()
82    {
83       return super.getStateString();
84    }
85
86    /**
87     * @jmx.managed-attribute access="read-write"
88     */

89    public void setDataFileLocation(Element JavaDoc dataFileLocation)
90    {
91       m_dataFileLocation = dataFileLocation;
92    }
93
94    /**
95     * @jmx.managed-attribute access="read-write"
96     */

97    public Element JavaDoc getDataFileLocation()
98    {
99       return m_dataFileLocation;
100    }
101
102
103    /**
104     * @jmx.managed-attribute
105     */

106    public void setDataLoaderConfig(DataLoaderConfig dataLoaderConfig)
107    {
108       m_dataLoaderConfig = (DataLoaderConfig)dataLoaderConfig.getConfiguration();
109    }
110
111    /**
112     * @jmx.managed-attribute
113     */

114    public void setSchemaLoaderConfig(SchemaLoaderConfig schemaLoaderConfig)
115    {
116       m_schemaLoaderConfig = (SchemaLoaderConfig)schemaLoaderConfig.getConfiguration();
117    }
118
119    /**
120     * @jmx.managed-operation
121     */

122    public void create() throws Exception JavaDoc
123    {
124       super.create();
125    }
126
127    /**
128     * @jmx.managed-operation
129     */

130    public void start() throws Exception JavaDoc
131    {
132       super.start();
133    }
134
135    /**
136     * @jmx.managed-operation
137     */

138    public void stop()
139    {
140       super.stop();
141    }
142
143    /**
144     * @jmx.managed-operation
145     */

146    public void destroy()
147    {
148       super.destroy();
149    }
150
151
152    public boolean getPerformLoad()
153    {
154       return m_performLoad;
155    }
156
157    public void setPerformLoad(boolean performLoad)
158    {
159       m_performLoad = performLoad;
160    }
161
162
163    public DataLoaderConfig getDataLoaderConfig()
164    {
165       return m_dataLoaderConfig;
166    }
167
168
169    public SchemaLoaderConfig getSchemaLoaderConfig()
170    {
171       return m_schemaLoaderConfig;
172    }
173
174
175    public void initializeLoaders() throws PortalSetupException
176    {
177
178    }
179
180
181    public void load() throws PortalSetupException
182    {
183
184    }
185
186
187    public void clearLoaders() throws PortalSetupException
188    {
189       if (null != m_dataLoader)
190       {
191          m_dataLoader.destroy();
192       }
193       if (null != m_schemaLoader)
194       {
195          m_schemaLoader.destroy();
196       }
197    }
198
199    /**
200     * Closes all resources ( jdbc, streams and etc and destroys the service
201     *
202     * @see ServiceMBeanSupport#destroyService()
203     */

204    protected void destroyService() throws Exception JavaDoc
205    {
206       if (m_log.isInfoEnabled())
207       {
208          m_log.info("Processing request to destroy service for: " + getClass().getName());
209       }
210       clearLoaders();
211       super.destroyService();
212
213       if (m_log.isInfoEnabled())
214       {
215          m_log.info("Finished process request to destroy service for: " + getClass().getName());
216       }
217    }
218
219    protected void stopService() throws Exception JavaDoc
220    {
221       if (m_log.isInfoEnabled())
222       {
223          m_log.info("Processing request to stop service for: " + getClass().getName());
224       }
225       super.stopService();
226       clearLoaders();
227       if (m_log.isInfoEnabled())
228       {
229          m_log.info("Finished process request to stop service for: " + getClass().getName());
230       }
231
232    }
233
234    /**
235     * Initializes data loader and schema loader; performs dbloader schema load according to the rule specified in schema
236     * loader configuration; Verifies dbloader schema; Loads data file; Performs dbloader data initialization if required.
237     *
238     * @see ServiceMBeanSupport#startService()
239     */

240    protected void createService() throws Exception JavaDoc
241    {
242
243       if (m_log.isInfoEnabled())
244       {
245          m_log.info("Processing request to create service for: " + getClass().getName());
246       }
247       super.createService();
248       if (m_log.isInfoEnabled())
249       {
250          m_log.info("Finished process request to create service for: " + getClass().getName());
251       }
252    }
253
254
255    protected void startService() throws Exception JavaDoc
256    {
257       if (m_log.isInfoEnabled())
258       {
259          m_log.info("Processing request to start service for: " + getClass().getName());
260       }
261       if (m_performLoad)
262       {
263          initializeLoaders();
264          load();
265       }
266
267
268       super.startService();
269       if (m_log.isInfoEnabled())
270       {
271          m_log.info("Finished process request to start service for: " + getClass().getName());
272       }
273    }
274
275    protected void parseDataLoacation() throws PortalSetupException
276    {
277
278       Element JavaDoc files = getDataFileLocation();
279       if (null == files)
280       {
281
282          setDefaultDataUri();
283       }
284       else
285       {
286          List JavaDoc fileElements = XML.getChildren(files, "data-uri");
287          if (null == fileElements)
288          {
289             setDefaultDataUri();
290          }
291          else
292          {
293             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
294
295             for (int i = 0, size = fileElements.size(); i < size; i++)
296             {
297                Element JavaDoc fileElmt = (Element JavaDoc)fileElements.get(i);
298                String JavaDoc fileUri = XML.asString(fileElmt);
299
300                if (null == cl.getResource(fileUri))
301                {
302                   String JavaDoc msg = "Failed to load resource: " + fileUri;
303                   m_log.error(msg);
304                   throw new PortalSetupException(msg);
305                }
306                else
307                {
308                   m_dataUriList.add(fileUri);
309                }
310             }
311          }
312       }
313    }
314
315    protected void loadData() throws PortalSetupException
316    {
317       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
318       DocumentBuilder JavaDoc db = null;
319
320       try
321       {
322          db = XML.getDocumentBuilderFactory().newDocumentBuilder();
323       }
324       catch (ParserConfigurationException JavaDoc pce)
325       {
326          String JavaDoc msg = "Failed to create a document builder!";
327          m_log.error(msg, pce);
328          throw new PortalSetupException(msg, pce);
329       }
330       Iterator JavaDoc iter = m_dataUriList.iterator();
331       while (iter.hasNext())
332       {
333          String JavaDoc uri = (String JavaDoc)iter.next();
334          InputStream JavaDoc is = cl.getResourceAsStream(uri);
335          if (null == is)
336          {
337             String JavaDoc msg = "Failed to load resource: " + uri;
338             m_log.error(msg);
339             throw new PortalSetupException(msg);
340          }
341          Document JavaDoc doc = null;
342          try
343          {
344             doc = db.parse(is);
345          }
346          catch (SAXException JavaDoc se)
347          {
348             throw new PortalSetupException(se);
349          }
350          catch (IOException JavaDoc ioe)
351          {
352             throw new PortalSetupException(ioe);
353          }
354
355          Element JavaDoc dataLoadElmt = doc.getDocumentElement();
356          List JavaDoc actions = XML.getChildren(dataLoadElmt, "action");
357          for (int j = 0; j < actions.size(); j++)
358          {
359             Element JavaDoc action = (Element JavaDoc)actions.get(j);
360             {
361                m_dataLoader.loadData(action);
362             }
363          }
364
365       }
366    }
367
368    protected void setDefaultDataUri() throws PortalSetupException
369    {
370       if (m_log.isInfoEnabled())
371       {
372          m_log.info("No data load file uri have been scpecified; Trying to use default URI + " + DEFAULT_DATA_URI + "'.");
373       }
374       if (null == DEFAULT_DATA_URI)
375       {
376          throw new PortalSetupException("DefaultUri is undefoned!");
377       }
378       m_dataUriList.add(DEFAULT_DATA_URI);
379    }
380
381
382 }
383
Popular Tags