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