You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jv...@apache.org on 2001/06/20 01:47:21 UTC

cvs commit: jakarta-turbine/src/java/org/apache/turbine/services/bsf BSFService.java TurbineBSF.java TurbineBSFService.java

jvanzyl     01/06/19 16:47:21

  Added:       src/java/org/apache/turbine/services/bsf BSFService.java
                        TurbineBSF.java TurbineBSFService.java
  Log:
  - bean scripting framework service. very rudimentary right now.
  
  - hopefully it can used for a new classloader/assembler broker to
    make turbine modules fully scriptable.
  
  - it can be used for configuration scripts that need to be run when
    a subapp is dropped into a running system.
  
  Revision  Changes    Path
  1.1                  jakarta-turbine/src/java/org/apache/turbine/services/bsf/BSFService.java
  
  Index: BSFService.java
  ===================================================================
  package org.apache.turbine.services.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.turbine.services.Service;
  
  /**
   *
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   */
  public interface BSFService 
      extends Service
  {
      public static final String SERVICE_NAME = "BSFService";
  
      /**
       * Execute a script supported by the BSF.
       *
       * @param String name of the script
       */
      public void execute(String script);
  }
  
  
  
  1.1                  jakarta-turbine/src/java/org/apache/turbine/services/bsf/TurbineBSF.java
  
  Index: TurbineBSF.java
  ===================================================================
  package org.apache.turbine.services.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.turbine.services.TurbineServices;
  
  /**
   *
   *
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   */
  public class TurbineBSF
  {
      /**
       * Utility method for accessing the service 
       * implementation
       *
       * @return a BSFService implementation instance
       */
      protected static BSFService getService()
      {
          return (BSFService)TurbineServices
              .getInstance().getService(BSFService.SERVICE_NAME);
      }
  
      public static void execute(String script)
      {
          getService().execute(script);
      }
  }
  
  
  
  1.1                  jakarta-turbine/src/java/org/apache/turbine/services/bsf/TurbineBSFService.java
  
  Index: TurbineBSFService.java
  ===================================================================
  package org.apache.turbine.services.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.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.turbine.services.BaseService;
  import org.apache.turbine.services.InitializationException;
  
  /**
   *
   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
   */
  public class TurbineBSFService 
      extends BaseService
      implements BSFService
  {
      /**
       * 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;
      
      /**
       * Tag for scripts directory in the service
       * configuration.
       */     
      protected static final String SCRIPTS_DIRECTORY = "scriptsDirectory";
  
      /**
       * Default extension for scripts if an extension is not
       * provided.
       */
      protected String defaultExtension;
  
      /**
       * Tag for default extension in the service
       * configuration.
       */
      protected static final String DEFAULT_EXTENSION = "defaultExtension";
  
      /**
       * Initialize the TurbineBSF Service.
       */
      public void init()
          throws InitializationException
      {
          scriptsDirectory = getConfiguration().getString(SCRIPTS_DIRECTORY);
         
          if (scriptsDirectory == null)
          {
              throw new InitializationException(
                  "You must provide a scripts directory in " +
                      "order to executes scripts!");
          }                    
         
          defaultExtension = getConfiguration().getString(DEFAULT_EXTENSION);
          initBSFManagers();
          setInit(true);
      }
  
      private void initBSFManagers()
      {
          manager = new BSFManager();
      }
      
      /**
       * 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);
          
          getCategory().debug("Script to execute: " + script);
          
          try
          {
              manager.exec(manager.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();
      }
  }
  
  
  

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