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.helper.spring;
19  
20  import org.springframework.context.support.AbstractApplicationContext;
21  import org.springframework.context.support.AbstractXmlApplicationContext;
22  import org.springframework.core.io.ByteArrayResource;
23  import org.springframework.core.io.Resource;
24  
25  /**
26   * A SpringFramework Application Context which can process a spring-XML Content from byte[] as input.
27   * 
28   * @author Harald.Brabenetz
29   */
30  public class ByteArrayXMLApplicationContext extends AbstractXmlApplicationContext {
31  
32      private final Resource[] configResources;
33  
34      /**
35       * Static method to get a single Bean from a Spring Configuration XML.
36       * 
37       * @param content The Spring configuration XML as byte[].
38       * @param beanName The bean name/identifier.
39       * @return The Bean-Object or <code>null</code>.
40       */
41      public static Object getBean(final byte[] content, final String beanName) {
42  
43          final AbstractApplicationContext context = new ByteArrayXMLApplicationContext(content);
44          context.refresh();
45  
46          final Object result = context.getBean(beanName);
47          context.close();
48          return result;
49      }
50  
51      /**
52       * @param content The Spring configuration XML as byte[].
53       */
54      public ByteArrayXMLApplicationContext(final byte[] content) {
55          super();
56          configResources = new Resource[1];
57          configResources[0] = new ByteArrayResource(content);
58      }
59  
60      @Override
61      protected Resource[] getConfigResources() {
62          return configResources;
63      }
64  }