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

📄 smsserver.java

📁 java平台 smslib 短 信 开 发 包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// Copyright (C) 2002-2008, Thanasis Delenikas, Athens/GREECE.
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib.smsserver;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import org.smslib.InboundMessage;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Message.MessageTypes;
import org.smslib.smsserver.gateways.AGateway;
import org.smslib.smsserver.interfaces.Interface;
import org.smslib.smsserver.interfaces.Interface.InterfaceTypes;

/**
 * SMSServer Application.
 */
public class SMSServer
{
	Service srv;

	Properties props;

	boolean shutdown = false;

	List<Interface<? extends Object>> infList;

	InboundNotification inboundNotification;

	OutboundNotification outboundNotification;

	CallNotification callNotification;

	InboundPollingThread inboundPollingThread;

	OutboundPollingThread outboundPollingThread;

	boolean optRunOnce = false;

	public SMSServer()
	{
		this.srv = new Service();
		this.infList = new ArrayList<Interface<? extends Object>>();
		Runtime.getRuntime().addShutdownHook(new Shutdown());
		this.inboundNotification = new InboundNotification();
		this.outboundNotification = new OutboundNotification();
		this.callNotification = new CallNotification();
		this.inboundPollingThread = null;
		this.outboundPollingThread = null;
		this.srv.setInboundNotification(this.inboundNotification);
		this.srv.setOutboundNotification(this.outboundNotification);
		this.srv.setCallNotification(this.callNotification);
	}

	public Service getService()
	{
		return this.srv;
	}

	public List<Interface<? extends Object>> getInfList()
	{
		return this.infList;
	}

	public Properties getProperties()
	{
		return this.props;
	}

	class Shutdown extends Thread
	{
		@Override
		public void run()
		{
			getService().getLogger().logInfo("SMSServer shutting down, please wait...", null, null);
			SMSServer.this.shutdown = true;
			try
			{
				stopInterfaces();
				getService().stopService();
				if (SMSServer.this.inboundPollingThread != null)
				{
					SMSServer.this.inboundPollingThread.interrupt();
					SMSServer.this.inboundPollingThread.join();
				}
				if (SMSServer.this.outboundPollingThread != null)
				{
					SMSServer.this.outboundPollingThread.interrupt();
					SMSServer.this.outboundPollingThread.join();
				}
			}
			catch (Exception e)
			{
				getService().getLogger().logError("Shutdown hook error.", e, null);
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void loadConfiguration() throws Exception
	{
		FileInputStream f;
		this.props = new Properties();
		if (System.getProperty("smsserver.configdir") != null) f = new FileInputStream(System.getProperty("smsserver.configdir") + "SMSServer.conf");
		else if (System.getProperty("smsserver.configfile") != null) f = new FileInputStream(System.getProperty("smsserver.configfile"));
		else throw new SMSLibException("Cannot find SMSServer configuration file!");
		getProperties().load(f);
		f.close();
		if (getProperties().getProperty("smsserver.balancer", "").length() > 0)
		{
			try
			{
				Object[] args = new Object[] { getService() };
				Class<?>[] argsClass = new Class[] { Service.class };
				Class<?> c = Class.forName("org.smslib.balancing." + getProperties().getProperty("smsserver.balancer", ""));
				Constructor<?> constructor = c.getConstructor(argsClass);
				org.smslib.balancing.LoadBalancer balancer = (org.smslib.balancing.LoadBalancer) constructor.newInstance(args);
				getService().setLoadBalancer(balancer);
				getService().getLogger().logInfo("SMSServer: set balancer to: " + getProperties().getProperty("smsserver.balancer", ""), null, null);
			}
			catch (Exception e)
			{
				getService().getLogger().logError("SMSServer: error setting custom balancer!", null, null);
			}
		}
		if (getProperties().getProperty("smsserver.router", "").length() > 0)
		{
			try
			{
				Object[] args = new Object[] { getService() };
				Class<?>[] argsClass = new Class[] { Service.class };
				Class<?> c = Class.forName("org.smslib.routing." + getProperties().getProperty("smsserver.router", ""));
				Constructor<?> constructor = c.getConstructor(argsClass);
				org.smslib.routing.Router router = (org.smslib.routing.Router) constructor.newInstance(args);
				getService().setRouter(router);
				getService().getLogger().logInfo("SMSServer: set router to: " + getProperties().getProperty("smsserver.router", ""), null, null);
			}
			catch (Exception e)
			{
				getService().getLogger().logError("SMSServer: error setting custom balancer!", null, null);
			}
		}
		for (int i = 0; i < Integer.MAX_VALUE; i++)
		{
			try
			{
				String propName = "gateway." + i;
				String propValue = getProperties().getProperty(propName, "");
				if (propValue.length() == 0) break;
				StringTokenizer tokens = new StringTokenizer(propValue, ",");
				String gtwId = tokens.nextToken().trim();
				String gtwClass = tokens.nextToken().trim();
				Object[] args = new Object[] { gtwId, getProperties(), this };
				Class<?>[] argsClass = new Class[] { String.class, Properties.class, SMSServer.class };
				Class<?> c = Class.forName("org.smslib.smsserver.gateways." + gtwClass);
				Constructor<?> constructor = c.getConstructor(argsClass);
				AGateway gtw = (AGateway) constructor.newInstance(args);
				gtw.create();
				getService().addGateway(gtw.getGateway());
				getService().getLogger().logInfo("SMSServer: added gateway " + gtwId + " / " + gtw.getDescription(), null, null);
			}
			catch (Exception e)
			{
				getService().getLogger().logError("SMSServer: Unknown Gateway in configuration file!", null, null);
			}
		}
		for (int i = 0; i < Integer.MAX_VALUE; i++)
		{
			try
			{
				String propName = "interface." + i;
				String propValue = getProperties().getProperty(propName, "");
				if (propValue.length() == 0) break;
				StringTokenizer tokens = new StringTokenizer(propValue, ",");
				String infId = tokens.nextToken().trim();
				String infClass = tokens.nextToken().trim();
				InterfaceTypes infType = null;
				String sinfType = tokens.hasMoreTokens() ? tokens.nextToken() : "inoutbound";
				sinfType = sinfType.trim();
				if ("inbound".equalsIgnoreCase(sinfType))
				{
					infType = InterfaceTypes.INBOUND;
				}
				else if ("outbound".equalsIgnoreCase(sinfType))
				{
					infType = InterfaceTypes.OUTBOUND;
				}
				else
				{ // NULL or other crap
					infType = InterfaceTypes.INOUTBOUND;
				}
				Object[] args = new Object[] { infId, getProperties(), this, infType };
				Class<?>[] argsClass = new Class[] { String.class, Properties.class, SMSServer.class, InterfaceTypes.class };
				Class<?> c = Class.forName("org.smslib.smsserver.interfaces." + infClass);
				Constructor<?> constructor = c.getConstructor(argsClass);
				Interface<? extends Object> inf = (Interface<? extends Object>) constructor.newInstance(args);
				getInfList().add(inf);
				getService().getLogger().logInfo("SMSServer: added interface " + infId + " / " + inf.getDescription() + " / " + inf.getType(), null, null);
			}
			/* Check for exceptions thrown by the constructor itself */
			catch (InvocationTargetException e)
			{
				getService().getLogger().logError("SMSServer: Illegal Interface configuration: " + e.getCause().getMessage(), null, null);
			}
			catch (Exception e)
			{
				getService().getLogger().logError("SMSServer: Unknown Interface in configuration file!", null, null);
			}
		}
	}

	class InboundPollingThread extends Thread
	{
		@Override
		public void run()
		{
			try
			{
				while (!SMSServer.this.shutdown)
				{
					getService().getLogger().logDebug("InboundPollingThread() run.", null, null);

⌨️ 快捷键说明

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