View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   *****************************************************************************/
17  
18  package org.settings4j.objectresolver;
19  
20  import java.util.Properties;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.settings4j.ContentResolver;
24  import org.settings4j.helper.spring.ByteArrayXMLApplicationContext;
25  
26  /**
27   * This implementation parses a Spring-Beans XML File and returns the Object from the generated Spring Application
28   * Context.
29   * <p>
30   * Per Default the bean with id = key.replace('/', '.') will be returned. But you can configure a key "bean-ref" in the
31   * {@link Properties}-File for the given Object.
32   * <p>
33   * Example: The following code should return a {@link javax.sql.DataSource} Object:<br/>
34   * <code>
35   * Settings4j.getObject("com/myCompany/myApp/MyDatasource");
36   * </code>
37   * <p>
38   * In normal Cases the DataSource comes from the JNDI-Context (available in most Servlet Containers).<br/>
39   * But in some environments there are no JNDI-Context (Commandline-Clients, UnitTests).<br/>
40   * <p>
41   * With Settings4j (default configuration) you can also place two Files into your Classpath:
42   * <ol>
43   * <li><code>"com/myCompany/myApp/MyDatasource"</code>: The File which defines the DataSource
44   * <li><code>"com/myCompany/myApp/MyDatasource.properties"</code>: Some Properties, like which ObjectResolver should be
45   * use.
46   * </ol>
47   * <p>
48   * The File Content whould be the following:
49   * 
50   * <ul>
51   * <li><b>objectResolverKey:</b> A unique identifier for the Object Resolver Implementation.
52   * <li><b>bean-ref:</b> The bean name/id from the Spring-Context.
53   * </ul>
54   * <pre>
55   * Classpath File "com/myCompany/myApp/MyDatasource.properties":
56   * <div style="border-width:1px;border-style:solid;">
57   * objectResolverKey=org.settings4j.objectresolver.SpringConfigObjectResolver
58   * bean-ref=<b>SpringBeanReferneceId</b>
59   * </div>
60   * </pre>
61   * <p>
62   * <pre>
63   * Classpath File "com/myCompany/myApp/MyDatasource":
64   * <div style="border-width:1px;border-style:solid;">
65   * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
66   * &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
67   * &lt;beans&gt;
68   *   &lt;!-- HSQLDB --&gt;
69   *   &lt;bean id="<b>SpringBeanReferneceId</b>" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
70   *     &lt;property name="driverClassName"&gt;&lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;&lt;/property&gt;
71   *     &lt;property name="url"&gt;&lt;value&gt;jdbc:hsqldb:mem:test&lt;/value&gt;&lt;/property&gt;
72   *     &lt;property name="username"&gt;&lt;value&gt;sa&lt;/value&gt;&lt;/property&gt;
73   *     &lt;property name="password"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;
74   *   &lt;/bean&gt;
75   * &lt;/beans&gt;
76   * </div>
77   * </pre>
78   * <p>
79   * 
80   * @author Harald.Brabenetz
81   */
82  public class SpringConfigObjectResolver extends AbstractObjectResolver {
83  
84      /** General Logger for this Class. */
85      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SpringConfigObjectResolver.class);
86  
87      private static final String PROPERTY_BEAN_REF = "bean-ref";
88  
89      /** {@inheritDoc} */
90      @Override
91      protected Object contentToObject(final String key, final Properties properties, final byte[] content,
92              final ContentResolver contentResolver) {
93  
94          String beanRef = properties.getProperty(PROPERTY_BEAN_REF);
95          if (StringUtils.isEmpty(beanRef)) {
96              beanRef = key.replace('/', '.');
97          }
98  
99          final Object result = ByteArrayXMLApplicationContext.getBean(content, beanRef);
100         if (result == null) {
101             LOG.warn("SpringContext-Object with ID '{}' for Key '{}' is null!", beanRef, key);
102         }
103         return result;
104     }
105 
106 }