KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > QSAdminServer


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.qsadmin;
16
17 import org.quickserver.net.*;
18 import org.quickserver.net.server.*;
19
20 import java.io.*;
21 //v1.2
22
import java.util.logging.*;
23
24 /**
25  * QSAdminServer Main class.
26  * <p>
27  * This is can be used to setup a admin server to a
28  * {@link org.quickserver.net.server.QuickServer}, it is implemented
29  * as a QuickServer. {@link org.quickserver.net.server.QuickServer}
30  * comes with a very use full method
31  * {@link org.quickserver.net.server.QuickServer#startQSAdminServer} that
32  * creates QSAdminServer associated with itself and starts it at
33  * the specified port or the default port 9877.
34  * </p>
35  * @see #startServer()
36  * @since 1.1
37  */

38 public class QSAdminServer {
39     private static Logger logger = Logger.getLogger(
40             QSAdminServer.class.getName());
41     private final static String JavaDoc VER = "1.3";
42
43     private QuickServer controlServer;
44     private QuickServer adminServer; //this server
45
private int port = 9877;
46
47     private String JavaDoc cmdHandle = "org.quickserver.net.qsadmin.CommandHandler";
48     private String JavaDoc auth = "org.quickserver.net.qsadmin.Authenticator";
49     private String JavaDoc data = "org.quickserver.net.qsadmin.Data";
50     
51     //v1.2
52
private String JavaDoc pluginClass;
53     private CommandPlugin plugin;
54
55     //v1.3.2
56
private boolean shellEnable;
57     private String JavaDoc promptName;
58
59     /**
60      * Creates QSAdminServer with default settings.
61      * By default it has been set to allow only 1 client
62      * connection to it and binds to <code>127.0.0.1</code>.
63      * @param controlServer QuickServer to control.
64      */

65     public QSAdminServer(QuickServer controlServer) {
66         this.controlServer = controlServer;
67         adminServer = new QuickServer();
68
69         adminServer.setClientEventHandler(cmdHandle);
70         adminServer.setClientCommandHandler(cmdHandle);
71         adminServer.setClientAuthenticationHandler(auth);
72         adminServer.setClientData(data);
73         adminServer.setPort(port);
74
75         adminServer.setAppLogger(logger); //v1.2
76
adminServer.setName("QSAdminServer v "+VER);
77         adminServer.setMaxConnection(1);
78         adminServer.getBasicConfig().getServerMode().setBlocking(false);
79
80         try {
81             adminServer.setBindAddr("127.0.0.1");
82         } catch(java.net.UnknownHostException JavaDoc e) {
83             logger.warning("Could not bind to 127.0.0.1");
84             throw new RuntimeException JavaDoc("Could not bind to 127.0.0.1 : "+e);
85         }
86         adminServer.setQSAdminServer(this);//lets set to self
87
}
88
89     /**
90      * Sets the Authenticator class that handles the
91      * authentication of a client, if null uses default
92      * {@link Authenticator}.
93      * @param authenticator full class name of the class that
94      * implements {@link org.quickserver.net.server.Authenticator}.
95      * @since 1.3
96      * @deprecated since 1.4.6 use setClientAuthenticationHandler
97      */

98     public void setAuthenticator(String JavaDoc authenticator) {
99         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 JavaDoc 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 JavaDoc[] store = new Object JavaDoc[]{(Object JavaDoc) getControlServer(),
140             (Object JavaDoc) plugin, (Object JavaDoc) QSAdminServer.this };
141         adminServer.setStoreObjects(store);
142
143         if(getControlServer()==null)
144             throw new NullPointerException JavaDoc("control Server was null");
145         try {
146             adminServer.startServer();
147             if(isShellEnable()==true) {
148                 QSAdminShell.getInstance(getControlServer(), getPromptName());
149             }
150         } catch(AppException e) {
151             logger.warning("AppError : "+e);
152             throw e;
153         }
154     }
155
156     /**
157      * Returns the QuickServer object that created it.
158      */

159     public QuickServer getServer() {
160         return adminServer;
161     }
162
163     /**
164      * Returns the QuickServer object that is being
165      * controled by this QSAdminServer.
166      */

167     public QuickServer getControlServer() {
168         return controlServer;
169     }
170
171     private void prepareCommandPlugin() {
172         String JavaDoc _pluginClass = getCommandPlugin();
173         if(_pluginClass==null) return;
174         try {
175             Class JavaDoc cl = getControlServer().getClass(pluginClass, true);
176             plugin = (CommandPlugin) cl.newInstance();
177         } catch(Exception JavaDoc e) {
178             logger.warning("Error loading plugin : " + e);
179         }
180     }
181
182     /**
183      * Sets the {@link CommandPlugin} class which plugs into
184      * {@link CommandHandler} of QsAdminServer. It should be set
185      * before QSAdminServer is started. Or QSAdminServer must be
186      * restarted.
187      * @param pluginClass the fully qualified name of the
188      * desired class that implements {@link CommandPlugin}
189      * @exception if could not load the class
190      * @since 1.2
191      */

192     public void setCommandPlugin(String JavaDoc pluginClass)
193             throws Exception JavaDoc {
194         if(pluginClass==null)
195             return;
196         this.pluginClass = pluginClass;
197     }
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 JavaDoc getCommandPlugin() {
206         return pluginClass;
207     }
208
209     public static String JavaDoc getVersion() {
210         return VER;
211     }
212
213
214     /**
215      * Returns flag indicated if command shell is enabled.
216      * @since 1.3.2
217      */

218     public boolean isShellEnable() {
219         return shellEnable;
220     }
221
222     /**
223      * Sets the flag indicated if command shell is enabled.
224      * @since 1.3.2
225      */

226     public void setShellEnable(boolean flag) {
227         shellEnable = flag;
228     }
229
230     /**
231      * Set the prompt name for QSAdminShell
232      * Default values = <code>QSAdmin</code>
233      * @since 1.3.2
234      */

235     public void setPromptName(String JavaDoc promptName) {
236         if(promptName!=null && promptName.equals("")==false)
237             this.promptName = promptName;
238     }
239     /**
240      * Gets the prompt name for QSAdminShell
241      * @since 1.3.2
242      */

243     public String JavaDoc getPromptName() {
244         return promptName;
245     }
246 }
247
Popular Tags