1 package org.apache.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.InetAddress;
24 import java.net.UnknownHostException;
25 import java.rmi.RemoteException;
26 import java.rmi.registry.LocateRegistry;
27 import java.util.Properties;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.jcs.utils.props.PropertyLoader;
32
33 /***
34 *Starts the registry and runs the server via the factory.
35 *<p>
36 * @author Aaron Smuts
37 *
38 */
39 public class RemoteCacheServerStartupUtil
40 {
41 private final static Log log = LogFactory.getLog( RemoteCacheServerStartupUtil.class );
42
43 private static final int DEFAULT_REGISTRY_PORT = 1101;
44
45
46 /***
47 * Starts the registry on port "registry.port"
48 * <p>
49 * @param propsFileName
50 * @return RemoteCacheServer
51 */
52 public static RemoteCacheServer startServerUsingProperties( String propsFileName )
53 {
54
55
56 int registryPort = DEFAULT_REGISTRY_PORT;
57
58 try
59 {
60 Properties props = PropertyLoader.loadProperties( propsFileName );
61 if ( props != null )
62 {
63 String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );
64
65 try
66 {
67 registryPort = Integer.parseInt( portS );
68 }
69 catch ( NumberFormatException e )
70 {
71 log.error( "Problem converting port to an int.", e );
72 }
73 }
74 }
75 catch ( Exception e )
76 {
77 log.error( "Problem loading props.", e );
78 }
79 catch ( Throwable t )
80 {
81 log.error( "Problem loading props.", t );
82 }
83
84
85 String registryHost;
86 try
87 {
88 registryHost = InetAddress.getLocalHost().getHostAddress();
89
90 if ( log.isDebugEnabled() )
91 {
92 log.debug( "registryHost = [" + registryHost + "]" );
93 }
94
95 if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
96 {
97 log.warn( "The local address [" + registryHost
98 + "] is INVALID. Other machines must be able to use the address to reach this server." );
99 }
100
101 try
102 {
103 LocateRegistry.createRegistry( registryPort );
104 }
105 catch ( RemoteException e )
106 {
107 log.error( "Problem creating registry. It may already be started. " + e.getMessage() );
108 }
109 catch ( Throwable t )
110 {
111 log.error( "Problem creating registry.", t );
112 }
113
114 try
115 {
116 RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + propsFileName );
117 }
118 catch ( IOException e )
119 {
120 log.error( "Problem starting remote cache server.", e );
121 }
122
123 catch ( Throwable t )
124 {
125 log.error( "Problem starting remote cache server.", t );
126 }
127 }
128 catch ( UnknownHostException e )
129 {
130 log.error( "Could not get local address to use for the registry!", e );
131 }
132
133 return RemoteCacheServerFactory.getRemoteCacheServer();
134 }
135
136 }