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

Axis 2 example source code file (ResourceInjectionServiceRuntimeDescriptionBuilder.java)

This example Axis 2 source code file (ResourceInjectionServiceRuntimeDescriptionBuilder.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 - Axis 2 tags/keywords

annotation, arraylist, class, class, field, field, list, list, method, method, object, privilegedaction, reflection, resource, resourceinjectionserviceruntimedescriptionbuilder, resourceinjectionserviceruntimedescriptionimpl, security, util

The Axis 2 ResourceInjectionServiceRuntimeDescriptionBuilder.java source code

/*
 * 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.axis2.jaxws.runtime.description.injection.impl;

import org.apache.axis2.java.security.AccessController;
import org.apache.axis2.jaxws.description.ServiceDescription;
import org.apache.axis2.jaxws.runtime.description.injection.ResourceInjectionServiceRuntimeDescription;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

public class ResourceInjectionServiceRuntimeDescriptionBuilder {

    /** Intentionally Private */
    private ResourceInjectionServiceRuntimeDescriptionBuilder() {
    }

    /**
     * create
     *
     * @param opDesc
     * @param implClassName
     * @return
     */
    static public ResourceInjectionServiceRuntimeDescription create(ServiceDescription serviceDesc,
                                                                    Class implClass) {
        ResourceInjectionServiceRuntimeDescriptionImpl desc =
                new ResourceInjectionServiceRuntimeDescriptionImpl(getKey(implClass), serviceDesc);

        boolean value = hasResourceAnnotation(implClass);
        desc.setResourceAnnotation(value);

        Method method = getPostConstructMethod(implClass);
        desc.setPostConstructMethod(method);

        method = getPreDestroyMethod(implClass);
        desc.setPreDestroyMethod(method);

        return desc;
    }

    static public String getKey(Class implClass) {
        return "Resource Injection:" + implClass.getCanonicalName();
    }

    /**
     * @param implClass
     * @return true if Field or Method has a @Resource annotation
     */
    static private boolean hasResourceAnnotation(Class implClass) {
        // Getting this information is expensive, but fortunately is cached.
        List<Field> fields = getFields(implClass);
        for (Field field : fields) {
            if (field.getAnnotation(Resource.class) != null) {
                return true;
            }
        }
        List<Method> methods = getMethods(implClass);
        for (Method method : methods) {
            if (method.getAnnotation(Resource.class) != null) {
                return true;
            }
        }
        return false;

    }

    static private Method getPostConstructMethod(Class implClass) {
        List<Method> methods = getMethods(implClass);
        for (Method method : methods) {
            if (method.getAnnotation(PostConstruct.class) != null) {
                return method;
            }
        }
        return null;
    }

    static private Method getPreDestroyMethod(Class implClass) {
        List<Method> methods = getMethods(implClass);
        for (Method method : methods) {
            if (method.getAnnotation(PreDestroy.class) != null) {
                return method;
            }
        }
        return null;
    }

    /**
     * Gets all of the fields in this class and the super classes
     *
     * @param beanClass
     * @return
     */
    static private List<Field> getFields(final Class beanClass) {
        // This class must remain private due to Java 2 Security concerns
        List<Field> fields;
        fields = (List<Field>)AccessController.doPrivileged(
                new PrivilegedAction() {
                    public Object run() {
                        List<Field> fields = new ArrayList();
                        Class cls = beanClass;
                        while (cls != null) {
                            Field[] fieldArray = cls.getDeclaredFields();
                            for (Field field : fieldArray) {
                                fields.add(field);
                            }
                            cls = cls.getSuperclass();
                        }
                        return fields;
                    }
                }
        );

        return fields;
    }

    /**
     * Gets all of the fields in this class and the super classes
     *
     * @param beanClass
     * @return
     */
    static private List<Method> getMethods(final Class beanClass) {
        // This class must remain private due to Java 2 Security concerns
        List<Method> methods;
        methods = (List<Method>)AccessController.doPrivileged(
                new PrivilegedAction() {
                    public Object run() {
                        List<Method> methods = new ArrayList();
                        Class cls = beanClass;
                        while (cls != null) {
                            Method[] methodArray = cls.getDeclaredMethods();
                            for (Method method : methodArray) {
                                methods.add(method);
                            }
                            cls = cls.getSuperclass();
                        }
                        return methods;
                    }
                }
        );

        return methods;
    }
}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 ResourceInjectionServiceRuntimeDescriptionBuilder.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.