You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by sg...@apache.org on 2005/03/31 14:12:25 UTC

cvs commit: jakarta-turbine-fulcrum/yaafi/src/java/org/apache/fulcrum/yaafi/cli Main.java Shutdown.java

sgoeschl    2005/03/31 04:12:25

  Modified:    yaafi/src/java/org/apache/fulcrum/yaafi/cli Main.java
                        Shutdown.java
  Log:
  Various changes to put this code into production
  
  Revision  Changes    Path
  1.4       +250 -87   jakarta-turbine-fulcrum/yaafi/src/java/org/apache/fulcrum/yaafi/cli/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/yaafi/src/java/org/apache/fulcrum/yaafi/cli/Main.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Main.java	16 Feb 2005 11:24:52 -0000	1.3
  +++ Main.java	31 Mar 2005 12:12:25 -0000	1.4
  @@ -17,13 +17,14 @@
    * limitations under the License.
    */
   
  +import java.io.File;
  +
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.logger.ConsoleLogger;
   import org.apache.avalon.framework.logger.Logger;
  -import org.apache.fulcrum.yaafi.framework.container.ServiceConstants;
   import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
  -import org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl;
  -import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
   import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
  +import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
   
   
   /**
  @@ -32,20 +33,14 @@
    * simple embedded scenario.
    */
   
  -public class Main
  +public class Main implements Runnable, Disposable
   {
       /** The service manager */
  -    private ServiceContainer manager;
  +    private ServiceContainer container;
       
  -    /** The name of the block config file */
  -    private String componentRoleValue;
  +    /** The location of the container configuration */
  +	private String containerConfigValue;    
       
  -    /** The name of the component config file */
  -	private String componentConfigValue;
  -
  -	/** The name of the parameters file */
  -	private String componentParametersValue;
  -
   	/** Thread for processing the shutdown notification of the JVM */
   	private Thread shutdownThread;
   	
  @@ -54,18 +49,33 @@
   	
   	/** The logger being used */
   	private Logger logger;
  +	
  +	/** the interval to check for termination */
  +	private int sleepTime = 100; 
       
  +	/** the name of the application */
  +	private String applicationName;
  +	
  +	/** the working directory */
  +	private File applicationHome;
  +
  +	/** the temp directory */
  +	private File tempHome;	 
  +	
  +	/** is the instance properly initialized */
  +	private boolean isInitialized;
  +	
   	/**
   	 * Constructor
   	 */
  -    private Main()
  +    public Main()
       {
  -        this.isServerMode				= false;
  -        this.componentRoleValue 		= ServiceConstants.COMPONENT_ROLE_VALUE;
  -        this.componentConfigValue 		= ServiceConstants.COMPONENT_CONFIG_VALUE;
  -        this.componentParametersValue	= ServiceConstants.COMPONENT_PARAMETERS_VALUE;
  -                
  -        this.logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG );
  +        this.containerConfigValue   = "./conf/containerConfiguration.xml";
  +        this.isServerMode			= true;   
  +        this.logger 				= new ConsoleLogger();
  +        this.applicationHome 		= new File( new File("").getAbsolutePath() );
  +        this.tempHome				= new File( System.getProperty("java.io.tmpdir",".") );
  +        this.applicationName		= "yaafi.cli";
       }
   
       /**
  @@ -75,67 +85,55 @@
        */
       public static void main( String[] args ) throws Exception
       {
  -       Main impl = new Main();
  -       
  -       // Initialize the service manager
  +       int exitCode = 0;
          
  -       impl.initialize();             
  -       
  -       boolean terminateNow = ( impl.isServerMode ? false : true );
  +       Main impl = new Main();
          
  -       while( terminateNow == false )
  -       {
  -           try
  -           {
  -               Thread.sleep(1000);
  -           }
  -    	   catch (InterruptedException e)
  -    	   {
  -    	       terminateNow = true;
  -    	   }
  +       try
  +	   {
  +	       impl.run();
  +	   }
  +	   catch (Throwable t)
  +	   {
  +	       exitCode = 1;
  +	   }
  +	   finally
  +	   {
  +	       System.exit(exitCode);
   	   }
  -       
  -       impl.dispose();
       }
  -    
  -    protected void initialize() throws Exception
  -    {
  -        ServiceContainerConfiguration config = new ServiceContainerConfiguration();
           
  -        // intialize the service manager
  -        
  -        config.setLogger( new ConsoleLogger() );
  -        config.setComponentRolesLocation( this.componentRoleValue );
  -        config.setComponentConfigurationLocation( this.componentConfigValue );
  -        config.setParametersLocation( this.componentParametersValue );
  -        
  -        this.manager = ServiceContainerFactory.create(
  -            config
  -            );
  -             
  -        // initialize shutdown hoook of JVM
  -        
  -        Shutdown shutdown = new Shutdown( this.getManager(), this.getLogger() );
  -        this.shutdownThread = new Thread( shutdown );
  -        Runtime.getRuntime().addShutdownHook( this.shutdownThread );		       
  -    }
  +    /**
  +     * Dispose the YAAFI container
  +     */
   
  -    protected synchronized void dispose() throws Exception
  +    public synchronized void dispose()
       {
  -        if( this.getManager() != null )
  -        {
  -            this.getManager().dispose();
  -        }
  -    }
  -
  +        this.shutdown();
  +    }        
  +   
       /**
  -     * @return Returns the logger.
  +     * @see java.lang.Runnable#run()
        */
  -    protected Logger getLogger()
  +    public void run()
       {
  -        return this.logger;
  -    }
  +        try
  +        {
  +            this.initialize();             
   
  +            if( this.isServerMode() == false )
  +            {
  +                this.shutdown();
  +            }                                                
  +        }        
  +        catch (Throwable t)
  +        {
  +            String msg = "Failed to run " + this.getClass().getName(); 
  +            this.getLogger().error(msg,t);
  +            throw new RuntimeException(t.getMessage());
  +        }        
  +    }
  +    
       /////////////////////////////////////////////////////////////////////////
       // Generated getters & setters
       /////////////////////////////////////////////////////////////////////////
  @@ -143,43 +141,208 @@
       /**
        * @return Returns the manager.
        */
  -    public ServiceContainer getManager()
  +    public ServiceContainer getServiceContainer()
  +    {
  +        return this.container;
  +    }
  +        
  +    /**
  +     * @return Returns the applicationHome.
  +     */
  +    public File getApplicationHome()
  +    {
  +        return applicationHome;
  +    }
  +    
  +    /**
  +     * @param applicationHome The applicationHome to set.
  +     */
  +    public void setApplicationHome(File applicationHome)
  +    {
  +        this.applicationHome = applicationHome;
  +    }
  +    
  +    /**
  +     * @return Returns the containerConfigValue.
  +     */
  +    public String getContainerConfigValue()
  +    {
  +        return containerConfigValue;
  +    }
  +    
  +    /**
  +     * @param containerConfigValue The containerConfigValue to set.
  +     */
  +    public void setContainerConfigValue(String containerConfigValue)
  +    {
  +        this.containerConfigValue = containerConfigValue;
  +    }
  +    
  +    /**
  +     * @return Returns the isServerMode.
  +     */
  +    public boolean isServerMode()
  +    {
  +        return isServerMode;
  +    }
  +    
  +    /**
  +     * @param isServerMode The isServerMode to set.
  +     */
  +    public void setServerMode(boolean isServerMode)
  +    {
  +        this.isServerMode = isServerMode;
  +    }
  +    
  +    /**
  +     * @return Returns the tempHome.
  +     */
  +    public File getTempHome()
  +    {
  +        return tempHome;
  +    }
  +    
  +    /**
  +     * @param tempHome The tempHome to set.
  +     */
  +    public void setTempHome(File tempHome)
       {
  -        return this.manager;
  +        this.tempHome = tempHome;
       }
  +    
       /**
  -     * @param manager The manager to set.
  +     * @return Returns the logger.
        */
  -    public void setManager(ServiceContainerImpl manager)
  +    public Logger getLogger()
       {
  -        this.manager = manager;
  +        return this.logger;
       }
  +            
       /**
  -     * @return Returns the componentConfigValue.
  +     * @param logger The logger to set.
        */
  -    public String getComponentConfigValue()
  +    public void setLogger(Logger logger)
       {
  -        return this.componentConfigValue;
  +        this.logger = logger;
       }
  +        
       /**
  -     * @param componentConfigValue The componentConfigValue to set.
  +     * @return Returns the applicationName.
        */
  -    public void setComponentConfigValue(String componentConfigValue)
  +    public String getApplicationName()
       {
  -        this.componentConfigValue = componentConfigValue;
  +        return applicationName;
       }
  +
       /**
  -     * @return Returns the componentRoleValue.
  +     * @param applicationName The applicationName to set.
        */
  -    public String getComponentRoleValue()
  +    public void setApplicationName(String applicationName)
       {
  -        return this.componentRoleValue;
  +        this.applicationName = applicationName;
       }
  +    
  +    /**
  +     * @return Returns the isInitialized.
  +     */
  +    protected boolean isInitialized()
  +    {
  +        return isInitialized;
  +    }
  +    
       /**
  -     * @param componentRoleValue The componentRoleValue to set.
  +     * @param isInitialized The isInitialized to set.
        */
  -    public void setComponentRoleValue(String componentRoleValue)
  +    protected void setInitialized(boolean isInitialized)
       {
  -        this.componentRoleValue = componentRoleValue;
  +        this.isInitialized = isInitialized;
       }
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    // Implementation
  +    /////////////////////////////////////////////////////////////////////////
  +
  +    /**
  +     * Initialize the instance
  +     * 
  +     * @throws Exception the initialization failed
  +     */
  +    protected void initialize() throws Exception
  +    {       
  +        this.getLogger().info( "Initializing " + this.getClass().getName() );
  +        
  +        ServiceContainerConfiguration config = new ServiceContainerConfiguration();
  +        
  +        // intialize the Avalon container
  +                                
  +        config.setLogger( this.getLogger() );
  +        config.setApplicationRootDir( this.getApplicationHome() );
  +        config.setTempRootDir( this.getTempHome() );
  +        config.loadContainerConfiguration( this.getContainerConfigValue(), "auto" );
  +        this.container = ServiceContainerFactory.create( config );            
  +             
  +        // initialize shutdown hook of JVM for a server application
  +
  +        if( this.isServerMode() )
  +        {
  +	        this.getLogger().debug( "Registering shutdown hook" );
  +	        Shutdown shutdown = new Shutdown( this );
  +	        this.shutdownThread = new Thread( shutdown, "ShutdownThread" );
  +	        Runtime.getRuntime().addShutdownHook( this.shutdownThread );
  +        }
  +        
  +        this.setInitialized(true);
  +    }
  +
  +    /**
  +     * Terminates the instance
  +     * 
  +     * @throws Exception the termination failed
  +     */
  +    protected void shutdown()
  +    {
  +        if( this.isInitialized() == false )
  +        {
  +            return;
  +        }
  +        
  +        this.getLogger().info( "Terminating " + this.getClass().getName() );
  +
  +        try
  +        {            
  +            // wait for the shutdown thread
  +            
  +            if( this.shutdownThread != null )
  +            {
  +                try
  +                {
  +                    this.getLogger().debug( "Waiting for shutdown handler thread to terminate" );
  +                    this.shutdownThread.join(1000);
  +                    this.shutdownThread = null;
  +                    this.getLogger().debug( "Shutdown handler thread is terminated" );
  +                }
  +                catch (InterruptedException e)
  +                {
  +                    // nothing to do
  +                }                                
  +            }
  +            
  +            // dispose the service container
  +            
  +            if( this.getServiceContainer() != null )
  +            {
  +                this.getServiceContainer().dispose();
  +                this.container = null;
  +            }
  +            
  +            this.setInitialized(false);
  +        }
  +        
  +        catch (Exception e)
  +        {
  +            String msg = "Failed to terminate " + this.getClass().getName(); 
  +            this.getLogger().error(msg,e);
  +        }        
  +    }
  +    
   }
  
  
  
  1.5       +3 -9      jakarta-turbine-fulcrum/yaafi/src/java/org/apache/fulcrum/yaafi/cli/Shutdown.java
  
  Index: Shutdown.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/yaafi/src/java/org/apache/fulcrum/yaafi/cli/Shutdown.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Shutdown.java	1 Mar 2005 10:41:54 -0000	1.4
  +++ Shutdown.java	31 Mar 2005 12:12:25 -0000	1.5
  @@ -18,7 +18,6 @@
    */
   
   import org.apache.avalon.framework.activity.Disposable;
  -import org.apache.avalon.framework.logger.Logger;
   
   /**
    * This class process the shutdown notification from the JVM.
  @@ -30,26 +29,21 @@
   {
       /** The service manager tobe disposed */
       private Disposable disposable;
  -
  -    /** The logger to use */
  -    private Logger logger;
  -
  +    
       /**
        * Constructor
        * @param disposable The service manager to be disposed
        */
  -    public Shutdown( Disposable disposable, Logger logger )
  +    public Shutdown( Disposable disposable )
       {
           this.disposable = disposable;
  -        this.logger     = logger;
       }
   
       /**
        * @see java.lang.Runnable#run()
        */
       public void run()
  -    {
  -        this.logger.debug("The JVM is shutting down");
  +    {        
           this.disposable.dispose();
           this.disposable = null;
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org