KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > monitor > FileMonitor


1 /*
2
3    Derby - Class org.apache.derby.impl.services.monitor.FileMonitor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.monitor;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25 import org.apache.derby.iapi.reference.Property;
26
27 import org.apache.derby.iapi.services.io.FileUtil;
28 import org.apache.derby.iapi.services.info.ProductVersionHolder;
29 import org.apache.derby.iapi.services.info.ProductGenusNames;
30
31 import java.io.FileInputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 import java.net.URL JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /**
41     Implementation of the monitor that uses the class loader
42     that the its was loaded in for all class loading.
43
44 */

45
46 public final class FileMonitor extends BaseMonitor implements java.security.PrivilegedExceptionAction JavaDoc
47 {
48
49     /* Fields */
50     private File JavaDoc home;
51
52     private ProductVersionHolder engineVersion;
53
54     public FileMonitor() {
55         initialize(true);
56         applicationProperties = readApplicationProperties();
57     }
58
59     public FileMonitor(java.util.Properties JavaDoc properties, java.io.PrintStream JavaDoc log) {
60         runWithState(properties, log);
61     }
62
63
64
65     private InputStream JavaDoc PBapplicationPropertiesStream()
66       throws IOException JavaDoc {
67
68         File JavaDoc sr = FileUtil.newFile(home, Property.PROPERTIES_FILE);
69
70         if (!sr.exists())
71             return null;
72
73         return new FileInputStream JavaDoc(sr);
74     }
75
76     public Object JavaDoc getEnvironment() {
77         return home;
78     }
79
80
81
82     /**
83         SECURITY WARNING.
84
85         This method is run in a privileged block in a Java 2 environment.
86
87         Set the system home directory. Returns false if it couldn't for
88         some reason.
89
90     **/

91     private boolean PBinitialize(boolean lite)
92     {
93         if (!lite) {
94             try {
95                 // Create a ThreadGroup and set the daemon property to
96
// make sure the group is destroyed and garbage
97
// collected when all its members have finished (i.e.,
98
// when the driver is unloaded).
99
daemonGroup = new ThreadGroup JavaDoc("derby.daemons");
100                 daemonGroup.setDaemon(true);
101             } catch (SecurityException JavaDoc se) {
102             }
103         }
104
105         InputStream JavaDoc versionStream = getClass().getResourceAsStream(ProductGenusNames.DBMS_INFO);
106
107         engineVersion = ProductVersionHolder.getProductVersionHolderFromMyEnv(versionStream);
108
109         String JavaDoc systemHome;
110         // create the system home directory if it doesn't exist
111
try {
112             // SECURITY PERMISSION - OP2
113
systemHome = System.getProperty(Property.SYSTEM_HOME_PROPERTY);
114         } catch (SecurityException JavaDoc se) {
115             // system home will be the current directory
116
systemHome = null;
117         }
118
119         if (systemHome != null) {
120             home = new File JavaDoc(systemHome);
121
122             // SECURITY PERMISSION - OP2a
123
if (home.exists()) {
124                 if (!home.isDirectory()) {
125                     report(Property.SYSTEM_HOME_PROPERTY + "=" + systemHome
126                         + " does not represent a directory");
127                     return false;
128                 }
129             } else if (!lite) {
130
131                 try {
132                     // SECURITY PERMISSION - OP2b
133
home.mkdirs();
134                 } catch (SecurityException JavaDoc se) {
135                     return false;
136                 }
137             }
138         }
139
140         return true;
141     }
142
143     /**
144         SECURITY WARNING.
145
146         This method is run in a privileged block in a Java 2 environment.
147
148         Return a property from the JVM's system set.
149         In a Java2 environment this will be executed as a privileged block
150         if and only if the property starts with 'derby.'.
151         If a SecurityException occurs, null is returned.
152     */

153     private String JavaDoc PBgetJVMProperty(String JavaDoc key) {
154
155         try {
156             // SECURITY PERMISSION - OP1
157
return System.getProperty(key);
158         } catch (SecurityException JavaDoc se) {
159             return null;
160         }
161     }
162
163     /*
164     ** Priv block code, moved out of the old Java2 version.
165     */

166
167     private int action;
168     private String JavaDoc key3;
169     private Runnable JavaDoc task;
170     private int intValue;
171
172     /**
173         Initialize the system in a privileged block.
174     **/

175     synchronized final boolean initialize(boolean lite)
176     {
177         action = lite ? 0 : 1;
178         try {
179             Object JavaDoc ret = java.security.AccessController.doPrivileged(this);
180
181             return ((Boolean JavaDoc) ret).booleanValue();
182         } catch (java.security.PrivilegedActionException JavaDoc pae) {
183             throw (RuntimeException JavaDoc) pae.getException();
184         }
185     }
186
187     synchronized final Properties JavaDoc getDefaultModuleProperties() {
188         action = 2;
189         try {
190             return (Properties JavaDoc) java.security.AccessController.doPrivileged(this);
191         } catch (java.security.PrivilegedActionException JavaDoc pae) {
192            throw (RuntimeException JavaDoc) pae.getException();
193         }
194     }
195
196     public synchronized final String JavaDoc getJVMProperty(String JavaDoc key) {
197         if (!key.startsWith("derby."))
198             return PBgetJVMProperty(key);
199
200         try {
201
202             action = 3;
203             key3 = key;
204             String JavaDoc value = (String JavaDoc) java.security.AccessController.doPrivileged(this);
205             key3 = null;
206             return value;
207         } catch (java.security.PrivilegedActionException JavaDoc pae) {
208             throw (RuntimeException JavaDoc) pae.getException();
209         }
210     }
211
212     public synchronized final Thread JavaDoc getDaemonThread(Runnable JavaDoc task, String JavaDoc name, boolean setMinPriority) {
213
214         action = 4;
215         key3 = name;
216         this.task = task;
217         this.intValue = setMinPriority ? 1 : 0;
218
219         try {
220
221             Thread JavaDoc t = (Thread JavaDoc) java.security.AccessController.doPrivileged(this);
222
223             key3 = null;
224             task = null;
225
226             return t;
227         } catch (java.security.PrivilegedActionException JavaDoc pae) {
228             throw (RuntimeException JavaDoc) pae.getException();
229         }
230     }
231
232     public synchronized final void setThreadPriority(int priority) {
233         action = 5;
234         intValue = priority;
235         try {
236             java.security.AccessController.doPrivileged(this);
237         } catch (java.security.PrivilegedActionException JavaDoc pae) {
238             throw (RuntimeException JavaDoc) pae.getException();
239         }
240     }
241
242     synchronized final InputStream JavaDoc applicationPropertiesStream()
243       throws IOException JavaDoc {
244         action = 6;
245         try {
246             // SECURITY PERMISSION - OP3
247
return (InputStream JavaDoc) java.security.AccessController.doPrivileged(this);
248         }
249         catch (java.security.PrivilegedActionException JavaDoc pae)
250         {
251             throw (IOException JavaDoc) pae.getException();
252         }
253     }
254
255
256     public synchronized final Object JavaDoc run() throws IOException JavaDoc {
257         switch (action) {
258         case 0:
259         case 1:
260             // SECURITY PERMISSION - OP2, OP2a, OP2b
261
return new Boolean JavaDoc(PBinitialize(action == 0));
262         case 2:
263             // SECURITY PERMISSION - IP1
264
return super.getDefaultModuleProperties();
265         case 3:
266             // SECURITY PERMISSION - OP1
267
return PBgetJVMProperty(key3);
268         case 4:
269             return super.getDaemonThread(task, key3, intValue != 0);
270         case 5:
271             super.setThreadPriority(intValue);
272             return null;
273         case 6:
274             // SECURITY PERMISSION - OP3
275
return PBapplicationPropertiesStream();
276
277         default:
278             return null;
279         }
280     }
281
282     public final ProductVersionHolder getEngineVersion() {
283         return engineVersion;
284     }
285 }
286
Popular Tags