001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.console.manager;
023:
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.PrintWriter;
028:
029: import javax.management.MBeanServer;
030: import javax.management.ObjectName;
031:
032: import org.jboss.system.ServiceMBeanSupport;
033: import org.jboss.system.server.ServerConfig;
034: import org.jboss.system.server.ServerConfigLocator;
035:
036: /**
037: * This class wraps the file system
038: * for deployments. It gives a file-based
039: * persistence mechanism for deployments.
040: * Used by web-console to store -service.xml files,
041: * -ds.xml files, etc..., really anything text based.
042: *
043: * Deployments are tied to a specific name and that name
044: * corresponds to the base file name.
045: *
046: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
047: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
048: * @version $Revision: 58459 $
049: */
050: public class DeploymentFileRepository extends ServiceMBeanSupport
051: implements DeploymentFileRepositoryMBean {
052: private String baseDir;
053: private File base;
054:
055: /** The server's home directory, for relative paths. */
056: protected File serverHome;
057:
058: /**
059: *
060: * @param folder relative directory
061: * @param name base name of file. Whitespace will be removed from name and replaced with '_'
062: * @param fileExtension must have a '.' in ext
063: * @param data
064: * @param noHotDeploy keep timestamp of file so it doesn't do a redeploy
065: * @throws IOException
066: */
067: public void store(String folder, String name, String fileExtension,
068: String data, boolean noHotDeploy) throws IOException {
069: log.debug("store called");
070: File dir = getFile(base, folder);
071: log.debug("repository folder: " + dir.toString());
072: log.debug("absolute: " + dir.getAbsolutePath());
073: if (!dir.exists()) {
074: if (!dir.mkdirs()) {
075: throw new RuntimeException(
076: "Failed to create directory: " + dir.toString());
077: }
078: }
079: String filename = name.replace(' ', '_') + fileExtension;
080: File file = getFile(dir, filename);
081:
082: File tmpfile = new File(dir, filename + ".tmp");
083: PrintWriter writer = new PrintWriter(new FileOutputStream(
084: tmpfile));
085: writer.write(data);
086: writer.close();
087:
088: if (file.exists() && noHotDeploy) {
089: long modified = file.lastModified();
090: tmpfile.setLastModified(modified);
091: file.delete();
092: }
093: if (!tmpfile.renameTo(file)) {
094: throw new RuntimeException("Failed to rename tmpfile to "
095: + file.toString());
096: }
097: }
098:
099: public void remove(String folder, String name, String fileExtension)
100: throws IOException {
101: File dir = getFile(base, folder);
102: String filename = name.replace(' ', '_') + fileExtension;
103: File file = getFile(dir, filename);
104: file.delete();
105: }
106:
107: public boolean isStored(String folder, String name,
108: String fileExtension) throws IOException {
109: File dir = getFile(base, folder);
110: String filename = name.replace(' ', '_') + fileExtension;
111: File file = getFile(dir, filename);
112: return file.exists();
113: }
114:
115: public String getBaseDir() {
116: return baseDir;
117: }
118:
119: public void setBaseDir(String baseDir) throws IOException {
120: this .base = getFile(serverHome, baseDir);
121: this .baseDir = baseDir;
122:
123: log.debug("BaseDir set to: " + this .base);
124: }
125:
126: public ObjectName preRegister(MBeanServer server, ObjectName name)
127: throws Exception {
128: // get server's home for relative paths, need this for setting
129: // attribute final values, so we need to do it here
130: ServerConfig serverConfig = ServerConfigLocator.locate();
131: serverHome = serverConfig.getServerHomeDir();
132: return super .preRegister(server, name);
133: }
134:
135: /**
136: * Wrap the File(File parent, String child) CTOR to make sure the
137: * resulting child is indeed under the parent hierarchy,
138: * i.e. don't allow a ../../../rogue-child.txt
139: *
140: * see JBAS-3861
141: */
142: private File getFile(File parent, String child) throws IOException {
143: File childFile = new File(parent, child);
144:
145: if (childFile.getCanonicalPath().indexOf(
146: parent.getCanonicalPath()) != 0)
147: throw new IllegalArgumentException("child '" + child
148: + "' should be a child of parent '" + parent + "'");
149:
150: return childFile;
151: }
152: }
|