Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed 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.
 
 package org.apache.tapestry.bean;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.ClassResolver;
Basic implementation of the IBeanProvider interface.

Author(s):
Howard Lewis Ship
Since:
1.0.4
 
 
 {
     private static final Log LOG = LogFactory.getLog(BeanProvider.class);

    
Indicates whether this instance has been registered with its page as a PageDetachListener. Registration only occurs the first time a bean with lifecycle REQUEST is instantiated.
 
 
     private boolean _registeredForDetach = false;

    
Indicates whether this instance has been registered as a render listener with the page.
 
 
     private boolean _registeredForRender = false;

    
The component for which beans are being created and tracked.
 
 
     private final IComponent _component;

    
Used for instantiating classes.
 
 
     private final ClassResolver _resolver;

    
Used for resolving partial class names.
 
 
     private final ClassFinder _classFinder;
 
     private final String _packageList;

    
Map of beans, keyed on name.
 
 
     private Map _beans;

    
Set of bean names provided by this provider.

Since:
2.2
 
 
     private Set _beanNames;
 
    public BeanProvider(IComponent component)
    {
         = component;
        Infrastructure infrastructure = component.getPage().getRequestCycle().getInfrastructure();
         = infrastructure.getClassResolver();
        INamespace namespace = component.getNamespace();
         = namespace.getPropertyValue("org.apache.tapestry.bean-class-packages");
         = infrastructure.getClassFinder();
    }

    

Since:
1.0.6 *
    public Collection getBeanNames()
    {
        if ( == null)
        {
            Collection c = .getSpecification().getBeanNames();
            if (c == null || c.isEmpty())
                 = .;
            else
                 = Collections.unmodifiableSet(new HashSet(c));
        }
        return ;
    }

    

Since:
1.0.5
    public IComponent getComponent()
    {
        return ;
    }
    public Object getBean(String name)
    {
        if (.isDebugEnabled())
            .debug("getBean(" + name + ")");
        Object bean = null;
        if ( != null)
            bean = .get(name);
        if (bean != null)
            return bean;
        if (spec == null)
            throw new ApplicationRuntimeException(BeanMessages.beanNotDefined(name));
        bean = instantiateBean(namespec);
        BeanLifecycle lifecycle = spec.getLifecycle();
        if (lifecycle == .)
            return bean;
        if ( == null)
             = new HashMap();
        .put(namebean);
        // The first time in a request that a REQUEST lifecycle bean is created,
        // register with the page to be notified at the end of the
        // request cycle.
        if (lifecycle == . && !)
        {
            .getPage().addPageDetachListener(this);
             = true;
        }
        if (lifecycle == . && !)
        {
            .getPage().addPageEndRenderListener(this);
             = true;
        }
        // No need to register if a PAGE lifecycle bean; those can stick around
        // forever.
        return bean;
    }
    {
        String className = spec.getClassName();
        Object bean = null;
        if (.isDebugEnabled())
            .debug("Instantiating instance of " + className);
        Class beanClass = .findClass(className);
        if (beanClass == null)
            throw new ApplicationRuntimeException(BeanMessages.missingBeanClass(
                    ,
                    beanName,
                    className,
                    ), spec.getLocation(), null);
        // Do it the hard way!
        try
        {
            bean = beanClass.newInstance();
        }
        catch (Exception ex)
        {
            throw new ApplicationRuntimeException(BeanMessages.instantiationError(
                    beanName,
                    ,
                    beanClass,
                    ex), spec.getLocation(), ex);
        }
        // OK, have the bean, have to initialize it.
        List initializers = spec.getInitializers();
        if (initializers == null)
            return bean;
        Iterator i = initializers.iterator();
        while (i.hasNext())
        {
            IBeanInitializer iz = (IBeanInitializeri.next();
            if (.isDebugEnabled())
                .debug("Initializing property " + iz.getPropertyName());
            try
            {
                iz.setBeanProperty(thisbean);
            }
            catch (Exception ex)
            {
                throw new ApplicationRuntimeException(BeanMessages.initializationError(
                        ,
                        beanName,
                        iz.getPropertyName(),
                        ex), beaniz.getLocation(), ex);
            }
        }
        return bean;
    }

    
Removes all beans with the REQUEST lifecycle. Beans with the PAGE lifecycle stick around, and beans with no lifecycle were never stored in the first place.
    public void pageDetached(PageEvent event)
    {
    }

    
Removes any beans with the specified lifecycle.

Since:
2.2
    private void removeBeans(BeanLifecycle lifecycle)
    {
        if ( == null)
            return;
        IComponentSpecification spec = null;
        Iterator i = .entrySet().iterator();
        while (i.hasNext())
        {
            Map.Entry e = (Map.Entryi.next();
            String name = (Stringe.getKey();
            if (spec == null)
                spec = .getSpecification();
            IBeanSpecification s = spec.getBeanSpecification(name);
            if (s.getLifecycle() == lifecycle)
            {
                Object bean = e.getValue();
                if (.isDebugEnabled())
                    .debug("Removing " + lifecycle.getName() + " bean " + name + ": " + bean);
                i.remove();
            }
        }
    }

    

Since:
1.0.8 *
    public ClassResolver getClassResolver()
    {
        return ;
    }

    

Since:
2.2 *
    public void pageEndRender(PageEvent event)
    {
    }

    

Since:
2.2 *
    public boolean canProvideBean(String name)
    {
        return getBeanNames().contains(name);
    }
New to GrepCode? Check out our FAQ X