📄 service.java
字号:
// 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;
import java.io.*;
import org.apache.log4j.*;
import org.apache.log4j.xml.*;
import java.util.*;
/**
* This is main library class. Your primary interface with SMSLib is via methods
* defined in this class.
*/
public class Service
{
private static final String LOG4J_CONF = "smslib-log4j.properties";
private static final String LOG4J_CONF_XML = "smslib-log4j.xml";
private Logger logger;
private List gtwList;
private Router router;
private LoadBalancer loadBalancer;
private WatchDog watchDog;
/**
* Configuration settings.
*
* @see Settings
*/
public Settings S;
/**
* Default Service constructor. Will set SMSLib to use its own logger.
*/
public Service()
{
S = new Settings();
try
{
logger = Logger.getLogger("org.smslib");
logger.setLevel(S.DEBUG ? Level.ALL : Level.WARN);
Properties p = new Properties();
p = loadLoggerProps(LOG4J_CONF);
if (p != null) PropertyConfigurator.configure(p);
else
{
if (new File(LOG4J_CONF).exists()) PropertyConfigurator.configure(LOG4J_CONF);
else if (new File(LOG4J_CONF_XML).exists()) DOMConfigurator.configure(LOG4J_CONF_XML);
else BasicConfigurator.configure();
}
logger.info(Library.getLibraryDescription());
logger.info("Version: " + Library.getLibraryVersion());
logger.info("JRE Version: " + System.getProperty("java.version"));
logger.info("JRE Impl Version: " + System.getProperty("java.vm.version"));
logger.info("O/S: " + System.getProperty("os.name") + " / " + System.getProperty("os.arch") + " / " + System.getProperty("os.version"));
gtwList = new ArrayList();
setRouter(new Router(this));
setLoadBalancer(new RoundRobinLoadBalancer(this));
}
catch (Exception e)
{
logger.fatal(e);
}
}
/**
* Service constructor. Will set SMSLib to use the provided log4j logger.
*
* @param logger
* A ready log4j logger to use.
*/
public Service(Logger logger)
{
S = new Settings();
this.logger = logger;
logger.setLevel(S.DEBUG ? Level.ALL : Level.WARN);
logger.info(Library.getLibraryDescription());
logger.info("Version: " + Library.getLibraryVersion());
logger.info("JRE Version: " + System.getProperty("java.version"));
logger.info("JRE Impl Version: " + System.getProperty("java.vm.version"));
logger.info("O/S: " + System.getProperty("os.name") + " / " + System.getProperty("os.arch") + " / " + System.getProperty("os.version"));
gtwList = new ArrayList();
setRouter(new Router(this));
setLoadBalancer(new RoundRobinLoadBalancer(this));
}
private Properties loadLoggerProps(String filename)
{
Properties props = new Properties();
try
{
props.load(getClass().getResourceAsStream(filename));
}
catch (Exception ex)
{
return null;
}
Enumeration keys = props.keys();
while (keys.hasMoreElements())
{
String prop = (String) keys.nextElement();
String val = props.getProperty(prop);
props.setProperty(prop, val);
}
return props;
}
/**
* Returns the logger used by SMSLib.
*
* @return The logger in use.
*/
public Logger getLogger()
{
return logger;
}
/**
* Adds a gateway to the list of gateways managed by the Service class.
*
* @param gtw
* The gateway to be added.
* @see #getGatewayList()
*/
public void addGateway(AGateway gtw)
{
synchronized (gtwList)
{
gtw.setService(this);
gtwList.add(gtw);
}
}
/**
* Initializes all gateways. This should be the first call before you use
* the Service class for sending/receiving messages. The call will try to
* start all defined gateways.
*
* @throws SMSLibException
* No Gateways are defined.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #stopService()
*/
public synchronized void startService() throws SMSLibException, TimeoutException, GatewayException, IOException, InterruptedException
{
watchDog = new WatchDog();
synchronized (gtwList)
{
if (gtwList.size() == 0) throw new SMSLibException("No gateways are defined.");
for (int i = 0, n = gtwList.size(); i < n; i++)
((AGateway) gtwList.get(i)).startGateway();
}
}
/**
* Stops all gateways - does not remove them from Service's internal list.
* Once stopped, all SMSLib operations will fail. You need to start the
* gateways again before proceeding.
*
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #startService()
*/
public synchronized void stopService() throws TimeoutException, GatewayException, IOException, InterruptedException
{
if (watchDog != null)
{
watchDog.interrupt();
watchDog.join();
}
synchronized (gtwList)
{
for (int i = 0, n = gtwList.size(); i < n; i++)
((AGateway) gtwList.get(i)).stopGateway();
}
}
/**
* Reads inbound messages from ALL gateways with the Inbound attribute set.
* When successful, the message list will contain all messages read.
*
* @param msgList
* A (probably empty) list that will be populated with Inbound
* messages read.
* @param msgClass
* Filtering: Class of messages that need to be read.
* @return The number of messages read.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see MessageClasses
*/
public int readMessages(List msgList, MessageClasses msgClass) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (gtwList)
{
for (int i = 0, n = gtwList.size(); i < n; i++)
{
AGateway gtw = (AGateway) gtwList.get(i);
if (gtw.isInbound())
{
try
{
readMessages(msgList, msgClass, gtw);
}
catch (TimeoutException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
catch (IOException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
}
}
}
return msgList.size();
}
/**
* Reads inbound messages from the SPECIFIC gateway. When successful, the
* message list will contain all messages read.
*
* @param msgList
* A (probably empty) list that will be populated with Inbound
* messages read.
* @param msgClass
* Filtering: Class of messages that need to be read.
* @param gtwId
* The identifier of the gateway from which to read messages.
* @return The number of messages read.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see MessageClasses
* @see AGateway
*/
public int readMessages(List msgList, MessageClasses msgClass, String gtwId) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (gtwList)
{
AGateway gtw = findGateway(gtwId);
if ((gtw != null) && (gtw.isInbound()))
{
try
{
readMessages(msgList, msgClass, gtw);
}
catch (TimeoutException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
catch (IOException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
}
}
return msgList.size();
}
/**
* Reads inbound messages from the SPECIFIC gateway. When successful, the
* message list will contain all messages read.
*
* @param msgList
* A (probably empty) list that will be populated with inbound
* messages read.
* @param msgClass
* Filtering: Class of messages that need to be read.
* @param gtw
* The gateway object from which to read messages.
* @return The number of messages read.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see MessageClasses
* @see AGateway
*/
public int readMessages(List msgList, MessageClasses msgClass, AGateway gtw) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (gtwList)
{
try
{
gtw.readMessages(msgList, msgClass);
}
catch (TimeoutException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
catch (IOException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
}
return msgList.size();
}
/**
* Reads a specific gateway for a message matching the given Memory Location
* and Memory Index.
* <p>
* This is a "dummy" approach. It does not implement the CGMR command,
* rather it reads all messages and searches for a match.
*
* @param gtwId
* The Gateway ID of the gateway to read from.
* @param memLoc
* The memory location string.
* @param memIndex
* The memory index.
* @return The message read. Null if no relevant message is found or if the
* Gateway ID given is invalid.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
*/
public InboundMessage readMessage(String gtwId, String memLoc, int memIndex) throws TimeoutException, GatewayException, IOException, InterruptedException
{
InboundMessage msg = null;
synchronized (gtwList)
{
AGateway gtw = findGateway(gtwId);
if ((gtw != null) && (gtw.isInbound()))
{
try
{
msg = gtw.readMessage(memLoc, memIndex);
}
catch (TimeoutException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
catch (IOException e)
{
logWarn("readMessages(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
}
}
}
return msg;
}
/**
* Sends a single message. The following logic is applied in order for
* SMSLib to decide from which gateway it will send the message:<br>
* 1. If the message holds gateway information (member field "gatewayId"),
* SMSLib will try to send it from that gateway.<br>
* 2. If the message does not hold gateway information (member field
* "gatewayId" is empty or "*") then if router and load balancer is
* defined, then message is processed by these classes.<br>
* 3. Otherwise the method selects the first outbound-capable gateway
* defined and sends the message from it.<br>
* The method blocks until the message is actually sent (synchronous
* operation).
*
* @param msg
* An OutboundMessage object.
* @return True if the message is sent.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #queueMessage(OutboundMessage)
*/
public boolean sendMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (gtwList)
{
AGateway gtw = routeMessage(msg);
if (gtw != null)
{
try
{
return gtw.sendMessage(msg);
}
catch (TimeoutException e)
{
logWarn("sendMessage(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
msg.setMessageStatus(MessageStatuses.FAILED);
msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
return false;
}
catch (IOException e)
{
logWarn("sendMessage(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
msg.setMessageStatus(MessageStatuses.FAILED);
msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
return false;
}
}
else return false;
}
}
/**
* Sends a single message from the specified gateway.
*
* @param msg
* An OutboundMessage object.
* @param gtwId
* The id of the gateway that will be used for sending.
* @return True if the message is sent.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #sendMessage(OutboundMessage)
*/
public boolean sendMessage(OutboundMessage msg, String gtwId) throws TimeoutException, GatewayException, IOException, InterruptedException
{
msg.setGatewayId(gtwId);
return sendMessage(msg);
}
/**
* Sends a list of messages.
*
* @param msgList
* A list of OutboundMessage objects.
* @return The number of messages sent.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #sendMessage(OutboundMessage)
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -