001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.input;
017:
018: import java.io.File;
019:
020: import org.tp23.antinstaller.InstallerContext;
021: import org.tp23.antinstaller.ValidationException;
022: import org.tp23.antinstaller.renderer.AIResourceBundle;
023: import org.tp23.antinstaller.renderer.MessageRenderer;
024:
025: /**
026: *
027: * <p>Input type to select a directory and validate it by checking the existence of
028: * files relative to the directory selected. An Expected use for this is to find the
029: * Application root of an exiting app on the clients machine. for example to find
030: * Tomcat, ask the user to select the tomcat root and check the existence of ./conf/tomcat-users.xml
031: * and ./webapps </p>
032: * @author Paul Hinds
033: * @version $Id: AppRootInput.java,v 1.2 2005/10/23 14:39:20 teknopaul Exp $
034: */
035: public class AppRootInput extends DirectoryInput {
036:
037: private static final AIResourceBundle res = new AIResourceBundle();
038:
039: private String checkFile1;
040: private String checkFile2;
041: private String checkDir1;
042: private String checkDir2;
043:
044: public AppRootInput() {
045: }
046:
047: /**
048: * Called to validate the user input
049: */
050: public boolean validate(InstallerContext cxt)
051: throws ValidationException {
052: if (getInputResult() == null)
053: return false;
054: MessageRenderer mr = cxt.getMessageRenderer();
055: String directorySelected = getInputResult();
056: File file = new File(directorySelected);
057: // removed in response to BUG:1303230
058: // if(!file.exists()){
059: // if(mr.prompt(res.getString("dir.not.exist.create"))){
060: // boolean ok = file.mkdirs();
061: // if(!ok)mr.printMessage(res.getString("dir.not.created"));
062: // }
063: // }
064: if (!file.isDirectory()) {
065: mr.printMessage(res.getString("dir.not.exist") + ":"
066: + file.getAbsolutePath());
067: return false;
068: } else if (checkFile1 != null
069: && !checkExists(mr, file, checkFile1)) {
070: return false;
071: } else if (checkFile2 != null
072: && !checkExists(mr, file, checkFile2)) {
073: return false;
074: } else if (checkDir1 != null
075: && !checkExists(mr, file, checkDir1)) {
076: return false;
077: } else if (checkDir2 != null
078: && !checkExists(mr, file, checkDir2)) {
079: return false;
080: }
081: return true;
082: }
083:
084: private boolean checkExists(MessageRenderer mr, File root,
085: String check) {
086: File checkFile = new File(root, check);
087: if (!checkFile.exists()) {
088: reportMissing(mr, checkFile);
089: return false;
090: }
091: return true;
092: }
093:
094: private void reportMissing(MessageRenderer mr, File missing) {
095: StringBuffer message = new StringBuffer();
096: message.append(res.getString("app.root.invalid"));
097: message.append(System.getProperty("line.separator"));
098: if (missing.isDirectory()) {
099: message.append(res.getString("dir.not.exist"));
100: } else {
101: message.append(res.getString("file.not.exist"));
102: }
103: message.append(":");
104: message.append(missing.getAbsolutePath());
105: mr.printMessage(message.toString());
106: }
107:
108: public String getCheckDir1() {
109: return checkDir1;
110: }
111:
112: public String getCheckDir2() {
113: return checkDir2;
114: }
115:
116: public String getCheckFile1() {
117: return checkFile1;
118: }
119:
120: public String getCheckFile2() {
121: return checkFile2;
122: }
123:
124: public void setCheckFile2(String checkFile2) {
125: this .checkFile2 = checkFile2;
126: }
127:
128: public void setCheckFile1(String checkFile1) {
129: this .checkFile1 = checkFile1;
130: }
131:
132: public void setCheckDir2(String checkDir2) {
133: this .checkDir2 = checkDir2;
134: }
135:
136: public void setCheckDir1(String checkDir1) {
137: this .checkDir1 = checkDir1;
138: }
139:
140: /**
141: * Used by checkConfig to validate the configuration file.
142: * Not used at runtime.
143: * @return boolean
144: */
145: public boolean validateObject() {
146: if (getDisplayText() == null) {
147: System.out.println("AppRoot:displayText must be set");
148: return false;
149: }
150: if (getProperty() == null) {
151: System.out.println("AppRoot:property must be set");
152: return false;
153: }
154: if (getDefaultValue() == null) {
155: System.out.println("AppRoot:defaultValue must be set");
156: return false;
157: }
158: return true;
159: }
160:
161: }
|