View Javadoc

1   package org.wcb.installs;
2   
3   import java.io.IOException;
4   import org.wcb.installs.OSSystem;
5   import java.util.Vector;
6   
7   public class UnixSystem extends OSSystem{
8          
9       private static String homeInstallString = "/usr/local/java/jars";
10      private static String homeJarString = "/usr/local/java/jars";
11  
12      /***
13       * Accessor to allow the user to specify the location
14       * they want to install the program binaries.
15       */
16      public void setInstallDirectory(String binDir){
17  	homeInstallString = binDir;
18      }
19  
20      /***
21       * Acessor to set the jar file location and check if
22       * the necessary jars exist in that directory.
23       */
24      public Vector setJarDirectory(String jarDir){
25  	homeJarString = jarDir;
26          return jarFilesExist(homeJarString);
27      }
28  
29      /***
30       * Takes the binary directory string and
31       * Starts creating the necessary directories so that
32       * files can be written into them.
33       */
34      public void makeFile(String homeBinString){
35  	if(!directoryExists(homeBinString)) {
36  	    if(!createNewDirectory(homeBinString)) 
37  		System.out.println("Failed to bin directory");
38  	    else {
39  		writeShellScript(homeBinString);
40  		changeToExecutable(homeBinString,"jhome");
41  		writeServerScript(homeBinString);
42  		changeToExecutable(homeBinString,"HAserver");
43  
44  	    }
45  	} else {
46  	    writeShellScript(homeBinString);
47  	    changeToExecutable(homeBinString,"jhome");
48  	    writeServerScript(homeBinString);
49  	    changeToExecutable(homeBinString,"HAserver");
50  	}
51      }
52  
53      private void writeShellScript(String homeBinString){
54  	StringBuffer sb = new StringBuffer("#!/bin/sh"+LINE_SEP);
55  	sb.append("exec ${JAVA-java} ${JHOME} -classpath \"${CLASSPATH}:");
56  	sb.append(homeInstallString+SEP+"alice.jar:");
57  	sb.append(homeJarString+SEP+"comm.jar:");
58  	sb.append(homeJarString+SEP+"jcl.jar\" ");
59  	sb.append("org.wcb.autohome.Main $@"+LINE_SEP);
60  	sb.append("echo Cannot find ${JAVA-java}. make sure its directory appears in your PATH environment variable.");
61  	writeFile(sb.toString(), homeBinString+SEP+"jhome");
62      }  
63  
64      private void writeServerScript(String homeBinString){
65  	StringBuffer sb = new StringBuffer("#!/bin/sh"+LINE_SEP);
66  	sb.append("exec ${JAVA-java} ${JHOME} -classpath \"${CLASSPATH}:");
67  	sb.append(homeInstallString+SEP+"alice.jar:");
68  	sb.append(homeJarString+SEP+"comm.jar:");
69  	sb.append(homeJarString+SEP+"jcl.jar\" ");
70  	sb.append("org.wcb.autohome.JHomeServer "+homeInstallString+SEP+"alice.jar $@"+LINE_SEP);
71  	sb.append("echo Cannot find ${JAVA-java}. make sure its directory appears in your PATH environment variable.");
72  	writeFile(sb.toString(), homeBinString+SEP+"HAserver");
73      }
74  
75      /***
76       * Takes the bin directory of the application execution script
77       * and changes it to an executable type file.
78       */
79      private void changeToExecutable(String homeBin, String filename){
80  	String cmd[] = new String[3];
81  	cmd[0] = new String("chmod");
82  	cmd[1] = new String("755");
83  	cmd[2] = new String(homeBin+SEP+filename); 
84  	try {
85  	    Runtime r = Runtime.getRuntime();	    
86  	    Process p = r.exec(cmd);	  
87  	}catch (IOException err) {
88  	    System.err.println("Failed to change to executable:"+err);
89  	    //Toolkit.getDefaultToolkit().beep();
90  	}
91      }
92  }
93