⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quickserver.java

📁 一个用java编写的服务器,对于学习网络编程的人来说是个很好的例子
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			throw e;
		}		
	}

	/**
	 * Initialise all Object and Thread pools.
	 * @since 1.3
	 */
	public void initAllPools() throws Exception {
		logger.fine("Creating pools");
		if(getBlockingMode()==false) {			
			makeByteBufferPool(getBasicConfig().getObjectPoolConfig().getByteBufferObjectPoolConfig());
		}
		
		makeClientPool(getBasicConfig().getObjectPoolConfig().getThreadObjectPoolConfig());
		
		makeClientHandlerPool(
			getBasicConfig().getObjectPoolConfig().getClientHandlerObjectPoolConfig());
		
		//check if client data is poolable
		if(clientDataClass!=null) {
			try {
				clientData = (ClientData)clientDataClass.newInstance();
				if(PoolableObject.class.isInstance(clientData)==true) {
					PoolableObject po = (PoolableObject)clientData;
					if( po.isPoolable()==true) {						
						makeClientDataPool(po.getPoolableObjectFactory(),
							getBasicConfig().getObjectPoolConfig().getClientDataObjectPoolConfig() );
					} else {
						clientDataPool = null;
						logger.fine("ClientData is not poolable!");
					}
				}
			} catch(Exception e) {
				logger.warning("Error: "+e);
				throw e;
			}
		}

		try {
			makeDBObjectPool();
		} catch(Exception e) {
			logger.warning("Error in makeDBObjectPool() : "+e);
			logger.fine("StackTrace:\n"+MyString.getStackTrace(e));
			throw e;
		}
		logger.fine("Created pools");
	}


	/**
	 * Returns {@link org.quickserver.util.pool.thread.ClientPool} class that 
	 * managing the pool of threads for handling clients.
	 * @exception IllegalStateException if pool is not created yet.
	 * @since 1.3
	 */
	public ClientPool getClientPool() {
		if(pool==null)
			throw new IllegalStateException("No ClientPool available yet!");
		return pool;
	}

	/** 
	 * Makes the pool of ClientHandler
	 * @since 1.3
	 */
	private void makeClientHandlerPool(PoolConfig opConfig) throws Exception {
		logger.finer("Creating ClientHandler pool");
		PoolableObjectFactory factory = new ClientHandlerObjectFactory(getBlockingMode());
		clientHandlerPool = poolManager.makeClientHandlerPool(factory, opConfig);
		poolManager.initPool(clientHandlerPool, opConfig);
		clientHandlerPool = makeQSObjectPool(clientHandlerPool);
		clientIdentifier.setClientHandlerPool((QSObjectPool)clientHandlerPool);
	}

	/**
	 * Returns ObjectPool of {@link org.quickserver.net.server.ClientHandler} 
	 * class.
	 * @exception IllegalStateException if pool is not created yet.
	 * @since 1.3
	 */
	public ObjectPool getClientHandlerPool() {
		if(clientHandlerPool==null)
			throw new IllegalStateException("No ClientHandler Pool available yet!");
		return clientHandlerPool;
	}

	
	/**
	 * Sets the confiuration of the QuickServer.
	 * @since 1.3
	 */
	public void setConfig(QuickServerConfig config) {
		this.config = config;
	}

	/**
	 * Returns the confiuration of the QuickServer.
	 * @since 1.3
	 */
	public QuickServerConfig getConfig() {
		return config;
	}

	/** 
	 * Makes the pool of ClientData
	 * @since 1.3
	 */
	private void makeClientDataPool(PoolableObjectFactory factory, 
			PoolConfig opConfig) throws Exception {
		logger.finer("Creating ClientData pool");
		clientDataPool = poolManager.makeClientDataPool(factory, opConfig);
		poolManager.initPool(clientDataPool, opConfig);
		clientDataPool = makeQSObjectPool(clientDataPool);		
	}

	/**
	 * Returns ObjectPool of {@link org.quickserver.net.server.ClientData} 
	 * class. If ClientData was not poolable will return  null.
	 * @since 1.3
	 */
	public ObjectPool getClientDataPool() {
		return clientDataPool;
	}

	/**
	 * Returns {@link org.quickserver.sql.DBPoolUtil} object if
	 * {@link org.quickserver.util.xmlreader.DBObjectPoolConfig} was set.
	 * @return DBPoolUtil object if object could be loaded, else will return <code>null</code>
	 * @since 1.3
	 */
	public DBPoolUtil getDBPoolUtil() {
		return dBPoolUtil;
	}
	/**
	 * Sets {@link org.quickserver.util.xmlreader.DBObjectPoolConfig}
	 * @since 1.3
	 */
	public void setDBObjectPoolConfig(DBObjectPoolConfig dBObjectPoolConfig) {
		getConfig().setDBObjectPoolConfig(dBObjectPoolConfig);
	}

	/** 
	 * Makes the pool of Database Objects
	 * @since 1.3
	 */
	private void makeDBObjectPool() throws Exception {
		if(getConfig().getDBObjectPoolConfig()!=null) {
			logger.fine("Creating DBObject Pool");
			//logger.finest("Got:\n"+getConfig().getDBObjectPoolConfig().toXML(null));
			Class dbPoolUtilClass = getClass(
				getConfig().getDBObjectPoolConfig().getDbPoolUtil(), true);
			dBPoolUtil = (DBPoolUtil) dbPoolUtilClass.newInstance();
			dBPoolUtil.setDatabaseConnections(
				getConfig().getDBObjectPoolConfig().getDatabaseConnectionSet().iterator());
			dBPoolUtil.initPool();
		}
	}

	/**
	 * Tries to find the Client by the Id passed.
	 * <p>
	 * Note: This command is an expensive so do use it limitedly and
	 * cache the returned object. But before you start sending message to the 
	 * cached object do validate that ClientHandler with you is currently 
	 * connected and is pointing to the same clinet has it was before.
	 * This can be done as follows. <pre>
	foundClientHandler.isConnected(); //this method will through SocketException if not connected
	Date newTime = foundClientHandler.getClientConnectedTime();
	if(oldCachedTime!=newTime) {
		//Client had disconnected and ClientHandler was reused for
		//someother client, so write code to again find ur client
		foundClientHandler = handler.getServer().findFirstClientById("friendsid");
		...
	}</pre>
	 * </p>
	 * @see ClientIdentifiable
	 * @return ClientHandler object if client was found else <code>null</code>
	 * @since 1.3.1
	 */
	public ClientHandler findFirstClientById(String id) {
		return clientIdentifier.findFirstClientById(id);
	}

	/**
	 * Returns an iterator containing all the 
	 * {@link org.quickserver.net.server.ClientHandler} that
	 * are currently handling clients. 
	 * It is recommended not to change the collection under an iterator. 
	 *
	 * It is imperative that the user manually synchronize on the returned collection 
	 * when iterating over it: 
	 * <code><pre>
   Eg:

	ClientData foundClientData = null;
	Object syncObj = quickserver.getClientIdentifier().getObjectToSynchronize();
	synchronized(syncObj) {	
		Iterator iterator = quickserver.findAllClient();
		while(iterator.hasNext()) {
			foundClientHandler = (ClientHandler) iterator.next();
			....
		}
	}

	//OR

	ClientData foundClientData = null;
	ClientIdentifier clientIdentifier = quickserver.getClientIdentifier();
	synchronized(clientIdentifier.getObjectToSynchronize()) {	
		Iterator iterator = clientIdentifier.findAllClient();
		while(iterator.hasNext()) {
			foundClientHandler = (ClientHandler) iterator.next();
			....
		}
	}
   </code></pre>
	 * @since 1.3.1
	 */
	public Iterator findAllClient() {
		return clientIdentifier.findAllClient();
	}

	/**
	 * Tries to find the Client by the matching pattern passed to the Id.
	 * <p>
	 * Note: This command is an expensive so do use it limitedly and
	 * cache the returned object. But before you start sending message to the 
	 * cached object do validate that ClientHandler with you is currently 
	 * connected and is pointing to the same clinet has it was before.
	 * This can be done as follows. <pre>
	foundClientHandler.isConnected(); //this method will through SocketException if not connected
	Date newTime = foundClientHandler.getClientConnectedTime();
	if(oldCachedTime!=newTime) {
		//Client had disconnected and ClientHandler was reused for
		//someother client, so write code to again find ur client
		foundClientHandler = handler.getServer().findFirstClientById("friendsid");
		...
	}</pre>
	 * </p>
	 * @see ClientIdentifiable
	 * @return ClientHandler object if client was found else <code>null</code>
	 * @since 1.3.2
	 */
	public Iterator findAllClientById(String pattern) {
		return clientIdentifier.findAllClientById(pattern);
	}

	/**
	 * Tries to find the Client by the Key passed.
	 * <p>
	 * Note: This command is an expensive so do use it limitedly and
	 * cache the returned object. But before you start sending message to the 
	 * cached object do validate that ClientHandler with you is currently 
	 * connected and is pointing to the same clinet has it was before.
	 * This can be done as follows. <pre>
	foundClientHandler.isConnected(); //this method will through SocketException if not connected
	Date newTime = foundClientHandler.getClientConnectedTime();
	if(oldCachedTime!=newTime) {
		//Client had disconnected and ClientHandler was reused for
		//someother client, so write code to again find ur client
		foundClientHandler = handler.getServer().findClientByKey("friendskey");
		...
	}</pre>
	 * </p>
	 * @see ClientIdentifiable
	 * @return ClientHandler object if client was found else <code>null</code>
	 * @since 1.3.1
	 */
	public ClientHandler findClientByKey(String key) {
		return clientIdentifier.findClientByKey(key);
	}

	/**
	 * Tries to find the Client by the matching pattern passed to the key.
	 * <p>
	 * Note: This command is an expensive so do use it limitedly and
	 * cache the returned object. But before you start sending message to the 
	 * cached object do validate that ClientHandler with you is currently 
	 * connected and is pointing to the same clinet has it was before.
	 * This can be done as follows. <pre>
	foundClientHandler.isConnected(); //this method will through SocketException if not connected
	Date newTime = foundClientHandler.getClientConnectedTime();
	if(oldCachedTime!=newTime) {
		//Client had disconnected and ClientHandler was reused for
		//some other client, so write code to again find ur client
		foundClientHandler = handler.getServer().findFirstClientByKey("friendsid");
		...
	}</pre>
	 * </p>
	 * @see ClientIdentifiable
	 * @return ClientHandler object if client was found else <code>null</code>
	 * @since 1.4
	 */
	public Iterator findAllClientByKey(String pattern) {
		return clientIdentifier.findAllClientByKey(pattern);
	}

	/**
	 * Sets next client has a trusted client. 
	 * <p>This will skip any authentication and will not set any timout.</p>
	 * @since 1.3.2
	 */
	public void nextClientIsTrusted() {
		setSkipValidation(true);
	}
	/**
	 * @since 1.3.2
	 */
	private boolean getSkipValidation() {
		return skipValidation;
	}
	/**
	 * @since 1.3.2
	 */
	private synchronized void setSkipValidation(boolean validation) {
		skipValidation = validation;
	}

	/**
	 * Sets the communication logging flag.
	 * @see #getCommunicationLogging
	 * @since 1.3.2
	 */
	public void setCommunicationLogging(boolean communicationLogging) {
		this.communicationLogging = communicationLogging;
	}
	/**
	 * Returns the communication logging flag.
	 * @see #setCommunicationLogging
	 * @since 1.3.2
	 */
	public boolean getCommunicationLogging() {
		return communicationLogging;
	}

	/**
	 * Sets the SecurityManager class
	 * @param securityManagerClass the fully qualified name of the class 
	 * that extends {@link java.lang.SecurityManager}.
	 * @see #getSecurityManagerClass
	 * @since 1.3.3
	 */
	public void setSecurityManagerClass(String securityManagerClass) {
		if(securityManagerClass!=null)
			this.securityManagerClass = securityManagerClass;
	}
	/**
	 * Returns the SecurityManager class
	 * @see #setSecurityManagerClass
	 * @since 1.3.3
	 */
	public String getSecurityManagerClass() {
		return securityManagerClass;
	}

	public SecurityManager getSecurityManager() throws AppException {
		if(getSecurityManagerClass()==null)
			return null;
		SecurityManager sm = null;
		try {
			sm = (SecurityManager) 
				getClass(getSecurityManagerClass(), true).newInstance();
		} catch(ClassNotFoundException e) {
			throw new AppException(e.getMessage());
		} catch(InstantiationException e) {
			throw new AppException(e.getMessage());
		} catch(IllegalAccessException e) {
			throw new AppException(e.getMessage());
		}
		return sm;
	}

	/**
	 * Sets the Access constraints
	 * @since 1.3.3
	 */
	public void setAccessConstraintConfig(
			AccessConstraintConfig accessConstraintConfig) {
		this.accessConstraintConfig = accessConstraintConfig;
	}
	/**
	 * Returns Access constraints if present else <code>null</code>.
	 * @since 1.3.3
	 */
	public AccessConstraintConfig getAccessConstraintConfig() {
		return accessConstraintConfig;
	}

	/**
	 * Sets the classloader to be used to load the dynamicaly resolved 
	 * classes
	 * @since 1.3.3
	 */
	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
		Thread.currentThread().setContextClassLoader(classLoader);
	}

	/**
	 * Gets the classloader used to load the dynamicaly resolved 
	 * classes.
	 * @since 1.4.6
	 */
	public ClassLoader getClassLoader() {
		return classLoader;
	}

	/**
	 * Utility method to load a class
	 * @since 1.3.3
	 */
	 public Class getClass(String name, boolean reload) 
			throws ClassNotFoundException {
		if(name==null) throw new IllegalArgumentException("Class name can't be null!");
		logger.finest("Class: "+name+", reload: "+reload);
		if(reload==true && classLoader!=null) {
			return classLoader.loadClass(name);
		} else if(reload==true && classLoader==null && this.getClass().getClassLoader()!=null) {
			return this.getClass().getClassLoader().loadClass(name);
		} else if(reload==false && classLoader!=null) {
			return Class.forName(name, true, classLoader);
		} else /*if(reload==false && classLo

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -