You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2004/02/13 15:22:48 UTC

cvs commit: jakarta-turbine-fulcrum/bsf/src/test TestComponentConfig.xml TestRoleConfig.xml

epugh       2004/02/13 06:22:47

  Added:       bsf      LICENSE.txt .cvsignore project.properties
                        project.xml
               bsf/xdocs navigation.xml changes.xml index.xml
               bsf/src/java/org/apache/fulcrum/bsf BSFService.java
                        DefaultBSFService.java
               bsf/src/test TestComponentConfig.xml TestRoleConfig.xml
  Log:
  Add BSF component
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-fulcrum/bsf/LICENSE.txt
  
  Index: LICENSE.txt
  ===================================================================
  /* ----------------------------------------------------------------------------
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Plexus", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ----------------------------------------------------------------------------
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ----------------------------------------------------------------------------
   */
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  target
  .classpath
  .project
  *.ser
  *.log
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/project.properties
  
  Index: project.properties
  ===================================================================
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0"?>
  <project>
    <extend>${basedir}/../project.xml</extend>
    <id>fulcrum-bsf</id>
    <name>Fulcrum BSF Component</name>
    <currentVersion>1.0-alpha-1</currentVersion>
  
    <dependencies>
      
      <dependency>
        <groupId>bsf</groupId>
        <artifactId>bsf</artifactId>
        <version>2.2</version>
      </dependency>  
      
      <!--  Needed only for testing -->
      <dependency>
        <groupId>fulcrum</groupId>
        <artifactId>fulcrum-testcontainer</artifactId>
        <version>1.0-alpha-2</version>
      </dependency>
  
      <dependency>
        <id>xerces:xercesImpl</id>
        <version>2.0.2</version>
        <url>http://xml.apache.org/xerces2-j/</url>
      </dependency>
  
      <dependency>
        <id>xerces:xmlParserAPIs</id>
        <version>2.0.2</version>
        <url>http://xml.apache.org/xerces2-j/</url>
      </dependency>
  
    </dependencies>
  
  
  </project>
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/xdocs/navigation.xml
  
  Index: navigation.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <project
    name="cache"
    href="http://jakarta.apache.org/turbine/fulcrum/cache/">
  
    <body>
      <links>
        <item name="Turbine"
              href="http://jakarta.apache.org/turbine/"/>
        <item name="Fulcrum"
              href="http://jakarta.apache.org/turbine/fulcrum/"/>
      </links>
  
      <menu name="Overview">
        <item name="Main"                 href="/index.html"/>
      </menu>
    </body>
  </project>
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  <?xml version="1.0"?>
  <document>
    <properties>
      <title>Fulcrum BSF</title>
      <author email="epugh@opensourceconnections.com">Eric Pugh</author>
    </properties>
  
    <body>
      <release version="1.0-alpha-2" date="in CVS">
       <action dev="epugh" type="add">
          Initial conversion to Avalon component.
        </action>      
      </release>
  
    </body>
  </document>
  
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/xdocs/index.xml
  
  Index: index.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <document>
  
    <properties>
      <title>BSF Component</title>
      <author email="epugh@opensourceconnections.com">Eric Pugh</author>
    </properties>
  
    <body>
  
    <section name="Overview">
      <p>
       This Service functions as a BSF component.
     </p>
        
      <p>
        It is written 
        for use in Turbine but it can be used in any container compatible 
        with Avalon's ECM container.
      </p>    
    </section>
    
  <section name="Configuration">
  
      <p>
        First, here is the role configuration.
      </p>
  
      <source>
      <![CDATA[
          <role
              name="org.apache.fulcrum.bsf.BSFService"
              shorthand="cache"
              default-class="org.apache.fulcrum.bsf.DefaultBSFService"/>
      ]]>
      </source>
  
        <p>
          And here is the configuration:
        </p>
      <source>
  
      <![CDATA[
          <cache cacheInitialSize="20" cacheCheckFrequency="5"/>
      ]]>
      </source>
  
    </section>
  
    <section name="Usage">
  
      <source><![CDATA[
  
      GlobalCacheService gs = null;
      try
      {
          /*
           * Look for the item in the cache.
           * If it doesn't exist or the item is stale,
           * the cache will throw an exception.
           */
          gs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE)
  
          CachedObject obj = gs.getObject("cached_object");
  
          data.setMessage( data.getScreen() + " Got " +
              obj.getContents().toString() + " from global cache!" );
      }
      catch(ObjectExpiredException gone)
      {
          /*
           * Add the item to the cache.
           */
          gs.addObject("cached_object",
              new CachedObject("in_the_cache",5000));
  
          data.setMessage( data.getScreen() +
              " Refreshed/or added new item to" +
              " the cache! Expires in 5 seconds" );
      }
  
      ]]></source>
  
      <p>
      You can also place an expiration time on your objects so the Service will
      automatically remove them when they expire. If you don't specify an expiration
      time, the Service uses 5 seconds. To see an example, look at the 
      test case <a href="xref-test/org/apache/fulcrum/cache/CacheTest.html">CacheTest</a>
      </p>
  
    </section> 
  
  </body>
  </document>
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/src/java/org/apache/fulcrum/bsf/BSFService.java
  
  Index: BSFService.java
  ===================================================================
  package org.apache.fulcrum.bsf;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.avalon.framework.component.Component;
  
  /**
   *
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   */
  public interface BSFService
      extends Component
  {
      /** Avalon role - used to id the component within the manager */
      String ROLE = BSFService.class.getName();
  
      /**
       * Execute a script supported by the BSF.
       *
       * @param String name of the script
       */
      public void execute(String script);
  }
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/src/java/org/apache/fulcrum/bsf/DefaultBSFService.java
  
  Index: DefaultBSFService.java
  ===================================================================
  package org.apache.fulcrum.bsf;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.File;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.IOException;
  import java.io.FileInputStream;
  import java.io.StringWriter;
  import com.ibm.bsf.BSFManager;
  import com.ibm.bsf.BSFException;
  
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.thread.ThreadSafe;
  
  /**
   *
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   */
  public class DefaultBSFService extends AbstractLogEnabled
      implements BSFService, Configurable, Initializable, ThreadSafe, Contextualizable
  {
  
        /**
         * The application root
         */
      private String applicationRoot;
      /**
       * Tag for scripts directory in the service
       * configuration.
       */
      protected static final String SCRIPTS_DIRECTORY = "scriptsDirectory";
  
      /**
       * Tag for default extension in the service
       * configuration.
       */
      protected static final String DEFAULT_EXTENSION = "defaultExtension";
  
      /**
       * BSF manager that is responsible for executing scripts.
       * This may eventually be a pool of managers and
       * utilize the pool service.
       */
      protected BSFManager manager;
  
      /**
       * Directory where scripts are stored
       */
      protected String scriptsDirectory;
  
      /**
       * Default extension for scripts if an extension is not
       * provided.
       */
      protected String defaultExtension;
  
  
      public DefaultBSFService()
      {
      }
  
      /**
       * Execute a script. The script can be in any of
       * the scripting languages supported by the BSF.
       *
       * @param String name of the script
       */
      public void execute(String script)
      {
          if (script.lastIndexOf('.') == -1)
          {
              script += '.' + defaultExtension;
          }
  
          script = getRealPath(scriptsDirectory + "/" + script);
  
          getLogger().debug("[BSFService] Script to execute: " + script);
  
          try
          {
              manager.exec(BSFManager.getLangFromFilename(script),
                  script, 0, 0, fileContentsToString(script));
          }
          catch (IOException ioe)
          {
              ioe.printStackTrace();
          }
          catch (BSFException bsfe)
          {
              /*
               * Find out what went wrong with the processing
               * of the script.
               */
              int reason = bsfe.getReason();
              bsfe.printStackTrace();
          }
      }
  
      /**
       * Create a string from the contents of a file.
       *
       * @param String file from which to read the contents
       * @return String file contents
       *
       */
      protected String fileContentsToString(String file)
          throws IOException
      {
          StringWriter sw = new StringWriter();
  
          BufferedReader reader = new BufferedReader(
              new InputStreamReader(new FileInputStream(file)));
  
          char buf[] = new char[1024];
          int len = 0;
  
          while ( ( len = reader.read( buf, 0, 1024 )) != -1)
          {
              sw.write( buf, 0, len );
          }
  
          return sw.toString();
      }
  
    /**
       * @see org.apache.fulcrum.ServiceBroker#getRealPath(String)
       */
      public String getRealPath(String path)
      {
          String absolutePath = null;
          if (applicationRoot == null)
          {
              absolutePath = new File(path).getAbsolutePath();
          }
          else
          {
              absolutePath = new File(applicationRoot, path).getAbsolutePath();
          }
  
          return absolutePath;
      }
  
      // ---------------- Avalon Lifecycle Methods ---------------------
  
      /**
       * Avalon component lifecycle method
       */
      public void configure(Configuration conf)
          throws ConfigurationException
      {
  
              scriptsDirectory = conf.getAttribute(SCRIPTS_DIRECTORY, null);
              if (scriptsDirectory == null)
              {
                  throw new ConfigurationException(
                      "You must provide a scripts directory in " +
                      "order to executes scripts!");
              }
  
              defaultExtension = conf.getAttribute(DEFAULT_EXTENSION, "bsf");
  
      }
  
      public void contextualize(Context context) throws ContextException {
          this.applicationRoot = context.get( "urn:avalon:home" ).toString();
      }
  
      /**
       * Avalon component lifecycle method
       */
      public void initialize()
      {
          manager = new BSFManager();
      }
  
  }
  
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/src/test/TestComponentConfig.xml
  
  Index: TestComponentConfig.xml
  ===================================================================
  <componentConfig>
      <cache cacheInitialSize="20" cacheCheckFrequency="5"/>
  </componentConfig>
  
  
  1.1                  jakarta-turbine-fulcrum/bsf/src/test/TestRoleConfig.xml
  
  Index: TestRoleConfig.xml
  ===================================================================
  <!-- This configuration file for Avalon components is used for testing the TestComponent -->
  <role-list>
      <role
          name="org.apache.fulcrum.cache.GlobalCacheService"
          shorthand="cache"
          default-class="org.apache.fulcrum.cache.DefaultGlobalCacheService"/>
  </role-list>
  
  

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