KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > manager > DeploymentFileRepository


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.console.manager;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.system.ServiceMBeanSupport;
33 import org.jboss.system.server.ServerConfig;
34 import org.jboss.system.server.ServerConfigLocator;
35
36 /**
37  * This class wraps the file system
38  * for deployments. It gives a file-based
39  * persistence mechanism for deployments.
40  * Used by web-console to store -service.xml files,
41  * -ds.xml files, etc..., really anything text based.
42  *
43  * Deployments are tied to a specific name and that name
44  * corresponds to the base file name.
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
48  * @version $Revision: 58460 $
49  */

50 public class DeploymentFileRepository extends ServiceMBeanSupport
51    implements DeploymentFileRepositoryMBean
52 {
53    private String JavaDoc baseDir;
54    private File JavaDoc base;
55    
56    /** The server's home directory, for relative paths. */
57    protected File JavaDoc serverHome;
58
59    /**
60     *
61     * @param folder relative directory
62     * @param name base name of file. Whitespace will be removed from name and replaced with '_'
63     * @param fileExtension must have a '.' in ext
64     * @param data
65     * @param noHotDeploy keep timestamp of file so it doesn't do a redeploy
66     * @throws IOException
67     */

68    public void store(String JavaDoc folder, String JavaDoc name, String JavaDoc fileExtension, String JavaDoc data, boolean noHotDeploy) throws IOException JavaDoc
69    {
70       log.debug("store called");
71       File JavaDoc dir = getFile(base, folder);
72       log.debug("repository folder: " + dir.toString());
73       log.debug("absolute: " + dir.getAbsolutePath());
74       if (!dir.exists())
75       {
76          if (!dir.mkdirs())
77          {
78             throw new RuntimeException JavaDoc("Failed to create directory: " + dir.toString());
79          }
80       }
81       String JavaDoc filename = name.replace(' ', '_') + fileExtension;
82       File JavaDoc file = getFile(dir, filename);
83       
84       File JavaDoc tmpfile = new File JavaDoc(dir, filename + ".tmp");
85       PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(tmpfile));
86       writer.write(data);
87       writer.close();
88       
89       if (file.exists() && noHotDeploy)
90       {
91          long modified = file.lastModified();
92          tmpfile.setLastModified(modified);
93          file.delete();
94       }
95       if (!tmpfile.renameTo(file))
96       {
97          throw new RuntimeException JavaDoc("Failed to rename tmpfile to " + file.toString());
98       }
99    }
100
101    public void remove(String JavaDoc folder, String JavaDoc name, String JavaDoc fileExtension) throws IOException JavaDoc
102    {
103       File JavaDoc dir = getFile(base, folder);
104       String JavaDoc filename = name.replace(' ', '_') + fileExtension;
105       File JavaDoc file = getFile(dir, filename);
106       file.delete();
107    }
108
109    public boolean isStored(String JavaDoc folder, String JavaDoc name, String JavaDoc fileExtension) throws IOException JavaDoc
110    {
111       File JavaDoc dir = getFile(base, folder);
112       String JavaDoc filename = name.replace(' ', '_') + fileExtension;
113       File JavaDoc file = getFile(dir, filename);
114       return file.exists();
115    }
116
117    public String JavaDoc getBaseDir()
118    {
119       return baseDir;
120    }
121
122    public void setBaseDir(String JavaDoc baseDir) throws IOException JavaDoc
123    {
124       this.base = getFile(serverHome, baseDir);
125       this.baseDir = baseDir;
126       
127       log.debug("BaseDir set to: " + this.base);
128    }
129
130    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
131    {
132       // get server's home for relative paths, need this for setting
133
// attribute final values, so we need to do it here
134
ServerConfig serverConfig = ServerConfigLocator.locate();
135       serverHome = serverConfig.getServerHomeDir();
136       return super.preRegister(server, name);
137    }
138
139    /**
140     * Wrap the File(File parent, String child) CTOR to make sure the
141     * resulting child is indeed under the parent hierarchy,
142     * i.e. don't allow a ../../../rogue-child.txt
143     *
144     * see JBAS-3861
145     */

146    private File JavaDoc getFile(File JavaDoc parent, String JavaDoc child) throws IOException JavaDoc
147    {
148       File JavaDoc childFile = new File JavaDoc(parent, child);
149       
150       if (childFile.getCanonicalPath().indexOf(parent.getCanonicalPath()) != 0)
151          throw new IllegalArgumentException JavaDoc("child '" + child + "' should be a child of parent '" + parent + "'");
152       
153       return childFile;
154    }
155 }
156
Popular Tags