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 */
16package org.tp23.antinstaller.input;
17
18import java.io.File;
19import java.util.ResourceBundle;
20
21import org.tp23.antinstaller.InstallerContext;
22import org.tp23.antinstaller.ValidationException;
23import 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 */
35public class AppRootInput
36    extends DirectoryInput {
37
38    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
39
40
41    private String checkFile1;
42    private String checkFile2;
43    private String checkDir1;
44    private String checkDir2;
45
46    public AppRootInput() {
47    }
48
49    /**
50     * Called to validate the user input
51     */
52    public boolean validate(InstallerContext cxt) throws ValidationException{
53        if (getInputResult() == null)return false;
54        MessageRenderer mr = cxt.getMessageRenderer();
55        String directorySelected = getInputResult();
56        File file = new File(directorySelected);
57        // removed in response to BUG:1303230
58//      if(!file.exists()){
59//          if(mr.prompt(res.getString("dirNotExistCreate"))){
60//              boolean ok = file.mkdirs();
61//              if(!ok)mr.printMessage(res.getString("dirNotCreated"));
62//          }
63//      }
64        if(!file.isDirectory()){
65            mr.printMessage(res.getString("dirNotExist")+":"+file.getAbsolutePath());
66            return false;
67        }
68        else if(checkFile1!=null && ! checkExists(mr,file,checkFile1)){
69            return false ;
70        }
71        else if(checkFile2!=null && ! checkExists(mr,file,checkFile2)){
72            return false ;
73        }
74        else if(checkDir1!=null && ! checkExists(mr,file,checkDir1)){
75            return false ;
76        }
77        else if(checkDir2!=null && ! checkExists(mr,file,checkDir2)){
78            return false ;
79        }
80        return true;
81    }
82
83    private boolean checkExists(MessageRenderer mr,File root,String check){
84        File checkFile = new File(root,checkFile1);
85        if(!checkFile.exists()){
86            reportMissing(mr,checkFile);
87            return false;
88        }
89        return true;
90    }
91
92    private void reportMissing(MessageRenderer mr,File missing){
93        StringBuffer message = new StringBuffer();
94        message.append(res.getString("appRootInvalid"));
95        message.append(System.getProperty("line.separator"));
96        if(missing.isDirectory()){
97            message.append(res.getString("dirNotExist"));
98        }
99        else{
00            message.append(res.getString("fileNotExist"));
01        }
02        message.append(":");
03        message.append(missing.getAbsolutePath());
04        mr.printMessage(message.toString());
05    }
06
07
08
09    public String getCheckDir1() {
10        return checkDir1;
11    }
12
13    public String getCheckDir2() {
14        return checkDir2;
15    }
16
17    public String getCheckFile1() {
18        return checkFile1;
19    }
20
21    public String getCheckFile2() {
22        return checkFile2;
23    }
24
25    public void setCheckFile2(String checkFile2) {
26        this.checkFile2 = checkFile2;
27    }
28
29    public void setCheckFile1(String checkFile1) {
30        this.checkFile1 = checkFile1;
31    }
32
33    public void setCheckDir2(String checkDir2) {
34        this.checkDir2 = checkDir2;
35    }
36
37    public void setCheckDir1(String checkDir1) {
38        this.checkDir1 = checkDir1;
39    }
40    
41    /**
42     * Used by checkConfig to validate the configuration file.
43     * Not used at runtime.
44     * @return boolean
45     */
46    public boolean validateObject() {
47        if(getDisplayText()==null){
48            System.out.println("AppRoot:displayText must be set");
49            return false;
50        }
51        if(getProperty()==null){
52            System.out.println("AppRoot:property must be set");
53            return false;
54        }
55        if(getDefaultValue()==null){
56            System.out.println("AppRoot:defaultValue must be set");
57            return false;
58        }
59        return true;
60    }
61
62}
63