1 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 import java.util.logging.*; 23 24 38 public class QSAdminServer { 39 private static Logger logger = Logger.getLogger( 40 QSAdminServer.class.getName()); 41 private final static String VER = "1.3"; 42 43 private QuickServer controlServer; 44 private QuickServer adminServer; private int port = 9877; 46 47 private String cmdHandle = "org.quickserver.net.qsadmin.CommandHandler"; 48 private String auth = "org.quickserver.net.qsadmin.Authenticator"; 49 private String data = "org.quickserver.net.qsadmin.Data"; 50 51 private String pluginClass; 53 private CommandPlugin plugin; 54 55 private boolean shellEnable; 57 private String promptName; 58 59 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); 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 e) { 83 logger.warning("Could not bind to 127.0.0.1"); 84 throw new RuntimeException ("Could not bind to 127.0.0.1 : "+e); 85 } 86 adminServer.setQSAdminServer(this); } 88 89 98 public void setAuthenticator(String authenticator) { 99 if(authenticator != null) 100 adminServer.setClientAuthenticationHandler(authenticator); 101 } 102 103 111 public void setClientAuthenticationHandler(String authenticator) { 112 if(authenticator != null) 113 adminServer.setClientAuthenticationHandler(authenticator); 114 } 115 116 120 public void startServer(int port) throws AppException { 121 adminServer.setPort(port); 122 startServer(); 123 } 124 125 134 public void startServer() throws AppException { 135 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(), getPromptName()); 149 } 150 } catch(AppException e) { 151 logger.warning("AppError : "+e); 152 throw e; 153 } 154 } 155 156 159 public QuickServer getServer() { 160 return adminServer; 161 } 162 163 167 public QuickServer getControlServer() { 168 return controlServer; 169 } 170 171 private void prepareCommandPlugin() { 172 String _pluginClass = getCommandPlugin(); 173 if(_pluginClass==null) return; 174 try { 175 Class cl = getControlServer().getClass(pluginClass, true); 176 plugin = (CommandPlugin) cl.newInstance(); 177 } catch(Exception e) { 178 logger.warning("Error loading plugin : " + e); 179 } 180 } 181 182 192 public void setCommandPlugin(String pluginClass) 193 throws Exception { 194 if(pluginClass==null) 195 return; 196 this.pluginClass = pluginClass; 197 } 198 199 200 205 public String getCommandPlugin() { 206 return pluginClass; 207 } 208 209 public static String getVersion() { 210 return VER; 211 } 212 213 214 218 public boolean isShellEnable() { 219 return shellEnable; 220 } 221 222 226 public void setShellEnable(boolean flag) { 227 shellEnable = flag; 228 } 229 230 235 public void setPromptName(String promptName) { 236 if(promptName!=null && promptName.equals("")==false) 237 this.promptName = promptName; 238 } 239 243 public String getPromptName() { 244 return promptName; 245 } 246 } 247 | Popular Tags |