You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cr...@apache.org on 2002/07/22 19:54:32 UTC

cvs commit: jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer ComponentViewer.java ComponentViewer.roles ComponentViewer.xconf ComponentViewer.xlog Main.java

crafterm    2002/07/22 10:54:32

  Added:       fortress/examples/bin runviewer.sh
               fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer
                        ComponentViewer.java ComponentViewer.roles
                        ComponentViewer.xconf ComponentViewer.xlog
                        Main.java
  Log:
  Initial commit of a small component 'viewer' tool I wrote to test out the
  different initialization policies a component can specify about it's
  respective ComponentHandler.
  
  It provides a small gui with a combobox list of all registered components
  this container has, and the ability fire a 'lookup' event on any one of
  them.
  
  (by tracing the log, it's possible to see the difference on a lookup() between
  request/startup based inits).
  
  Perhaps this might be useful for others, or maybe extended to become a
  gui component testing tool, etc ? Ideas more than welcome.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/fortress/examples/bin/runviewer.sh
  
  Index: runviewer.sh
  ===================================================================
  #!/bin/bash
  #
  # Startup script.
  #
  
  #
  # Determine if JAVA_HOME is set and if so then use it
  #
  if [ -z "$JAVA_HOME" ] ;  then
    JAVA=`which java`
    if [ -z "$JAVA" ] ; then
      echo "Cannot find JAVA. Please set your PATH."
      exit 1
    fi
    JAVA_BINDIR=`dirname $JAVA`
    JAVA_HOME=$JAVA_BINDIR/..
  fi
  
  if [ "$JAVACMD" = "" ] ; then
     # it may be defined in env - including flags!!
     JAVACMD=$JAVA_HOME/bin/java
  fi
  
  # Main.java has hard coded config values so this script must be run from
  # altprofile/bin (any better ideas ?)
  EXAMPLE_HOME=..
  
  #
  # Build the runtime classpath
  #
  for i in ${EXAMPLE_HOME}/lib/*.jar ; do
      CP=${CP}:$i
  done
  
  CP=${CP}:${EXAMPLE_HOME}/build/classes
  
  echo $CP
  
  # Run the example application
  $JAVACMD -classpath $CP org.apache.excalibur.fortress.examples.viewer.Main $@
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer/ComponentViewer.java
  
  Index: ComponentViewer.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.fortress.examples.viewer;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import java.util.Iterator;
  import java.util.Set;
  
  import javax.swing.BoxLayout;
  import javax.swing.JButton;
  import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import org.apache.avalon.framework.activity.Startable;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  
  import org.apache.excalibur.fortress.AbstractContainer;
  
  /**
   * Simple Fortress container containing a Swing based viewer for performing
   * lookups on registered components.
   *
   * <p>
   * The intention of the viewer is to allow you to perform a lookup of a component
   * manually, (currently) so you can check the see the effect of lazy vs startup
   * initialization.
   * </p>
   *
   * <p>
   * REVISIT: add a text component which tracks the log file to make it easier to
   * see a component being initialized upon first lookup.
   * </p>
   *
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/07/22 17:54:31 $
   */
  public final class ComponentViewer extends AbstractContainer
      implements Startable, ActionListener
  {
      // GUI references
      private JFrame m_frame;
      private JComboBox m_components;
  
      /**
       * Initializes this component. Creates simple Swing GUI containing
       * available translations for the key 'hello-world'.
       *
       * @exception Exception if an error occurs
       */
      public void initialize()
          throws Exception
      {
          super.initialize();
  
          // create main frame
          m_frame = new JFrame( "Component Viewer" );
          m_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  
  	m_components = new JComboBox( getRoles() );
  
  	JButton button = new JButton( "Lookup!" );
  	button.addActionListener( this );
  
  	/*
  	// can we output the log data into this text area somehow ?
  	JTextArea logData = new JTextArea();
  	logData.setEditable( false );
  	*/
  
  	JPanel selectionPanel = new JPanel();
  	selectionPanel.add( m_components );
  	selectionPanel.add( button );
  
  	JPanel mainPanel = new JPanel();
  	mainPanel.setLayout( new BoxLayout( mainPanel, BoxLayout.Y_AXIS) );
  	mainPanel.add( selectionPanel );
  
          m_frame.setContentPane( mainPanel );
          m_frame.pack();
  
          // all done
          if ( getLogger().isDebugEnabled() )
          {
              getLogger().debug( "Initialized" );
          }
      }
  
      /**
       * Helper method to obtain a list of all Roles registered with this container
       *
       * @return an array of roles
       */
      private Object[] getRoles()
      {
          Set keys = m_mapper.keySet();
          Object[] roles = new Object[ keys.size() ];
          int j = 0;
  
          for ( Iterator i = keys.iterator(); i.hasNext(); )
          {
              roles[j++] = (String) i.next();
          }
  
          return roles;
      }
  
      /**
       * Starts the component, makes GUI visible, ready for use.
       */
      public void start()
      {
          m_frame.setVisible( true );
  
          if ( getLogger().isDebugEnabled() )
          {
              getLogger().debug( "GUI Activated" );
          }
      }
  
      /**
       * Stops component, make GUI invisible, ready for decomissioning.
       */
      public void stop()
      {
          m_frame.setVisible( false );
  
          if ( getLogger().isDebugEnabled() )
          {
              getLogger().debug( "GUI Disactivated" );
          }
      }
  
      /**
       * Handles the <i>lookup</i> event. Finds out which component
       * was selected and performs a lookup and release on this component.
       *
       * @param evt an <code>ActionEvent</code> value
       */
      public void actionPerformed( ActionEvent evt )
      {
          String selected = (String) m_components.getSelectedItem();
  
          if ( getLogger().isDebugEnabled() )
          {
              getLogger().debug("Looking up component " + selected);
          }
  
  	Object component = null;
  
          try
          {
              component = m_serviceManager.lookup( selected );
          }
          catch (ServiceException e)
          {
              if ( getLogger().isWarnEnabled() )
              {
                  getLogger().warn( "Error looking up component: "  + e.getRole(), e );
              }
          }
  	finally
  	{
              if ( component != null) m_serviceManager.release( component );
  	}
      }
  }
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer/ComponentViewer.roles
  
  Index: ComponentViewer.roles
  ===================================================================
  <fortress-roles>
  
    <role name="org.apache.excalibur.fortress.examples.components.Translator">
      <component shorthand="translator"
                 class="org.apache.excalibur.fortress.examples.components.TranslatorImpl"
                 handler="org.apache.excalibur.fortress.handler.ThreadSafeComponentHandler"/>
    </role>
  
  </fortress-roles>
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer/ComponentViewer.xconf
  
  Index: ComponentViewer.xconf
  ===================================================================
  <?xml version="1.0" encoding="iso-8859-1" ?>
  
  <!-- Example Fortress configuration file -->
  <fortress-example>
  
    <!-- Simple translation component. This component maintains translations for a
         given key in different languages.
     -->
    <translator id="translator" logger="translator" activation="request">
  	<dictionary>
  	  <translation key="hello-world">
  	    <value language="Deutsch">Hallo Welt</value>
  	    <value language="English">Hello World</value>
  	    <value language="Fran�ais">Bonjour la monde</value>
  	    <value language="Indonesia">Apa kabar Dunia</value>
  	    <value language="Espan�l">Hola Mundo</value>
  	    <value language="Italiano">Ciao Mondo</value>
  	  </translation>
  	</dictionary>
    </translator>
  
  </fortress-example>
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer/ComponentViewer.xlog
  
  Index: ComponentViewer.xlog
  ===================================================================
  <logkit>
      <factories>
        <factory type="file" class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/>
      </factories>
  
      <targets>
         <file id="root">
          <filename>fortress-viewer.log</filename>
          <format type="extended">
            %7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\n%{throwable}
          </format>
         </file>
       </targets>
  
       <categories>
         <category name="" log-level="DEBUG">
           <log-target id-ref="root"/>
         </category>
       </categories>
  
  </logkit>
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/viewer/Main.java
  
  Index: Main.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.fortress.examples.viewer;
  
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.logger.LogKitLogger;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.CascadingException;
  import org.apache.excalibur.fortress.Container;
  import org.apache.excalibur.fortress.ContainerManager;
  import org.apache.excalibur.fortress.DefaultContainerManager;
  import org.apache.excalibur.fortress.util.ContextBuilder;
  import org.apache.excalibur.fortress.util.ContextManager;
  
  /**
   * Fortress container example allowing you to perform lookups on components
   * via a simple swing gui.
   *
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @version $Id: Main.java,v 1.1 2002/07/22 17:54:31 crafterm Exp $
   */
  public final class Main
  {
      // container reference
      private static ComponentViewer m_container;
  
      /**
       * @param args a <code>String[]</code> array of command line arguments
       * @exception Exception if an error occurs
       */
      public static final void main( String[] args )
          throws Exception
      {
          try
          {
              ContextBuilder contextBuilder = new ContextBuilder();
              contextBuilder.setContainerClass( "org.apache.excalibur.fortress.examples.viewer.ComponentViewer" );
              contextBuilder.setContextDirectory( "./" );
              contextBuilder.setWorkDirectory( "./" );
              contextBuilder.setContainerConfiguration( "resource://org/apache/excalibur/fortress/examples/viewer/ComponentViewer.xconf" );
              contextBuilder.setLoggerManagerConfiguration( "resource://org/apache/excalibur/fortress/examples/viewer/ComponentViewer.xlog" );
              contextBuilder.setRoleManagerConfiguration( "resource://org/apache/excalibur/fortress/examples/viewer/ComponentViewer.roles" );
  
              ContextManager contextManager = new ContextManager( contextBuilder.getContext(), null );
              contextManager.initialize();
  
              ContainerManager cm = new DefaultContainerManager( contextManager );
              cm.initialize();
  
              m_container = ( ComponentViewer ) cm.getContainer();
          }
          catch (CascadingException e)
          {
              e.printStackTrace();
  
              Throwable t = e.getCause();
  
              while ( t != null )
              {
                  t.printStackTrace();
  
                  if ( t instanceof CascadingException )
                      t = ((CascadingException) t).getCause();
                  else
                      t = null;
              }
          }
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>