View Javadoc

1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.osgi;
23  
24  import java.util.Map;
25  
26  import com.opensymphony.xwork2.ObjectFactory;
27  import com.opensymphony.xwork2.config.PackageProvider;
28  import com.opensymphony.xwork2.inject.Container;
29  import com.opensymphony.xwork2.inject.Inject;
30  import org.apache.struts2.util.ObjectFactoryDestroyable;
31  
32  public class DelegatingObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
33      private ObjectFactory delegateObjectFactory;
34      private BundleAccessor bundleResourceLoader;
35      private OsgiConfigurationProvider osgiConfigurationProvider;
36  
37      @Inject
38      public void setDelegateObjectFactory(@Inject Container container,
39                                           @Inject("struts.objectFactory.delegate") String delegate) {
40          if (delegate == null) {
41              delegate = "struts";
42          }
43          delegateObjectFactory = container.getInstance(ObjectFactory.class, delegate);
44      }
45  
46      @Inject
47      public void setBundleResourceLoader(BundleAccessor rl) {
48          this.bundleResourceLoader = rl;
49      }
50  
51  
52      public boolean isNoArgConstructorRequired() {
53          return delegateObjectFactory.isNoArgConstructorRequired();
54      }
55  
56      public Object buildBean(Class clazz, Map extraContext) throws Exception {
57          return delegateObjectFactory.buildBean(clazz, extraContext);
58      }
59  
60      public Object buildBean(String className, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
61          try {
62              return delegateObjectFactory.buildBean(className, extraContext, injectInternal);
63          } catch (Exception e) {
64              Object object = bundleResourceLoader.loadClass(className).newInstance();
65              if (injectInternal)
66                  injectInternalBeans(object);
67              return object;
68          }
69      }
70  
71      @Override
72      public Class getClassInstance(String className) throws ClassNotFoundException {
73          try {
74              return delegateObjectFactory.getClassInstance(className);
75          }
76          catch (Exception e) {
77              return bundleResourceLoader.loadClass(className);
78          }
79      }
80  
81      public void destroy() {
82          if (osgiConfigurationProvider != null) {
83              osgiConfigurationProvider.destroy();
84          }
85      }
86  
87      @Inject("osgi")
88      public void setOsgiConfigurationProvider(PackageProvider osgiConfigurationProvider) {
89          this.osgiConfigurationProvider = (OsgiConfigurationProvider) osgiConfigurationProvider;
90      }
91  }