论坛首页 Java企业应用论坛

Embebed jetty + h2 + jdic + substance => “桌面应用”也可以这么开发

浏览 2335 次
精华帖 (0) :: 良好帖 (6) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-08-14   最后修改:2010-08-14

1. 搞一个差不多的界面,要不要登陆之类的就看自己情况了;

https://substance.dev.java.net/learn.html

 

2. 适当地方放一个jdic webbrowser的jpanel,在windows下虽然只能嵌入IE,但做一个本地软件速度应该不慢了;

 

3. 嵌入一个Jetty Server和H2 Server

我还是把datasource放到jndi了,当然也可以不这么搞。(我用的是jetty6.1x)

这个,我干脆献丑上个代码妥了——

 

package com.cisee.bsw.server;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.Properties;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.naming.NamingException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.dy.common.util.PropHelper;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.plus.naming.Resource;
import org.mortbay.jetty.webapp.WebAppContext;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ServerLauncher {
	
	Logger log = LogManager.getLogger(ServerLauncher.class);
	
	String locatePath = ServerLauncher.class.getResource("/").getPath() + "..";
    Properties pp;
	
	static ServerLauncher instance;
	
    public static ServerLauncher getInstance(){
    	if(instance == null){
    		instance = new ServerLauncher();
    	}
    	return instance;
    }
    
    private ServerLauncher() {
    	pp = PropHelper.loadProperties("/cisee.properties", PropHelper.SCOPE_CP);
    }
    
    private String c(String key){
    	return pp.getProperty(key);
    }

    private int ci(String key){
    	try {
    		return Integer.parseInt(c(key));
		} catch (Exception e) {
			return 0;
		}
    }	

    // jetty server
	private Server server;
	private org.h2.tools.Server h2Server;
	
	private void startH2() throws SQLException{
		String[] args = (c("h2_server_args") + "  -baseDir " + c("h2_data_dir")).split(" ");
		h2Server = org.h2.tools.Server.createTcpServer(args);
		h2Server.start();
	}
	
	public void start() {
		// start db server
		try {
			startH2();
			log.info("H2 Database Server Started..");
		} catch (SQLException e) {
			log.error(e);
			log.error("H2 Database Server Start Failed..");
		}
		
		server = new Server(ci("port"));
		WebAppContext context = new WebAppContext();
		context.setContextPath("/");
		context.setWar(locatePath + c("context_path"));
		
		// jndi - datasource
		try {
			ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
			pooledDataSource.setDriverClass(c("driver"));
			pooledDataSource.setJdbcUrl(c("url"));
			pooledDataSource.setUser(c("u"));
			pooledDataSource.setPassword(c("p"));
			
			pooledDataSource.setMaxStatements(ci("max_stmt"));
			pooledDataSource.setMinPoolSize(ci("min_pool_size"));
			pooledDataSource.setMaxPoolSize(ci("max_pool_size"));
			pooledDataSource.setInitialPoolSize(ci("init_pool_size"));
			pooledDataSource.setMaxIdleTime(ci("max_idle_time"));
			new Resource(c("jndi_name"), pooledDataSource);
		} catch (PropertyVetoException e) {
			log.error(e);
		} catch (NamingException e) {
			log.error(e);
		}
		
		server.addHandler(context);
		server.setStopAtShutdown(true);
		try {
			server.start();
			log.info("Jetty Server Started..");
		} catch (Exception e) {
			log.error(e);
		}
	}
	
	public void stop(){
		if(server != null){
			try {
				server.stop();
				log.info("Jetty Server Stopped..");
			} catch (Exception e) {
				log.error(e);
			}
		}
		
		if(h2Server != null){
			h2Server.stop();
			log.info("H2 Database Server Stopped..");
		}
	}
	
	public static void main(String[] args) {
		ServerLauncher launcher = ServerLauncher.getInstance();
		launcher.start();
		
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		try {
			while(true){
				String line;
				line = r.readLine();
				if("quit".equals(line)){
					launcher.stop();
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}

 

    web.xml里最好加一个h2的consoleservlet

<servlet>

		<servlet-name>H2Console</servlet-name>
		<servlet-class>org.h2.server.web.WebServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>H2Console</servlet-name>
		<url-pattern>/console/*</url-pattern>
	</servlet-mapping>

 

4. 当然为了好搞热部署或开发,弄一个lightweight的脚本开发支架就更好点;

 

5. 最后用jsmooth或launch4j打个包,这么搞下子一般“领导”就能糊弄过去了;嘿嘿

 

 

BTW——本来有个需求,做桌面应用的,本打算用swing(其他的不会么,而且swing也菜的很),可是用户对LAF要求太高了,拿css的一些思想做设计,比如,一些button的hover效果,一个**渐变背景图片,控件的位置要很灵活——反正一些需求不是像修改个html几行代码就能搞定了,我对swing的LAF定制也没啥研究,所以就有了以上这个变通。用起来还不错!

   发表时间:2010-08-17  
代码方面可以参考——

http://key232323.iteye.com/blog/737814
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics