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