1   package org.apache.jcs.auxiliary.remote.server;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // TODO load from props file or get as init param or get from jndi, or
55          // all three
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          // we will always use the local machine for the registry
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 }