001: package com.jat.core.installer;
002:
003: import java.io.File;
004: import java.io.IOException;
005:
006: import java.awt.Dimension;
007: import java.awt.Toolkit;
008:
009: import com.jat.core.config.Config;
010: import com.jat.core.init.Initable;
011: import com.jat.core.installer.gui.ApplicationFrame;
012: import com.jat.core.installer.step.JatProject;
013: import com.jat.core.installer.step.WebApplication;
014: import com.jat.core.log.LogManager;
015:
016: /**
017: * <p>Title: JAT</p>
018: * <p>Description:</p>
019: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
020: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
021: * @author stf
022: * @version 1.1
023: * @since 1.2
024: */
025:
026: public class Installer implements Initable {
027:
028: private String warFile;
029: private String tempDir;
030: private String configDir;
031: private String configFileName;
032:
033: private String projectName;
034: private String projectDir;
035: private String webAppDir;
036:
037: private boolean packFrame = false;
038:
039: public void setProjectName(String projectName) {
040: this .projectName = projectName;
041: }
042:
043: public void setProjectDir(String projectDir) {
044: this .projectDir = projectDir;
045: }
046:
047: public void setWebAppDir(String webAppDir) {
048: this .webAppDir = webAppDir;
049: }
050:
051: protected void initFields() throws Exception {
052: this .warFile = Config.getCurrent().getValue("static parameter",
053: "warFile");
054: File file = new File(this .warFile);
055: //if (!file.exists()) throw new IOException("File '" + this.warFile + "' does not exists");
056: this .tempDir = Config.getCurrent().getValue("static parameter",
057: "tempDir");
058: this .configDir = Config.getCurrent().getValue(
059: "static parameter", "configDir");
060: file = new File(this .configDir);
061: //if (!file.exists()) throw new IOException("Directory '" + this.configDir + "' does not exists");
062: this .configFileName = Config.getCurrent().getValue(
063: "static parameter", "configFileName");
064: file = new File(JatProject.getFilename(this .configDir,
065: this .configFileName));
066: //if (!file.exists()) throw new IOException("File '" + JatProject.getFilename(this.configDir, this.configFileName) + "' does not exists");
067: this .projectName = Config.getCurrent().getValue("parameter",
068: "projectName");
069: this .projectDir = Config.getCurrent().getValue("parameter",
070: "projectDir");
071: this .webAppDir = Config.getCurrent().getValue("parameter",
072: "webAppDir");
073: }
074:
075: public void init() throws Exception {
076: //System.out.println(System.getProperty("os.name"));
077: try {
078: initFields();
079: boolean gui = false;
080: try {
081: String gm = Config.getCurrent().getValue("general",
082: "graphic_mode");
083: gui = gm.equalsIgnoreCase("on");
084: } catch (Exception ignored) {
085: }
086: if (gui)
087: runGraphicMode();
088: else
089: this .runBatch();
090: } catch (Exception ex) {
091: System.out.println("Installer error: " + ex.getMessage());
092: throw new Exception("Installer error: " + ex);
093: }
094: }
095:
096: public String getWarFilename() {
097: return this .warFile;
098: }
099:
100: public void setWarFilename(String filename) throws IOException {
101: File file = new File(filename);
102: if (!file.exists())
103: throw new IOException("File '" + filename
104: + "' does not exists");
105: this .warFile = filename;
106: }
107:
108: public String getConfigFilename() {
109: return JatProject.getFilename(this .configDir,
110: this .configFileName);
111: }
112:
113: public void setConfigFilename(String filename) throws IOException {
114: File file = new File(filename);
115: if (!file.exists())
116: throw new IOException("File '" + this .warFile
117: + "' does not exists");
118: this .configDir = file.getAbsolutePath().substring(0,
119: file.getAbsolutePath().indexOf(file.getName()));
120: this .configFileName = file.getName();
121: }
122:
123: protected void createWebApplication() throws Exception {
124: WebApplication webApp = WebApplication.getWebApplication();
125: try {
126: webApp.unwar(this .warFile, this .tempDir);
127: String projectConfigPath = JatProject.getFilename(
128: JatProject.getFilename(this .projectDir,
129: this .projectName), "config");
130: webApp.modifyWebXml(this .tempDir, projectConfigPath,
131: this .configFileName);
132: String file = webApp.war(this .tempDir, this .projectName);
133: webApp.moveWar(this .tempDir, file, this .webAppDir);
134: } finally {
135: try {
136: webApp.deleteTemp(this .tempDir);
137: } catch (Exception ex) {
138: LogManager
139: .sendError(this .getClass().getName()
140: + "createWebApplication: unable to remove temp dir '"
141: + tempDir + "': " + ex);
142: }
143: }
144: }
145:
146: protected void createProject() throws Exception {
147: JatProject project = new JatProject(this .projectDir,
148: this .projectName);
149: project.copyConfigFiles(this .configDir, this .configFileName);
150: }
151:
152: public void runBatch() throws Exception {
153: File f = new File(JatProject.getFilename(this .projectDir,
154: this .projectName));
155: if (f.exists())
156: throw new Exception("Directory '" + this .projectName
157: + "' already exists");
158: f = new File(this .webAppDir);
159: if (!f.exists())
160: throw new Exception("Directory '" + f.getAbsolutePath()
161: + "' does not exists");
162: createWebApplication();
163: createProject();
164: try {
165: String res = "Project successfully installed and configured on '"
166: + JatProject.getFilename(this .projectDir,
167: this .projectName)
168: + "' (config) and '"
169: + f.getAbsolutePath() + "' (war archive)";
170:
171: LogManager.sendLog(res);
172: System.out.println(res);
173: } catch (Exception ex) {
174: System.out.println("Project installed. Warning: " + ex);
175: }
176: }
177:
178: private void runGraphicMode() {
179: ApplicationFrame frame = new ApplicationFrame(this ,
180: this .projectName, this .projectDir, this .webAppDir);
181: //Validate frames that have preset sizes
182: //Pack frames that have useful preferred size info, e.g. from their layout
183: if (packFrame) {
184: frame.pack();
185: } else {
186: frame.validate();
187: }
188: //Center the window
189: Dimension screenSize = Toolkit.getDefaultToolkit()
190: .getScreenSize();
191: Dimension frameSize = frame.getSize();
192: if (frameSize.height > screenSize.height) {
193: frameSize.height = screenSize.height;
194: }
195: if (frameSize.width > screenSize.width) {
196: frameSize.width = screenSize.width;
197: }
198: frame.setLocation((screenSize.width - frameSize.width) / 2,
199: (screenSize.height - frameSize.height) / 2);
200: frame.setVisible(true);
201:
202: }
203: }
|