Skip to content

Commit 923a9ed

Browse files
committed
AJ-281 - Cleanup
1 parent 13c9092 commit 923a9ed

9 files changed

+118
-114
lines changed

src/main/java/org/asteriskjava/fastagi/AbstractAgiServer.java

+30-19
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,42 @@ public abstract class AbstractAgiServer
5454
/**
5555
* The factory to use for creating new AgiChannel instances.
5656
*/
57-
private AgiChannelFactory apiChannelFactory;
57+
private final AgiChannelFactory agiChannelFactory;
5858

5959
private volatile boolean die = false;
60-
61-
public AbstractAgiServer() {
62-
this(new DefaultAgiChannelFactory());
63-
}
64-
65-
public AbstractAgiServer(AgiChannelFactory apiChannelFactory) {
66-
if (apiChannelFactory == null) {
67-
logger.warn("apiChannelFactory is null; use default");
68-
this.apiChannelFactory = new DefaultAgiChannelFactory();
69-
} else {
70-
logger.debug("use channelFactory: " + apiChannelFactory.getClass().getCanonicalName());
71-
this.apiChannelFactory = apiChannelFactory;
72-
}
60+
61+
public AbstractAgiServer()
62+
{
63+
this(new DefaultAgiChannelFactory());
64+
}
65+
66+
/**
67+
* Creates a new AbstractAgiServer with the given channel factory.
68+
*
69+
* @param agiChannelFactory the AgiChannelFactory to use for creating new AgiChannel instances.
70+
* @since 1.0.0
71+
*/
72+
public AbstractAgiServer(AgiChannelFactory agiChannelFactory)
73+
{
74+
if (agiChannelFactory == null)
75+
{
76+
throw new IllegalArgumentException("AgiChannelFactory must not be null");
77+
}
78+
79+
logger.debug("Using channelFactory " + agiChannelFactory.getClass().getCanonicalName());
80+
this.agiChannelFactory = agiChannelFactory;
7381
}
74-
82+
7583
/**
76-
* The used factory for creating new AgiChannel instances.
84+
* Returns the AgiChannelFactory to use for creating new AgiChannel instances.
85+
*
86+
* @return the AgiChannelFactory to use for creating new AgiChannel instances.
7787
*/
78-
protected AgiChannelFactory getAgiChannelFactory() {
79-
return this.apiChannelFactory;
88+
protected AgiChannelFactory getAgiChannelFactory()
89+
{
90+
return this.agiChannelFactory;
8091
}
81-
92+
8293
/**
8394
* Returns the default number of worker threads in the thread pool.
8495
*

src/main/java/org/asteriskjava/fastagi/AsyncAgiServer.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,27 @@ public class AsyncAgiServer extends AbstractAgiServer implements ManagerEventLis
2727
private final Map<Integer, AsyncAgiConnectionHandler> connectionHandlers;
2828

2929
/**
30-
* Creates a new AsyncAgiServer.<p>
30+
* Creates a new AsyncAgiServer with a {@link DefaultAgiChannelFactory}.<p>
3131
* Note that you must set a {@link org.asteriskjava.fastagi.MappingStrategy} before using it.
32-
* @see #setMappingStrategy(MappingStrategy)
32+
*
33+
* @see #setMappingStrategy(MappingStrategy)
3334
*/
3435
public AsyncAgiServer()
3536
{
36-
this(new DefaultAgiChannelFactory());
37+
this(new DefaultAgiChannelFactory());
3738
}
38-
39+
3940
/**
40-
* Creates a new AsyncAgiServer.<p>
41+
* Creates a new AsyncAgiServer with a custom {@link AgiChannelFactory}.<p>
4142
* Note that you must set a {@link org.asteriskjava.fastagi.MappingStrategy} before using it.
42-
* @see #setMappingStrategy(MappingStrategy)
43+
*
4344
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
45+
* @see #setMappingStrategy(MappingStrategy)
46+
* @since 1.0.0
4447
*/
4548
public AsyncAgiServer(AgiChannelFactory agiChannelFactory)
4649
{
47-
super(agiChannelFactory);
50+
super(agiChannelFactory);
4851
this.connectionHandlers = new HashMap<Integer, AsyncAgiConnectionHandler>();
4952
}
5053

@@ -59,18 +62,18 @@ public AsyncAgiServer(AgiChannelFactory agiChannelFactory)
5962
*/
6063
public AsyncAgiServer(MappingStrategy mappingStrategy)
6164
{
62-
this(mappingStrategy, new DefaultAgiChannelFactory());
63-
logger.debug("use default AgiChannelFactory");
65+
this(mappingStrategy, new DefaultAgiChannelFactory());
66+
logger.debug("use default AgiChannelFactory");
6467
}
65-
68+
6669
/**
6770
* Creates a new AsyncAgiServer with the given MappingStrategy.<p>
6871
* Please note that Async AGI does not currently support passing a script name, so your
6972
* MappingStrategy must be aware that the {@link org.asteriskjava.fastagi.AgiRequest#getScript() script}
7073
* property of the AgiRequests will likely be <code>null</code>.
7174
*
72-
* @param mappingStrategy the MappingStrategy to use to determine which AGI script to run
73-
* for a certain request.
75+
* @param mappingStrategy the MappingStrategy to use to determine which AGI script to run
76+
* for a certain request.
7477
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
7578
*/
7679
public AsyncAgiServer(MappingStrategy mappingStrategy, AgiChannelFactory agiChannelFactory)
@@ -84,7 +87,7 @@ public AsyncAgiServer(MappingStrategy mappingStrategy, AgiChannelFactory agiChan
8487
* request.<p>
8588
* Internally this constructor uses a {@link org.asteriskjava.fastagi.StaticMappingStrategy}.
8689
*
87-
* @param agiScript the AGI script to execute.
90+
* @param agiScript the AGI script to execute.
8891
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
8992
*/
9093
public AsyncAgiServer(AgiScript agiScript, AgiChannelFactory agiChannelFactory)
@@ -102,11 +105,11 @@ public AsyncAgiServer(AgiScript agiScript, AgiChannelFactory agiChannelFactory)
102105
*/
103106
public AsyncAgiServer(AgiScript agiScript)
104107
{
105-
this(agiScript, new DefaultAgiChannelFactory());
106-
logger.debug("use default AgiChannelFactory");
108+
this(agiScript, new DefaultAgiChannelFactory());
109+
logger.debug("use default AgiChannelFactory");
107110
}
108111

109-
112+
110113
public void onManagerEvent(ManagerEvent event)
111114
{
112115
if (event instanceof AsyncAgiEvent)
@@ -130,7 +133,7 @@ private void handleAsyncAgiEvent(AsyncAgiEvent asyncAgiEvent)
130133

131134
if (asyncAgiEvent.isStart())
132135
{
133-
connectionHandler = new AsyncAgiConnectionHandler(getMappingStrategy(), asyncAgiEvent,this.getAgiChannelFactory());
136+
connectionHandler = new AsyncAgiConnectionHandler(getMappingStrategy(), asyncAgiEvent, this.getAgiChannelFactory());
134137
setConnectionHandler(connection, channelName, connectionHandler);
135138
try
136139
{

src/main/java/org/asteriskjava/fastagi/DefaultAgiServer.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class DefaultAgiServer extends AbstractAgiServer implements AgiServer
4949
private static final int DEFAULT_BIND_PORT = 4573;
5050

5151
private ServerSocketFacade serverSocket;
52-
52+
5353
private String configResourceBundleName = DEFAULT_CONFIG_RESOURCE_BUNDLE_NAME;
5454
private int port = DEFAULT_BIND_PORT;
5555

@@ -59,18 +59,19 @@ public class DefaultAgiServer extends AbstractAgiServer implements AgiServer
5959
public DefaultAgiServer()
6060
{
6161
this(null, null);
62-
62+
6363
}
6464

6565
/**
6666
* Creates a new DefaultAgiServer and set a custom factory for creating AgiChannels
6767
*
6868
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
6969
*/
70-
public DefaultAgiServer(AgiChannelFactory agiChannelFactory) {
71-
this(null,null,agiChannelFactory);
70+
public DefaultAgiServer(AgiChannelFactory agiChannelFactory)
71+
{
72+
this(null, null, agiChannelFactory);
7273
}
73-
74+
7475
/**
7576
* Creates a new DefaultAgiServer and loads its configuration from an alternative resource bundle.
7677
*
@@ -113,22 +114,22 @@ public DefaultAgiServer(AgiScript agiScript)
113114
*/
114115
public DefaultAgiServer(String configResourceBundleName, MappingStrategy mappingStrategy)
115116
{
116-
this(configResourceBundleName, mappingStrategy, new DefaultAgiChannelFactory());
117+
this(configResourceBundleName, mappingStrategy, new DefaultAgiChannelFactory());
117118
}
118-
119+
119120
/**
120121
* Creates a new DefaultAgiServer and loads its configuration from an alternative resource bundle and
121122
* uses the given {@link MappingStrategy}.
122123
*
123124
* @param configResourceBundleName the name of the conifiguration resource bundle (default is "fastagi").
124125
* @param mappingStrategy the MappingStrategy to use to determine the AgiScript to run.
125-
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
126+
* @param agiChannelFactory The factory to use for creating new AgiChannel instances.
126127
* @since 1.0.0
127128
*/
128129
public DefaultAgiServer(String configResourceBundleName, MappingStrategy mappingStrategy, AgiChannelFactory agiChannelFactory)
129130
{
130-
super(agiChannelFactory);
131-
131+
super(agiChannelFactory);
132+
132133
if (mappingStrategy == null)
133134
{
134135
final CompositeMappingStrategy compositeMappingStrategy = new CompositeMappingStrategy();
@@ -173,7 +174,8 @@ public DefaultAgiServer(String configResourceBundleName, MappingStrategy mapping
173174
* @param bindPort the port to bind to.
174175
* @deprecated use {@see #setPort(int)} instead
175176
*/
176-
@Deprecated public void setBindPort(int bindPort)
177+
@Deprecated
178+
public void setBindPort(int bindPort)
177179
{
178180
this.port = bindPort;
179181
}
@@ -307,7 +309,7 @@ public void startup() throws IOException, IllegalStateException
307309
{
308310
execute(connectionHandler);
309311
}
310-
catch(RejectedExecutionException e)
312+
catch (RejectedExecutionException e)
311313
{
312314
logger.warn("Execution was rejected by pool. Try to increase the pool size.");
313315
// release resources like closing the socket if execution was rejected due to the pool size
@@ -320,7 +322,8 @@ public void startup() throws IOException, IllegalStateException
320322
/**
321323
* @deprecated use {@link #startup()} instead.
322324
*/
323-
@Deprecated public void run()
325+
@Deprecated
326+
public void run()
324327
{
325328
try
326329
{
@@ -380,7 +383,8 @@ protected void finalize() throws Throwable
380383
* @throws Exception if the server can't be started
381384
* @deprecated since 1.0.0 use {@link org.asteriskjava.Cli} instead.
382385
*/
383-
@Deprecated public static void main(String[] args) throws Exception
386+
@Deprecated
387+
public static void main(String[] args) throws Exception
384388
{
385389
final AgiServer server;
386390

src/main/java/org/asteriskjava/fastagi/internal/AgiChannelFactory.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
import org.asteriskjava.util.SocketConnectionFacade;
66

77
/**
8-
* An AgiChannelFactory creates instances of AgiChannels,
8+
* An AgiChannelFactory creates instances of AgiChannels,
99
* that are passed to agi scripts.
10-
*
11-
* An instance of the AgiChannelFactory can be passed to the
12-
* DefaultAgiServer's constructor.
10+
* <p/>
11+
* An instance of the AgiChannelFactory can be passed to the
12+
* DefaultAgiServer's constructor.
13+
*
14+
* @since 1.0.0
1315
*/
14-
public interface AgiChannelFactory {
15-
16-
AgiChannel Create(AgiRequest request, SocketConnectionFacade socket);
17-
18-
AgiChannel Create(AgiRequest request, AgiWriter agiWriter, AgiReader agiReader);
16+
public interface AgiChannelFactory
17+
{
18+
/**
19+
* Creates a new AgiChannel.
20+
*
21+
* @param request the request to build the channel for.
22+
* @param agiWriter the writer.
23+
* @param agiReader the reader.
24+
* @return the created channel.
25+
*/
26+
AgiChannel createAgiChannel(AgiRequest request, AgiWriter agiWriter, AgiReader agiReader);
1927
}

src/main/java/org/asteriskjava/fastagi/internal/AgiChannelImpl.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* Default implementation of the AgiChannel interface.
26-
*
26+
*
2727
* @author srt
2828
* @version $Id$
2929
*/
@@ -35,11 +35,6 @@ public class AgiChannelImpl implements AgiChannel
3535

3636
private AgiReply lastReply;
3737

38-
public AgiChannelImpl(AgiRequest request, SocketConnectionFacade socket)
39-
{
40-
this(request,new FastAgiWriter(socket),new FastAgiReader(socket));
41-
}
42-
4338
protected AgiChannelImpl(AgiRequest request, AgiWriter agiWriter, AgiReader agiReader)
4439
{
4540
this.request = request;
@@ -83,7 +78,7 @@ public synchronized AgiReply sendCommand(AgiCommand command) throws AgiException
8378

8479
return lastReply;
8580
}
86-
81+
8782
public void answer() throws AgiException
8883
{
8984
sendCommand(new AnswerCommand());
@@ -317,7 +312,7 @@ public void sayDateTime(long time) throws AgiException
317312
{
318313
sendCommand(new SayDateTimeCommand(time));
319314
}
320-
315+
321316
public String databaseGet(String family, String key) throws AgiException
322317
{
323318
sendCommand(new DatabaseGetCommand(family, key));
@@ -495,7 +490,7 @@ public void continueAt(String context, String extension, String priority) throws
495490
setExtension(extension);
496491
setPriority(priority);
497492
}
498-
493+
499494
public void gosub(String context, String extension, String priority) throws AgiException
500495
{
501496
sendCommand(new GosubCommand(context, extension, priority));

0 commit comments

Comments
 (0)