summaryrefslogtreecommitdiff
path: root/java/JACE/netsvcs/Logger/ServerLoggingAcceptor.java
blob: c0ef8831fc6ddb83e295e51f54050eae2551ea06 (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
package JACE.netsvcs.Logger;

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

/**
 * Server for the logging service.  Sets the default logging strategy
 * to StderrStrategy so that logging requests are printed on the
 * System.err stream.  Other strategies can be specified on the
 * command line.  
 * <P>
 * <B>Valid command line arguments:</B>
 * <PRE>
 *   -r (class name)  Specify a LoggingStrategy
 *                    (Default is StdErrStrategy)
 *   -p (port)        Port to listen on for clients
 *   -d               Enable debugging messages
 *   -a (class name)  Specify ActivateStrategy
 *                    (Default is thread per connection)
 * </PRE>
 *
 *@see LoggingStrategy
 *@see StderrStrategy
 */
public class ServerLoggingAcceptor extends Server
{
  /**
   * Default constructor.  Sets the default LoggingStrategy to
   * StderrStrategy.
   */
  public ServerLoggingAcceptor ()
  {
    name ("Logging Service");
    logStrategy_ = new StderrStrategy ();
  }

  /**
   * Simple main program for running the logging service without the
   * service configurator.
   *
   *@param args command line arguments
   */
  public static void main (String [] args)
  {
    ServerLoggingAcceptor sla = new ServerLoggingAcceptor();
    
    sla.init(args);
  }

  /** 
   * Accessor for the LoggingStrategy
   */
  public LoggingStrategy loggingStrategy ()
  {
    return this.logStrategy_;
  }

  /**
   * Creates a new ServerLoggingHandler instance.
   */
  protected SvcHandler makeSvcHandler ()
  {
    return new ServerLoggingHandler ();
  }

  /**
   * Prints out the valid command line arguments.  See the class
   * description for more information.  Called by Server.init when
   * parseArgs returns -1.
   */
  protected void printUsage ()
  {
    ACE.ERROR ("Valid options:\n");
    ACE.ERROR ("-r <class name>  Specify a LoggingStrategy");
    ACE.ERROR ("                 (Default is StdErrStrategy");
    ACE.ERROR ("-p <port>        Port to listen on for clients");
    ACE.ERROR ("-d               Enable debugging messages");
    ACE.ERROR ("-a <class name>  Specify ActivateStrategy");
    ACE.ERROR ("                 (Default is single threaded");
  }
  
  /**
   * Parses the command line arguments.  See the class description
   * for more information.
   *
   *@param args command line arguments
   *@return -1 on failure, 0 on success
   */
  protected int parseArgs (String args[])
  {
    String s;
    Object strategy;
    GetOpt opt = new GetOpt (args, "p:r:da:", true);
    int c = 0;
   
    try {

      while ((c = opt.next ()) != -1)
	{
	  switch (c)
	    {
	    case 'd':
	      ACE.enableDebugging ();
	      ACE.DEBUG ("Debugging is enabled");
	      break;
	    case 'p':
	      if (!port (opt.optarg ()))
		return -1;
	      break;
	    case 'a':
	      strategy = newStrategyInstance (opt.optarg (),
					      "ActivateStrategy");
	      if (strategy == null)
		return -1;

	      activateStrategy ((ActivateStrategy) strategy);
	      break;
	    case 'r':
	      // Load the Strategy with the given name
	      strategy = newStrategyInstance (opt.optarg (),
					      "LoggingStrategy");
	      if (strategy == null) 
		return -1;
	      
	      logStrategy_ = (LoggingStrategy)strategy;
	      break;
	    default:
	      ACE.ERROR ("Unknown argument: " + c);
	      return -1;
	    }
	}
    } catch (ArrayIndexOutOfBoundsException e) {
      ACE.ERROR ("Option -" + (char)c + " requires an argument");
      return -1;
    }

    return 0;
  }

  private LoggingStrategy logStrategy_;
}