1 22 package org.jboss.console.manager; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.system.ServiceMBeanSupport; 33 import org.jboss.system.server.ServerConfig; 34 import org.jboss.system.server.ServerConfigLocator; 35 36 50 public class DeploymentFileRepository extends ServiceMBeanSupport 51 implements DeploymentFileRepositoryMBean 52 { 53 private String baseDir; 54 private File base; 55 56 57 protected File serverHome; 58 59 68 public void store(String folder, String name, String fileExtension, String data, boolean noHotDeploy) throws IOException 69 { 70 log.debug("store called"); 71 File 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 ("Failed to create directory: " + dir.toString()); 79 } 80 } 81 String filename = name.replace(' ', '_') + fileExtension; 82 File file = getFile(dir, filename); 83 84 File tmpfile = new File (dir, filename + ".tmp"); 85 PrintWriter writer = new PrintWriter (new FileOutputStream (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 ("Failed to rename tmpfile to " + file.toString()); 98 } 99 } 100 101 public void remove(String folder, String name, String fileExtension) throws IOException 102 { 103 File dir = getFile(base, folder); 104 String filename = name.replace(' ', '_') + fileExtension; 105 File file = getFile(dir, filename); 106 file.delete(); 107 } 108 109 public boolean isStored(String folder, String name, String fileExtension) throws IOException 110 { 111 File dir = getFile(base, folder); 112 String filename = name.replace(' ', '_') + fileExtension; 113 File file = getFile(dir, filename); 114 return file.exists(); 115 } 116 117 public String getBaseDir() 118 { 119 return baseDir; 120 } 121 122 public void setBaseDir(String baseDir) throws IOException 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 preRegister(MBeanServer server, ObjectName name) throws Exception 131 { 132 ServerConfig serverConfig = ServerConfigLocator.locate(); 135 serverHome = serverConfig.getServerHomeDir(); 136 return super.preRegister(server, name); 137 } 138 139 146 private File getFile(File parent, String child) throws IOException 147 { 148 File childFile = new File (parent, child); 149 150 if (childFile.getCanonicalPath().indexOf(parent.getCanonicalPath()) != 0) 151 throw new IllegalArgumentException ("child '" + child + "' should be a child of parent '" + parent + "'"); 152 153 return childFile; 154 } 155 } 156 | Popular Tags |