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.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 }