001: /*
002:
003: Derby - Class org.apache.derby.impl.services.monitor.FileMonitor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.monitor;
023:
024: import org.apache.derby.iapi.services.monitor.Monitor;
025: import org.apache.derby.iapi.reference.Property;
026:
027: import org.apache.derby.iapi.services.io.FileUtil;
028: import org.apache.derby.iapi.services.info.ProductVersionHolder;
029: import org.apache.derby.iapi.services.info.ProductGenusNames;
030:
031: import java.io.FileInputStream;
032: import java.io.File;
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: import java.net.URL;
037: import java.util.Enumeration;
038: import java.util.Properties;
039:
040: /**
041: Implementation of the monitor that uses the class loader
042: that the its was loaded in for all class loading.
043:
044: */
045:
046: public final class FileMonitor extends BaseMonitor implements
047: java.security.PrivilegedExceptionAction {
048:
049: /* Fields */
050: private File home;
051:
052: private ProductVersionHolder engineVersion;
053:
054: public FileMonitor() {
055: initialize(true);
056: applicationProperties = readApplicationProperties();
057: }
058:
059: public FileMonitor(java.util.Properties properties,
060: java.io.PrintStream log) {
061: runWithState(properties, log);
062: }
063:
064: private InputStream PBapplicationPropertiesStream()
065: throws IOException {
066:
067: File sr = FileUtil.newFile(home, Property.PROPERTIES_FILE);
068:
069: if (!sr.exists())
070: return null;
071:
072: return new FileInputStream(sr);
073: }
074:
075: public Object getEnvironment() {
076: return home;
077: }
078:
079: /**
080: SECURITY WARNING.
081:
082: This method is run in a privileged block in a Java 2 environment.
083:
084: Set the system home directory. Returns false if it couldn't for
085: some reason.
086:
087: **/
088: private boolean PBinitialize(boolean lite) {
089: if (!lite) {
090: try {
091: // Create a ThreadGroup and set the daemon property to
092: // make sure the group is destroyed and garbage
093: // collected when all its members have finished (i.e.,
094: // when the driver is unloaded).
095: daemonGroup = new ThreadGroup("derby.daemons");
096: daemonGroup.setDaemon(true);
097: } catch (SecurityException se) {
098: }
099: }
100:
101: InputStream versionStream = getClass().getResourceAsStream(
102: ProductGenusNames.DBMS_INFO);
103:
104: engineVersion = ProductVersionHolder
105: .getProductVersionHolderFromMyEnv(versionStream);
106:
107: String systemHome;
108: // create the system home directory if it doesn't exist
109: try {
110: // SECURITY PERMISSION - OP2
111: systemHome = System
112: .getProperty(Property.SYSTEM_HOME_PROPERTY);
113: } catch (SecurityException se) {
114: // system home will be the current directory
115: systemHome = null;
116: }
117:
118: if (systemHome != null) {
119: home = new File(systemHome);
120:
121: // SECURITY PERMISSION - OP2a
122: if (home.exists()) {
123: if (!home.isDirectory()) {
124: report(Property.SYSTEM_HOME_PROPERTY + "="
125: + 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 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 PBgetJVMProperty(String key) {
154:
155: try {
156: // SECURITY PERMISSION - OP1
157: return System.getProperty(key);
158: } catch (SecurityException 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 key3;
169: private Runnable task;
170: private int intValue;
171:
172: /**
173: Initialize the system in a privileged block.
174: **/
175: synchronized final boolean initialize(boolean lite) {
176: action = lite ? 0 : 1;
177: try {
178: Object ret = java.security.AccessController
179: .doPrivileged(this );
180:
181: return ((Boolean) ret).booleanValue();
182: } catch (java.security.PrivilegedActionException pae) {
183: throw (RuntimeException) pae.getException();
184: }
185: }
186:
187: synchronized final Properties getDefaultModuleProperties() {
188: action = 2;
189: try {
190: return (Properties) java.security.AccessController
191: .doPrivileged(this );
192: } catch (java.security.PrivilegedActionException pae) {
193: throw (RuntimeException) pae.getException();
194: }
195: }
196:
197: public synchronized final String getJVMProperty(String key) {
198: if (!key.startsWith("derby."))
199: return PBgetJVMProperty(key);
200:
201: try {
202:
203: action = 3;
204: key3 = key;
205: String value = (String) java.security.AccessController
206: .doPrivileged(this );
207: key3 = null;
208: return value;
209: } catch (java.security.PrivilegedActionException pae) {
210: throw (RuntimeException) pae.getException();
211: }
212: }
213:
214: public synchronized final Thread getDaemonThread(Runnable task,
215: String name, boolean setMinPriority) {
216:
217: action = 4;
218: key3 = name;
219: this .task = task;
220: this .intValue = setMinPriority ? 1 : 0;
221:
222: try {
223:
224: Thread t = (Thread) java.security.AccessController
225: .doPrivileged(this );
226:
227: key3 = null;
228: task = null;
229:
230: return t;
231: } catch (java.security.PrivilegedActionException pae) {
232: throw (RuntimeException) pae.getException();
233: }
234: }
235:
236: public synchronized final void setThreadPriority(int priority) {
237: action = 5;
238: intValue = priority;
239: try {
240: java.security.AccessController.doPrivileged(this );
241: } catch (java.security.PrivilegedActionException pae) {
242: throw (RuntimeException) pae.getException();
243: }
244: }
245:
246: synchronized final InputStream applicationPropertiesStream()
247: throws IOException {
248: action = 6;
249: try {
250: // SECURITY PERMISSION - OP3
251: return (InputStream) java.security.AccessController
252: .doPrivileged(this );
253: } catch (java.security.PrivilegedActionException pae) {
254: throw (IOException) pae.getException();
255: }
256: }
257:
258: public synchronized final Object run() throws IOException {
259: switch (action) {
260: case 0:
261: case 1:
262: // SECURITY PERMISSION - OP2, OP2a, OP2b
263: return new Boolean(PBinitialize(action == 0));
264: case 2:
265: // SECURITY PERMISSION - IP1
266: return super .getDefaultModuleProperties();
267: case 3:
268: // SECURITY PERMISSION - OP1
269: return PBgetJVMProperty(key3);
270: case 4:
271: return super .getDaemonThread(task, key3, intValue != 0);
272: case 5:
273: super .setThreadPriority(intValue);
274: return null;
275: case 6:
276: // SECURITY PERMISSION - OP3
277: return PBapplicationPropertiesStream();
278:
279: default:
280: return null;
281: }
282: }
283:
284: public final ProductVersionHolder getEngineVersion() {
285: return engineVersion;
286: }
287: }
|