summaryrefslogtreecommitdiff
path: root/java/src/ServiceConfig.java
blob: 8ffa26d9e98bdf0650c135d489e2e3b3571048ba (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
/*************************************************
 *
 * = PACKAGE
 *    ACE.ServiceConfigurator
 *
 * = FILENAME
 *    ServiceConfig.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package ACE.ServiceConfigurator;

import java.io.*;
import ACE.OS.*;
import ACE.Misc.*;

/**
 * <hr>
 * <p><b>TITLE</b><br>
 *     Provide the base class that supplies common server daemon
 *     operations.
 */
public class ServiceConfig
{
  public static int open (String [] args) throws
  FileNotFoundException, IOException, ClassNotFoundException,
    IllegalAccessException, InstantiationException
  {
    ServiceConfig.parseArgs (args);
    if (ServiceConfig.svcRep_ == null)
      ServiceConfig.svcRep_ = new ServiceRepository ();
    return ServiceConfig.processDirectives ();
  }

  protected static int processDirectives () throws
  FileNotFoundException, IOException, ClassNotFoundException,
    IllegalAccessException, InstantiationException 
  {
    File configFile = new File (ServiceConfig.serviceConfigFile_);
    
    // Check if file exists and is a normal file
    if (!configFile.exists () || !configFile.isFile ())
      throw new FileNotFoundException ("File " + ServiceConfig.serviceConfigFile_ + " not found");
    
    // Check if the file is readable
    if (!configFile.canRead ())
      throw new IOException ("File " + ServiceConfig.serviceConfigFile_ + " not readable");

    // Set up the stream
    FileInputStream fileIn = new FileInputStream (configFile);

    // Parse the file
    StreamTokenizer in = new StreamTokenizer (fileIn);

    // Set '#' as comment character to be ignored and set '/' as
    // ordinary character (was original comment character) 
    in.commentChar ('#');
    in.ordinaryChar ('/');

    // Set characters in ASCII range 33 to 47 and ASCII range 91 to 96
    // as ordinary characters
    in.wordChars ('!', '/');  // ASCII range 33 to 47
    in.wordChars ('[', '`');  // ASCII range 91 to 96

    String className = null;
    String classType = null;
    String args = null;
    // Create a state machine
    int state = ServiceConfig.CLASS_NAME;

    while (in.nextToken () != StreamTokenizer.TT_EOF)
      {
	switch (state)
	  {
	  case ServiceConfig.CLASS_NAME:
	    if (in.ttype == StreamTokenizer.TT_WORD)
	      className = in.sval;
	    else
	      throw new IOException ("Illegal CLASS NAME argument in file " + ServiceConfig.serviceConfigFile_);
	    state = ServiceConfig.CLASS_TYPE;
	    break;
	  case ServiceConfig.CLASS_TYPE:
	    if (in.ttype == StreamTokenizer.TT_WORD)
	      classType = in.sval;
	    else
	      throw new IOException ("Illegal CLASS TYPE argument in file " + ServiceConfig.serviceConfigFile_);
	    state = ServiceConfig.ARGS;
	    // Set space to be an ordinary character to allow
	    // arguments to be parsed in
	    in.wordChars (' ', ' ');
	    break;
	  case ServiceConfig.ARGS:
	    if (in.ttype == StreamTokenizer.TT_WORD)
	      {
		args = in.sval;
		// Load the class
		Class c = ServiceConfig.svcRep_.load (className);

		// Note that c should be defined else an exception
		// would have been thrown by now
		
		// Figure out the type of the class, create an
		// instance and then invoke the initialization method
		if (classType.equals ("ServiceObject"))
		  {
		    ServiceObject svcObj = (ServiceObject) c.newInstance ();

		    // Create an array of String from args String
		    String [] argArray = OS.createStringArray (args, " ");
		    
		    // Call init on the Service Object passing in arguments
		    svcObj.init (argArray);
		  }
		else
		  throw new IOException ("Illegal CLASS TYPE argument in file " + ServiceConfig.serviceConfigFile_);
	      }
	    else
	      throw new IOException ("Illegal ARGS argument in file " + ServiceConfig.serviceConfigFile_);
	    state = ServiceConfig.CLASS_NAME;
	    // Set space back to whitespace-character to extract the
	    // next token
	    in.whitespaceChars (' ', ' ');
	    break;
	  default:
	    throw new IOException ("Illegal argument in file " + ServiceConfig.serviceConfigFile_);
	  }
      }
    return 0;
  }

  protected static void parseArgs (String [] args)
  {
    GetOpt getopt = new GetOpt (args, "bdnf:");
    for (int c; (c = getopt.next ()) != -1; )
      switch (c)
	{
	case 'b':
	  // Note: not supported yet!
	  ServiceConfig.beADaemon_ = true;
	  break;
	case 'd': 
	  ServiceConfig.debug_ = true;
	  break;
	case 'n':
	  ServiceConfig.noDefaults_ = true;
	  break;
	case 'f':
	  ServiceConfig.serviceConfigFile_ = getopt.optarg ();
	  break;
	default:
	  ACE.ERROR ((char ) c + " is not a ServiceConfig option");
	  break;
	}
  }

  // Set by command line options
  private static boolean beADaemon_ = false;
  private static boolean debug_ = false;
  private static boolean noDefaults_ = false;
  private static String serviceConfigFile_ = "svc.conf";
  private static ServiceRepository svcRep_ = null;

  // States for the state-machine used in parsing the config file
  private final static int CLASS_NAME = 0;
  private final static int CLASS_TYPE = 1;
  private final static int ARGS = 2;
}