service.java

来自「手机业务程序例子」· Java 代码 · 共 198 行

JAVA
198
字号
/*
 * Service.java
 *
 * Created on 2003年4月6日, 下午2:59
 */

package mobile.smsservice;
import java.util.*;
import org.apache.log4j.*;
import java.sql.*;
import java.io.*;
/**
 *
 * @author  hkai
 */
public abstract class Service
{
	protected Properties serviceProperty;
	protected Logger log;
	
	protected Connection conn;
	private static Random random= new Random();
	private String serviceName;
	private Operator[] operators;
	
	/** Creates a new instance of Service */
	public Service(Properties p)throws ServiceException
	{
		serviceProperty= p;
	}
	
	public Service(String propertyFileName)throws ServiceException
	{
		initProperty(propertyFileName);
	}
	
	
	private void initProperty(String url) throws ServiceException
	{
		try
		{
			serviceProperty= new Properties();
			serviceProperty.load(new FileInputStream(url));
		} catch(Exception e)
		{
			throw new ServiceException("init property error:"+e.getMessage());
		}
	}
	
	protected void initService()throws ServiceException
	{
		if( serviceProperty.contains("service.name"))
		{
			serviceName= serviceProperty.getProperty("service.name");
		}
		
		
		//load operators
		connectDatabase();
		try
		{
			Vector os= new Vector();
			
			Connection conn= getConnection();
			Statement st= conn.createStatement();
			ResultSet rs= st.executeQuery("select * from operator where enabled=1");
			while( rs.next())
			{
				Operator o = new Operator();
				o.pattern= rs.getString("pattern");
				o.operator= rs.getString("operator");
				
				os.add( o);
				
			}
			rs.close();
			st.close();
			operators = new Operator[os.size()];
			os.copyInto(operators);
		}catch(Exception e)
		{
			log.error(e);
			e.printStackTrace();
		}
		disconnectDatabase();
		log.info("database initialized");
		
	}
	
	protected void initLogger(String name)
	{
		BasicConfigurator.configure();
		log= Logger.getLogger(name);
		serviceName= name;
		//if( System.getProperty("os.name").toLowerCase().startsWith("linux"))
		
			String dir= serviceProperty.getProperty("service.log");
			try
			{
				log.addAppender(new DailyRollingFileAppender(new PatternLayout("%d [%t] %p %c %x - %m%n"), dir+name+".log", "'.'yyyy-MM-dd"));
			}catch(Exception e)
			{
				log.error(e.getMessage());
			}
		
	}
	
	public void connectDatabase() throws ServiceException
	{
		try
		{
			if( conn!=null)
				return;
        		//need to initialize the driver first
			if( serviceProperty.containsKey("db.driver"))
			{
				Class.forName(serviceProperty.getProperty("db.driver")).newInstance();
			}
                        
			if( serviceProperty.containsKey("db.url"))
			{
				conn= DriverManager.getConnection(serviceProperty.getProperty("db.url"), serviceProperty.getProperty("db.username"), serviceProperty.getProperty("db.password"));
				log.info("Database server connected");
				
			}
			else
				log.info("No database connect infomation!");
		} catch(Exception e)
		{
			e.printStackTrace();
			throw new ServiceException("database connect error:"+ e.getMessage());
		}
	}
	
	public void disconnectDatabase()
	{
		try
		{
			if(conn!=null )
			{
				conn.close();
				log.info("Database disconnected!");
				conn=null;
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
		}
	}
	
	public Connection getConnection()
	{
		return conn;
	}
	abstract public void execute() throws ServiceException;
	
	public static void printHelpMessage()
	{
		//p:s:d:g:t:b:e:z:
		System.out.println("p: gateway property file name");
	}
	
	//split phone numbers according to the operators
	public Vector splitPhoneNumber(Vector srcTerms)
	{
		String[] ts= new String[ srcTerms.size()];
		srcTerms.copyInto(ts);
		Vector checkedUsers= new Vector();		
		for( int i=0; i< operators.length;i++)
		{
			operators[i].checkUser(ts);
			checkedUsers.add( operators[i].users);
		}
		return checkedUsers;
	}
	
	class Operator
	{
		String pattern, operator;
		Vector users;
		public Operator()
		{
			users= new Vector();
		}
		
		public void checkUser( String[] srcTerms)
		{
			for( int i=0; i< srcTerms.length;i++)
			{
				if( srcTerms[i].matches( pattern))
					users.add( srcTerms[i]);
			}
		}
	}
	
}

⌨️ 快捷键说明

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