Author: sgoeschl
Date: Tue Jul  1 08:08:55 2008
New Revision: 673094

URL: http://svn.apache.org/viewvc?rev=673094&view=rev
Log:
Added Spring Framework integration (1.2.x and 2.5.x) which allows to 
instantiate a YAAFI container. When running YAAFI inside of Spring the 
integration allows to access Spring beans from within Avalon services and also 
the other way around.

Added:
    turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/
    
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceContainerBean.java
    
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceFactoryBean.java
    
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/BeanFactoryServiceManager.java
    turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/AvalonIntoSpringTest.java
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonService.java
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonServiceImpl.java
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringService.java
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringServiceImpl.java
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml
    
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml
Modified:
    turbine/fulcrum/trunk/yaafi/pom.xml
    
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerConfiguration.java
    
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerFactory.java

Modified: turbine/fulcrum/trunk/yaafi/pom.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/pom.xml?rev=673094&r1=673093&r2=673094&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi/pom.xml (original)
+++ turbine/fulcrum/trunk/yaafi/pom.xml Tue Jul  1 08:08:55 2008
@@ -52,7 +52,7 @@
       <name>Siegfried Goeschl</name>
       <id>sgoeschl</id>
       <email>[EMAIL PROTECTED]</email>
-      <organization></organization>
+      <organization>Apache</organization>
       <roles>
         <role>Java Developer</role>
       </roles>
@@ -66,12 +66,29 @@
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
+    <!-- Spring 1.2.x depedencies -->
     <dependency>
-      <groupId>com.jamonapi</groupId>
-      <artifactId>jamon</artifactId>
-      <version>2.4</version>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring</artifactId>
+      <version>1.2.9</version>    
       <optional>true</optional>
+      <exclusions>
+        <exclusion>
+          <groupId>javax.mail</groupId>
+          <artifactId>mail</artifactId>
+        </exclusion>
+      </exclusions>      
     </dependency>    
+    <!-- Spring 2.5.x depedencies -->
+      <!--
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring</artifactId>
+      <version>2.5.4</version>    
+      <optional>true</optional>
+    </dependency>
+    -->
+    <!-- Avalon depedencies -->
     <dependency>
       <groupId>avalon-framework</groupId>
       <artifactId>avalon-framework-api</artifactId>
@@ -126,6 +143,13 @@
         </exclusion>        
       </exclusions>
     </dependency>
+    <!-- Interceptor dependencies -->
+    <dependency>
+      <groupId>com.jamonapi</groupId>
+      <artifactId>jamon</artifactId>
+      <version>2.4</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>
@@ -133,4 +157,4 @@
     <testSourceDirectory>${basedir}/src/test</testSourceDirectory>
   </build>
        
-</project>
+</project>
\ No newline at end of file

Modified: 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerConfiguration.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerConfiguration.java?rev=673094&r1=673093&r2=673094&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerConfiguration.java
 (original)
+++ 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerConfiguration.java
 Tue Jul  1 08:08:55 2008
@@ -32,6 +32,7 @@
 import org.apache.avalon.framework.context.DefaultContext;
 import org.apache.avalon.framework.logger.ConsoleLogger;
 import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
 import org.apache.fulcrum.yaafi.framework.constant.AvalonMerlinConstants;
 import org.apache.fulcrum.yaafi.framework.container.ServiceConstants;
@@ -99,6 +100,9 @@
     /** the caller-supplied container configuration */
     private Configuration containerConfiguration;
 
+    /** to lookup service in the parent container */
+    private ServiceManager parentServiceManager;
+
     /** Constructor */
     public ServiceContainerConfiguration()
     {
@@ -588,6 +592,14 @@
     {
         this.containerConfiguration = containerConfiguration;
     }
+    
+    public ServiceManager getParentServiceManager() {
+        return parentServiceManager;
+    }
+
+    public void setParentServiceManager(ServiceManager parentServiceManager) {
+        this.parentServiceManager = parentServiceManager;
+    }
 
     /**
      * Loads a containerConfiguration file and set is as the Avalon
@@ -679,7 +691,7 @@
     {
         File result = new File(fileName);
 
-        if( result.isAbsolute() == false )
+        if(!result.isAbsolute())
         {
             result = new File( baseDir, fileName );
         }

Modified: 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerFactory.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerFactory.java?rev=673094&r1=673093&r2=673094&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerFactory.java
 (original)
+++ 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/framework/factory/ServiceContainerFactory.java
 Tue Jul  1 08:08:55 2008
@@ -121,6 +121,12 @@
 
         ContainerUtil.enableLogging( result, serviceManagerConfig.getLogger() 
);
         ContainerUtil.contextualize( result, context );
+
+        if(serviceManagerConfig.getParentServiceManager() != null)
+        {
+            ContainerUtil.service(result, 
serviceManagerConfig.getParentServiceManager());
+        }
+        
         ContainerUtil.configure( result, configuration );
         ContainerUtil.initialize( result );
 
@@ -154,7 +160,10 @@
     }
 
     /**
-     * Reads the implementation class of the YAAFI container
+     * Reads the implementation class of the YAAFI container.
+     *
+     * @param configuration the Avalon configuration
+     * @return the implementation class name of the container
      */
     private static String getServiceContainerClazzName( Configuration 
configuration )
     {

Added: 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceContainerBean.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceContainerBean.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceContainerBean.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceContainerBean.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,310 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.File;
+
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.logger.CommonsLogger;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
+import 
org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
+import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanNameAware;
+import org.springframework.beans.BeansException;
+
+/**
+ * A POJO starting/stopping the YAAFI Avalon container and exposing a 
ServiceManager.
+ * This allows to run an Avalon container within Spring and to lookup Avalon
+ * services.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a>
+ */
+
+public class AvalonServiceContainerBean implements BeanNameAware, 
BeanFactoryAware, ServiceManager, Initializable, Disposable
+{
+    /** The service manager */
+    private Object serviceContainer;
+
+    /** The location of the container configuration */
+    private String containerConfigValue;
+
+    /** The logger being used */
+    private Logger logger;
+
+    /** the working directory */
+    private String applicationHome;
+
+    /** the temp directory */
+    private String tempHome;
+
+    /** the Spring bean factory creating this instance */
+    private BeanFactory beanFactory;
+
+    /** the name of the bean */
+    private String beanName;
+
+    /** the name of the CommonsLogger instance */
+    private String loggerName;
+    
+    /**
+     * Constructor
+     */
+    public AvalonServiceContainerBean()
+    {
+        this.containerConfigValue   = "./conf/containerConfiguration.xml";
+        this.applicationHome        = ".";
+        this.tempHome               = System.getProperty("java.io.tmpdir",".");
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+    // Interface Implementation
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Initialize the instance. This method must be configured using
+     * the 'init-method' attribute. 
+     *
+     * @see org.apache.avalon.framework.activity.Initializable#initialize()
+     * @throws Exception the initialization failed
+     */
+    public void initialize() throws Exception
+    {
+        ServiceContainerConfiguration config = new 
ServiceContainerConfiguration();
+
+        // wrap Spring's BeanFactory to allow service lookups
+        BeanFactoryServiceManager beanFactoryServiceManager = new 
BeanFactoryServiceManager(this.beanFactory);
+
+        // create a logger
+        this.createLogger();
+
+        // intialize the Avalon serviceContainer
+        config.setLogger( this.getLogger() );
+        config.setApplicationRootDir( this.getApplicationHome() );
+        config.setTempRootDir( this.getTempHome() );
+        config.loadContainerConfiguration( this.getContainerConfigValue(), 
"auto" );
+        config.setParentServiceManager(beanFactoryServiceManager);
+
+        this.serviceContainer = ServiceContainerFactory.create( config );
+    }
+
+    /**
+     * Dispose the YAAFI container. This method must be configured using
+     * the 'destroy-method' attribute.
+     *
+     * @see org.apache.avalon.framework.activity.Disposable#dispose()
+     */
+    public void dispose()
+    {
+        if( this.serviceContainer == null)
+        {
+            return;
+        }
+
+        this.getLogger().debug( "Terminating " + this.getClass().getName() );
+
+        try
+        {
+            // dispose the service serviceContainer
+
+            if( this.serviceContainer != null )
+            {
+                ((Disposable) this.serviceContainer).dispose();
+            }
+        }
+        catch (Exception e)
+        {
+            String msg = "Failed to terminate " + this.getClass().getName();
+            this.getLogger().error(msg,e);
+        }
+        finally {
+            this.serviceContainer = null;
+        }
+    }
+
+    /** @see org.apache.avalon.framework.service.ServiceManager#lookup(String) 
*/
+    public Object lookup(String s) throws ServiceException
+    {
+        return ((ServiceManager) this.serviceContainer).lookup(s);
+    }
+
+    /** @see 
org.apache.avalon.framework.service.ServiceManager#hasService(String) */
+    public boolean hasService(String s)
+    {
+        return ((ServiceManager) this.serviceContainer).hasService(s);
+    }
+
+    /** @see 
org.apache.avalon.framework.service.ServiceManager#release(Object) */
+    public void release(Object o)
+    {
+        ((ServiceManager) this.serviceContainer).release(o);
+    }
+
+    /** @see 
org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
  */
+    public void setBeanFactory(BeanFactory beanFactory) throws BeansException
+    {
+        this.beanFactory = beanFactory;
+    }
+
+    /** @see 
org.springframework.beans.factory.BeanNameAware#setBeanName(String) */
+    public void setBeanName(String name)
+    {
+        this.beanName = name;
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+    // Generated getters & setters
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * @return Returns the applicationHome.
+     */
+    public String getApplicationHome()
+    {
+        return this.applicationHome;
+    }
+
+    /**
+     * @param applicationHome The applicationHome to set.
+     */
+    public void setApplicationHome(String 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 tempHome.
+     */
+    public String getTempHome()
+    {
+        return this.tempHome;
+    }
+
+    /**
+     * @param tempHome The tempHome to set.
+     */
+    public void setTempHome(String tempHome)
+    {
+        this.tempHome = tempHome;
+    }
+
+    /**
+     * @return Returns the loggerName.
+     */
+    public String getLoggerName()
+    {
+        if(this.loggerName == null)
+        {
+            return this.beanName;
+        }
+        else
+        {
+            return loggerName;
+        }
+    }
+
+    /**
+     * @param loggerName The loggerName to set.
+     */
+    public void setLoggerName(String loggerName)
+    {
+        this.loggerName = loggerName;
+    }
+
+    /**
+     * @return Returns the logger.
+     */
+    public Logger getLogger()
+    {
+        return this.logger;
+    }
+
+    /**
+     * @param logger The logger to set.
+     */
+    public void setLogger(Logger logger)
+    {
+        this.logger = logger;
+    }
+
+    /////////////////////////////////////////////////////////////////////////
+    // Implementation
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Create the Avalon Logger to be used for the Avalon container. This
+     * method can be overridden if you need a different logger.
+     *
+     * @return avalon loggger
+     */
+    public Logger createLogger()
+    {
+        if(this.logger == null)
+        {
+            Log log = LogFactory.getLog(this.getLoggerName());
+            this.logger = new CommonsLogger(log, this.getLoggerName());
+        }
+
+        return this.logger;
+    }
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuffer result = new StringBuffer();
+        
result.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode()));
+        result.append('[');
+        result.append("workingDir=").append(new File("").getAbsolutePath());
+        result.append(',');
+        result.append("applicationHome=").append(this.getApplicationHome());
+        result.append(',');
+        result.append("tempHome=").append(this.getTempHome());
+        result.append(',');
+        result.append("logger=").append(this.getLogger().getClass().getName());
+        result.append(',');
+        
result.append("containerConfigValue=").append(this.getContainerConfigValue());
+        result.append(']');
+        return result.toString();
+    }
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceFactoryBean.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceFactoryBean.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceFactoryBean.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/AvalonServiceFactoryBean.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,86 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.apache.avalon.framework.service.ServiceManager;
+
+/**
+ * A Spring factoryy bean to lookup Avalon service and inject
+ * them to other Spring beans.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a>
+ */
+
+public class AvalonServiceFactoryBean implements FactoryBean, InitializingBean 
{
+
+    /** the Avalon ServiceManager */
+    private ServiceManager serviceManager;
+
+    /** the name of the Avalon service to resolve */
+    private String serviceName;
+
+    /** the resolved Avalon service */
+    private Object service;
+
+    /**
+     * Resolve the service name to a service.
+     * 
+     * @see 
org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
+     */
+    public void afterPropertiesSet() throws Exception {
+        this.service = this.serviceManager.lookup(this.serviceName);           
 
+    }
+
+    /**
+     * Set the Avalon ServiceManager to lookup the Avalon service.
+     *
+     * @param serviceManager the Avalon ServiceManager
+     */
+    public void setServiceManager(ServiceManager serviceManager) {
+        this.serviceManager = serviceManager;
+    }
+
+    /**
+     * Set the name of the Avalon service to be resolved by
+     * this Spring bean factory.
+     *
+     * @param serviceName the Avalon service name
+     */
+    public void setServiceName(String serviceName) {
+        this.serviceName = serviceName;
+    }
+
+    /** @see org.springframework.beans.factory.FactoryBean#getObject() */
+    public Object getObject() throws Exception {
+        return this.service;
+    }
+
+    /** @see org.springframework.beans.factory.FactoryBean#getObjectType() */
+    public Class getObjectType() {
+        return this.service.getClass();
+    }
+
+    /** @see org.springframework.beans.factory.FactoryBean#isSingleton() */
+    public boolean isSingleton() {
+        return true;
+    }
+}

Added: 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/BeanFactoryServiceManager.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/BeanFactoryServiceManager.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/BeanFactoryServiceManager.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/java/org/apache/fulcrum/yaafi/spring/BeanFactoryServiceManager.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,70 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.springframework.beans.factory.BeanFactory;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
+
+/**
+ * Wraps a Spring bean factory to implement an Avalon service
+ * lookup.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a>
+ */
+public class BeanFactoryServiceManager implements ServiceManager
+{
+    /** Spring's bean factory */
+    private BeanFactory beanFactory;
+
+    /**
+     * Constructor
+     *
+     * @param beanFactory Spring's bean factory
+     */
+    public BeanFactoryServiceManager(BeanFactory beanFactory)
+    {
+        this.beanFactory = beanFactory;
+    }
+
+    /** @see org.apache.avalon.framework.service.ServiceManager#lookup(String) 
*/
+    public Object lookup(String key) throws ServiceException
+    {
+        try
+        {
+            return beanFactory.getBean(key);
+        }
+        catch(Exception e)
+        {
+            throw new ServiceException(key, "Unable to lookup service using 
Spring's BeanFactory", e);
+        }
+    }
+
+    /** @see 
org.apache.avalon.framework.service.ServiceManager#hasService(String) */
+    public boolean hasService(String key)
+    {
+        return beanFactory.containsBean(key);
+    }
+
+    /** @see 
org.apache.avalon.framework.service.ServiceManager#release(Object) */
+    public void release(Object o) {
+        // nothing to do for Spring beans
+    }
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/AvalonIntoSpringTest.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/AvalonIntoSpringTest.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/AvalonIntoSpringTest.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/AvalonIntoSpringTest.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,95 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import junit.framework.TestCase;
+import org.springframework.context.support.FileSystemXmlApplicationContext;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.apache.avalon.framework.service.ServiceManager;
+
+/**
+ * Test suite for the project
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a>
+ */
+
+public class AvalonIntoSpringTest extends TestCase
+{
+
+    private static final String GREETING = "Hello Avalon!!!";
+    private AbstractApplicationContext ctx;
+
+    /** @see junit.framework.TestCase#setUp() */
+    protected void setUp() throws Exception {
+        super.setUp();
+        this.ctx = new 
FileSystemXmlApplicationContext("./src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml");
+    }
+
+    /** @see junit.framework.TestCase#tearDown() () */
+    protected void tearDown() throws Exception {
+        this.ctx.close();        
+        super.tearDown();
+    }
+
+    /**
+     * Constructor
+     *
+     * @param name the name of the test case
+     */
+    public AvalonIntoSpringTest( String name )
+    {
+        super(name);
+    }
+
+    /**
+     * Test the Avalon into Spring integration.
+     *
+     * @throws Exception the test failed
+     */
+    public void testAvalonIntoSpringIntegration() throws Exception
+    {
+        // ensure that the Avalon SystemPropertyService updated the system 
properties (so it was properly started)
+        assertTrue(System.getProperty("FOO").equals("BAR"));
+
+        CustomAvalonService customAvalonService;
+        CustomSpringService customSpringService;
+        ServiceManager serviceManager = (ServiceManager) 
ctx.getBean("avalonContainerBean");
+
+        // lookup and use the Spring bean using Spring's context
+        customSpringService = (CustomSpringService) 
ctx.getBean("customSpringService");
+        customSpringService.sayGretting();
+        assertEquals(customSpringService.getGreeting(), GREETING);
+
+        // lookup and use the Spring bean using Avalon's Service Manager
+        customSpringService = (CustomSpringService) 
serviceManager.lookup("customSpringService");
+        customSpringService.sayGretting();
+        assertEquals(customSpringService.getGreeting(), GREETING);
+
+        // lookup and use the Avalon service using Spring's context
+        customAvalonService = (CustomAvalonService) 
ctx.getBean("customAvalonService");
+        customAvalonService.sayGretting();
+        assertEquals(customAvalonService.getGreeting(), GREETING);
+
+        // lookup and use the Avalon service using Avalon's Service Manager
+        customAvalonService = (CustomAvalonService) 
serviceManager.lookup("customAvalonService");
+        customAvalonService.sayGretting();
+        assertEquals(customAvalonService.getGreeting(), GREETING);
+    }
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonService.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonService.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonService.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonService.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,26 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public interface CustomAvalonService
+{
+    void sayGretting();
+    String getGreeting();
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonServiceImpl.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonServiceImpl.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonServiceImpl.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomAvalonServiceImpl.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,63 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
+
+
+/**
+ * Copies the properties found in the configuration into the SystemProperties
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a>
+ */
+
+public class CustomAvalonServiceImpl
+    implements CustomAvalonService, Serviceable
+{
+    // a spring bean resolved by the Avalon ServiceManager */
+    private CustomSpringService customSpringService;
+    
+    /**
+     * Constructor
+     */
+    public CustomAvalonServiceImpl()
+    {
+        // nothing to do here
+    }
+
+    public void service(ServiceManager serviceManager) throws ServiceException
+    {
+        this.customSpringService = (CustomSpringService) 
serviceManager.lookup("customSpringService");
+    }
+
+    public void sayGretting()
+    {
+        // delegate the functionality to the Spring bean
+        this.customSpringService.sayGretting();
+    }
+
+    public String getGreeting()
+    {
+        // delegate the functionality to the Spring bean
+        return this.customSpringService.getGreeting();
+    }
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringService.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringService.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringService.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringService.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,26 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public interface CustomSpringService {
+
+    String getGreeting();
+    void sayGretting();
+}
\ No newline at end of file

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringServiceImpl.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringServiceImpl.java?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringServiceImpl.java
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/CustomSpringServiceImpl.java
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,63 @@
+package org.apache.fulcrum.yaafi.spring;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.fulcrum.yaafi.service.systemproperty.SystemPropertyService;
+import org.apache.avalon.framework.service.ServiceManager;
+
+/**
+ * A Spring bean using Avalon services.
+ */
+public class CustomSpringServiceImpl implements CustomSpringService {
+
+    /** message injected from the Spring configuration */
+    private String greeting;
+
+    /** directly injected as reference*/
+    private ServiceManager serviceManager;
+
+    /** injected using AvalonServiceFactoryBean (the service is instantiated 
by YAAFI) */
+    private SystemPropertyService systemPropertyService;
+
+    public CustomSpringServiceImpl() {
+    }
+
+    public String getGreeting() {
+        return greeting;
+    }
+
+    public void setGreeting(String greeting) {
+        this.greeting = greeting;
+    }
+
+    public void setServiceManager(ServiceManager serviceManager) {
+        this.serviceManager = serviceManager;
+    }
+
+    public void setSystemPropertyService(SystemPropertyService 
systemPropertyService) {
+        this.systemPropertyService = systemPropertyService;
+    }
+
+    public void sayGretting() {
+        System.out.println(this.greeting);
+        System.out.println(this.serviceManager.toString());
+        System.out.println(this.systemPropertyService.toString());
+    }
+}

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/avalonIntoSpringApplicationContext.xml
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd";>        
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.
+-->
+<beans>
+
+    <!-- Instantiate YAAFI using a container configuration file -->
+    <bean id="avalonContainerBean" 
class="org.apache.fulcrum.yaafi.spring.AvalonServiceContainerBean" 
init-method="initialize" destroy-method="dispose">
+        <property name="containerConfigValue" 
value="./src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml"/>
+    </bean>
+
+    <!-- Create service bean using a factory bean -->
+    <bean id="systemPropertyService" 
class="org.apache.fulcrum.yaafi.spring.AvalonServiceFactoryBean">
+        <property name="serviceManager" ref="avalonContainerBean"/>
+        <property name="serviceName" value="systemPropertyService"/>
+    </bean>
+
+    <!-- Create a Spring bean using Avalon services -->    
+    <bean id="customSpringService" 
class="org.apache.fulcrum.yaafi.spring.CustomSpringServiceImpl">
+        <property name="greeting" value="Hello Avalon!!!"/>
+        <property name="serviceManager" ref="avalonContainerBean"/>
+        <property name="systemPropertyService" ref="systemPropertyService"/>
+    </bean>
+
+    <!-- Create service bean using a factory bean -->
+    <bean id="customAvalonService" 
class="org.apache.fulcrum.yaafi.spring.AvalonServiceFactoryBean">
+        <property name="serviceManager" ref="avalonContainerBean"/>
+        <property name="serviceName" value="customAvalonService"/>
+    </bean>
+    
+</beans>

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.
+-->
+<componentConfig>
+  <systemPropertyService>
+    <property name="FOO">BAR</property>
+  </systemPropertyService>
+</componentConfig>

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.
+-->
+<!-- This configuration file for Avalon components is used for testing the 
TestComponent -->
+<role-list>
+    <role
+        name="systemPropertyService"
+        shorthand="systemPropertyService"
+        
default-class="org.apache.fulcrum.yaafi.service.systemproperty.SystemPropertyServiceImpl"
+    />
+    <!--
+      we have to rely on lazy init otherwise the service lookups between the to
+      service frameworks fails (Yaafi tries to resolve the Sprin Bean, whereas 
Spring
+      tries to resolve the Avalon service at the same time)
+    -->
+    <role
+        name="customAvalonService"
+        shorthand="CustomAvalonService"
+        default-class="org.apache.fulcrum.yaafi.spring.CustomAvalonServiceImpl"
+        early-init="false"
+    />
+
+</role-list>

Added: 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml?rev=673094&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml
 (added)
+++ 
turbine/fulcrum/trunk/yaafi/src/test/org/apache/fulcrum/yaafi/spring/containerConfiguration.xml
 Tue Jul  1 08:08:55 2008
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.
+-->
+<fulcrum-yaafi>
+  <componentRoles>
+    
<location>./src/test/org/apache/fulcrum/yaafi/spring/componentRoles.xml</location>
+  </componentRoles>
+  <componentConfiguration>
+    
<location>./src/test/org/apache/fulcrum/yaafi/spring/componentConfiguration.xml</location>
+  </componentConfiguration>
+</fulcrum-yaafi>


Reply via email to