alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (OsgiInterceptor.java)

This example Struts source code file (OsgiInterceptor.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

abstractinterceptor, bundlecontext, bundlecontext, bundlecontextaware, class, class, log, object, object, osgi, parameterizedtype, parameterizedtype, reflection, servicereference, servlet, type, util

The Struts OsgiInterceptor.java source code

/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts2.osgi.interceptor;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import com.opensymphony.xwork2.inject.Inject;

import javax.servlet.ServletContext;

import org.apache.struts2.osgi.OsgiHost;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.ArrayList;

/**
 * If a class implements BundleContextAware, this interceptor will call the setBundleContext(BundleContext)
 * method on it. If a class implements ServiceAware<T>, this interceptor will call setService(List)
 */
public class OsgiInterceptor extends AbstractInterceptor {
    private static final Logger LOG = LoggerFactory.getLogger(OsgiInterceptor.class);

    private BundleContext bundleContext;

    public String intercept(ActionInvocation invocation) throws Exception {
        if (bundleContext != null) {
            Object action = invocation.getAction();

            //inject BundleContext
            if (action instanceof BundleContextAware)
                ((BundleContextAware)action).setBundleContext(bundleContext);

            //inject service implementations
            if (action instanceof ServiceAware) {
                Type[] types = action.getClass().getGenericInterfaces();
                if (types != null) {
                    for (Type type : types) {
                        if (type instanceof ParameterizedType) {
                            ParameterizedType parameterizedType = (ParameterizedType) type;
                            if (parameterizedType.getRawType() instanceof Class) {
                                Class clazz = (Class) parameterizedType.getRawType();
                                if (ServiceAware.class.equals(clazz)) {
                                    Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0];
                                    ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null);
                                    //get the services
                                    if (refs != null) {
                                        List services = new ArrayList(refs.length);
                                        for (ServiceReference ref : refs) {
                                            Object service = bundleContext.getService(ref);
                                            //wow, that's a lot of nested ifs
                                            if (service != null)
                                                services.add(service);
                                        }

                                        if (!services.isEmpty())
                                            ((ServiceAware)action).setServices(services);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (LOG.isWarnEnabled()){
            LOG.warn("The OSGi interceptor was not able to find the BundleContext in the ServletContext");          
        }

        return invocation.invoke();
    }

    @Inject
    public void setServletContext(ServletContext servletContext) {
        this.bundleContext = (BundleContext) servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT);
    }
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts OsgiInterceptor.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.