com.sun.enterprise.util.io.ServerDirs Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2010 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 com.sun.enterprise.util.io;
import com.sun.enterprise.universal.StringUtils;
import com.sun.enterprise.universal.i18n.LocalStringsImpl;
import com.sun.enterprise.universal.io.SmartFile;
import java.io.*;
import java.io.File;
/**
* The hierarchy of directories above a running DAS or server instance can get
* messy to deal with -- thus this class. This class is a bullet-proof holder of
* that information.
*
* Instances and DAS are arranged differently:
*
* examples:
* DAS
* domainDir = getServerDir() == C:/glassfishv3/glassfish/domains/domain1
* domainsDir = getServerParentDir() == C:/glassfishv3/glassfish/domains
* grandparent-dir is meaningless
*
* Instance
* instanceDir = getServerDir() == C:/glassfishv3/glassfish/nodeagents/mymachine/instance1
* agentDir = getServerParentDir() == C:/glassfishv3/glassfish/nodeagents/mymachine
* agentsDir = getServerGrandParentDir() == C:/glassfishv3/glassfish/nodeagents
*
* Currently in all cases the name of the serverDir is the name of the server --
* by our definition.
*
* THIS CLASS IS GUARANTEED THREAD SAFE
* THIS CLASS IS GUARANTEED IMMUTABLE
*
* Contract: Caller is supposed to NOT call methods on an instance of this class.
* It's "advanced java" to be able to do that anyway.
* I don't allow half-baked data out. It's all or none. The "valid" flag
* is checked and if invalid -- all methods return null. They don't throw an Exception
* because the caller is not supposed to call the methods - it would just annoy
* the caller.
*
* @author Byron Nevins
* @since 3.1
* Created: April 19, 2010
*/
public class ServerDirs {
// do-nothing constructor
/**
*
*/
public ServerDirs() {
serverName = null;
serverDir = null;
parentDir = null;
grandParentDir = null;
configDir = null;
domainXml = null;
pidFile = null;
valid = false;
localPassword = null;
localPasswordFile = null;
dasPropertiesFile = null;
}
public ServerDirs(File leaf) throws IOException {
if(leaf == null)
throw new IllegalArgumentException(strings.get("ServerDirs.nullArg", "ServerDirs.ServerDirs()"));
if(!leaf.isDirectory())
throw new IOException(strings.get("ServerDirs.badDir", leaf));
serverDir = SmartFile.sanitize(leaf);
serverName = serverDir.getName();
// note that serverDir has been "smart-filed" so we don't have to worry
// about getParentFile() which has issues with relative paths...
parentDir = serverDir.getParentFile();
if(parentDir == null || !parentDir.isDirectory())
throw new IOException(strings.get("ServerDirs.badParentDir", serverDir));
// grandparent dir is optional. It can be null for DAS for instance...
grandParentDir = parentDir.getParentFile();
configDir = new File(serverDir, "config");
domainXml = new File(configDir, "domain.xml");
pidFile = new File(configDir, "pid");
localPasswordFile = new File(configDir, "local-password");
String localPasswordBuffer = null; // need an atomic assign tor localPassword
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(localPasswordFile));
localPasswordBuffer = r.readLine();
}
catch(Exception e) {
// needs no handling
} finally {
localPassword = localPasswordBuffer;
if (r != null) {
try {
r.close();
} catch (IOException ex) {
// ignore
}
}
}
// bnevins May 17 -- perhaps we should have a NodeAgentDirs ???
dasPropertiesFile = new File(parentDir, "agent/config/das.properties");
valid = true;
}
public final String getServerName() {
if(!valid)
return null;
return serverName;
}
/**
* Return a message suitable for printing, not just for errors.
* @return
*/
public final String deletePidFile() {
if(!valid)
return "Internal Error: ServerDirs is in an invalid state";
if(!pidFile.isFile())
return null;
String message = "pid file " + pidFile + " exists, removing it.";
if(!pidFile.delete()) {
// Hmmm... can't delete it, don't use it
// TODO -- try to go inside it and delete the contents
return message + " Couldn't remove pid file";
}
return message;
}
public ServerDirs refresh
() throws IOException {
return new ServerDirs(serverDir);
}
// getters & setters section below
public final File getServerDir() {
if(!valid)
return null;
return serverDir;
}
public final File getServerParentDir() {
if(!valid)
return null;
return parentDir;
}
public final File getServerGrandParentDir() {
if(!valid)
return null;
return grandParentDir;
}
public final File getDomainXml() {
if(!valid)
return null;
return domainXml;
}
public final File getConfigDir() {
if(!valid)
return null;
return configDir;
}
public final File getPidFile() {
if(!valid)
return null;
return pidFile;
}
public final File getDasPropertiesFile() {
return dasPropertiesFile;
}
public String getLocalPassword() {
return localPassword;
}
public final File getLocalPasswordFile() {
if (!valid)
return null;
return localPasswordFile;
}
public final boolean isValid() {
return valid;
}
///////////////////////////////////////////////////////////////////////////
/////////// All Private Below /////////////////////////
///////////////////////////////////////////////////////////////////////////
private final String serverName;
private final File serverDir;
private final File parentDir;
private final File grandParentDir;
private final File configDir;
private final File domainXml;
private final File pidFile;
private final boolean valid;
private final String localPassword;
private final File localPasswordFile;
private final File dasPropertiesFile; // this only makes sense for instances...
// Can be shared among classes in the package
static final LocalStringsImpl strings = new LocalStringsImpl(ServerDirs.class);
// root-dir/config/domain.xml
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy