001: package com.jat.core.installer.step;
002:
003: import java.io.File;
004: import java.util.Enumeration;
005: import java.util.Vector;
006:
007: import com.jat.core.config.Config;
008: import com.jat.util.file.FileUtil;
009: import java.io.IOException;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description:</p>
014: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
015: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
016: * @author stf
017: * @version 1.1
018: * @since 1.2
019: */
020:
021: public class JatProject {
022:
023: private String projectDir;
024: private String projectName;
025:
026: public JatProject(String dir, String projectName) throws Exception {
027: this .projectDir = dir;
028: this .projectName = projectName;
029: createProject();
030: }
031:
032: public static String getFilename(String path, String name) {
033: return path
034: + (path.endsWith("/") || path.endsWith("\\") ? ""
035: : File.separator) + name;
036: }
037:
038: protected String getProjectDir() {
039: return getFilename(this .projectDir, this .projectName);
040: }
041:
042: protected String getConfigDirForProject() {
043: return getFilename(getProjectDir(), "config");
044: }
045:
046: private void createProject() throws Exception {
047: this .createPath(this .getProjectDir());
048: this .createPath(this .getConfigDirForProject());
049: }
050:
051: private void createPath(String path) throws Exception {
052: File dir = new File(path);
053: if (dir.exists()) {
054: deleteContentsOf(dir);
055: } else {
056: dir.mkdirs();
057: }
058: }
059:
060: private void deleteContentsOf(File file) throws IOException {
061: FileUtil.deleteDirectory(file);
062: }
063:
064: public void copyConfigFiles(String configDir, String configFileName)
065: throws Exception {
066: String configFile = getFilename(configDir, configFileName);
067: Config config = Config.init(configFile);
068: Vector configFiles = config.getSubValues("config", "config");
069: config.addFiles(configFiles);
070: Config principleConfig = null;
071: Vector files = new Vector();
072: for (Enumeration e = config.configNames(); e.hasMoreElements();) {
073: String origConfName = (String) e.nextElement();
074: Config conf = config.getConfig(origConfName);
075: if (origConfName.equals(configFileName)) {
076: principleConfig = conf;
077: } else {
078: String newName = getFilename(this
079: .getConfigDirForProject(), origConfName);
080: FileUtil.copy(conf, new File(newName), true);
081: files.addElement(newName);
082: }
083: }
084: if (principleConfig == null) {
085: throw new Exception(
086: "Technical error: principle config not found");
087: }
088: principleConfig.removeSection("config");
089: principleConfig.addSection("config");
090: for (int i = 0; i < files.size(); i++) {
091: principleConfig.addValue("config", "config" + (i + 1),
092: (String) files.elementAt(i));
093: }
094: principleConfig.addValue("log", "path", getFilename(this
095: .getProjectDir(), "log"));
096: String newName = getFilename(this.getConfigDirForProject(),
097: configFileName);
098: principleConfig.save(newName);
099: }
100: }
|