Search in sources :

Example 6 with Unmarshaller

use of org.exolab.castor.xml.Unmarshaller in project ACS by ACS-Community.

the class ACSAlarmDAOImpl method loadAlarms.

/**
	 * Load alarms from CDB.
	 * 
	 * Read the alarms by the alarm definitions file of the CDB.
	 * The initialization of the alarms is not complete at this stage.
	 * In fact the categories are assigned to alarms when reading the
	 * categories from the CDB by ACSCategoryDAOImpl
	 * 
	 *  
	 * @throws Exception In case of error while parsing XML definition files
	 * 
	 * @see ACSCategoryDAOImpl
	 */
public List<FaultFamily> loadAlarms() throws Exception {
    if (conf == null) {
        throw new IllegalStateException("Missing dal");
    }
    String xml;
    try {
        xml = conf.getConfiguration(ALARM_DEFINITION_PATH);
    } catch (Throwable t) {
        return new ArrayList<FaultFamily>();
    //throw new RuntimeException("Couldn't read alarm XML", t);
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (Exception e) {
        throw new Exception("Error building the DocumentBuilder from the DocumentBuilderFactory", e);
    }
    StringReader stringReader = new StringReader(xml);
    InputSource inputSource = new InputSource(stringReader);
    Document doc;
    try {
        doc = builder.parse(inputSource);
        if (doc == null) {
            throw new Exception("The builder returned a null Document after parsing");
        }
    } catch (Exception e) {
        throw new Exception("Error parsing XML: " + xml, e);
    }
    NodeList docChilds = doc.getChildNodes();
    if (docChilds == null || docChilds.getLength() != 1) {
        throw new Exception("Malformed xml: only one node (AlarmDefinitions) expected");
    }
    Node alarmDefNode = docChilds.item(0);
    if (alarmDefNode == null || alarmDefNode.getNodeName() != XML_DOCUMENT_TYPE) {
        throw new Exception("Malformed xml: " + XML_DOCUMENT_TYPE + " not found");
    }
    NodeList alarmDefsNodes = alarmDefNode.getChildNodes();
    Unmarshaller FF_unmarshaller = new Unmarshaller(FaultFamily.class);
    FF_unmarshaller.setValidation(false);
    FF_unmarshaller.setWhitespacePreserve(true);
    Vector<FaultFamily> cdbFamilies = new Vector<FaultFamily>();
    for (int t = 0; t < alarmDefsNodes.getLength(); t++) {
        Node alarmDef = alarmDefsNodes.item(t);
        FaultFamily family;
        try {
            family = (FaultFamily) FF_unmarshaller.unmarshal(alarmDef);
            if (family == null) {
                throw new Exception("Error unmarshalling a family node");
            }
            cdbFamilies.add(family);
        } catch (Exception e) {
            throw new Exception("Error parsing " + alarmDef.getNodeName(), e);
        }
        family.validate();
    }
    alarmDefs.clear();
    srcDefs.clear();
    generateAlarmsMap(cdbFamilies);
    loadReductionRules();
    return cdbFamilies;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FaultFamily(alma.acs.alarmsystem.generated.FaultFamily) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller) Vector(java.util.Vector)

Example 7 with Unmarshaller

use of org.exolab.castor.xml.Unmarshaller in project ACS by ACS-Community.

the class ACSCategoryDAOImpl method getCategories.

public Categories getCategories() {
    if (conf == null || !conf.isWriteable())
        throw new IllegalStateException("no writable configuration accessor");
    Categories cats;
    String xml;
    try {
        xml = conf.getConfiguration(CATEGORY_DEFINITION_PATH);
    } catch (CDBRecordDoesNotExistEx e) {
        cats = new Categories();
        return cats;
    } catch (Exception e1) {
        e1.printStackTrace();
        return null;
    }
    StringReader FFReader = new StringReader(xml);
    Unmarshaller FF_unmarshaller = new Unmarshaller(Categories.class);
    FF_unmarshaller.setValidation(false);
    try {
        cats = (Categories) FF_unmarshaller.unmarshal(FFReader);
    } catch (MarshalException e1) {
        e1.printStackTrace();
        return null;
    } catch (ValidationException e1) {
        e1.printStackTrace();
        return null;
    }
    try {
        cats.validate();
    } catch (ValidationException e1) {
        e1.printStackTrace();
    }
    return cats;
}
Also used : MarshalException(org.exolab.castor.xml.MarshalException) ValidationException(org.exolab.castor.xml.ValidationException) Categories(alma.acs.alarmsystem.generated.Categories) StringReader(java.io.StringReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) IOException(java.io.IOException) CDBRecordDoesNotExistEx(alma.cdbErrType.CDBRecordDoesNotExistEx)

Example 8 with Unmarshaller

use of org.exolab.castor.xml.Unmarshaller in project ACS by ACS-Community.

the class EntityDeserializer method deserializeEntity.

/**
	 * Deserializes (parses) an entity xml and instantiates the binding objects.
	 * 
	 * @param xmlString  the stringified xml. 
	 * @param entityClass  the binding class of which we want an instance
	 *                      filled with the xml data.
	 * @param timestamp  the timestamp to be stored in the entity object, for later serialization.
	 * @return  the binding class instance (should be casted by the caller).
	 * @throws EntityException if the binding class instance can't be constructed from the xml. 
	 *         Note that this exception is only logged at FINER level by this method, so make sure to log it appropriately.
	 */
private Object deserializeEntity(String xmlString, Class entityClass, String timestamp) throws EntityException {
    if (xmlString == null)
        throw new EntityException("xmlString was null.");
    if (entityClass == null)
        throw new EntityException("entityClass was null.");
    if (timestamp == null)
        timestamp = "";
    Object entity;
    try {
        Unmarshaller unmarsh = new Unmarshaller(entityClass);
        unmarsh.setValidation(false);
        unmarsh.setWhitespacePreserve(true);
        //			unmarsh.setIgnoreExtraAttributes(true);
        //			unmarsh.setIgnoreExtraElements(true);
        entity = unmarsh.unmarshal(new StringReader(xmlString));
    } catch (Exception e) {
        // trim huge xml to avoid overly long log message
        String xmlStringStart = xmlString.substring(0, Math.min(512, xmlString.length()));
        String msg = "Failed to parse xml into entity binding class '" + entityClass.getName() + "'. The xml data was " + xmlStringStart;
        m_logger.log(Level.FINER, msg, e);
        throw new EntityException(msg, e);
    }
    // becomes irrelevant.
    if (entity != null) {
        EntityT entityMeta = m_finder.extractEntityT(entity);
        if (entityMeta != null) {
            entityMeta.setTimestamp(timestamp);
        }
    }
    return entity;
}
Also used : EntityT(alma.entities.commonentity.EntityT) StringReader(java.io.StringReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller)

Example 9 with Unmarshaller

use of org.exolab.castor.xml.Unmarshaller in project ACS by ACS-Community.

the class XmlInOut method unmarshalObsProposal.

public ObsProposal unmarshalObsProposal(String xmlFileName) throws FileNotFoundException, MarshalException, ValidationException {
    File xmlFile = new File(module_dir, pathToXmlSamples + xmlFileName);
    Reader xmlReader = new FileReader(xmlFile);
    Unmarshaller unm = new Unmarshaller(ObsProposal.class);
    unm.setValidation(false);
    ObsProposal oprop = (ObsProposal) unm.unmarshal(xmlReader);
    return oprop;
}
Also used : FileReader(java.io.FileReader) Reader(java.io.Reader) ObsProposal(alma.xmljbind.test.obsproposal.ObsProposal) FileReader(java.io.FileReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller) File(java.io.File)

Example 10 with Unmarshaller

use of org.exolab.castor.xml.Unmarshaller in project ACS by ACS-Community.

the class ACSCategoryDAOImpl method loadCategories.

/**
	 * Load the categories from the CDB.
	 * <P>
	 * Loads all the category from the CDB and build an internal
	 * representation of category.
	 * The category is also added to all the alarms having the
	 * fault family specified in the XML.
	 * <P>
	 * All the categories derive from ROOT that is built here
	 * as default (in this way the user does ot need to add the ROOT
	 * entry in the CDB).
	 * 
	 * @return list of Category entries read from CDB 
	 * @throws Exception In case of error reading the values from the CDB
	 */
public alma.acs.alarmsystem.generated.Category[] loadCategories() throws Exception {
    if (conf == null) {
        throw new IllegalStateException("Missing dal");
    }
    String xml;
    try {
        xml = conf.getConfiguration(CATEGORY_DEFINITION_PATH);
    } catch (Throwable t) {
        throw new RuntimeException("Couldn't read alarm list", t);
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (Exception e) {
        throw new Exception("Error building the DocumentBuilder from the DocumentBuilderFactory", e);
    }
    StringReader stringReader = new StringReader(xml);
    InputSource inputSource = new InputSource(stringReader);
    Document doc;
    try {
        doc = builder.parse(inputSource);
        if (doc == null) {
            throw new Exception("The builder returned a null Document after parsing");
        }
    } catch (Exception e) {
        throw new Exception("Error parsing XML: " + xml, e);
    }
    NodeList docChilds = doc.getChildNodes();
    if (docChilds == null || docChilds.getLength() != 1) {
        throw new Exception("Malformed xml: only one node (categories) expected");
    }
    Node categoriesNode = docChilds.item(0);
    Unmarshaller FF_unmarshaller = new Unmarshaller(Categories.class);
    FF_unmarshaller.setValidation(false);
    FF_unmarshaller.setWhitespacePreserve(true);
    Categories daoCategories;
    try {
        daoCategories = (Categories) FF_unmarshaller.unmarshal(categoriesNode);
        logger.log(AcsLogLevel.DEBUG, "Categories definition read");
    } catch (Exception e) {
        throw new Exception("Error parsing " + CATEGORY_DEFINITION_PATH, e);
    }
    alma.acs.alarmsystem.generated.Category[] daoCategory = daoCategories.getCategory();
    if (daoCategory == null || daoCategory.length == 0) {
        logger.log(AcsLogLevel.DEBUG, "No category defined");
    }
    // Add the root Category
    addRootCategory();
    // Goes through all the Categories read from the CDB
    for (alma.acs.alarmsystem.generated.Category category : daoCategory) {
        cern.laser.business.definition.data.CategoryDefinition definition = new cern.laser.business.definition.data.CategoryDefinition(category.getPath(), category.getDescription());
        CategoryImpl ci = new CategoryImpl();
        // will get filled in later
        ci.setAlarmIds(new HashSet());
        ci.setCategoryId(new Integer(nextCatID++));
        ci.setChildrenIds(new HashSet<Integer>());
        ci.setDescription(definition.getDescription());
        ci.setName(definition.getPath());
        ci.setPath(definition.getPath());
        ci.setAlarmIds(new HashSet());
        setParentID(ci);
        // Stores the categories
        categories.put(ci.getCategoryId(), ci);
        catPathToCategory.put(ci.getPath(), ci);
        logger.log(AcsLogLevel.DEBUG, "Category " + ci.getName() + " added with ID=" + ci.getCategoryId());
        // Check if the category is defined as default.
        if (category.hasIsDefault() && category.getIsDefault() == true) {
            if (defaultCategory != null) {
                StringBuilder str = new StringBuilder("CDB misconfiguration: default category defined more then once (actual default: ");
                str.append(defaultCategory.getPath());
                str.append(", new default: ");
                str.append(category.getPath());
                logger.log(AcsLogLevel.WARNING, str.toString());
            } else {
                defaultCategory = ci;
            }
        }
        // A category contains a set of child ids.
        // This method adjusts the references of categories between the parent 
        // and the child
        adjustParentIDs(ci.getName(), ci.getCategoryId());
        // Connect alarms to this category
        for (alma.acs.alarmsystem.generated.Category cat : daoCategories.getCategory()) {
            if (cat.getPath().equals(ci.getPath())) {
                String[] families = cat.getAlarms().getFaultFamily();
                for (String faultFamily : families) {
                    assignCategoryToAlarms(ci, faultFamily);
                }
            }
        }
    }
    // Assign core alarms to ROOT category
    assignCategoryOfCoreAlarms();
    // Log a message if no category has been defined in the CDB
    if (defaultCategory == null) {
        logger.log(AcsLogLevel.WARNING, "No default category defined in CDB");
    } else {
        // Check if there are alarms without category to assign to the default
        assignDefaultCategory(defaultCategory);
    }
    return daoCategory;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Category(cern.laser.business.data.Category) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) StringReader(java.io.StringReader) Unmarshaller(org.exolab.castor.xml.Unmarshaller) CategoryDefinition(alma.alarmsystem.alarmmessage.generated.CategoryDefinition) HashSet(java.util.HashSet) Categories(alma.acs.alarmsystem.generated.Categories) NodeList(org.w3c.dom.NodeList) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) CategoryImpl(cern.laser.business.data.CategoryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

Unmarshaller (org.exolab.castor.xml.Unmarshaller)20 StringReader (java.io.StringReader)14 IOException (java.io.IOException)8 MarshalException (org.exolab.castor.xml.MarshalException)8 ValidationException (org.exolab.castor.xml.ValidationException)8 LaserObjectNotFoundException (cern.laser.business.LaserObjectNotFoundException)6 Mapping (org.exolab.castor.mapping.Mapping)5 InputSource (org.xml.sax.InputSource)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 Categories (alma.acs.alarmsystem.generated.Categories)3 CDBRecordDoesNotExistEx (alma.cdbErrType.CDBRecordDoesNotExistEx)3 File (java.io.File)3 FileReader (java.io.FileReader)3 Reader (java.io.Reader)3 MalformedURLException (java.net.MalformedURLException)3 FaultFamily (alma.acs.alarmsystem.generated.FaultFamily)2