1 23 package com.sun.enterprise.admin.server.core.channel; 24 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.FileReader ; 28 import java.io.BufferedReader ; 29 import com.sun.enterprise.util.io.FileUtils; 30 import com.sun.enterprise.util.SystemPropertyConstants; 31 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import com.sun.enterprise.util.i18n.StringManager; 35 36 import java.io.IOException ; 37 38 44 public class RRStateFactory { 45 46 52 public static void saveState(boolean state) throws IOException { 53 54 File stateFile = getStateFile(null); 55 FileWriter fw = new FileWriter (stateFile); 56 try { 57 fw.write(Boolean.toString(state)); 58 fw.flush(); 59 } finally { 60 if (fw != null) { 61 try { 62 fw.close(); 63 } catch (IOException e) {} 64 } 65 } 66 } 67 68 77 public static boolean getState() { 78 return getState(null); 79 } 80 81 89 public static boolean getState(String instanceRoot) { 90 91 boolean restartNeeded = false; 92 File f = getStateFile(instanceRoot); 93 94 if (f.exists()) { 95 BufferedReader br = null; 96 try { 97 br = new BufferedReader (new FileReader (f)); 98 String state = br.readLine(); 99 restartNeeded = new Boolean (state.trim()).booleanValue(); 100 } catch (IOException ioe) { 101 } finally { 102 if (br != null) { 103 try { 104 br.close(); 105 } catch (IOException e) {} 106 } 107 } 108 } 109 110 return restartNeeded; 111 } 112 113 116 public static void removeStateFile() { 117 File state = getStateFile(null); 118 if (state.exists()) { 119 FileUtils.liquidate(state); 120 } 121 } 122 123 129 private static File getStateFile(String instanceRoot) { 130 131 if (instanceRoot == null) { 133 instanceRoot = System.getProperty( 134 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, DEF_LOCATION); 135 } 136 137 File f = new File (instanceRoot + File.separator + STATE_FILE_NM); 139 140 return f; 141 } 142 143 public static void main(String [] args) { 144 try { 145 System.setProperty( 146 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, "/tmp"); 147 saveState(true); 148 boolean state = getState(); 149 System.out.println(state); 150 removeStateFile(); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 } 155 156 private static final String STATE_FILE_NM = ".restart-required-state"; 158 private static final String DEF_LOCATION = "."; 159 } 160 | Popular Tags |