View Javadoc
1   package org.apache.commons.jcs3.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.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   * Starts the registry and runs the server via the factory.
33   */
34  public class RemoteCacheServerStartupUtil
35  {
36      /** The logger */
37      private static final Log log = LogManager.getLog( RemoteCacheServerStartupUtil.class );
38  
39      /** Registry to use in the test. */
40      private static final int DEFAULT_REGISTRY_PORT = 1101;
41  
42      /**
43       * Starts the registry on port "registry.port"
44       * <p>
45       * @param propsFileName
46       * @return RemoteCacheServer
47       */
48      public static <K, V> RemoteCacheServer<K, V> startServerUsingProperties( final String propsFileName )
49      {
50          // TODO load from props file or get as init param or get from jndi, or
51          // all three
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          // we will always use the local machine for the registry
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 }