
org.glassfish.embed.EmbeddedFileSystem Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2008 Sun Microsystems, Inc. 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.html
* or glassfish/bootstrap/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 glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [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 org.glassfish.embed;
import com.sun.enterprise.util.diagnostics.ObjectAnalyzer;
import static com.sun.enterprise.universal.glassfish.SystemPropertyConstants.*;
import java.net.MalformedURLException;
import static org.glassfish.embed.ServerConstants.*;
import com.sun.enterprise.universal.io.SmartFile;
import com.sun.enterprise.util.io.FileUtils;
import java.io.File;
import java.net.URL;
import org.glassfish.embed.util.EmbeddedUtils;
/**
* There are currently some ugly things we MUST do:
*
* - write out our hard-wired domain.xml to disk for use by core V3
*
- write out magic JDBC xml files to disk
*
* We are concentrating these things here for ease of maintenance.
*
* @author bnevins
*/
public final class EmbeddedFileSystem {
/**
*
*/
EmbeddedFileSystem() {
}
// ****************************************************
// ************* public setters
// ****************************************************
/**
* Set the root directory of the Embedded GlassFish file system.
* The directory specified must exist.
*
* @param f install root directory
* @throws org.glassfish.embed.EmbeddedException
*/
public void setInstallRoot(File f) throws EmbeddedException {
mustNotBeInitialized("setInstallRoot");
installRoot = SmartFile.sanitize(f);
if (!EmbeddedUtils.mkdirsIfNotExist(f)) {
throw new EmbeddedException("bad_install_root", f);
}
}
/**
* Set the directory for the server instance
* The directory specified must exist.
*
* @param f instance root directory
* @throws org.glassfish.embed.EmbeddedException
*/
public void setInstanceRoot(File f) throws EmbeddedException {
mustNotBeInitialized("setInstanceRoot");
instanceRoot = SmartFile.sanitize(f);
if (!EmbeddedUtils.mkdirsIfNotExist(f)) {
throw new EmbeddedException("bad_instance_root", f);
}
}
/**
* Use the specified file as the target domain.xml.
* The target domain.xml is the output of the in-memory domain.xml
*
* @param f file that the server writes out to as domain.xml
* @throws org.glassfish.embed.EmbeddedException
*/
public void setDomainXmlTarget(File f) throws EmbeddedException {
mustNotBeInitialized("setDomainXmlTarget");
domainXmlTarget = SmartFile.sanitize(f);
}
/**
* Use the specified URL as the source domain.xml.
* The source domain.xml is the input to the in-memory domain.xml
*
* @param url URL that is read in by the server as domain.xml
* @throws org.glassfish.embed.EmbeddedException
*/
public void setDomainXmlSource(URL url) throws EmbeddedException {
mustNotBeInitialized("setDomainXmlSource(URL)");
domainXmlSource = url;
}
/**
* Use the specified file as the source domain.xml.
* The source domain.xml is the input to the in-memory domain.xml.
*
* @param f file that is read in by the server as domain.xml
* @throws org.glassfish.embed.EmbeddedException
*/
public void setDomainXmlSource(File f) throws EmbeddedException {
mustNotBeInitialized("setDomainXmlSource(File)");
setDomainXmlTarget(f);
try {
domainXmlSource = f.toURI().toURL();
} catch (Exception e) {
throw new EmbeddedException("bad_file", f, e);
}
}
/**
* Set the docroot directory of the Embedded GlassFish file system.
* The default is instance-dir/docroot
*
* The directory must exist or it must be possible to create it.
*
* @param docRoot the desired docroot directory
* @throws EmbeddedException
*/
public void setDocRootDir(File docRoot) throws EmbeddedException {
mustNotBeInitialized("setDocRootDir");
docRootDir = SmartFile.sanitize(docRoot);
if (!EmbeddedUtils.mkdirsIfNotExist(docRootDir)) {
throw new EmbeddedException("bad_docroot", docRootDir);
}
}
/**
* Specifies whether to delete the Embedded file system after stopping Embedded
* GlassFish process. If set to true
the install root
* and all directories and files under it will be deleted upon exit.
*
* @param b true - deletes the install root upon exit
* false - keeps the install root upon exit
* @throws org.glassfish.embed.EmbeddedException
*/
public void setAutoDelete(boolean b) throws EmbeddedException {
mustNotBeInitialized("setAutoDelete");
autoDelete = b;
}
/**
* Specify the file to be used for logging
*
* @param f file for logging
*/
public void setLogFile(File f) {
// f can be null -- it is pointless but OK
logFile = f;
}
// ****************************************************
// ************* public getters
// ****************************************************
/**
* Returns the root directory of the Embedded GlassFish file system.
*
* @return install root directory
* @throws org.glassfish.embed.EmbeddedException
*/
public File getInstallRoot() throws EmbeddedException {
mustBeInitialized("getInstallRoot");
return installRoot;
}
/**
* Returns the root directory of the server instance
*
* @return instance root directory
* @throws org.glassfish.embed.EmbeddedException
*/
public File getInstanceRoot() throws EmbeddedException {
mustBeInitialized("getInstanceRoot");
return instanceRoot;
}
/**
* Returns the file that the server writes out to as domain.xml
*
* @return target domain.xml file
* @throws org.glassfish.embed.EmbeddedException
*/
public File getTargetDomainXml() throws EmbeddedException {
mustBeInitialized("getTargetDomainXml");
return domainXmlTarget;
}
/**
* Returns the URL that the server reads in as domain.xml
*
* @return source domain.xml URL
* @throws org.glassfish.embed.EmbeddedException
*/
public URL getSourceDomainXml() throws EmbeddedException {
mustBeInitialized("getSourceDomainXml");
return domainXmlSource;
}
/**
* Returns the log file
*
* @return logfile
* @throws org.glassfish.embed.EmbeddedException
*/
public File getLogFile() throws EmbeddedException {
mustBeInitialized("getLogFile");
return logFile;
}
/**
* Returns the applications directory of the Embedded GlassFish file system
*
* @return applications directory
*/
public File getApplicationsDir() {
return appsDir;
}
/**
* Returns the docroot directory of the Embedded GlassFish file system
*
* @return docroot directory
*/
public File getDocRootDir() throws EmbeddedException {
mustBeInitialized("getDocRootDir");
return docRootDir;
}
/*
* Return a String representation.
*/
@Override
public String toString() {
return ObjectAnalyzer.toString(this);
}
// ****************************************************
// ************* package private. Think long and hard before making public!
// ****************************************************
/**
*
* @return fake modules directory. This is for the benefit of StartupContext
* which insists that the install root should be the PARENT of some other directory.
* This is some other directory.
* @throws org.glassfish.embed.EmbeddedException
*/
File getModulesDirectory() throws EmbeddedException {
mustBeInitialized("getModulesDirectory");
return modulesDir;
}
void cleanup() throws EmbeddedException {
mustBeInitialized("cleanup");
defaultsAreInUse = false;
if (shouldCleanup()) {
// note that Logger will not work now because the JVM has shut it down
System.out.println("Cleaning up files");
FileUtils.whack(installRoot);
if (!instanceRoot.equals(installRoot)) {
FileUtils.whack(instanceRoot);
}
}
}
/* do NOT make this public!
* if user set their own stuff - just validate. If not then setup defaults
* calling this method shuts the window on ALL setters so we don't have to
* worry about getting into a weird inconsistent state later.
*/
void initialize() throws EmbeddedException {
initializeDirectories();
initializeDomainXml(); // very complicated!!
setSystemProps();
initializeLogFile();
initialized = true;
}
public boolean isOurDomainXml() {
return domainXmlSource == DEFAULT_DOMAIN_XML_URL;
}
// ****************************************************
// ************* private
// ****************************************************
private void initializeDirectories() throws EmbeddedException {
initializeInstanceAndInstallDirs();
// bnevins note:
//it is too early to call the getters for installRoot and instanceRoot - so
// we access the variables themselves.
appsDir = initializeDirectory(instanceRoot, APPLICATIONS_DIR_NAME, "Applications");
generatedDir = initializeDirectory(instanceRoot, GENERATED_DIR_NAME, "Generated");
modulesDir = initializeDirectory(installRoot, MODULES_DIR_NAME, "Modules");
if(docRootDir == null)
docRootDir = initializeDirectory(instanceRoot, DOCROOT_DIR_NAME, "Docroot");
}
/**
* The idea here is that the Url ALWAYS points at the source of data.
* the File always points t where on disk the data will be written to (and
* maybe is the source of data)
* @throws org.glassfish.embed.EmbeddedException
*/
private void initializeDomainXml() throws EmbeddedException {
initializeTargetDomainXml();
initializeSourceDomainXml();
}
private synchronized void initializeInstanceAndInstallDirs() throws EmbeddedException {
// booleans for readability...
boolean hasInstance = instanceRoot != null;
boolean hasInstall = installRoot != null;
/*
* both are set to something, just return
*/
if(hasInstance && hasInstall) {
return;
}
if (defaultsAreInUse) {
throw new EmbeddedException("EFS_defaults_in_use");
}
defaultsAreInUse = true;
/*
* both are null -- use all defaults
*/
if(!hasInstance && !hasInstall) {
setInstallRoot(defaultInstallRoot);
setInstanceRoot(new File(installRoot, DEFAULT_PATH_TO_INSTANCE));
}
/*
* instance but no install.
*/
else if(hasInstance && !hasInstall) {
setInstallRoot(defaultInstallRoot);
}
/*
* install but no instance.
*/
else { // if(hasInstall && !hasInstance)
setInstanceRoot(new File(installRoot, DEFAULT_PATH_TO_INSTANCE));
}
}
private void initializeLogFile() throws EmbeddedException {
// The user may have already specified a logfile.
// if so - do a sanity check and return it.
if(logFile != null) {
return;
}
File logDir = new File(instanceRoot, LOG_FILE_DIR);
if (!EmbeddedUtils.mkdirsIfNotExist(logDir)) {
throw new EmbeddedException("cant_make_log_dir", logDir);
}
logFile = new File(logDir, LOG_FILE);
}
private void initializeTargetDomainXml() throws EmbeddedException {
if (domainXmlTarget == null) {
domainXmlTarget = new File(instanceRoot, DEFAULT_PATH_TO_DOMAIN_XML);
} else {
// they specified it -- insist that it exist and be non-empty
if (!ok(domainXmlTarget)) {
throw new EmbeddedException("EFS_bad_domain_xml_file", domainXmlTarget);
}
}
File parent = new File(domainXmlTarget, "..");
parent = SmartFile.sanitize(parent); // get rid of the trailing ".."
if (!EmbeddedUtils.mkdirsIfNotExist(parent)) {
throw new EmbeddedException("cant_create_parent_dir_domain_xml", parent);
}
}
private void initializeSourceDomainXml() throws EmbeddedException {
if (domainXmlSource != null) {
// they specified it -- insist that it be bonafide.
if (!ok(domainXmlSource)) {
throw new EmbeddedException("EFS_bad_domain_xml_file", domainXmlTarget);
}
} else {
File dx = SmartFile.sanitize(new File(instanceRoot, DEFAULT_PATH_TO_DOMAIN_XML));
File our_generated_dx = SmartFile.sanitize(new File(DEFAULT_PATH_TO_DOMAIN_XML_IN_GFE));
if (dx.isFile() && !dx.equals(our_generated_dx)) {
try {
domainXmlSource = dx.toURI().toURL();
} catch (MalformedURLException ex) {
throw new EmbeddedException(ex);
}
}
else
domainXmlSource = DEFAULT_DOMAIN_XML_URL; // use the hard-wired file.
}
}
private File initializeDirectory(File parent, String filename, String messageName) throws EmbeddedException {
File dir = new File(parent, filename);
if (!EmbeddedUtils.mkdirsIfNotExist(dir)) {
throw new EmbeddedException("cant_create_dir", messageName, dir);
}
return dir;
}
private void setSystemProps() {
System.setProperty(INSTANCE_ROOT_PROPERTY, instanceRoot.getPath());
System.setProperty(INSTALL_ROOT_PROPERTY, installRoot.getPath());
System.setProperty(INSTANCE_ROOT_URI_PROPERTY, instanceRoot.toURI().toString());
System.setProperty(INSTALL_ROOT_URI_PROPERTY, installRoot.toURI().toString());
// Surprisingly this is the most reliable way to get parent with JDK!
File domainsDir = SmartFile.sanitize(new File(instanceRoot, ".."));
System.setProperty(DOMAINS_ROOT_PROPERTY, domainsDir.getPath());
}
private boolean shouldCleanup() {
// don't EVER delete if the flag is false!!!
return autoDelete;
}
private void mustBeInitialized(String name) throws EmbeddedException {
if (!initialized) {
throw new EmbeddedException("must_be_initialized", name);
}
}
private void mustNotBeInitialized(String name) throws EmbeddedException {
if (initialized) {
throw new EmbeddedException("must_not_be_initialized", name);
}
}
private boolean ok(File f) {
return f.length() > 0L;
}
private boolean ok(URL url) {
// TODO -- what else besides not null?
return url != null;
}
private static final File defaultInstallRoot = SmartFile.sanitize(new File(DEFAULT_GFE_DIR));
private static final File defaultInstanceRoot = SmartFile.sanitize(new File(defaultInstallRoot, "domains/domain1"));
private static boolean defaultsAreInUse = false;
private File installRoot;
private File instanceRoot;
private File domainXmlTarget;
private File appsDir;
private File docRootDir;
private File generatedDir;
private File modulesDir;
private File logFile;
private URL domainXmlSource;
private boolean autoDelete = true;
private boolean initialized = false;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy