directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r373257 - in /directory/trunks/apacheds/standalone: daemon/src/main/java/org/apache/directory/server/standalone/daemon/ simple/main/src/main/java/org/apache/ldap/server/
Date Sat, 28 Jan 2006 23:06:44 GMT
Author: akarasulu
Date: Sat Jan 28 15:06:30 2006
New Revision: 373257

URL: http://svn.apache.org/viewcvs?rev=373257&view=rev
Log:
changes ...

 o adding copy of AvailablePortFinder which I forgot to add on my last commit
 o reverted to start(boolean) to start() which no longer needs to handle 
   blocking for procrun - this is taken care of different for each framework
   using it's own specific bootstrapper


Added:
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
  (with props)
Modified:
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/Bootstrapper.java
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/DaemonApplication.java
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/JsvcBootstrapper.java
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/MainBootstrapper.java
    directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/ProcrunBootstrapper.java
    directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/DirectoryServer.java
    directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/ServerMain.java

Added: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java?rev=373257&view=auto
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
(added)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
Sat Jan 28 15:06:30 2006
@@ -0,0 +1,201 @@
+/*
+ *   @(#) $Id: AvailablePortFinder.java 329145 2005-10-28 07:24:00Z trustin $
+ * 
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.
+ *
+ */
+package org.apache.directory.server.standalone.daemon;
+
+import java.io.IOException;
+import java.net.DatagramSocket;
+import java.net.ServerSocket;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.TreeSet;
+
+
+/**
+ * Finds currently available server ports.
+ *
+ * @author The Apache Directory Project
+ * @version $Rev: 329145 $
+ * @see <a href="http://www.iana.org/assignments/port-numbers">IANA.org</a>
+ */
+public class AvailablePortFinder
+{
+    /**
+     * The minimum number of server port number.
+     */
+    public static final int MIN_PORT_NUMBER = 1;
+
+    /**
+     * The maximum number of server port number.
+     */
+    public static final int MAX_PORT_NUMBER = 49151;
+
+    /**
+     * Creates a new instance.
+     */
+    private AvailablePortFinder()
+    {
+    }
+
+    /**
+     * Returns the {@link Set} of currently available port numbers
+     * ({@link Integer}).  This method is identical to
+     * <code>getAvailablePorts(MIN_PORT_NUMBER, MAX_PORT_NUMBER)</code>.
+     *
+     * WARNING: this can take a very long time.
+     */
+    public static Set getAvailablePorts()
+    {
+        return getAvailablePorts(MIN_PORT_NUMBER, MAX_PORT_NUMBER);
+    }
+
+    /**
+     * Gets the next available port starting at the lowest port number.
+     *
+     * @throws NoSuchElementException if there are no ports available
+     */
+    public static int getNextAvailable()
+    {
+        return getNextAvailable(MIN_PORT_NUMBER);
+    }
+
+    /**
+     * Gets the next available port starting at a port.
+     *
+     * @param fromPort the port to scan for availability
+     * @throws NoSuchElementException if there are no ports available
+     */
+    public static int getNextAvailable(int fromPort)
+    {
+        if ((fromPort < MIN_PORT_NUMBER) || (fromPort > MAX_PORT_NUMBER))
+        {
+            throw new IllegalArgumentException("Invalid start port: "
+                                               + fromPort);
+        }
+
+        for (int i = fromPort; i <= MAX_PORT_NUMBER; i++)
+        {
+            if( available( i ) )
+            {
+                return i;
+            }
+        }
+
+        throw new NoSuchElementException("Could not find an available port "
+                                         + "above " + fromPort);
+    }
+
+    /**
+     * Checks to see if a specific port is available.
+     *
+     * @param port the port to check for availability
+     */
+    public static boolean available( int port )
+    {
+        if ( ( port < MIN_PORT_NUMBER) || ( port > MAX_PORT_NUMBER ) )
+        {
+            throw new IllegalArgumentException( "Invalid start port: " + port );
+        }
+
+        ServerSocket ss = null;
+        DatagramSocket ds = null;
+        try
+        {
+            ss = new ServerSocket( port );
+            ss.setReuseAddress( true );
+            ds = new DatagramSocket( port );
+            ds.setReuseAddress( true );
+            return true;
+        }
+        catch (IOException e)
+        {
+        }
+        finally
+        {
+            if (ds != null)
+            {
+                ds.close();
+            }
+
+            if (ss != null)
+            {
+                try
+                {
+                    ss.close();
+                }
+                catch (IOException e)
+                {
+                    /* should not be thrown */
+                }
+            }
+        }
+        
+        return false;
+    }
+
+    /**
+     * Returns the {@link Set} of currently avaliable port numbers ({@link Integer})
+     * between the specified port range.
+     *
+     * @throws IllegalArgumentException if port range is not between
+     * {@link #MIN_PORT_NUMBER} and {@link #MAX_PORT_NUMBER} or
+     * <code>fromPort</code> if greater than <code>toPort</code>.
+     */
+    public static Set getAvailablePorts(int fromPort, int toPort)
+    {
+        if (
+            (fromPort < MIN_PORT_NUMBER) || (toPort > MAX_PORT_NUMBER)
+                || (fromPort > toPort))
+        {
+            throw new IllegalArgumentException("Invalid port range: "
+                                               + fromPort + " ~ " + toPort);
+        }
+
+        Set result = new TreeSet();
+
+        for (int i = fromPort; i <= toPort; i++)
+        {
+            ServerSocket s = null;
+
+            try
+            {
+                s = new ServerSocket(i);
+                result.add(new Integer(i));
+            }
+            catch (IOException e)
+            {
+            }
+            finally
+            {
+                if (s != null)
+                {
+                    try
+                    {
+                        s.close();
+                    }
+                    catch (IOException e)
+                    {
+                        /* should not be thrown */
+                    }
+                }
+            }
+        }
+
+        return result;
+    }
+}

Propchange: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/AvailablePortFinder.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/Bootstrapper.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/Bootstrapper.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/Bootstrapper.java
(original)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/Bootstrapper.java
Sat Jan 28 15:06:30 2006
@@ -156,12 +156,12 @@
     }
 
     
-    public void callStart( boolean nowait )
+    public void callStart()
     {
         Thread.currentThread().setContextClassLoader( application );
         try
         {
-            start.start( nowait );
+            start.start();
         }
         catch ( Exception e )
         {
@@ -260,6 +260,13 @@
                 BufferedReader in = new BufferedReader( new FileReader( shutdownPortFile
) );
                 shutdownPort = Integer.parseInt( in.readLine() );
                 in.close();
+            }
+            else
+            {
+                String msg = "The server does not seem to be running!  The shutdown port
file\n";
+                msg += shutdownPortFile + " does not exist!";
+                log.error( msg );
+                throw new IllegalStateException( msg );
             }
         }
         

Modified: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/DaemonApplication.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/DaemonApplication.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/DaemonApplication.java
(original)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/DaemonApplication.java
Sat Jan 28 15:06:30 2006
@@ -35,13 +35,9 @@
     void init( InstallationLayout layout, String[] args ) throws Exception;
     
     /**
-     * Start threads and bind sockets here.  If nowait is toggled this method 
-     * should not return until the server is ready to be shutdown.
-     * 
-     * @param nowait true if this method should return control immediately, false
-     * if it should block until it is ready to be shutdown
+     * Start threads and bind sockets here.
      */
-    void start( boolean nowait );
+    void start();
     
     /**
      * Stop threads and close sockets opened in start() here.

Modified: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/JsvcBootstrapper.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/JsvcBootstrapper.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/JsvcBootstrapper.java
(original)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/JsvcBootstrapper.java
Sat Jan 28 15:06:30 2006
@@ -54,7 +54,7 @@
     public void start()
     {
         log.debug( "start() called" );
-        callStart( true );
+        callStart();
     }
 
 

Modified: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/MainBootstrapper.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/MainBootstrapper.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/MainBootstrapper.java
(original)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/MainBootstrapper.java
Sat Jan 28 15:06:30 2006
@@ -76,14 +76,18 @@
                 log.debug( "calling callInit(String[]) from main(String[])" );
                 instance.callInit( shift( args, 2 ) );
                 log.debug( "calling callStart() from main(String[])" );
-                instance.callStart( true );
-            }
-            else if ( command.equalsIgnoreCase( "stop" ) )
-            {
+                instance.callStart();
+
+                instance.waitForShutdown();
+                
                 log.debug( "calling callStop() from main(String[])" );
                 instance.callStop( shift( args, 2 ) );
                 log.debug( "calling callDestroy() from main(String[])" );
                 instance.callDestroy();
+            }
+            else if ( command.equalsIgnoreCase( "stop" ) )
+            {
+                instance.sendShutdownCommand();
             }
             else
             {

Modified: directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/ProcrunBootstrapper.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/ProcrunBootstrapper.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/ProcrunBootstrapper.java
(original)
+++ directory/trunks/apacheds/standalone/daemon/src/main/java/org/apache/directory/server/standalone/daemon/ProcrunBootstrapper.java
Sat Jan 28 15:06:30 2006
@@ -62,7 +62,7 @@
             log.debug( "prunsrvStart(String[]) calling callInit()" );
             instance.callInit( shift( args, 1 ) );
             log.debug( "prunsrvStart(String[]) calling callStart()" );
-            instance.callStart( false );
+            instance.callStart();
 
             log.debug( "prunsrvStart(String[]) block waitForShutdown()" );
             instance.waitForShutdown();

Modified: directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/DirectoryServer.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/DirectoryServer.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/DirectoryServer.java
(original)
+++ directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/DirectoryServer.java
Sat Jan 28 15:06:30 2006
@@ -42,15 +42,13 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class DirectoryServer implements DaemonApplication
+public class DirectoryServer implements DaemonApplication 
 {
     private static final Logger log = LoggerFactory.getLogger( DirectoryServer.class );
     private Properties env;
     private Thread workerThread = null;
     private SynchWorker worker = new SynchWorker();
     private boolean startNoWait = false;
-    private boolean initialized = false;
-    private boolean started = false;
 
 
     public void init( InstallationLayout install, String[] args ) throws Exception
@@ -80,8 +78,6 @@
         new InitialDirContext( env );
 
         workerThread = new Thread( worker, "SynchWorkerThread" );
-        initialized = true;
-
 
         if (log.isInfoEnabled())
         {
@@ -97,35 +93,15 @@
     }
     
 
-    // @todo don't think this nowait is needed here if the procrun bootstrapper
-    // handles the creation of the socket and the blocking.
-    public void start( boolean nowait ) 
+    public void start()
     {
-        startNoWait = nowait;
-        
-        if ( nowait )
-        {
-            workerThread.start();
-            started = true;
-            return;
-        }
-
-        started = true;
-//        worker.run();  // - blocks here 
+        workerThread.start();
+        return;
     }
     
 
     public void stop( String[] args ) throws Exception
     {
-        if ( ! initialized || ! started )
-        {
-            log.warn( "stop(String[]) called without calling init() and start()" );
-            log.info( "Might be a procrun invocation as opposed to jsvc so we'll initiate
external shutdown procedure" );
-            
-            // real shutdown sequence goes here.
-            return;
-        }
-        
         worker.stop = true;
         synchronized ( worker.lock )
         {

Modified: directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/ServerMain.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/ServerMain.java?rev=373257&r1=373256&r2=373257&view=diff
==============================================================================
--- directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/ServerMain.java
(original)
+++ directory/trunks/apacheds/standalone/simple/main/src/main/java/org/apache/ldap/server/ServerMain.java
Sat Jan 28 15:06:30 2006
@@ -42,12 +42,12 @@
         if ( args.length > 0 )
         {
             server.init( new InstallationLayout( args[0] ), null );
-            server.start( true );
+            server.start();
         }
         else
         {
             server.init( null, null );
-            server.start( true );
+            server.start();
         }
     }
 }



Mime
View raw message