1 package org.apache.commons.jcs.auxiliary.remote.server;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.net.UnknownHostException;
24 import java.util.Properties;
25
26 import org.apache.commons.jcs.auxiliary.remote.RemoteUtils;
27 import org.apache.commons.jcs.utils.net.HostNameUtil;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35
36 public class RemoteCacheServerStartupUtil
37 {
38
39 private static final Log log = LogFactory.getLog( RemoteCacheServerStartupUtil.class );
40
41
42 private static final int DEFAULT_REGISTRY_PORT = 1101;
43
44
45
46
47
48
49
50 public static <K, V> RemoteCacheServer<K, V> startServerUsingProperties( String propsFileName )
51 {
52
53
54 int registryPort = DEFAULT_REGISTRY_PORT;
55
56 Properties props = null;
57 try
58 {
59 props = RemoteUtils.loadProps(propsFileName);
60 }
61 catch (IOException e)
62 {
63 log.error( "Problem loading configuration from " + propsFileName, e);
64 }
65
66 if ( props != null )
67 {
68 String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
69
70 try
71 {
72 registryPort = Integer.parseInt( portS );
73 }
74 catch ( NumberFormatException e )
75 {
76 log.error( "Problem converting port to an int.", e );
77 }
78 }
79
80
81 try
82 {
83 String registryHost = HostNameUtil.getLocalHostAddress();
84
85 if ( log.isDebugEnabled() )
86 {
87 log.debug( "registryHost = [" + registryHost + "]" );
88 }
89
90 if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
91 {
92 log.warn( "The local address [" + registryHost
93 + "] is INVALID. Other machines must be able to use the address to reach this server." );
94 }
95
96 try
97 {
98 RemoteCacheServerFactory.startup( registryHost, registryPort, props );
99 }
100 catch ( IOException e )
101 {
102 log.error( "Problem starting remote cache server.", e );
103 }
104 }
105 catch ( UnknownHostException e )
106 {
107 log.error( "Could not get local address to use for the registry!", e );
108 }
109
110 return RemoteCacheServerFactory.getRemoteCacheServer();
111 }
112 }