Search in sources :

Example 71 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project jPOS by jpos.

the class QThreadPoolExecutorTest method setQBeanConfig.

protected void setQBeanConfig(byte[] config) {
    ByteArrayInputStream bis = new ByteArrayInputStream(config);
    Document doc = getDocument(bis);
    qbean.setPersist(doc.getRootElement());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Document(org.jdom2.Document)

Example 72 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project jPOS by jpos.

the class Q2 method persist.

private long persist(File f, ObjectName name) {
    long deployed = f.lastModified();
    try {
        Element e = (Element) server.getAttribute(name, "Persist");
        if (e != null) {
            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
            Document doc = new Document();
            e.detach();
            doc.setRootElement(e);
            File tmp = new File(f.getAbsolutePath() + ".tmp");
            Writer writer = new BufferedWriter(new FileWriter(tmp));
            try {
                out.output(doc, writer);
            } finally {
                writer.close();
            }
            f.delete();
            tmp.renameTo(f);
            deployed = f.lastModified();
        }
    } catch (Exception ex) {
        log.warn("persist", ex);
    }
    return deployed;
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) GeneralSecurityException(java.security.GeneralSecurityException) BundleException(org.osgi.framework.BundleException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) SAXException(org.xml.sax.SAXException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ISOException(org.jpos.iso.ISOException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException)

Example 73 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project jPOS by jpos.

the class Q2 method deployBundle.

private void deployBundle(File bundle, boolean encrypt) throws JDOMException, IOException, ISOException, GeneralSecurityException {
    SAXBuilder builder = createSAXBuilder();
    Document doc = builder.build(bundle);
    Iterator iter = doc.getRootElement().getChildren().iterator();
    for (int i = 1; iter.hasNext(); i++) {
        Element e = (Element) iter.next();
        deployElement(e, String.format("%02d_%s.xml", i, e.getName()), encrypt, !encrypt);
    // the !encrypt above is tricky and deserves an explanation
    // if we are encrypting a QBean, we want it to stay in the deploy
    // directory for future runs. If on the other hand we are deploying
    // a bundle, we want it to be transient.
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 74 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project jPOS by jpos.

the class Q2 method deployElement.

public void deployElement(Element e, String fileName, boolean encrypt, boolean isTransient) throws ISOException, IOException, GeneralSecurityException {
    e = e.clone();
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    Document doc = new Document();
    doc.setRootElement(e);
    File qbean = new File(deployDir, fileName);
    if (isTransient) {
        e.setAttribute("instance", getInstanceId().toString());
        qbean.deleteOnExit();
    }
    if (encrypt) {
        doc = encrypt(doc);
    }
    try (Writer writer = new BufferedWriter(new FileWriter(qbean))) {
        out.output(doc, writer);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Document(org.jdom2.Document)

Example 75 with Document

use of org.datanucleus.samples.annotations.one_many.bidir_3.Document in project jPOS by jpos.

the class Q2 method deploy.

private boolean deploy(File f) {
    LogEvent evt = log != null ? log.createInfo() : null;
    try {
        QEntry qentry = dirMap.get(f);
        SAXBuilder builder = createSAXBuilder();
        Document doc;
        if (decorator != null && !f.getName().equals(LOGGER_CONFIG)) {
            doc = decrypt(builder.build(new StringReader(decorator.decorateFile(f))));
        } else {
            doc = decrypt(builder.build(f));
        }
        Element rootElement = doc.getRootElement();
        String iuuid = rootElement.getAttributeValue("instance");
        if (iuuid != null) {
            UUID uuid = UUID.fromString(iuuid);
            if (!uuid.equals(getInstanceId())) {
                deleteFile(f, iuuid);
                return false;
            }
        }
        String enabledAttribute = rootElement.getAttributeValue("enabled", "true");
        if ("true".equalsIgnoreCase(enabledAttribute) || "yes".equalsIgnoreCase(enabledAttribute)) {
            if (evt != null)
                evt.addMessage("deploy: " + f.getCanonicalPath());
            Object obj = factory.instantiate(this, rootElement);
            qentry.setObject(obj);
            ObjectInstance instance = factory.createQBean(this, doc.getRootElement(), obj);
            qentry.setInstance(instance);
        } else if (evt != null) {
            evt.addMessage("deploy ignored (enabled='" + enabledAttribute + "'): " + f.getCanonicalPath());
        }
    } catch (InstanceAlreadyExistsException e) {
        /*
            * Ok, the file we tried to deploy, holds an object
            *  that already has been deployed.
            *  
            * Rename it out of the way.
            * 
            */
        tidyFileAway(f, DUPLICATE_EXTENSION);
        if (evt != null)
            evt.addMessage(e);
        return false;
    } catch (Exception e) {
        if (evt != null)
            evt.addMessage(e);
        tidyFileAway(f, ERROR_EXTENSION);
        // This will also save deploy error repeats...
        return false;
    } catch (Error e) {
        if (evt != null)
            evt.addMessage(e);
        tidyFileAway(f, ENV_EXTENSION);
        // This will also save deploy error repeats...
        return false;
    } finally {
        if (evt != null)
            Logger.log(evt);
    }
    return true;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) LogEvent(org.jpos.util.LogEvent) Element(org.jdom2.Element) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ObjectInstance(javax.management.ObjectInstance) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) GeneralSecurityException(java.security.GeneralSecurityException) BundleException(org.osgi.framework.BundleException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) SAXException(org.xml.sax.SAXException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ISOException(org.jpos.iso.ISOException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException)

Aggregations

Document (org.jdom2.Document)391 Element (org.jdom2.Element)243 Test (org.junit.Test)104 SAXBuilder (org.jdom2.input.SAXBuilder)84 IOException (java.io.IOException)72 File (java.io.File)56 XMLOutputter (org.jdom2.output.XMLOutputter)56 JDOMException (org.jdom2.JDOMException)44 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)34 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)25 DocType (org.jdom2.DocType)24 ArrayList (java.util.ArrayList)22 MCRContent (org.mycore.common.content.MCRContent)22 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)22 MCRException (org.mycore.common.MCRException)21 Document (com.google.cloud.language.v1.Document)20 HashMap (java.util.HashMap)19 Attribute (org.jdom2.Attribute)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)19 URL (java.net.URL)18