1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.util.ResourceBundle;
|
---|
20 |
|
---|
21 | import org.tp23.antinstaller.InstallerContext;
|
---|
22 | import org.tp23.antinstaller.ValidationException;
|
---|
23 | import org.tp23.antinstaller.renderer.MessageRenderer;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | *
|
---|
27 | * <p>Input type to select a directory and validate it by checking the existence of
|
---|
28 | * files relative to the directory selected. An Expected use for this is to find the
|
---|
29 | * Application root of an exiting app on the clients machine. for example to find
|
---|
30 | * Tomcat, ask the user to select the tomcat root and check the existence of ./conf/tomcat-users.xml
|
---|
31 | * and ./webapps </p>
|
---|
32 | * @author Paul Hinds
|
---|
33 | * @version $Id: AppRootInput.java,v 1.2 2005/10/23 14:39:20 teknopaul Exp $
|
---|
34 | */
|
---|
35 | public class AppRootInput
|
---|
36 | extends DirectoryInput {
|
---|
37 |
|
---|
38 | private String checkFile1;
|
---|
39 | private String checkFile2;
|
---|
40 | private String checkDir1;
|
---|
41 | private String checkDir2;
|
---|
42 |
|
---|
43 | public AppRootInput() {
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Called to validate the user input
|
---|
48 | */
|
---|
49 | public boolean validate(InstallerContext cxt) throws ValidationException{
|
---|
50 | if (getInputResult() == null)return false;
|
---|
51 | MessageRenderer mr = cxt.getMessageRenderer();
|
---|
52 | String directorySelected = getInputResult();
|
---|
53 | File file = new File(directorySelected);
|
---|
54 | // removed in response to BUG:1303230
|
---|
55 | // if(!file.exists()){
|
---|
56 | // if(mr.prompt(res.getString("dirNotExistCreate"))){
|
---|
57 | // boolean ok = file.mkdirs();
|
---|
58 | // if(!ok)mr.printMessage(res.getString("dirNotCreated"));
|
---|
59 | // }
|
---|
60 | // }
|
---|
61 | if(!file.isDirectory()){
|
---|
62 | mr.printMessage(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist")+":"+file.getAbsolutePath());
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 | else if(checkFile1!=null && ! checkExists(mr,file,checkFile1)){
|
---|
66 | return false ;
|
---|
67 | }
|
---|
68 | else if(checkFile2!=null && ! checkExists(mr,file,checkFile2)){
|
---|
69 | return false ;
|
---|
70 | }
|
---|
71 | else if(checkDir1!=null && ! checkExists(mr,file,checkDir1)){
|
---|
72 | return false ;
|
---|
73 | }
|
---|
74 | else if(checkDir2!=null && ! checkExists(mr,file,checkDir2)){
|
---|
75 | return false ;
|
---|
76 | }
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private boolean checkExists(MessageRenderer mr,File root,String check){
|
---|
81 | File checkFile = new File(root,checkFile1);
|
---|
82 | if(!checkFile.exists()){
|
---|
83 | reportMissing(mr,checkFile);
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void reportMissing(MessageRenderer mr,File missing){
|
---|
90 | StringBuffer message = new StringBuffer();
|
---|
91 | message.append(org.tp23.antinstaller.Installer.langPack.getString("appRootInvalid"));
|
---|
92 | message.append(System.getProperty("line.separator"));
|
---|
93 | if(missing.isDirectory()){
|
---|
94 | message.append(org.tp23.antinstaller.Installer.langPack.getString("dirNotExist"));
|
---|
95 | }
|
---|
96 | else{
|
---|
97 | message.append(org.tp23.antinstaller.Installer.langPack.getString("fileNotExist"));
|
---|
98 | }
|
---|
99 | message.append(":");
|
---|
100 | message.append(missing.getAbsolutePath());
|
---|
101 | mr.printMessage(message.toString());
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | public String getCheckDir1() {
|
---|
107 | return checkDir1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public String getCheckDir2() {
|
---|
111 | return checkDir2;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public String getCheckFile1() {
|
---|
115 | return checkFile1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public String getCheckFile2() {
|
---|
119 | return checkFile2;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void setCheckFile2(String checkFile2) {
|
---|
123 | this.checkFile2 = checkFile2;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void setCheckFile1(String checkFile1) {
|
---|
127 | this.checkFile1 = checkFile1;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void setCheckDir2(String checkDir2) {
|
---|
131 | this.checkDir2 = checkDir2;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void setCheckDir1(String checkDir1) {
|
---|
135 | this.checkDir1 = checkDir1;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Used by checkConfig to validate the configuration file.
|
---|
140 | * Not used at runtime.
|
---|
141 | * @return boolean
|
---|
142 | */
|
---|
143 | public boolean validateObject() {
|
---|
144 | if(getDisplayText()==null){
|
---|
145 | System.out.println("AppRoot:displayText must be set");
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | if(getProperty()==null){
|
---|
149 | System.out.println("AppRoot:property must be set");
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | if(getDefaultValue()==null){
|
---|
153 | System.out.println("AppRoot:defaultValue must be set");
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | }
|
---|