Search in sources :

Example 11 with DomDocument

use of org.jvnet.hk2.config.DomDocument in project Payara by payara.

the class GeneratorResource method get.

@GET
@Produces({ "text/plain" })
public String get(@QueryParam("outputDir") String outputDir) {
    if (outputDir == null) {
        outputDir = DEFAULT_OUTPUT_DIR;
    }
    String retVal = "Code Generation done at : " + outputDir;
    try {
        LocatorBridge locatorBridge = habitat.getService(LocatorBridge.class);
        Dom dom = Dom.unwrap(locatorBridge.getRemoteLocator().<Domain>getService(Domain.class));
        DomDocument document = dom.document;
        ConfigModel rootModel = dom.document.getRoot().model;
        ResourcesGenerator resourcesGenerator = new TextResourcesGenerator(outputDir, habitat);
        resourcesGenerator.generateSingle(rootModel, document);
        resourcesGenerator.endGeneration();
    } catch (Exception ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
        retVal = "Exception encountered during generation process: " + ex.toString() + "\nPlease look at server.log for more information.";
    }
    return retVal;
}
Also used : Dom(org.jvnet.hk2.config.Dom) ConfigModel(org.jvnet.hk2.config.ConfigModel) ResourcesGenerator(org.glassfish.admin.rest.generator.ResourcesGenerator) TextResourcesGenerator(org.glassfish.admin.rest.generator.TextResourcesGenerator) TextResourcesGenerator(org.glassfish.admin.rest.generator.TextResourcesGenerator) LocatorBridge(org.glassfish.admin.rest.adapter.LocatorBridge) Domain(com.sun.enterprise.config.serverbeans.Domain) DomDocument(org.jvnet.hk2.config.DomDocument) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 12 with DomDocument

use of org.jvnet.hk2.config.DomDocument in project Payara by payara.

the class StatusGenerator method getPlain.

@GET
@Produces({ "text/plain" })
public String getPlain() {
    // status.append("Status of Command usage\n");
    try {
        Domain entity = serviceLocator.getService(Domain.class);
        Dom dom = Dom.unwrap(entity);
        DomDocument document = dom.document;
        ConfigModel rootModel = dom.document.getRoot().model;
        ResourcesGenerator resourcesGenerator = new NOOPResourcesGenerator(serviceLocator);
        resourcesGenerator.generateSingle(rootModel, document);
        resourcesGenerator.endGeneration();
    } catch (Exception ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    // retVal = "Exception encountered during generation process: " + ex.toString() + "\nPlease look at server.log for more information.";
    }
    status.append("\n------------------------");
    status.append("All Commands used in REST Admin:\n");
    for (String ss : commandsUsed) {
        status.append(ss + "\n");
    }
    listOfCommands();
    for (String ss : commandsUsed) {
        allCommands.remove(ss);
    }
    status.append("\n------------------------");
    status.append("Missing Commands not used in REST Admin:\n");
    for (String ss : allCommands) {
        if (hasTargetParam(ss)) {
            status.append(ss + "          has a target param " + "\n");
        } else {
            status.append(ss + "\n");
        }
    }
    status.append("\n------------------------");
    status.append("REST-REDIRECT Commands defined on ConfigBeans:\n");
    for (String ss : restRedirectCommands) {
        status.append(ss + "\n");
    }
    status.append("\n------------------------");
    status.append("Commands to Resources Mapping Usage in REST Admin:\n");
    for (String ss : commandsToResources.keySet()) {
        if (hasTargetParam(ss)) {
            status.append(ss + "   :::target:::   " + commandsToResources.get(ss) + "\n");
        } else {
            status.append(ss + "      :::      " + commandsToResources.get(ss) + "\n");
        }
    }
    status.append("\n------------------------");
    status.append("Resources with Delete Commands in REST Admin (not counting RESTREDIRECT:\n");
    for (String ss : resourcesToDeleteCommands.keySet()) {
        status.append(ss + "      :::      " + resourcesToDeleteCommands.get(ss) + "\n");
    }
    FileOutputStream f = null;
    try {
        f = new FileOutputStream(System.getProperty("user.home") + "/GlassFishI18NData.properties");
        propsI18N.store(f, "");
    } catch (Exception ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ex) {
                RestLogging.restLogger.log(Level.SEVERE, null, ex);
            }
        }
    }
    return status.toString();
}
Also used : Dom(org.jvnet.hk2.config.Dom) ConfigModel(org.jvnet.hk2.config.ConfigModel) FileOutputStream(java.io.FileOutputStream) ResourcesGenerator(org.glassfish.admin.rest.generator.ResourcesGenerator) IOException(java.io.IOException) Domain(com.sun.enterprise.config.serverbeans.Domain) IOException(java.io.IOException) MultiException(org.glassfish.hk2.api.MultiException) DomDocument(org.jvnet.hk2.config.DomDocument) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with DomDocument

use of org.jvnet.hk2.config.DomDocument in project Payara by payara.

the class TemplateListOfResource method getElementTypeByName.

public static Class<? extends ConfigBeanProxy> getElementTypeByName(Dom parentDom, String elementName) throws ClassNotFoundException {
    DomDocument document = parentDom.document;
    ConfigModel.Property a = parentDom.model.getElement(elementName);
    if (a != null) {
        if (a.isLeaf()) {
            // : I am not too sure, but that should be a String @Element
            return null;
        } else {
            ConfigModel childModel = ((ConfigModel.Node) a).getModel();
            return (Class<? extends ConfigBeanProxy>) childModel.classLoaderHolder.loadClass(childModel.targetTypeName);
        }
    }
    // global lookup
    ConfigModel model = document.getModelByElementName(elementName);
    if (model != null) {
        return (Class<? extends ConfigBeanProxy>) model.classLoaderHolder.loadClass(model.targetTypeName);
    }
    return null;
}
Also used : ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 14 with DomDocument

use of org.jvnet.hk2.config.DomDocument in project Payara by payara.

the class Utils method getNewHabitat.

private static synchronized ServiceLocator getNewHabitat(final ConfigApiTest test) {
    final ServiceLocator sl = getNewHabitat();
    final String fileName = test.getFileName();
    ConfigParser configParser = new ConfigParser(sl);
    long now = System.currentTimeMillis();
    URL url = Utils.class.getClassLoader().getResource(fileName + ".xml");
    if (url != null) {
        try {
            DomDocument testDocument = test.getDocument(sl);
            DomDocument document = configParser.parse(url, testDocument);
            ServiceLocatorUtilities.addOneConstant(sl, document);
            test.decorate(sl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.getAnonymousLogger().fine("time to parse domain.xml : " + String.valueOf(System.currentTimeMillis() - now));
    }
    return sl;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ConfigParser(org.jvnet.hk2.config.ConfigParser) URL(java.net.URL) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 15 with DomDocument

use of org.jvnet.hk2.config.DomDocument in project Payara by payara.

the class DomainXmlPersistence method save.

@Override
public void save(DomDocument doc) throws IOException {
    if (modularityUtils.isIgnorePersisting() && !modularityUtils.isCommandInvocation()) {
        if (skippedDoc != null) {
            assert (doc == skippedDoc);
        }
        skippedDoc = doc;
        return;
    }
    File destination = getDestination();
    if (destination == null) {
        String msg = localStrings.getLocalString("NoLocation", "domain.xml cannot be persisted, null destination");
        logger.severe(msg);
        throw new IOException(msg);
    }
    Lock writeLock = null;
    try {
        try {
            writeLock = accessWrite();
        } catch (TimeoutException e) {
            String msg = localStrings.getLocalString("Timeout", "Timed out when waiting for write lock on configuration file");
            logger.log(Level.SEVERE, msg);
            throw new IOException(msg, e);
        }
        // get a temporary file
        File f = File.createTempFile("domain", ".xml", destination.getParentFile());
        if (!f.exists()) {
            throw new IOException(localStrings.getLocalString("NoTmpFile", "Cannot create temporary file when saving domain.xml"));
        }
        // write to the temporary file
        XMLStreamWriter writer = null;
        OutputStream fos = getOutputStream(f);
        try {
            writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(fos));
            IndentingXMLStreamWriter indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
            doc.writeTo(indentingXMLStreamWriter);
            indentingXMLStreamWriter.close();
        } catch (XMLStreamException e) {
            String msg = localStrings.getLocalString("TmpFileNotSaved", "Configuration could not be saved to temporary file");
            logger.log(Level.SEVERE, msg, e);
            throw new IOException(e.getMessage(), e);
        // return after calling finally clause, because since temp file couldn't be saved,
        // renaming should not be attempted
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (XMLStreamException e) {
                    logger.log(Level.SEVERE, localStrings.getLocalString("CloseFailed", "Cannot close configuration writer stream"), e);
                    throw new IOException(e.getMessage(), e);
                }
            }
            fos.close();
        }
        // backup the current file
        File backup = new File(env.getConfigDirPath(), "domain.xml.bak");
        if (destination.exists() && backup.exists() && !backup.delete()) {
            String msg = localStrings.getLocalString("BackupDeleteFailed", "Could not delete previous backup file at {0}", backup.getAbsolutePath());
            logger.severe(msg);
            throw new IOException(msg);
        }
        if (destination.exists() && !FileUtils.renameFile(destination, backup)) {
            String msg = localStrings.getLocalString("TmpRenameFailed", "Could not rename {0} to {1}", destination.getAbsolutePath(), backup.getAbsolutePath());
            logger.severe(msg);
            throw new IOException(msg);
        }
        // save the temp file to domain.xml
        if (!FileUtils.renameFile(f, destination)) {
            String msg = localStrings.getLocalString("TmpRenameFailed", "Could not rename {0} to {1}", f.getAbsolutePath(), destination.getAbsolutePath());
            // try to rename backup to domain.xml (so that at least something is there)
            if (!FileUtils.renameFile(backup, destination)) {
                msg += "\n" + localStrings.getLocalString("RenameFailed", "Could not rename backup to {0}", destination.getAbsolutePath());
            }
            logger.severe(msg);
            throw new IOException(msg);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, localStrings.getLocalString("ioexception", "IOException while saving the configuration, changes not persisted"), e);
        throw e;
    } finally {
        if (writeLock != null) {
            writeLock.unlock();
        }
    }
    skippedDoc = null;
    saved(destination);
}
Also used : IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ManagedFile(org.glassfish.common.util.admin.ManagedFile) Lock(java.util.concurrent.locks.Lock) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DomDocument (org.jvnet.hk2.config.DomDocument)25 ConfigModel (org.jvnet.hk2.config.ConfigModel)12 ConfigParser (org.jvnet.hk2.config.ConfigParser)12 URL (java.net.URL)8 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)7 IOException (java.io.IOException)6 ThreadFactory (java.util.concurrent.ThreadFactory)6 GlassFishDocument (org.glassfish.config.support.GlassFishDocument)6 Domain (com.sun.enterprise.config.serverbeans.Domain)5 Dom (org.jvnet.hk2.config.Dom)5 PropertyVetoException (java.beans.PropertyVetoException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ResourcesGenerator (org.glassfish.admin.rest.generator.ResourcesGenerator)3 GlassFishConfigBean (org.glassfish.config.support.GlassFishConfigBean)3 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)3 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)3