01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /*
18: * @author Artem Aliev
19: * @version $Revision: 1.1.2.2.4.4 $
20: */
21:
22: package java.lang;
23:
24: import java.util.Properties;
25: import java.util.Enumeration;
26:
27: /**
28: * This class does the following:
29: * <li> starts Finalizer and Execution Manager helper threads.
30: * <li> parses system properties and configure VM environment.
31: * </ul>
32: */
33: class VMStart {
34:
35: public static void initialize() {
36: //start helper threads such as Finalizer
37: startHelperThreads();
38: // add default shutdown hooks, to stop helper threads.
39: Runtime.getRuntime().addShutdownHook(new DefaultShutDownHook());
40: // do additional tasks specified in system properties
41: parseSystemProperties();
42: }
43:
44: /**
45: Any component can ask VMStart to initialize it's class
46: using vm.component.<component name>.startupclass property
47: */
48: private static final String STARTUP_CLASS_PREFIX = "vm.components.";
49: private static final String STARTUP_CLASS_SUFFUX = ".startupclass";
50:
51: public static void parseSystemProperties() {
52: //call class.forName for all startup classes
53: Properties p = System.getProperties();
54: for (Enumeration it = p.propertyNames(); it.hasMoreElements();) {
55: String key = (String) it.nextElement();
56: if (key.startsWith(STARTUP_CLASS_PREFIX)
57: && key.endsWith(STARTUP_CLASS_SUFFUX)) {
58: String className = p.getProperty(key);
59: try {
60: Class.forName(className);
61: } catch (Exception e) {
62: e.printStackTrace();
63: //TODO: should be call System.exit() here?
64: }
65:
66: }
67: }
68:
69: }
70:
71: public static void startHelperThreads() {
72: try {
73: // start helper threads.
74: FinalizerThread.initialize();
75: EMThreadSupport.initialize();
76: } catch (Throwable e) {
77: System.err.println("Internal error");
78: e.printStackTrace(System.err);
79: Runtime.getRuntime().halt(1);
80: }
81: }
82:
83: // should shutdown helper threads
84: static class DefaultShutDownHook extends Thread {
85:
86: public DefaultShutDownHook() {
87: super ("Thread-shutdown");
88: }
89:
90: public void run() {
91: EMThreadSupport.shutdown();
92: }
93: }
94: }
|