001: package org.tanukisoftware.wrapper.jmx;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: */
027:
028: /**
029: * @author Leif Mortenson <leif@tanukisoftware.com>
030: */
031: public class WrapperManager implements WrapperManagerMBean {
032: /*---------------------------------------------------------------
033: * WrapperManagerMBean Methods
034: *-------------------------------------------------------------*/
035: /**
036: * Obtain the current version of Wrapper.
037: *
038: * @return The version of the Wrapper.
039: */
040: public String getVersion() {
041: return org.tanukisoftware.wrapper.WrapperManager.getVersion();
042: }
043:
044: /**
045: * Obtain the build time of Wrapper.
046: *
047: * @return The time that the Wrapper was built.
048: */
049: public String getBuildTime() {
050: return org.tanukisoftware.wrapper.WrapperManager.getBuildTime();
051: }
052:
053: /**
054: * Returns the Id of the current JVM. JVM Ids increment from 1 each time
055: * the wrapper restarts a new one.
056: *
057: * @return The Id of the current JVM.
058: */
059: public int getJVMId() {
060: return org.tanukisoftware.wrapper.WrapperManager.getJVMId();
061: }
062:
063: /**
064: * Sets the title of the console in which the Wrapper is running. This
065: * is currently only supported on Windows platforms.
066: * <p>
067: * As an alternative, it is also possible to set the console title from
068: * within the wrapper.conf file using the wrapper.console.title property.
069: *
070: * @param title The new title. The specified string will be encoded
071: * to a byte array using the default encoding for the
072: * current platform.
073: */
074: public void setConsoleTitle(String title) {
075: org.tanukisoftware.wrapper.WrapperManager
076: .setConsoleTitle(title);
077: }
078:
079: /**
080: * Returns the PID of the Wrapper process.
081: *
082: * A PID of 0 will be returned if the JVM was launched standalone.
083: *
084: * This value can also be obtained using the 'wrapper.pid' system property.
085: *
086: * @return The PID of the Wrpper process.
087: */
088: public int getWrapperPID() {
089: return org.tanukisoftware.wrapper.WrapperManager
090: .getWrapperPID();
091: }
092:
093: /**
094: * Returns the PID of the Java process.
095: *
096: * A PID of 0 will be returned if the native library has not been initialized.
097: *
098: * This value can also be obtained using the 'wrapper.java.pid' system property.
099: *
100: * @return The PID of the Java process.
101: */
102: public int getJavaPID() {
103: return org.tanukisoftware.wrapper.WrapperManager.getJavaPID();
104: }
105:
106: /**
107: * Requests that the current JVM process request a thread dump. This is
108: * the same as pressing CTRL-BREAK (under Windows) or CTRL-\ (under Unix)
109: * in the the console in which Java is running. This method does nothing
110: * if the native library is not loaded.
111: */
112: public void requestThreadDump() {
113: org.tanukisoftware.wrapper.WrapperManager.requestThreadDump();
114: }
115:
116: /**
117: * Returns true if the JVM was launched by the Wrapper application. False
118: * if the JVM was launched manually without the Wrapper controlling it.
119: *
120: * @return True if the current JVM was launched by the Wrapper.
121: */
122: public boolean isControlledByNativeWrapper() {
123: return org.tanukisoftware.wrapper.WrapperManager
124: .isControlledByNativeWrapper();
125: }
126:
127: /**
128: * Returns true if the Wrapper was launched as an NT service on Windows or
129: * as a daemon process on UNIX platforms. False if launched as a console.
130: * This can be useful if you wish to display a user interface when in
131: * Console mode. On UNIX platforms, this is not as useful because an
132: * X display may not be visible even if launched in a console.
133: *
134: * @return True if the Wrapper is running as an NT service or daemon
135: * process.
136: */
137: public boolean isLaunchedAsService() {
138: return org.tanukisoftware.wrapper.WrapperManager
139: .isLaunchedAsService();
140: }
141:
142: /**
143: * Returns true if the wrapper.debug property, or any of the logging
144: * channels are set to DEBUG in the wrapper configuration file. Useful
145: * for deciding whether or not to output certain information to the
146: * console.
147: *
148: * @return True if the Wrapper is logging any Debug level output.
149: */
150: public boolean isDebugEnabled() {
151: return org.tanukisoftware.wrapper.WrapperManager
152: .isDebugEnabled();
153: }
154:
155: /**
156: * Tells the native wrapper that the JVM wants to restart, then informs
157: * all listeners that the JVM is about to shutdown before killing the JVM.
158: * <p>
159: * The restart is actually performed in a background thread allowing JMX
160: * a chance to respond to the client.
161: */
162: public void restart() {
163: // This action normally will not return, so launch it in a background
164: // thread giving JMX a chance to return a response to its client.
165: new Thread() {
166: public void run() {
167: try {
168: Thread.sleep(1000);
169: } catch (InterruptedException e) {
170: }
171:
172: org.tanukisoftware.wrapper.WrapperManager.restart();
173: }
174: }.start();
175: }
176:
177: /**
178: * Tells the native wrapper that the JVM wants to shut down, then informs
179: * all listeners that the JVM is about to shutdown before killing the JVM.
180: * <p>
181: * The stop is actually performed in a background thread allowing JMX
182: * a chance to respond to the client.
183: *
184: * @param exitCode The exit code that the Wrapper will return when it exits.
185: */
186: public void stop(final int exitCode) {
187: // This action normally will not return, so launch it in a background
188: // thread giving JMX a chance to return a response to its client.
189: new Thread() {
190: public void run() {
191: try {
192: Thread.sleep(1000);
193: } catch (InterruptedException e) {
194: }
195:
196: org.tanukisoftware.wrapper.WrapperManager
197: .stop(exitCode);
198: }
199: }.start();
200: }
201:
202: /**
203: * Returns true if the ShutdownHook for the JVM has already been triggered.
204: * Some code needs to know whether or not the system is shutting down.
205: *
206: * @return True if the ShutdownHook for the JVM has already been triggered.
207: */
208: public boolean getHasShutdownHookBeenTriggered() {
209: return org.tanukisoftware.wrapper.WrapperManager
210: .hasShutdownHookBeenTriggered();
211: }
212: }
|