summaryrefslogtreecommitdiff
path: root/java/netsvcs/Logger/ServerLoggingAcceptor.java
blob: a44f2cfa58502776cac9289efbf1acec6fb2ae0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Logger
 *
 * = FILENAME
 *    ServerLoggingAcceptor.java
 *
 *@author Chris Cleeland, Everett Anderson
 *
 *************************************************/
package netsvcs.Logger;

import JACE.SOCK_SAP.*;
import JACE.Connection.*;
import netsvcs.Logger.*;
import JACE.OS.*;
import java.io.*;
import java.net.*;
import JACE.Misc.*;

/**
 *
 * <p><h2>DESCRIPTION</h2>
 *
 * <blockquote>
 * Acceptor:  Listens on a specified port and launches ServerLoggingHandlers
 * in response to requests.  A LogMessageReceiver can be specified on the
 * command line to change the way the logging service processes messages.
 *
 * @see netsvcs.Logger.ServerLoggingHandler, netsvcs.Logger.LogMessageReceiver
 */
public class ServerLoggingAcceptor extends Acceptor implements Runnable
{
  /** Main function to bootstrap the process
   *
   *  Command line arguments:
   *
   *  -p <port>         Select a port for listening for requests
   *  -r <class name>   Specify a LogMessageReceiver (make sure it's a full class name)
   * 
   */
  public static void main (String [] args)
    {
      ServerLoggingAcceptor sla = new ServerLoggingAcceptor();

      sla.init(args);
    }

  /** 
   * Receives the command line and launches its own thread
   */
  public int init (String [] args)
    {
      this.parseArgs(args);

      // If the user didn't specify a LogMessageReceiver, use the default
      // (which just calls a print method on LogMessage)
      if (this.receiver_ == null)
	this.receiver_ = new DefaultLMR();

      new Thread (this).start();
      return 0;
    }

  /**
   * Specify what LogMessageReceiver to use 
   */
  public void setLMR(LogMessageReceiver receiver)
    {
      this.receiver_ = receiver;
    }

  /** 
   * Accessor for the LogMessageReceiver
   */
  public LogMessageReceiver getLMR ()
    {
      return this.receiver_;
    }

  /** 
   * Create a new ServerLoggingHandler 
   */
  protected SvcHandler makeSvcHandler ()
    throws InstantiationException, IllegalAccessException
    {
      return new netsvcs.Logger.ServerLoggingHandler (this.receiver_);
    }

  /**
   * Run forever accepting new connections
   */
  public void run ()
    {
      try {

	this.open (this.port_);
	while (true) 
	  this.accept();  

      } catch (SocketException e)
	{
	  ACE.ERROR ("Socket Exception: " + e);
	}
      catch (InstantiationException e)
	{
	  ACE.ERROR (e);
	}
      catch (IllegalAccessException e)
	{
	  ACE.ERROR (e);
	}
      catch (IOException e)
	{
	  ACE.ERROR (e);
	}

      ACE.ERROR("ServerLoggingAcceptor has exited");
    }

  /**
   * Process the command line
   */
  protected void parseArgs (String args[])
  {
    String s;
    GetOpt opt = new GetOpt (args, "p:r:");
    for (int c; (c = opt.next ()) != -1; )
      {
	switch (c)
	  {
	  case 'p':
	    s = opt.optarg ();
	    this.port_ = (new Integer (s)).intValue ();
	    break;
	  case 'r':
	    // Load the LMR with the given name
	    s = new String(opt.optarg ());
	    Class LMRfactory;
	    try {
	      LMRfactory = Class.forName(s);

	      receiver_ = (LogMessageReceiver)LMRfactory.newInstance();

	    } catch (ClassNotFoundException e) {
	      ACE.ERROR("Unable to find LMR factory: " + e);
	    } catch (InstantiationException e) {
	      ACE.ERROR("Creating LMR: " + e);
	    } catch (IllegalAccessException e) {
	      ACE.ERROR("Creating LMR: " + e);
	    }
	    // Any of the above exceptions will result in just using the
	    // default LMR
	    break;
	  default:
	    ACE.ERROR ("Unknown argument: " + c);
	    ACE.ERROR ("Valid args: -p <port> -r <LogMessageReceiver name>");
	    break;
	  }
      }
  }

  private int port_ = ACE.DEFAULT_SERVER_PORT;
  private LogMessageReceiver receiver_ = null;
};