alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (DomainXmlPersistence.java)

This example Glassfish source code file (DomainXmlPersistence.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

cannot, cannot, could, file, file, io, ioexception, ioexception, lock, log, logging, managedfile, override, string, string, threading, threads, timeoutexception, xmlstreamexception

The Glassfish DomainXmlPersistence.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.enterprise.v3.server;

import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.io.FileUtils;
import org.glassfish.common.util.admin.ManagedFile;
import org.glassfish.common.util.admin.ParamTokenizer;
import org.glassfish.config.support.ConfigurationAccess;
import org.glassfish.server.ServerEnvironmentImpl;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Scoped;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.config.DomDocument;
import org.jvnet.hk2.config.IndentingXMLStreamWriter;
import org.jvnet.hk2.component.Singleton;
import org.glassfish.config.support.ConfigurationPersistence;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLStreamException;
import java.io.*;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * domain.xml persistence.
 *
 * @author Jerome Dochez
 */
@Service
@Scoped(Singleton.class)
public class DomainXmlPersistence implements ConfigurationPersistence, ConfigurationAccess {

    @Inject
    ServerEnvironmentImpl env;
    @Inject
    protected Logger logger;

    final XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();

    final static LocalStringManagerImpl localStrings =
            new LocalStringManagerImpl(DomainXmlPersistence.class);    


    private synchronized ManagedFile getPidFile() throws IOException {
        File location=null;
        try {
            // I am locking indefinitely with a 2 seconds timeOut.
            location = new File(env.getConfigDirPath(), "lockfile");
            if (!location.exists()) {
                if (!location.createNewFile()) {
                    if (!location.exists()) {
                        String message = localStrings.getLocalString("cannotCreateLockfile",
                                "Cannot create lock file at {0}, configuration changes will not be persisted",
                                location);
                        logger.log(Level.SEVERE, message);
                        throw new IOException(message);
                    }
                }
            }
            return new ManagedFile(location, 2000, -1);
        } catch (IOException e) {
            logger.log(Level.SEVERE,
                    localStrings.getLocalString("InvalidLocation",
                            "Cannot obtain lockfile location {0}, configuration changes will not be persisted",
                            location), e);
            throw e;
        }
    }

    @Override
    public Lock accessRead() throws IOException, TimeoutException {
        return getPidFile().accessRead();
    }

    @Override
    public Lock accessWrite() throws IOException, TimeoutException {
        return getPidFile().accessWrite();
    }

    @Override
    public void save(DomDocument doc) throws IOException {
        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 == null) {
                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 != null) {
                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();
            }
        }
        saved(destination);
    }

    protected void saved(File destination) {
        logger.fine("Configuration saved at " + destination);
    }

    protected File getDestination() throws IOException {
        return new File(env.getConfigDirPath(), "domain.xml");
    }

    protected OutputStream getOutputStream(File destination) throws IOException {
        return new FileOutputStream(destination);
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish DomainXmlPersistence.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.