package org.apache.commons.jcs.auxiliary.remote.server;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.IOException;
import java.io.Serializable;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;

import org.apache.commons.jcs.utils.net.HostNameUtil;
import org.apache.commons.jcs.utils.props.PropertyLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *Starts the registry and runs the server via the factory.
 *<p>
 * @author Aaron Smuts
 */
public class RemoteCacheServerStartupUtil
{
    /** The logger */
    private static final Log log = LogFactory.getLog( RemoteCacheServerStartupUtil.class );

    /** Registry to use in the test. */
    private static final int DEFAULT_REGISTRY_PORT = 1101;

    /**
     * Starts the registry on port "registry.port"
     * <p>
     * @param propsFileName
     * @return RemoteCacheServer
     */
    public static <K extends Serializable, V extends Serializable> RemoteCacheServer<K, V> startServerUsingProperties( String propsFileName )
    {
        // TODO load from props file or get as init param or get from jndi, or
        // all three
        int registryPort = DEFAULT_REGISTRY_PORT;

        try
        {
            Properties props = PropertyLoader.loadProperties( propsFileName );
            if ( props != null )
            {
                String portS = props.getProperty( "registry.port", String.valueOf( DEFAULT_REGISTRY_PORT ) );

                try
                {
                    registryPort = Integer.parseInt( portS );
                }
                catch ( NumberFormatException e )
                {
                    log.error( "Problem converting port to an int.", e );
                }
            }
        }
        catch ( Exception e )
        {
            log.error( "Problem loading props.", e );
        }
        catch ( Throwable t )
        {
            log.error( "Problem loading props.", t );
        }

        // we will always use the local machine for the registry
        String registryHost;
        try
        {
            registryHost = HostNameUtil.getLocalHostAddress();

            if ( log.isDebugEnabled() )
            {
                log.debug( "registryHost = [" + registryHost + "]" );
            }

            if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( registryHost ) )
            {
                log.warn( "The local address [" + registryHost
                    + "] is INVALID.  Other machines must be able to use the address to reach this server." );
            }

            try
            {
                LocateRegistry.createRegistry( registryPort );
            }
            catch ( RemoteException e )
            {
                log.error( "Problem creating registry.  It may already be started. " + e.getMessage() );
            }
            catch ( Throwable t )
            {
                log.error( "Problem creating registry.", t );
            }

            try
            {
                RemoteCacheServerFactory.startup( registryHost, registryPort, "/" + propsFileName );
            }
            catch ( IOException e )
            {
                log.error( "Problem starting remote cache server.", e );
            }
            catch ( Throwable t )
            {
                log.error( "Problem starting remote cache server.", t );
            }
        }
        catch ( UnknownHostException e )
        {
            log.error( "Could not get local address to use for the registry!", e );
        }

        return RemoteCacheServerFactory.getRemoteCacheServer();
    }
}

@@ -958,82 +958,8 @@
 on;%0A
-import java.rmi.RemoteException;%0Aimport java.rmi.registry.LocateRegistry;%0A
 impo
@@ -1979,34 +1979,8 @@
 T;%0A%0A
-        try%0A        %7B%0A    
     
@@ -2054,28 +2054,24 @@
  );%0A        
-  
-  
 if ( props !
@@ -2083,34 +2083,26 @@
 l )%0A        
-    %7B%0A    
+%7B%0A
             
@@ -2192,28 +2192,24 @@
 _PORT ) );%0A%0A
-    
             
@@ -2220,34 +2220,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2288,36 +2288,32 @@
  );%0A            
-    
 %7D%0A              
@@ -2302,36 +2302,32 @@
   %7D%0A            
- 
-   
 catch ( NumberFo
@@ -2352,34 +2352,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2444,236 +2444,10 @@
     
-   
- 
 %7D%0A
-            %7D%0A        %7D%0A        catch ( Exception e )%0A        %7B%0A            log.error( %22Problem loading props.%22, e );%0A        %7D%0A        catch ( Throwable t )%0A        %7B%0A            log.error( %22Problem loading props.%22, t );%0A
     
@@ -2522,37 +2522,8 @@
 try%0A
-        String registryHost;%0A
     
@@ -2544,32 +2544,39 @@
   %7B%0A            
+String 
 registryHost = H
@@ -3049,413 +3049,8 @@
  %7D%0A%0A
-            try%0A            %7B%0A                LocateRegistry.createRegistry( registryPort );%0A            %7D%0A            catch ( RemoteException e )%0A            %7B%0A                log.error( %22Problem creating registry.  It may already be started. %22 + e.getMessage() );%0A            %7D%0A            catch ( Throwable t )%0A            %7B%0A                log.error( %22Problem creating registry.%22, t );%0A            %7D%0A%0A
     
@@ -3331,143 +3331,8 @@
   %7D%0A
-            catch ( Throwable t )%0A            %7B%0A                log.error( %22Problem starting remote cache server.%22, t );%0A            %7D%0A
     

Remove duplicate code