| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2014 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 |
package com.sun.jersey.server.impl.ejb; |
| 41 |
|
| 42 |
import com.sun.jersey.api.container.ContainerException; |
| 43 |
import com.sun.jersey.core.spi.component.ComponentContext; |
| 44 |
import com.sun.jersey.core.spi.component.ComponentScope; |
| 45 |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProcessorFactory; |
| 46 |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProcessorFactoryInitializer; |
| 47 |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProvider; |
| 48 |
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory; |
| 49 |
import com.sun.jersey.core.spi.component.ioc.IoCFullyManagedComponentProvider; |
| 50 |
import com.sun.jersey.core.util.Priority; |
| 51 |
import java.util.logging.Level; |
| 52 |
import java.util.logging.Logger; |
| 53 |
import javax.ejb.Singleton; |
| 54 |
import javax.ejb.Stateless; |
| 55 |
import javax.naming.InitialContext; |
| 56 |
import javax.naming.NamingException; |
| 57 |
|
| 58 |
/** |
| 59 |
* Component provider factory that implements EJB support |
| 60 |
* for GlassFish container. |
| 61 |
* |
| 62 |
* @author Paul Sandoz (paul.sandoz at oracle.com) |
| 63 |
*/ |
| 64 |
@Priority(300) |
| 65 |
final class EJBComponentProviderFactory implements |
| 66 |
IoCComponentProviderFactory, |
| 67 |
IoCComponentProcessorFactoryInitializer { |
| 68 |
|
| 69 |
private static final Logger LOGGER = Logger.getLogger( |
| 70 |
EJBComponentProviderFactory.class.getName()); |
| 71 |
|
| 72 |
private final EJBInjectionInterceptor interceptor; |
| 73 |
|
| 74 |
public EJBComponentProviderFactory(EJBInjectionInterceptor interceptor) { |
| 75 |
this.interceptor = interceptor; |
| 76 |
} |
| 77 |
|
| 78 |
// IoCComponentProviderFactory |
| 79 |
|
| 80 |
public IoCComponentProvider getComponentProvider(Class<?> c) { |
| 81 |
return getComponentProvider(null, c); |
| 82 |
} |
| 83 |
|
| 84 |
public IoCComponentProvider getComponentProvider(ComponentContext cc, Class<?> c) { |
| 85 |
String name = getName(c); |
| 86 |
if (name == null) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
try { |
| 91 |
InitialContext ic = new InitialContext(); |
| 92 |
Object o = lookup(ic, c, name); |
| 93 |
|
| 94 |
LOGGER.info("Binding the EJB class " + c.getName() + |
| 95 |
" to EJBManagedComponentProvider"); |
| 96 |
|
| 97 |
return new EJBManagedComponentProvider(o); |
| 98 |
} catch (NamingException ex) { |
| 99 |
String message = "An instance of EJB class " + c.getName() + |
| 100 |
" could not be looked up using simple form name or the fully-qualified form name." + |
| 101 |
"Ensure that the EJB/JAX-RS component implements at most one interface."; |
| 102 |
LOGGER.log(Level.SEVERE, message, ex); |
| 103 |
throw new ContainerException(message); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private String getName(Class<?> c) { |
| 108 |
String name = null; |
| 109 |
if (c.isAnnotationPresent(Stateless.class)) { |
| 110 |
name = c.getAnnotation(Stateless.class).name(); |
| 111 |
} else if (c.isAnnotationPresent(Singleton.class)) { |
| 112 |
name = c.getAnnotation(Singleton.class).name(); |
| 113 |
} else { |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
if (name == null || name.length() == 0) { |
| 118 |
name = c.getSimpleName(); |
| 119 |
} |
| 120 |
return name; |
| 121 |
} |
| 122 |
|
| 123 |
private Object lookup(InitialContext ic, Class<?> c, String name) throws NamingException { |
| 124 |
try { |
| 125 |
return lookupSimpleForm(ic, c, name); |
| 126 |
} catch (NamingException ex) { |
| 127 |
LOGGER.log(Level.WARNING, "An instance of EJB class " + c.getName() + |
| 128 |
" could not be looked up using simple form name. " + |
| 129 |
"Attempting to look up using the fully-qualified form name.", ex); |
| 130 |
|
| 131 |
return lookupFullyQualfiedForm(ic, c, name); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
private Object lookupSimpleForm(InitialContext ic, Class<?> c, String name) throws NamingException { |
| 136 |
String jndiName = "java:module/" + name; |
| 137 |
return ic.lookup(jndiName); |
| 138 |
} |
| 139 |
|
| 140 |
private Object lookupFullyQualfiedForm(InitialContext ic, Class<?> c, String name) throws NamingException { |
| 141 |
String jndiName = "java:module/" + name + "!" + c.getName(); |
| 142 |
return ic.lookup(jndiName); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
private static class EJBManagedComponentProvider implements IoCFullyManagedComponentProvider { |
| 147 |
private final Object o; |
| 148 |
|
| 149 |
EJBManagedComponentProvider(Object o) { |
| 150 |
this.o = o; |
| 151 |
} |
| 152 |
|
| 153 |
public ComponentScope getScope() { |
| 154 |
return ComponentScope.Singleton; |
| 155 |
} |
| 156 |
|
| 157 |
public Object getInstance() { |
| 158 |
return o; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// IoCComponentProcessorFactoryInitializer |
| 163 |
|
| 164 |
public void init(IoCComponentProcessorFactory cpf) { |
| 165 |
interceptor.setFactory(cpf); |
| 166 |
} |
| 167 |
} |