[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard DashboardLoader.java,NONE,1.1
Brought to you by:
ghinkl
From: Greg H. <gh...@us...> - 2004-04-07 03:17:23
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17037/src/org/mc4j/console/dashboard Added Files: DashboardLoader.java Log Message: A new static-singleton to do the heavy lifting of building the dashboard object. --- NEW FILE: DashboardLoader.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.dashboard; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.openide.ErrorManager; import org.mc4j.console.dashboard.match.BeanCondition; import org.mc4j.console.dashboard.match.BeanMatch; import org.mc4j.console.dashboard.match.DashboardMatch; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * This static singleton is primarily here to load dashboard xml files into their * non-running object representations [Dashboard]. Once parsed and constructed * these object representations can be matched against either beans or globally * to provide cases of a dashboard that can actually be loaded. * * @author Greg Hinkle (gh...@us...), Apr 4, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/07 03:04:25 $) */ public class DashboardLoader { private static DashboardLoader instance; private DashboardLoader() { } public static DashboardLoader getInstance() { if (instance == null) { instance = new DashboardLoader(); } return instance; } /** * Construct the non-running dashboard object that represents a single dashboard file * that can then later be instantiated and shown to the user. * * @param dashboardFile * @return */ public Dashboard buildDashboard(File dashboardFile) { Dashboard dashboard = null; try { Document document = buildDocument(new FileInputStream(dashboardFile)); Element rootElement = document.getDocumentElement(); dashboard = new Dashboard( rootElement.getAttribute("name"), document); //dashboard.setNameFilter(rootElement.getAttribute("nameFilter")); dashboard.setDescription(rootElement.getElementsByTagName("Description").item(0).getNodeValue()); //dashboard.setIsMultiBean(Boolean.valueOf(rootElement.getAttribute("isMultiBean")).booleanValue()); //dashboard.setGlobal(Boolean.valueOf(rootElement.getAttribute("isGlobal")).booleanValue()); assignDashboardMatch(dashboard, rootElement); } catch(SAXException se) { ErrorManager.getDefault().notify(se); } catch(FileNotFoundException fnfe) { ErrorManager.getDefault().notify(fnfe); } catch(IOException ioe) { ErrorManager.getDefault().notify(ioe); } return dashboard; } /** * Build and assign an object representation of the match section of a * dashboard. This section is used to determine where and if a dashboard will be * an option as well as what beans will be viewable in that dashboard. * * @param dashboard * @param rootElement */ private void assignDashboardMatch(Dashboard dashboard, Element rootElement) { Element dashboardMatchElement = (Element) rootElement.getElementsByTagName("DashboardMatch").item(0); DashboardMatch dashboardMatch = new DashboardMatch(); String type = dashboardMatchElement.getAttribute("type"); if (type != null) dashboardMatch.setType(type); String location = dashboardMatchElement.getAttribute("location"); if (location != null) dashboardMatch.setLocation(location); assignBeanMatchList(dashboardMatch, dashboardMatchElement); dashboard.setDashboardMatch(dashboardMatch); } private void assignBeanMatchList(DashboardMatch dashboardMatch, Element dashboardMatchElement) { NodeList beanMatches = dashboardMatchElement.getElementsByTagName("BeanMatch"); for (int i = 0; i < beanMatches.getLength(); i++) { Element beanMatchElement = (Element) beanMatches.item(i); assignBeanMatch(dashboardMatch, beanMatchElement); } } private void assignBeanMatch(DashboardMatch dashboardMatch, Element beanMatchElement) { BeanMatch beanMatch = new BeanMatch(); beanMatch.setId(beanMatchElement.getAttribute("id")); beanMatch.setType(beanMatchElement.getAttribute("type")); NodeList nodes = beanMatchElement.getElementsByTagName("Condition"); for (int i = 0; i < nodes.getLength(); i++) { Element conditionElement = (Element) nodes.item(i); assignConditionList(beanMatch, conditionElement); } dashboardMatch.addBeanMatch(beanMatch); } private void assignConditionList(BeanMatch beanMatch, Element beanMatchElement) { String type = beanMatchElement.getAttribute("type"); try { Class conditionClass = Class.forName("org.mc4j.console.dashboard.match." + type); BeanCondition condition = (BeanCondition) conditionClass.newInstance(); condition.initializeCondition(beanMatchElement); beanMatch.addCondition(condition); } catch(ClassNotFoundException cnfe) { ErrorManager.getDefault().notify(cnfe); } catch(IllegalAccessException iae) { ErrorManager.getDefault().notify(iae); } catch(InstantiationException ie) { ErrorManager.getDefault().notify(ie); } } private Document buildDocument(InputStream is) throws SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.out.println(pce); } Document document = builder.parse(is); return document; } } |