Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * *********************************************************************************************************************
   *
   * NorthernWind - lightweight CMS
   * http://northernwind.tidalwave.it - hg clone https://bitbucket.org/tidalwave/northernwind-src
   * %%
   * Copyright (C) 2011 - 2014 Tidalwave s.a.s. (http://tidalwave.it)
   * %%
  * *********************************************************************************************************************
  *
  * 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.
  *
  * *********************************************************************************************************************
  *
  * $Id: ViewBuilder.java,v f214f4c8488f 2014/01/03 18:03:50 fabrizio $
  *
  * *********************************************************************************************************************
  * #L%
  */
 package it.tidalwave.northernwind.frontend.impl.ui;
 
 import  javax.annotation.Nonnull;
 import java.util.List;
 import  it.tidalwave.util.Id;
 import  it.tidalwave.util.NotFoundException;
A builder which creates a View - ViewController pair.

Author(s):
Fabrizio Giudici
Version:
$Id: ViewBuilder.java,v f214f4c8488f 2014/01/03 18:03:50 fabrizio $ /
Stereotype:
Factory
 
 @Configurable @Slf4j @ToString(exclude = "beanFactory")
 /* package */ class ViewBuilder
   {
     @Inject @Nonnull
     private BeanFactory beanFactory;
 
     @Nonnull
     /* package */ final Constructor<?> viewConstructor;
 
     @Nonnull
     /* package */ final Constructor<?> viewControllerConstructor;

    
/
 
     public ViewBuilder (final @Nonnull Class<?> viewClassfinal @Nonnull Class<?> viewControllerClass)
       {
          = viewClass.getConstructors()[0];
          = viewControllerClass.getConstructors()[0];
       }

    
Creates a new View - ViewController pair. They are first instantiated, and then dependency injection by means of constructor parameters occur. Injected fields are: the id, the SiteNode, any service declared in the Spring context, including Site; furthermore, a reference of the View is injected in the Controller.

Parameters:
id the view id
siteNode the SiteNode the view will be built for
Returns:
the created view /
 
     @Nonnull
     public ViewAndController createViewAndController (final @Nonnull Id idfinal @Nonnull SiteNode siteNode)
       throws HttpStatusException
       {
         log.debug("createViewAndController({}, {})"idsiteNode);
 
        try
          {
            final Object view = .newInstance(computeConstructorArguments(idsiteNode));
            final Object controller = .newInstance(
                    computeConstructorArguments(idsiteNodeview));
            return new ViewAndController(viewcontroller);
          }
        catch (InvocationTargetException e)
          {
            if (e.getCause() instanceof BeanCreationException// FIXME: cumbersome
              {
                if (e.getCause().getCause() instanceof HttpStatusException)
                  {
                    throw (HttpStatusException)e.getCause().getCause();
                  }
              }
            throw new RuntimeException(e);
          }
        catch (Exception e)
          {
            throw new RuntimeException(e);
          }
      }

    
Computes the argument values for calling the given constructor. They are taken from the current BeanFactory, with instanceArgs eventually overriding them.

Parameters:
constructor the constructor
overridingArgs the overriding arguments
Returns:
the arguments to pass to the constructor /
    @Nonnull
    private Object[] computeConstructorArguments (final @Nonnull Constructor<?> constructor,
                                                  final @Nonnull Object ... overridingArgs)
      throws NotFoundException
      {
        final List<Objectresult = new ArrayList<>();
        x: for (final Class<?> argumentType : constructor.getParameterTypes())
          {
            for (final Object overridingArg : overridingArgs)
              {
                if (argumentType.isAssignableFrom(overridingArg.getClass()))
                  {
                    result.add(overridingArg);
                    continue x;
                  }
              }
            if (Site.class.isAssignableFrom(argumentType))
              {
                result.add(.getBean(SiteProvider.class).getSite());
              }
            else
              {
                result.add(.getBean(argumentType));
              }
          }
        return result.toArray();
      }
  }
New to GrepCode? Check out our FAQ X