View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.xbean.recipe;
19  
20  import java.util.Collections;
21  import java.util.List;
22  import java.lang.reflect.Type;
23  
24  import org.apache.xbean.recipe.AbstractRecipe;
25  import org.apache.xbean.recipe.ConstructionException;
26  import org.apache.xbean.recipe.ExecutionContext;
27  import org.apache.xbean.recipe.NoSuchObjectException;
28  import org.apache.xbean.recipe.Recipe;
29  import org.apache.xbean.recipe.RecipeHelper;
30  
31  /*
32   * The ReferenceNameRecipe is used to inject the reference name into the object (as a String).
33   * The ReferenceNameRecipe ensures the actual reference object exists before the reference name is injected. 
34   */
35  public class ReferenceNameRecipe extends AbstractRecipe {
36      
37      private String referenceName;
38  
39      public ReferenceNameRecipe(String referenceName) {
40          this.referenceName = referenceName;
41      }
42  
43      public String getReferenceName() {
44          return referenceName;
45      }
46  
47      private Object getReference() {
48          if (referenceName == null) {
49              throw new ConstructionException("Reference name has not been set");
50          }
51          ExecutionContext context = ExecutionContext.getContext();
52          if (!context.containsObject(referenceName)) {
53              throw new NoSuchObjectException(referenceName);
54          }
55          return context.getObject(referenceName);
56      }
57      
58      public List<Recipe> getNestedRecipes() {
59          Object object = getReference();
60          if (object instanceof Recipe) {
61              Recipe recipe = (Recipe) object;
62              return Collections.singletonList(recipe);
63          } else {
64              return Collections.emptyList();
65          }
66      }
67  
68      public List<Recipe> getConstructorRecipes() {
69          return getNestedRecipes();
70      }
71      
72      public boolean canCreate(Type type) {
73          Object object = getReference();
74          return String.class == RecipeHelper.toClass(type);
75      }
76  
77      protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
78          Object object = getReference();
79          return referenceName;
80      }
81      
82  }