001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.qsadmin;
016:
017: import org.quickserver.net.*;
018: import org.quickserver.net.server.*;
019:
020: import java.io.*; //v1.2
021: import java.util.logging.*;
022:
023: /**
024: * QSAdminServer Main class.
025: * <p>
026: * This is can be used to setup a admin server to a
027: * {@link org.quickserver.net.server.QuickServer}, it is implemented
028: * as a QuickServer. {@link org.quickserver.net.server.QuickServer}
029: * comes with a very use full method
030: * {@link org.quickserver.net.server.QuickServer#startQSAdminServer} that
031: * creates QSAdminServer associated with itself and starts it at
032: * the specified port or the default port 9877.
033: * </p>
034: * @see #startServer()
035: * @since 1.1
036: */
037: public class QSAdminServer {
038: private static Logger logger = Logger.getLogger(QSAdminServer.class
039: .getName());
040: private final static String VER = "1.3";
041:
042: private QuickServer controlServer;
043: private QuickServer adminServer; //this server
044: private int port = 9877;
045:
046: private String cmdHandle = "org.quickserver.net.qsadmin.CommandHandler";
047: private String auth = "org.quickserver.net.qsadmin.Authenticator";
048: private String data = "org.quickserver.net.qsadmin.Data";
049:
050: //v1.2
051: private String pluginClass;
052: private CommandPlugin plugin;
053:
054: //v1.3.2
055: private boolean shellEnable;
056: private String promptName;
057:
058: /**
059: * Creates QSAdminServer with default settings.
060: * By default it has been set to allow only 1 client
061: * connection to it and binds to <code>127.0.0.1</code>.
062: * @param controlServer QuickServer to control.
063: */
064: public QSAdminServer(QuickServer controlServer) {
065: this .controlServer = controlServer;
066: adminServer = new QuickServer();
067:
068: adminServer.setClientEventHandler(cmdHandle);
069: adminServer.setClientCommandHandler(cmdHandle);
070: adminServer.setClientAuthenticationHandler(auth);
071: adminServer.setClientData(data);
072: adminServer.setPort(port);
073:
074: adminServer.setAppLogger(logger); //v1.2
075: adminServer.setName("QSAdminServer v " + VER);
076: adminServer.setMaxConnection(1);
077: adminServer.getBasicConfig().getServerMode().setBlocking(false);
078:
079: try {
080: adminServer.setBindAddr("127.0.0.1");
081: } catch (java.net.UnknownHostException e) {
082: logger.warning("Could not bind to 127.0.0.1");
083: throw new RuntimeException("Could not bind to 127.0.0.1 : "
084: + e);
085: }
086: adminServer.setQSAdminServer(this );//lets set to self
087: }
088:
089: /**
090: * Sets the Authenticator class that handles the
091: * authentication of a client, if null uses default
092: * {@link Authenticator}.
093: * @param authenticator full class name of the class that
094: * implements {@link org.quickserver.net.server.Authenticator}.
095: * @since 1.3
096: * @deprecated since 1.4.6 use setClientAuthenticationHandler
097: */
098: public void setAuthenticator(String authenticator) {
099: if (authenticator != null)
100: adminServer.setClientAuthenticationHandler(authenticator);
101: }
102:
103: /**
104: * Sets the ClientAuthenticationHandler class that handles the
105: * authentication of a client, if null uses default
106: * {@link Authenticator}.
107: * @param authenticator full class name of the class that
108: * implements {@link org.quickserver.net.server.ClientAuthenticationHandler}.
109: * @since 1.4.6
110: */
111: public void setClientAuthenticationHandler(String authenticator) {
112: if (authenticator != null)
113: adminServer.setClientAuthenticationHandler(authenticator);
114: }
115:
116: /**
117: * Starts the QSAdminServer.
118: * @param port to run QSAdminServer on
119: */
120: public void startServer(int port) throws AppException {
121: adminServer.setPort(port);
122: startServer();
123: }
124:
125: /**
126: * Starts the QSAdminServer.
127: * This method also sets the 'Store Objects' of QSAdminServer's
128: * QuickServer to the following <PRE>
129: POS 0 = QuickServer that is controled.
130: POS 1 = Command Plugin if present for QSAdminServer's CommandHandler
131: POS 3 = QSAdminServer own reference object. </PRE>
132: * @since 1.2
133: */
134: public void startServer() throws AppException {
135: //v1.2 - plugin stored in pos = 1,
136: // QSAdminServer stored at pos = 2
137:
138: prepareCommandPlugin();
139: Object[] store = new Object[] { (Object) getControlServer(),
140: (Object) plugin, (Object) QSAdminServer.this };
141: adminServer.setStoreObjects(store);
142:
143: if (getControlServer() == null)
144: throw new NullPointerException("control Server was null");
145: try {
146: adminServer.startServer();
147: if (isShellEnable() == true) {
148: QSAdminShell.getInstance(getControlServer(),
149: getPromptName());
150: }
151: } catch (AppException e) {
152: logger.warning("AppError : " + e);
153: throw e;
154: }
155: }
156:
157: /**
158: * Returns the QuickServer object that created it.
159: */
160: public QuickServer getServer() {
161: return adminServer;
162: }
163:
164: /**
165: * Returns the QuickServer object that is being
166: * controled by this QSAdminServer.
167: */
168: public QuickServer getControlServer() {
169: return controlServer;
170: }
171:
172: private void prepareCommandPlugin() {
173: String _pluginClass = getCommandPlugin();
174: if (_pluginClass == null)
175: return;
176: try {
177: Class cl = getControlServer().getClass(pluginClass, true);
178: plugin = (CommandPlugin) cl.newInstance();
179: } catch (Exception e) {
180: logger.warning("Error loading plugin : " + e);
181: }
182: }
183:
184: /**
185: * Sets the {@link CommandPlugin} class which plugs into
186: * {@link CommandHandler} of QsAdminServer. It should be set
187: * before QSAdminServer is started. Or QSAdminServer must be
188: * restarted.
189: * @param pluginClass the fully qualified name of the
190: * desired class that implements {@link CommandPlugin}
191: * @exception if could not load the class
192: * @since 1.2
193: */
194: public void setCommandPlugin(String pluginClass) throws Exception {
195: if (pluginClass == null)
196: return;
197: this .pluginClass = pluginClass;
198: }
199:
200: /**
201: * Returns the {@link CommandPlugin} class which plugs into
202: * {@link CommandHandler} of QsAdminServer,it will be null if not set.
203: * @since 1.2
204: */
205: public String getCommandPlugin() {
206: return pluginClass;
207: }
208:
209: public static String getVersion() {
210: return VER;
211: }
212:
213: /**
214: * Returns flag indicated if command shell is enabled.
215: * @since 1.3.2
216: */
217: public boolean isShellEnable() {
218: return shellEnable;
219: }
220:
221: /**
222: * Sets the flag indicated if command shell is enabled.
223: * @since 1.3.2
224: */
225: public void setShellEnable(boolean flag) {
226: shellEnable = flag;
227: }
228:
229: /**
230: * Set the prompt name for QSAdminShell
231: * Default values = <code>QSAdmin</code>
232: * @since 1.3.2
233: */
234: public void setPromptName(String promptName) {
235: if (promptName != null && promptName.equals("") == false)
236: this .promptName = promptName;
237: }
238:
239: /**
240: * Gets the prompt name for QSAdminShell
241: * @since 1.3.2
242: */
243: public String getPromptName() {
244: return promptName;
245: }
246: }
|