下面列出了怎么用javax.ejb.EJBLocalHome的API类实例代码及写法,或者点击链接到github查看源代码。
private void check_localHomeInterfaceMethods(final RemoteBean b) {
Class home = null;
Class bean = null;
try {
home = loadClass(b.getLocalHome());
bean = loadClass(b.getEjbClass());
} catch (final OpenEJBException e) {
return;
}
if (!EJBLocalHome.class.isAssignableFrom(home)) {
return;
}
if (check_hasCreateMethod(b, bean, home)) {
check_createMethodsAreImplemented(b, bean, home);
// check_postCreateMethodsAreImplemented(b, bean, home);
}
}
public ProxyFactory(final BeanContext beanContext) {
this.beanContext = beanContext;
keyGenerator = beanContext.getKeyGenerator();
remoteInterface = beanContext.getRemoteInterface();
if (remoteInterface != null) {
final EJBHome homeProxy = beanContext.getEJBHome();
remoteHandler = (EntityEjbHomeHandler) ProxyManager.getInvocationHandler(homeProxy);
} else {
remoteHandler = null;
}
localInterface = beanContext.getLocalInterface();
if (localInterface != null) {
final EJBLocalHome localHomeProxy = beanContext.getEJBLocalHome();
localHandler = (EntityEjbHomeHandler) ProxyManager.getInvocationHandler(localHomeProxy);
} else {
localHandler = null;
}
}
/**
* Check for EJB3-style home object that serves as EJB component directly.
*/
@Override
protected Method getCreateMethod(Object home) throws EjbAccessException {
if (this.homeAsComponent) {
return null;
}
if (!(home instanceof EJBLocalHome)) {
// An EJB3 Session Bean...
this.homeAsComponent = true;
return null;
}
return super.getCreateMethod(home);
}
/**
* Check for EJB3-style home object that serves as EJB component directly.
*/
@Override
protected Method getCreateMethod(Object home) throws EjbAccessException {
if (this.homeAsComponent) {
return null;
}
if (!(home instanceof EJBLocalHome)) {
// An EJB3 Session Bean...
this.homeAsComponent = true;
return null;
}
return super.getCreateMethod(home);
}
/**
* Check for EJB3-style home object that serves as EJB component directly.
*/
@Override
protected Method getCreateMethod(Object home) throws EjbAccessException {
if (this.homeAsComponent) {
return null;
}
if (!(home instanceof EJBLocalHome)) {
// An EJB3 Session Bean...
this.homeAsComponent = true;
return null;
}
return super.getCreateMethod(home);
}
/**
* Check for EJB3-style home object that serves as EJB component directly.
*/
@Override
protected Method getCreateMethod(Object home) throws EjbAccessException {
if (this.homeAsComponent) {
return null;
}
if (!(home instanceof EJBLocalHome)) {
// An EJB3 Session Bean...
this.homeAsComponent = true;
return null;
}
return super.getCreateMethod(home);
}
public void test02_getEjbLocalHome() {
try {
final EJBLocalHome localHome = ejbLocalObject.getEJBLocalHome();
assertNotNull("The EJBHome is null", localHome);
} catch (final Exception e) {
fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
}
public void test02_getEjbLocalHome() {
try {
final EJBLocalHome localHome = ejbLocalObject.getEJBLocalHome();
assertNotNull("The EJBLocalHome is null", localHome);
} catch (final Exception e) {
fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
}
public void test02_getEjbLocalHome() {
try {
final EJBLocalHome localHome = ejbLocalObject.getEJBLocalHome();
assertNotNull("The EJBLocalHome is null", localHome);
} catch (final Exception e) {
fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
}
public InterfaceType getInterfaceType(final Class clazz) {
final InterfaceType type = interfaces.get(clazz);
if (type != null) {
return type;
}
if (EJBLocalHome.class.isAssignableFrom(clazz)) {
return InterfaceType.EJB_LOCAL_HOME;
}
if (EJBLocalObject.class.isAssignableFrom(clazz)) {
return InterfaceType.EJB_LOCAL;
}
if (EJBHome.class.isAssignableFrom(clazz)) {
return InterfaceType.EJB_HOME;
}
if (EJBObject.class.isAssignableFrom(clazz)) {
return InterfaceType.EJB_OBJECT;
}
for (final Entry<Class, InterfaceType> entry : interfaces.entrySet()) { // for @Remote case where the loaded interface can be different from the stored one
if (entry.getKey().getName().equals(clazz.getName())) {
return entry.getValue();
}
}
return null;
}
public EJBLocalHome getEJBLocalHome() {
if (getLocalHomeInterface() == null) {
throw new IllegalStateException("This component has no local home interface: " + getDeploymentID());
}
if (getLegacyView().ejbLocalHomeRef == null) {
getLegacyView().ejbLocalHomeRef = (EJBLocalHome) EjbHomeProxyHandler.createHomeProxy(this, InterfaceType.EJB_LOCAL_HOME);
}
return getLegacyView().ejbLocalHomeRef;
}
private static boolean containerMethod(final Method method) {
return (method.getDeclaringClass() == EJBObject.class ||
method.getDeclaringClass() == EJBHome.class ||
method.getDeclaringClass() == EJBLocalObject.class ||
method.getDeclaringClass() == EJBLocalHome.class) &&
!method.getName().equals("remove");
}
@Override
public EJBLocalHome getEJBLocalHome() {
throw new UnsupportedOperationException();
}
public BeanContext(final String id, final Context jndiContext, final ModuleContext moduleContext,
final Class beanClass, final Class homeInterface,
final Class remoteInterface,
final Class localHomeInterface,
final Class localInterface,
final Class proxy,
final Class serviceEndpointInterface, final List<Class> businessLocals, final List<Class> businessRemotes, final Class pkClass,
final BeanType componentType,
final boolean localBean,
final boolean passivable) throws SystemException {
this(id, jndiContext, moduleContext, componentType, localBean, beanClass, passivable);
this.proxyClass = proxy;
if (homeInterface != null) {
this.getLegacyView().homeInterface = homeInterface;
}
if (localInterface != null) {
this.getLegacyView().localInterface = localInterface;
}
if (localHomeInterface != null) {
this.getLegacyView().localHomeInterface = localHomeInterface;
}
if (remoteInterface != null) {
this.getLegacyView().remoteInterface = remoteInterface;
}
if (businessLocals != null) {
this.businessLocals.addAll(businessLocals);
}
if (businessRemotes != null) {
this.businessRemotes.addAll(businessRemotes);
}
if (pkClass != null) {
getCmp().pkClass = pkClass;
}
this.serviceEndpointInterface = serviceEndpointInterface;
// if (businessLocal != null && localHomeInterface == null){
// this.localHomeInterface = BusinessLocalHome.class;
// }
//
// if (businessRemote != null && homeInterface == null){
// this.homeInterface = BusinessRemoteHome.class;
// }
// createMethodMap();
if (TimedObject.class.isAssignableFrom(beanClass)) {
try {
this.ejbTimeout = beanClass.getMethod("ejbTimeout", Timer.class);
} catch (final NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
addInterface(getServiceEndpointInterface(), InterfaceType.SERVICE_ENDPOINT);
addInterface(EJBHome.class, InterfaceType.EJB_HOME);
addInterface(EJBObject.class, InterfaceType.EJB_OBJECT);
addInterface(EJBLocalHome.class, InterfaceType.EJB_LOCAL_HOME);
addInterface(EJBLocalObject.class, InterfaceType.EJB_LOCAL);
addInterface(getHomeInterface(), InterfaceType.EJB_HOME);
addInterface(getRemoteInterface(), InterfaceType.EJB_OBJECT);
addInterface(getLocalHomeInterface(), InterfaceType.EJB_LOCAL_HOME);
addInterface(getLocalInterface(), InterfaceType.EJB_LOCAL);
addInterface(BeanContext.BusinessRemoteHome.class, InterfaceType.BUSINESS_REMOTE_HOME);
for (final Class businessRemote : this.businessRemotes) {
addInterface(businessRemote, InterfaceType.BUSINESS_REMOTE);
}
addInterface(BeanContext.BusinessLocalHome.class, InterfaceType.BUSINESS_LOCAL_HOME);
for (final Class businessLocal : this.businessLocals) {
addInterface(businessLocal, InterfaceType.BUSINESS_LOCAL);
}
if (localBean) {
addInterface(beanClass, InterfaceType.LOCALBEAN);
}
this.initDefaultLock();
}
@Override
public Object invoke(final Object deployID,
InterfaceType type,
final Class callInterface,
final Method callMethod,
final Object[] args,
final Object primKey) throws OpenEJBException {
final BeanContext beanContext = this.getBeanContext(deployID);
if (beanContext == null) {
throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
}
// Use the backup way to determine call type if null was supplied.
if (type == null) {
type = beanContext.getInterfaceType(callInterface);
}
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
try {
final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
if (!authorized) {
throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
}
final Class declaringClass = callMethod.getDeclaringClass();
final String methodName = callMethod.getName();
if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) {
if (methodName.startsWith("create")) {
return createEJBObject(callMethod, args, callContext, type);
} else if (methodName.startsWith("find")) {
return findMethod(callMethod, args, callContext, type);
} else {
return homeMethod(callMethod, args, callContext, type);
}
} else if (methodName.equals("remove")) {
removeEJBObject(callMethod, args, callContext, type);
return null;
}
} else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
removeEJBObject(callMethod, args, callContext, type);
return null;
}
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
callContext.set(Method.class, runMethod);
return invoke(type, callMethod, runMethod, args, callContext);
} finally {
ThreadContext.exit(oldCallContext);
}
}
public EJBLocalHome getEJBLocalHome() {
final ThreadContext threadContext = ThreadContext.getThreadContext();
final BeanContext di = threadContext.getBeanContext();
return di.getEJBLocalHome();
}
@Override
public Object invoke(final Object deployID,
InterfaceType type,
final Class callInterface,
final Method callMethod,
final Object[] args,
final Object primKey) throws OpenEJBException {
final BeanContext beanContext = this.getBeanContext(deployID);
if (beanContext == null) {
throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
}
// Use the backup way to determine call type if null was supplied.
if (type == null) {
type = beanContext.getInterfaceType(callInterface);
}
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
try {
final boolean authorized = securityService.isCallerAuthorized(callMethod, type);
if (!authorized) {
throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
}
final Class declaringClass = callMethod.getDeclaringClass();
final String methodName = callMethod.getName();
if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) {
if (methodName.startsWith("create")) {
return createEJBObject(callMethod, args, callContext, type);
} else if (methodName.equals("findByPrimaryKey")) {
return findByPrimaryKey(callMethod, args, callContext, type);
} else if (methodName.startsWith("find")) {
return findEJBObject(callMethod, args, callContext, type);
} else {
return homeMethod(callMethod, args, callContext, type);
}
} else if (methodName.equals("remove")) {
removeEJBObject(callMethod, callContext, type);
return null;
}
} else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
removeEJBObject(callMethod, callContext, type);
return null;
}
// business method
callContext.setCurrentOperation(Operation.BUSINESS);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
callContext.set(Method.class, runMethod);
return businessMethod(callMethod, runMethod, args, callContext, type);
} finally {
ThreadContext.exit(oldCallContext);
}
}
@Override
public Object invoke(final Object deployID,
InterfaceType type,
final Class callInterface,
final Method callMethod,
final Object[] args,
final Object primKey) throws OpenEJBException {
final BeanContext beanContext = this.getBeanContext(deployID);
if (beanContext == null) {
throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
}
// Use the backup way to determine call type if null was supplied.
if (type == null) {
type = beanContext.getInterfaceType(callInterface);
}
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
final CurrentCreationalContext currentCreationalContext = beanContext.get(CurrentCreationalContext.class);
Object runAs = null;
try {
if (oldCallContext != null) {
final BeanContext oldBc = oldCallContext.getBeanContext();
if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(callContext, beanContext, oldBc);
}
}
final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
if (!authorized) {
throw new org.apache.openejb.ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
}
final Class declaringClass = callMethod.getDeclaringClass();
if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
if (callMethod.getName().startsWith("create")) {
return createEJBObject(beanContext, callMethod);
} else {
return null;// EJBHome.remove( ) and other EJBHome methods are not process by the container
}
} else if (EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) {
return null;// EJBObject.remove( ) and other EJBObject methods are not process by the container
}
final Instance instance = instanceManager.getInstance(callContext);
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
callContext.setCurrentAllowedStates(null);
callContext.set(Method.class, runMethod);
callContext.setInvokedInterface(callInterface);
if (currentCreationalContext != null) {
//noinspection unchecked
currentCreationalContext.set(instance.creationalContext);
}
return _invoke(callMethod, runMethod, args, instance, callContext, type);
} finally {
if (runAs != null) {
try {
securityService.associate(runAs);
} catch (final LoginException e) {
// no-op
}
}
ThreadContext.exit(oldCallContext);
if (currentCreationalContext != null) {
currentCreationalContext.remove();
}
}
}
@Override
public EJBLocalHome getEJBLocalHome() {
throw new IllegalStateException();
}
private boolean isValidInterface(final RemoteBean b, final Class clazz, final Class beanClass, final String tag) {
if (clazz.equals(beanClass)) {
fail(b, "xml." + tag + ".beanClass", clazz.getName());
} else if (!clazz.isInterface()) {
fail(b, "xml." + tag + ".notInterface", clazz.getName());
} else if (EJBHome.class.isAssignableFrom(clazz)) {
if (tag.equals("home")) {
return true;
}
fail(b, "xml." + tag + ".ejbHome", clazz.getName());
} else if (EJBLocalHome.class.isAssignableFrom(clazz)) {
if (tag.equals("localHome")) {
return true;
}
fail(b, "xml." + tag + ".ejbLocalHome", clazz.getName());
} else if (EJBObject.class.isAssignableFrom(clazz)) {
if (tag.equals("remote")) {
return true;
}
fail(b, "xml." + tag + ".ejbObject", clazz.getName());
} else if (EJBLocalObject.class.isAssignableFrom(clazz)) {
if (tag.equals("local")) {
return true;
}
fail(b, "xml." + tag + ".ejbLocalObject", clazz.getName());
} else {
if (tag.equals("businessLocal") || tag.equals("businessRemote")) {
return true;
} else if (clazz.isAnnotationPresent(Local.class)) {
fail(b, "xml." + tag + ".businessLocal", clazz.getName());
} else if (clazz.isAnnotationPresent(Remote.class)) {
fail(b, "xml." + tag + ".businessRemote", clazz.getName());
} else {
fail(b, "xml." + tag + ".unknown", clazz.getName());
}
}
// must be tagged as <home>, <local-home>, <remote>, or <local>
return false;
}