001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.cmp.cmp2;
018:
019: import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
020: import org.apache.openejb.core.CoreDeploymentInfo;
021: import org.apache.openejb.core.cmp.CmpContainer;
022: import org.apache.openejb.core.cmp.KeyGenerator;
023: import org.apache.openejb.util.proxy.ProxyManager;
024: import org.apache.openejb.InterfaceType;
025:
026: import javax.ejb.EJBLocalObject;
027: import javax.ejb.EntityBean;
028: import javax.ejb.EJBObject;
029: import java.lang.reflect.Field;
030: import java.util.ArrayList;
031:
032: public class Cmp2Util {
033: public static Object getPrimaryKey(
034: CoreDeploymentInfo deploymentInfo, EntityBean entity) {
035: if (entity == null)
036: return null;
037:
038: // build the primary key
039: KeyGenerator kg = deploymentInfo.getKeyGenerator();
040: Object primaryKey = kg.getPrimaryKey(entity);
041: return primaryKey;
042: }
043:
044: public static <Bean extends EntityBean> Bean getEntityBean(
045: EJBLocalObject proxy) {
046: if (proxy == null)
047: return null;
048:
049: EjbObjectProxyHandler handler = (EjbObjectProxyHandler) ProxyManager
050: .getInvocationHandler(proxy);
051: if (handler.container == null) {
052: return null;
053: }
054: if (!(handler.container instanceof CmpContainer)) {
055: throw new IllegalArgumentException(
056: "Proxy is not connected to a CMP container but is conect to "
057: + handler.container.getClass().getName());
058: }
059: CmpContainer container = (CmpContainer) handler.container;
060: Bean entity = (Bean) container.getEjbInstance(handler
061: .getDeploymentInfo(), handler.primaryKey);
062: return entity;
063: }
064:
065: public static <Bean extends EntityBean> Bean getEntityBean(
066: EJBObject proxy) {
067: if (proxy == null)
068: return null;
069:
070: EjbObjectProxyHandler handler = (EjbObjectProxyHandler) ProxyManager
071: .getInvocationHandler(proxy);
072: if (handler.container == null) {
073: return null;
074: }
075: if (!(handler.container instanceof CmpContainer)) {
076: throw new IllegalArgumentException(
077: "Proxy is not connected to a CMP container but is conect to "
078: + handler.container.getClass().getName());
079: }
080: CmpContainer container = (CmpContainer) handler.container;
081: Bean entity = (Bean) container.getEjbInstance(handler
082: .getDeploymentInfo(), handler.primaryKey);
083: return entity;
084: }
085:
086: public static <Proxy extends EJBLocalObject> Proxy getEjbProxy(
087: CoreDeploymentInfo deploymentInfo, EntityBean entity) {
088: if (entity == null)
089: return null;
090:
091: // build the primary key
092: Object primaryKey = getPrimaryKey(deploymentInfo, entity);
093:
094: // get the cmp container
095: if (!(deploymentInfo.getContainer() instanceof CmpContainer)) {
096: throw new IllegalArgumentException(
097: "Proxy is not connected to a CMP container but is conect to "
098: + deploymentInfo.getContainer().getClass()
099: .getName());
100: }
101:
102: Proxy proxy = (Proxy) EjbObjectProxyHandler.createProxy(
103: deploymentInfo, primaryKey,
104: InterfaceType.EJB_LOCAL_HOME);
105: return proxy;
106: }
107:
108: public static CoreDeploymentInfo getDeploymentInfo(Class type) {
109: CoreDeploymentInfo deploymentInfo;
110: try {
111: Field deploymentInfoField = type.getField("deploymentInfo");
112: deploymentInfo = (CoreDeploymentInfo) deploymentInfoField
113: .get(null);
114: } catch (Exception e) {
115: throw new IllegalArgumentException(
116: "EntityBean class "
117: + type.getName()
118: + " does not contain a deploymentInfo field. Is this a generated CMP 2 entity implementation?",
119: e);
120: }
121: return deploymentInfo;
122: }
123: }
|