| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
|
| 41 |
package com.sun.jersey.server.impl.cdi; |
| 42 |
|
| 43 |
import java.lang.annotation.Annotation; |
| 44 |
import java.lang.reflect.Type; |
| 45 |
import java.util.Collections; |
| 46 |
import java.util.HashSet; |
| 47 |
import java.util.Set; |
| 48 |
import javax.enterprise.context.Dependent; |
| 49 |
import javax.enterprise.context.spi.CreationalContext; |
| 50 |
import javax.enterprise.inject.spi.Bean; |
| 51 |
import javax.enterprise.inject.spi.InjectionPoint; |
| 52 |
|
| 53 |
/** |
| 54 |
* Base class implementing the CDI Bean interface. |
| 55 |
* |
| 56 |
* Subclasses must implement the create method. |
| 57 |
* |
| 58 |
* Currently, AbstractBean only supports a single qualifier and a single bean type, |
| 59 |
* although it could easily be extended to support more. |
| 60 |
* |
| 61 |
* @author robc |
| 62 |
*/ |
| 63 |
public abstract class AbstractBean<T> implements Bean<T> { |
| 64 |
|
| 65 |
private Class<?> klass; |
| 66 |
private Set<Annotation> qualifiers; |
| 67 |
private Set<Type> types; |
| 68 |
|
| 69 |
public AbstractBean(Class<?> klass, Annotation qualifier) { |
| 70 |
this(klass, klass, qualifier); |
| 71 |
} |
| 72 |
|
| 73 |
public AbstractBean(Class<?> klass, Set<Annotation> qualifiers) { |
| 74 |
this(klass, klass, qualifiers); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
public AbstractBean(Class<?> klass, Type type, Annotation qualifier) { |
| 79 |
this.klass = klass; |
| 80 |
|
| 81 |
qualifiers = new HashSet<Annotation>(); |
| 82 |
qualifiers.add(qualifier); |
| 83 |
types = new HashSet<Type>(); |
| 84 |
types.add(type); |
| 85 |
} |
| 86 |
|
| 87 |
public AbstractBean(Class<?> klass, Type type, Set<Annotation> qualifiers) { |
| 88 |
this.klass = klass; |
| 89 |
this.qualifiers = qualifiers; |
| 90 |
types = new HashSet<Type>(); |
| 91 |
types.add(type); |
| 92 |
} |
| 93 |
|
| 94 |
public Class<?> getBeanClass() { |
| 95 |
return klass; |
| 96 |
} |
| 97 |
|
| 98 |
public Set<InjectionPoint> getInjectionPoints() { |
| 99 |
return (Set<InjectionPoint>)Collections.EMPTY_SET; |
| 100 |
} |
| 101 |
|
| 102 |
public String getName() { |
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
public Set<Annotation> getQualifiers() { |
| 107 |
return qualifiers; |
| 108 |
} |
| 109 |
|
| 110 |
public Class<? extends Annotation> getScope() { |
| 111 |
return Dependent.class; |
| 112 |
} |
| 113 |
|
| 114 |
public Set<Class<? extends Annotation>> getStereotypes() { |
| 115 |
return (Set<Class<? extends Annotation>>)Collections.EMPTY_SET; |
| 116 |
} |
| 117 |
|
| 118 |
public Set<Type> getTypes() { |
| 119 |
return types; |
| 120 |
} |
| 121 |
|
| 122 |
public boolean isAlternative() { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
public boolean isNullable() { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
public abstract T create(CreationalContext<T> creationalContext); |
| 131 |
|
| 132 |
public void destroy(T instance, CreationalContext<T> creationalContext) { |
| 133 |
// no-op |
| 134 |
} |
| 135 |
|
| 136 |
} |