📄 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
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// 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";
protected Logger logger;
protected List gtwList;
protected Router router;
protected LoadBalancer loadBalancer;
/**
* Default Service constructor. Will set SMSLib to use its own logger. By
* default, the logger is set to WARN level. If you define the
* "smslib.debug" property, it will set itself to DEBUG level.
*/
public Service()
{
logger = Logger.getLogger("org.smslib");
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();
if (System.getProperty("smslib.debug") != null) logger.setLevel(Level.ALL);
else logger.setLevel(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"));
initService();
setRouter(new Router(this));
setLoadBalancer(new RoundRobinLoadBalancer(this));
}
/**
* Service constructor. Will set SMSLib to use your provided log4j logger.
*
* @param logger
*/
public Service(Logger logger)
{
this.logger = logger;
initService();
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)
{
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
{
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
{
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 succesful, 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
{
for (int i = 0, n = gtwList.size(); i < n; i++)
{
AGateway gateway = (AGateway) gtwList.get(i);
if (gateway.isInbound()) readMessages(msgList, msgClass, gateway);
}
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
{
AGateway gateway = findGateway(gtwId);
if ((gateway != null) && (gateway.isInbound())) readMessages(msgList, msgClass, gateway);
return msgList.size();
}
/**
* Reads inbound messages from the SPECIFIC gateway. When succesfull, 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 gateway
* 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 gateway) throws TimeoutException, GatewayException, IOException, InterruptedException
{
gateway.readMessages(msgList, msgClass);
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;
AGateway gateway = findGateway(gtwId);
if ((gateway != null) && (gateway.isInbound())) msg = gateway.readMessage(memLoc, memIndex);
return msg;
}
/**
* Sends a single message. If router and load balancer is defined, then
* message is processed by these classes. Otherwise the method selects the
* first outbound-capable gateway defined and sends the message from it. 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
{
AGateway gtw = routeMessage(msg);
if (gtw != null) return gtw.sendMessage(msg);
else return false;
}
/**
* Sends a single message from the specified gateway. The method blocks
* until the message are actually sent (synchronous operation).
*
* @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 #queueMessage(OutboundMessage)
*/
public boolean sendMessage(OutboundMessage msg, String gtwId) throws TimeoutException, GatewayException, IOException, InterruptedException
{
AGateway gtw = findGateway(gtwId);
if (gtw != null) return gtw.sendMessage(msg);
else return false;
}
/**
* Sends a list of messages. The method selects the first outbound-capable
* gateway defined and sends the messages from it. The method blocks until
* the messages are actually sent (synchronous operation).
*
* @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 #queueMessage(OutboundMessage, String)
*/
public int sendMessages(List msgList) throws TimeoutException, GatewayException, IOException, InterruptedException
{
for (int i = 0, n = gtwList.size(); i < n; i++)
{
AGateway gateway = (AGateway) gtwList.get(i);
if (gateway.isOutbound()) return gateway.sendMessages(msgList);
}
return 0;
}
/**
* Sends a list of messages from the specified gateway. The method blocks
* until the messages are actually sent (synchronous operation).
*
* @param msgList
* A list of OutboundMessage objects.
* @param gtwId
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -