1. Project Clover database Fri Nov 20 2015 15:10:00 CET
  2. Package info.magnolia.module.blossom.context

File ConfiguredBeanFactoryBean.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
22
8
1
177
113
13
0.59
2.75
8
1.62
35.5% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ConfiguredBeanFactoryBean 60 22 35.5% 13 2
0.9595%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.blossom.context;
35   
36    import javax.jcr.Node;
37    import javax.jcr.RepositoryException;
38   
39    import org.springframework.beans.factory.BeanFactory;
40    import org.springframework.beans.factory.BeanFactoryAware;
41    import org.springframework.beans.factory.BeanNameAware;
42    import org.springframework.beans.factory.DisposableBean;
43    import org.springframework.beans.factory.FactoryBean;
44    import org.springframework.beans.factory.InitializingBean;
45    import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
46   
47    import info.magnolia.context.SystemContext;
48    import info.magnolia.jcr.node2bean.Node2BeanException;
49    import info.magnolia.jcr.node2bean.Node2BeanProcessor;
50    import info.magnolia.module.blossom.node2bean.SpringNode2BeanTransformer;
51    import info.magnolia.module.blossom.support.BeanFactoryUtils;
52    import info.magnolia.objectfactory.Components;
53    import info.magnolia.repository.RepositoryConstants;
54   
55    /**
56    * BeanFactory that creates a bean and populates its properties with values from the repository.
57    *
58    * @since 1.2
59    */
 
60    public class ConfiguredBeanFactoryBean implements FactoryBean, InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
61   
62    private static final String DEFAULT_WORKSPACE = RepositoryConstants.CONFIG;
63   
64    private AbstractAutowireCapableBeanFactory beanFactory;
65    private String beanName;
66    private boolean initialized = false;
67    private Class<?> targetClass;
68    private Object instance;
69    private Class<?> defaultClass;
70   
71    private String workspace = DEFAULT_WORKSPACE;
72    private String path;
73   
 
74    toggle @Deprecated
75    public String getRepository() {
76    return workspace;
77    }
78   
 
79    toggle @Deprecated
80    public void setRepository(String repository) {
81    this.workspace = repository;
82    }
83   
 
84    toggle public String getWorkspace() {
85    return workspace;
86    }
87   
 
88    toggle public void setWorkspace(String workspace) {
89    this.workspace = workspace;
90    }
91   
 
92    toggle public String getPath() {
93    return path;
94    }
95   
 
96    toggle public void setPath(String path) {
97    this.path = path;
98    }
99   
 
100    toggle public Class<?> getDefaultClass() {
101    return defaultClass;
102    }
103   
 
104    toggle public void setDefaultClass(Class<?> defaultClass) {
105    this.defaultClass = defaultClass;
106    }
107   
 
108  2 toggle @Override
109    public Object getObject() throws Exception {
110  2 if (!initialized) {
111  2 initialized = true;
112  2 instance = createInstance();
113    }
114  2 return instance;
115    }
116   
 
117  5 toggle @Override
118    public Class getObjectType() {
119  5 if (instance != null) {
120  1 return instance.getClass();
121    }
122  4 if (targetClass != null) {
123  2 return targetClass;
124    }
125  2 return defaultClass;
126    }
127   
 
128    toggle @Override
129    public boolean isSingleton() {
130    return true;
131    }
132   
 
133    toggle @Override
134    public void setBeanName(String name) {
135    this.beanName = name;
136    }
137   
 
138    toggle @Override
139    public void setBeanFactory(BeanFactory beanFactory) {
140    this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory;
141    }
142   
 
143  1 toggle @Override
144    public void destroy() throws Exception {
145  1 destroyInstance();
146    }
147   
 
148  2 toggle @Override
149    public void afterPropertiesSet() throws Exception {
150  2 Node node = getConfigurationNode();
151  2 if (node.hasProperty("class")) {
152  1 targetClass = Class.forName(node.getProperty("class").getString());
153    }
154    }
155   
 
156  2 toggle protected Object createInstance() throws Exception {
157  2 Node node = getConfigurationNode();
158  2 return transformNode(node);
159    }
160   
 
161  1 toggle protected void destroyInstance() {
162  1 if (instance != null) {
163  1 BeanFactoryUtils.destroyBean(instance, beanName, beanFactory);
164    }
165    }
166   
 
167  2 toggle protected Object transformNode(Node node) throws Node2BeanException, RepositoryException {
168  2 SpringNode2BeanTransformer transformer = new SpringNode2BeanTransformer(beanFactory);
169  2 transformer.setTopLevelBeanName(beanName);
170  2 transformer.setDefaultClass(defaultClass);
171  2 return Components.getComponent(Node2BeanProcessor.class).toBean(node, true, transformer, Components.getComponentProvider());
172    }
173   
 
174  4 toggle protected Node getConfigurationNode() throws RepositoryException {
175  4 return Components.getComponent(SystemContext.class).getJCRSession(workspace).getNode(path);
176    }
177    }