1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.osgi;
23
24 import java.io.IOException;
25 import java.net.URL;
26 import java.util.*;
27
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30
31 import com.opensymphony.xwork2.ObjectFactory;
32 import com.opensymphony.xwork2.ActionContext;
33 import com.opensymphony.xwork2.inject.Inject;
34 import com.opensymphony.xwork2.config.Configuration;
35 import com.opensymphony.xwork2.config.ConfigurationException;
36 import com.opensymphony.xwork2.config.PackageProvider;
37 import com.opensymphony.xwork2.config.entities.PackageConfig;
38 import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
39 import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
40 import com.opensymphony.xwork2.util.location.Location;
41 import com.opensymphony.xwork2.util.logging.Logger;
42 import com.opensymphony.xwork2.util.logging.LoggerFactory;
43
44 /***
45 * Package loader implementation that loads resources from a bundle
46 */
47 public class BundlePackageLoader implements PackageLoader {
48 private static final Logger LOG = LoggerFactory.getLogger(BundlePackageLoader.class);
49
50 public List<PackageConfig> loadPackages(Bundle bundle, BundleContext bundleContext, ObjectFactory objectFactory, Map<String, PackageConfig> pkgConfigs) throws ConfigurationException {
51 Configuration config = new DefaultConfiguration("struts.xml");
52 BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
53 for (PackageConfig pkg : pkgConfigs.values()) {
54 config.addPackageConfig(pkg.getName(), pkg);
55 }
56 prov.setObjectFactory(objectFactory);
57 prov.init(config);
58 prov.loadPackages();
59
60 List<PackageConfig> list = new ArrayList<PackageConfig>(config.getPackageConfigs().values());
61 list.removeAll(pkgConfigs.values());
62
63 return list;
64 }
65
66 static class BundleConfigurationProvider extends XmlConfigurationProvider {
67 private Bundle bundle;
68 private BundleContext bundleContext;
69
70 public BundleConfigurationProvider(String filename, Bundle bundle, BundleContext bundleContext) {
71 super(filename, false);
72 this.bundle = bundle;
73 this.bundleContext = bundleContext;
74 }
75
76 public BundleConfigurationProvider(String filename) {
77 super(filename);
78 }
79
80 @Override
81 protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
82 Enumeration<URL> e = bundle.getResources("struts.xml");
83 return e.hasMoreElements() ? new EnumeratorIterator<URL>(e) : null;
84 }
85 }
86
87 static class EnumeratorIterator<E> implements Iterator<E> {
88 Enumeration<E> e = null;
89
90 public EnumeratorIterator(Enumeration<E> e) {
91 this.e = e;
92 }
93
94 public boolean hasNext() {
95 return e.hasMoreElements();
96 }
97
98 public E next() {
99 return e.nextElement();
100 }
101
102 public void remove() {
103 throw new UnsupportedOperationException();
104 }
105 }
106
107 }