KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > KernelBasedServer


1 package com.ubermq.kernel;
2
3 import java.nio.channels.*;
4 import java.net.*;
5 import java.util.*;
6
7 /**
8  * An abstract base class for servers that will interpret a properties file,
9  * load it into the global configurator, and proceed to set up services that
10  * can be described by a URL.
11  */

12 public abstract class KernelBasedServer
13     implements Runnable JavaDoc
14 {
15     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(KernelBasedServer.class);
16     private java.net.URI JavaDoc url;
17
18     /**
19      * Creates a server, using the specified properties file.
20      *
21      * @param propsFile the filename of the properties file to retrieve. If this is null,
22      * the default is used.
23      * @see ConfigConstants.DEFAULT_PROPS_FILE
24      */

25     public KernelBasedServer(String JavaDoc propsFile)
26     {
27         try
28         {
29             Configurator.setup(new java.io.FileInputStream JavaDoc(propsFile != null ? propsFile : ConfigConstants.DEFAULT_PROPS_FILE));
30         }
31         catch (java.io.IOException JavaDoc e)
32         {
33             log.error("", e);
34         }
35         finally
36         {
37             init();
38         }
39     }
40
41     /**
42      * Creates a server using the specified Properties object
43      * to provide server configuration settings.
44      * @param props a Properties object, containing configuration settings.
45      */

46     public KernelBasedServer(Properties props)
47     {
48         Configurator.setup(props);
49         init();
50     }
51
52     /**
53      * Initializes the server. <P>
54      * Implementation specific initialization may take place here. This is called
55      * after the properties file is loaded into the global Configurator, and
56      * before <code>recover</code> or <code>exec</code>.
57      */

58     protected void init()
59     {
60     }
61
62     /**
63      * Recovers operation from a possible failure. This is called immediately
64      * prior to calling <code>exec</code>.
65      */

66     protected void recover()
67     {
68     }
69
70     /**
71      * Runs the server.
72      */

73     public void run()
74     {
75         // recover and go
76
recover();
77         url = exec();
78     }
79
80     public static final String JavaDoc DEFAULT_SCHEME = "ubermq";
81
82     /**
83      * Provides a URL at which the server is providing its functionality.
84      * The default implementation returns <code>host:port</code>. Subclasses
85      * may override in order to provide a protocol specifier prefix, or replace
86      * the logic here entirely.
87      */

88     public String JavaDoc getServiceUrl()
89     {
90         return url.toString();
91     }
92
93     public URI getServiceURI()
94     {
95         return url;
96     }
97
98     /**
99      * Begins the operation of the server. Called third - after <code>init</code>
100      * and <code>recover</code>.
101      *
102      * @return the service URL
103      */

104     protected abstract URI exec();
105 }
106
Popular Tags