View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 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.cms.util.ContentUtil;
48  import info.magnolia.content2bean.Content2BeanException;
49  import info.magnolia.content2bean.Content2BeanUtil;
50  import info.magnolia.context.SystemContext;
51  import info.magnolia.module.blossom.content2bean.SpringContent2BeanTransformer;
52  import info.magnolia.module.blossom.support.BeanFactoryUtils;
53  import info.magnolia.objectfactory.Components;
54  import info.magnolia.repository.RepositoryConstants;
55  
56  /**
57   * BeanFactory that creates a bean and populates its properties with values from the repository.
58   *
59   * @since 1.2
60   */
61  public class ConfiguredBeanFactoryBean implements FactoryBean, InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
62  
63      private static final String DEFAULT_WORKSPACE = RepositoryConstants.CONFIG;
64  
65      private AbstractAutowireCapableBeanFactory beanFactory;
66      private String beanName;
67      private boolean initialized = false;
68      private Class<?> targetClass;
69      private Object instance;
70      private Class<?> defaultClass;
71  
72      private String workspace = DEFAULT_WORKSPACE;
73      private String path;
74  
75      @Deprecated
76      public String getRepository() {
77          return workspace;
78      }
79  
80      @Deprecated
81      public void setRepository(String repository) {
82          this.workspace = repository;
83      }
84  
85      public String getWorkspace() {
86          return workspace;
87      }
88  
89      public void setWorkspace(String workspace) {
90          this.workspace = workspace;
91      }
92  
93      public String getPath() {
94          return path;
95      }
96  
97      public void setPath(String path) {
98          this.path = path;
99      }
100 
101     public Class<?> getDefaultClass() {
102         return defaultClass;
103     }
104 
105     public void setDefaultClass(Class<?> defaultClass) {
106         this.defaultClass = defaultClass;
107     }
108 
109     @Override
110     public Object getObject() throws Exception {
111         if (!initialized) {
112             initialized = true;
113             instance = createInstance();
114         }
115         return instance;
116     }
117 
118     @Override
119     public Class getObjectType() {
120         if (instance != null) {
121             return instance.getClass();
122         }
123         if (targetClass != null) {
124             return targetClass;
125         }
126         return defaultClass;
127     }
128 
129     @Override
130     public boolean isSingleton() {
131         return true;
132     }
133 
134     @Override
135     public void setBeanName(String name) {
136         this.beanName = name;
137     }
138 
139     @Override
140     public void setBeanFactory(BeanFactory beanFactory) {
141         this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory;
142     }
143 
144     @Override
145     public void destroy() throws Exception {
146         destroyInstance();
147     }
148 
149     @Override
150     public void afterPropertiesSet() throws Exception {
151         Node node = getConfigurationNode();
152         if (node.hasProperty("class")) {
153             targetClass = Class.forName(node.getProperty("class").getString());
154         }
155     }
156 
157     protected Object createInstance() throws Exception {
158         Node node = getConfigurationNode();
159         return transformNode(node);
160     }
161 
162     protected void destroyInstance() {
163         if (instance != null) {
164             BeanFactoryUtils.destroyBean(instance, beanName, beanFactory);
165         }
166     }
167 
168     protected Object transformNode(Node node) throws Content2BeanException {
169         SpringContent2BeanTransformer transformer = new SpringContent2BeanTransformer(beanFactory);
170         transformer.setTopLevelBeanName(beanName);
171         transformer.setDefaultClass(defaultClass);
172         return Content2BeanUtil.toBean(ContentUtil.asContent(node), true, transformer);
173     }
174 
175     protected Node getConfigurationNode() throws RepositoryException {
176         return Components.getComponent(SystemContext.class).getJCRSession(workspace).getNode(path);
177     }
178 }