KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > channel > RRStateFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.server.core.channel;
24
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import com.sun.enterprise.util.io.FileUtils;
30 import com.sun.enterprise.util.SystemPropertyConstants;
31
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import com.sun.enterprise.util.i18n.StringManager;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Responsible for persisting restart required state.
40  *
41  * @author Nazrul Islam
42  * @since JDK1.4
43  */

44 public class RRStateFactory {
45
46     /**
47      * Saves the restart required state of the server instance.
48      *
49      * @param state restart required state
50      * @throws IOException if an i/o error
51      */

52     public static void saveState(boolean state) throws IOException JavaDoc {
53
54         File JavaDoc stateFile = getStateFile(null);
55         FileWriter JavaDoc fw = new FileWriter JavaDoc(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 JavaDoc e) {}
64             }
65         }
66     }
67
68     /**
69      * Reads the persisted server instance's restart required state.
70      * If the state file is not present, it returns false, i.e.,
71      * server instance does not require a restart.
72      *
73      * This uses current server's instance root.
74      *
75      * @return restart required state
76      */

77     public static boolean getState() {
78         return getState(null);
79     }
80
81     /**
82      * Reads the persisted server instance's restart required state.
83      * If the state file is not present, it returns false, i.e.,
84      * server instance does not require a restart.
85      *
86      * @param instanceRoot server instance root
87      * @return restart required state
88      */

89     public static boolean getState(String JavaDoc instanceRoot) {
90
91         boolean restartNeeded = false;
92         File JavaDoc f = getStateFile(instanceRoot);
93
94         if (f.exists()) {
95             BufferedReader JavaDoc br = null;
96             try {
97                 br = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
98                 String JavaDoc state = br.readLine();
99                 restartNeeded = new Boolean JavaDoc(state.trim()).booleanValue();
100             } catch (IOException JavaDoc ioe) {
101             } finally {
102                 if (br != null) {
103                     try {
104                         br.close();
105                     } catch (IOException JavaDoc e) {}
106                 }
107             }
108         }
109
110         return restartNeeded;
111     }
112
113     /**
114      * Removes the restart required state file.
115      */

116     public static void removeStateFile() {
117         File JavaDoc state = getStateFile(null);
118         if (state.exists()) {
119             FileUtils.liquidate(state);
120         }
121     }
122
123     /**
124      * Returns the state file handle.
125      *
126      * @param iRoot instance root
127      * @return the state file
128      */

129     private static File JavaDoc getStateFile(String JavaDoc instanceRoot) {
130
131         // instance root
132
if (instanceRoot == null) {
133             instanceRoot = System.getProperty(
134                 SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, DEF_LOCATION);
135         }
136
137         // state file
138
File JavaDoc f = new File JavaDoc(instanceRoot + File.separator + STATE_FILE_NM);
139
140         return f;
141     }
142
143     public static void main(String JavaDoc[] 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 JavaDoc e) {
152             e.printStackTrace();
153         }
154     }
155
156     // ---- INSTANCE VARIABLE(S) - PRIVATE ------------------------------
157
private static final String JavaDoc STATE_FILE_NM = ".restart-required-state";
158     private static final String JavaDoc DEF_LOCATION = ".";
159 }
160
Popular Tags