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.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.net.URL;
27  import java.net.MalformedURLException;
28  
29  import org.osgi.framework.BundleContext;
30  import org.osgi.framework.InvalidSyntaxException;
31  import org.osgi.framework.ServiceReference;
32  import org.osgi.framework.Bundle;
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  
36  public class OsgiUtil {
37      private static final Logger LOG = LoggerFactory.getLogger(OsgiUtil.class);
38  
39      /***
40       * A bundle is a jar, and a bunble URL will be useless to clients, this method translates
41       * a URL to a resource inside a bundle from "bundle:something/path" to "jar:file:bundlelocation!/path"
42       */
43      public static URL translateBundleURLToJarURL(URL bundleUrl, Bundle bundle) throws MalformedURLException {
44          if (bundleUrl != null && "bundle".equalsIgnoreCase(bundleUrl.getProtocol())) {
45              StringBuilder sb = new StringBuilder("jar:");
46              sb.append(bundle.getLocation());
47              sb.append("!");
48              sb.append(bundleUrl.getFile());
49              return new URL(sb.toString());
50          }
51  
52          return bundleUrl;
53      }
54  
55      /***
56       * Calls getBean() on the passed object using refelection. Used on Spring context
57       * because they are loaded from bundles (in anothe class loader)
58       */
59      public static Object getBean(Object beanFactory, String beanId) {
60          try {
61              Method getBeanMethod = beanFactory.getClass().getMethod("getBean", String.class);
62              return getBeanMethod.invoke(beanFactory, beanId);
63          } catch (Exception ex) {
64              if (LOG.isErrorEnabled())
65                  LOG.error("Unable to call getBean() on object of type [#0], with bean id [#1]", ex, beanFactory.getClass().getName(), beanId);
66          }
67  
68          return null;
69      }
70  
71      /***
72       * Calls containsBean on the passed object using refelection. Used on Spring context
73       * because they are loaded from bundles (in anothe class loader)
74       */
75      public static boolean containsBean(Object beanFactory, String beanId) {
76          try {
77              Method getBeanMethod = beanFactory.getClass().getMethod("containsBean", String.class);
78              return (Boolean) getBeanMethod.invoke(beanFactory, beanId);
79          } catch (Exception ex) {
80              if (LOG.isErrorEnabled())
81                  LOG.error("Unable to call containsBean() on object of type [#0], with bean id [#1]", ex, beanFactory.getClass().getName(), beanId);
82          }
83  
84          return false;
85      }
86  }