📄 basemonitor.java
字号:
/* Derby - Class org.apache.derby.impl.services.monitor.BaseMonitor Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.services.monitor;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.monitor.ModuleFactory;import org.apache.derby.iapi.services.monitor.ModuleControl;import org.apache.derby.iapi.services.monitor.ModuleSupportable;import org.apache.derby.iapi.services.monitor.PersistentService;import org.apache.derby.iapi.services.io.FormatIdUtil;import org.apache.derby.iapi.services.io.RegisteredFormatIds;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.context.Context;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.context.ShutdownException;import org.apache.derby.iapi.services.stream.InfoStreams;import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.services.uuid.UUIDFactory;import org.apache.derby.iapi.reference.Property;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.Attribute;import org.apache.derby.iapi.services.property.PropertyUtil;import org.apache.derby.iapi.services.io.AccessibleByteArrayOutputStream;import org.apache.derby.iapi.services.loader.ClassInfo;import org.apache.derby.iapi.services.loader.InstanceGetter;import org.apache.derby.iapi.services.io.FormatableInstanceGetter;import org.apache.derby.iapi.error.ExceptionSeverity;import org.apache.derby.io.StorageFactory;import org.apache.derby.iapi.services.context.ErrorStringBuilder;import org.apache.derby.iapi.services.info.JVMInfo;import org.apache.derby.iapi.services.i18n.BundleFinder;import org.apache.derby.iapi.services.i18n.MessageService;import org.apache.derby.impl.services.monitor.PersistentServiceImpl;import java.io.IOException;import java.io.InputStream;import java.io.StringWriter;import java.io.BufferedInputStream;import java.io.PrintWriter;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.ByteArrayInputStream;import java.io.PrintStream;import java.util.Hashtable;import java.util.HashMap;import java.util.Properties;import java.util.Enumeration;import java.util.StringTokenizer;import java.util.Vector;import java.util.Locale;import java.util.ResourceBundle;import java.util.NoSuchElementException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.InvocationTargetException;import java.security.AccessController;import java.security.PrivilegedExceptionAction;import java.security.PrivilegedActionException;import java.net.URL;/** Implementation of the monitor that uses the class loader that the its was loaded in for all class loading.*/abstract class BaseMonitor implements ModuleFactory, BundleFinder { /* Fields */ /** Hashtable of objects that implement PersistentService keyed by their getType() method. */ Hashtable serviceProviders; // Vector of class objects of implementations, found in the System, application // and default (modules.properties) properties Vector[] implementationSets; private Vector services; // Vector of TopServices Properties bootProperties; // specifc properties provided by the boot method, override everything else Properties applicationProperties; boolean inShutdown; // Here are the list of modules that we always boot private InfoStreams systemStreams; private ContextService contextService; private UUIDFactory uuidFactory; boolean reportOn; private PrintStream logging; ThreadGroup daemonGroup; // anti GC stuff AntiGC dontGC; // class registry/* one byte format identifiers never used private InstanceGetter[] rc1;*/ private InstanceGetter[] rc2;// private InstanceGetter[] rc4; /* Constructor */ BaseMonitor() { super(); services = new Vector(0, 1); services.addElement(new TopService(this)); // first element is always the free-floating service } /* Methods of ModuleFactory includes BootStrap and Runnable */ public InfoStreams getSystemStreams() { return systemStreams; } public void shutdown() { // allow only one caller to shut the monitor down synchronized (this) { if (inShutdown) return; inShutdown = true; } if (SanityManager.DEBUG && reportOn) { report("Shutdown request"); } // Shutdown all threads by iterrupting them contextService.notifyAllActiveThreads((Context) null); for (;;) { TopService ts; int position; synchronized (this) { position = services.size() - 1; if (position == 0) break; ts = (TopService) services.elementAt(position); } // push a new context manager ContextManager cm = contextService.newContextManager(); try { // pop the default shutdown context, we are shutting down cm.popContext(); contextService.setCurrentContextManager(cm); shutdown(ts.getService()); } finally { contextService.resetCurrentContextManager(cm); } } ((TopService) services.elementAt(0)).shutdown(); synchronized (dontGC) { dontGC.goAway = true; dontGC.notifyAll(); } contextService.stop(); Monitor.clearMonitor(); } /** Shut down a service that was started by this Monitor. Will cause the stop() method to be called on each loaded module. */ public void shutdown(Object serviceModule) { if (serviceModule == null) return; TopService ts = findTopService(serviceModule); if (ts == null) return; // shutdown() returns false if the service is already being shutdown boolean removeService = true; try { removeService = ts.shutdown(); } finally { synchronized (this) { if (removeService) { boolean found = services.removeElement(ts); if (SanityManager.DEBUG) { SanityManager.ASSERT(found, "service was not found " + serviceModule); } } } } } protected final void runWithState(Properties properties, PrintStream log) { bootProperties = properties; logging = log; // false indicates the full monitor is required, not the lite. if (!initialize(false)) return; // if monitor is already set then the system is already // booted or in the process of booting or shutting down. if (!Monitor.setMonitor(this)) return; Object msgService = MessageService.setFinder(this); // start a backgorund thread which keeps a reference to this // this monitor, and an instance of the Monitor class to ensure // that the monitor instance and the class is not garbage collected // See Sun's bug 4057924 in Java Developer Section 97/08/06 Object[] keepItems = new Object[3]; keepItems[0] = this; keepItems[1] = new Monitor(); keepItems[2] = msgService; dontGC = new AntiGC(keepItems); Thread dontGCthread = getDaemonThread(dontGC, "antiGC", true); dontGCthread.start(); if (SanityManager.DEBUG) { reportOn = Boolean.valueOf(PropertyUtil.getSystemProperty("derby.monitor.verbose")).booleanValue(); } // Set up the application properties applicationProperties = readApplicationProperties(); // The security manager may not let us get the System properties // object itself, although it may let us look at the properties in it. Properties systemProperties = null; if (SanityManager.DEBUG) { // In a production system having this call would // mean would we have to document it for security // permission reasons. Since we don't require it and // its a big security hole to allow external code to // overwrite our own implementations we just support // it for debugging. This means VM executions such as // java -Dderby.module.javaCompiler=com.ibm.db2j.impl.BasicServices.JavaCompiler.JavaLang.JLJava ... // would only work with a sane codeline. try { systemProperties = System.getProperties(); } catch (SecurityException se) { } } Vector bootImplementations = getImplementations(bootProperties, false); Vector systemImplementations = null; Vector applicationImplementations = null; // TEMP - making this sanity only breaks the unit test code // I will fix soon, djd. if (true || SanityManager.DEBUG) { // Don't allow external code to override our implementations. systemImplementations = getImplementations(systemProperties, false); applicationImplementations = getImplementations(applicationProperties, false); } Vector defaultImplementations = getDefaultImplementations(); int implementationCount = 0; if (bootImplementations != null) implementationCount++; // TEMP - making this sanity only breaks the unit test code if (true || SanityManager.DEBUG) { // Don't allow external code to override our implementations. if (systemImplementations != null) implementationCount++; if (applicationImplementations != null) implementationCount++; } if (defaultImplementations != null) implementationCount++; implementationSets = new Vector[implementationCount]; implementationCount = 0; if (bootImplementations != null) implementationSets[implementationCount++] = bootImplementations; if (true || SanityManager.DEBUG) { // Don't allow external code to override our implementations. if (systemImplementations != null) implementationSets[implementationCount++] = systemImplementations; if (applicationImplementations != null) implementationSets[implementationCount++] = applicationImplementations; } if (defaultImplementations != null) implementationSets[implementationCount++] = defaultImplementations; if (SanityManager.DEBUG) { // Look for the derby.debug.* properties. if (applicationProperties != null) { addDebugFlags(applicationProperties.getProperty(Monitor.DEBUG_FALSE), false); addDebugFlags(applicationProperties.getProperty(Monitor.DEBUG_TRUE), true); } addDebugFlags(System.getProperty(Monitor.DEBUG_FALSE), false); addDebugFlags(System.getProperty(Monitor.DEBUG_TRUE), true); } try { systemStreams = (InfoStreams) Monitor.startSystemModule("org.apache.derby.iapi.services.stream.InfoStreams"); if (SanityManager.DEBUG) { SanityManager.SET_DEBUG_STREAM(systemStreams.stream().getPrintWriter()); } contextService = new ContextService(); uuidFactory = (UUIDFactory) Monitor.startSystemModule("org.apache.derby.iapi.services.uuid.UUIDFactory"); } catch (StandardException se) { // if we can't create an error log or a context then there's no point going on reportException(se); // dump any messages we have been saving ... dumpTempWriter(true); return; } // switch cover to the real error stream and // dump any messages we have been saving ... dumpTempWriter(false); if (SanityManager.DEBUG && reportOn) { dumpProperties("-- Boot Properties --", bootProperties); dumpProperties("-- System Properties --", systemProperties); dumpProperties("-- Application Properties --", applicationProperties); } // bootup all the service providers bootServiceProviders(); // See if automatic booting of persistent services is required boolean bootAll = Boolean.valueOf(PropertyUtil.getSystemProperty(Property.BOOT_ALL)).booleanValue(); startServices(bootProperties, bootAll); startServices(systemProperties, bootAll); startServices(applicationProperties, bootAll); if (bootAll) // only if automatic booting is required bootPersistentServices( ); } public Object findService(String factoryInterface, String serviceName) { if (serviceName == null) return null; ProtocolKey key; try { key = ProtocolKey.create(factoryInterface, serviceName); } catch (StandardException se) { return null; } TopService myts = null; synchronized (this) { for (int i = 1; i < services.size(); i++) { TopService ts = (TopService) services.elementAt(i); if (ts.isPotentialService(key)) { myts = ts; break; } } } // the isActiveService() call may sleep // so don't hold the 'this' synchronization if (myts != null) { if (myts.isActiveService(key)) return myts.getService(); } return null; } public Locale getLocale(Object serviceModule) { TopService ts = findTopService(serviceModule); if (ts == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -