org.glassfish.ejb.deployment.descriptor.PersistenceDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019] Payara Foundation and/or affiliates
package org.glassfish.ejb.deployment.descriptor;
import com.sun.enterprise.deployment.MethodDescriptor;
import com.sun.enterprise.deployment.util.DOLUtils;
import com.sun.enterprise.deployment.util.TypeUtil;
import com.sun.enterprise.util.LocalStringManagerImpl;
import org.glassfish.deployment.common.DeploymentException;
import org.glassfish.deployment.common.Descriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.logging.Level;
/**
* This class contains information about the persistent state
* (abstract persistence schema)
* for EJB2.0 CMP EntityBeans and Join Objects.
*
* @author Sanjeev Krishnan
*/
public final class PersistenceDescriptor extends Descriptor {
private Set cmpFields = new HashSet();
// there can be 0 or more pkey fields
// This set contains FieldDescriptors for fields from bean or pkey class
private Set pkeyFields = new HashSet();
// true if primkey-field is set or for container-generated pk field
private boolean pkeyIsOneField = false;
// false for beans with no primkey-field and pk class = Object
private boolean pkeyFieldSpecified = true;
private String primaryKeyClassName;
private boolean pkeyStuffInitialized = false;
// true for beans whose primary fields are all primitive fields
private boolean pkeyFieldsAllPrimitive = false;
private static LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(PersistenceDescriptor.class);
private EjbCMPEntityDescriptor parentDesc; //the bean whose persistence I describe
private transient Class persistentClass; // the bean class
private transient Class stateClass; // the class which holds persistent fields
private transient Class primaryKeyClass;
private PersistentFieldInfo[] persFieldInfo;
private PersistentFieldInfo[] persNoPkeyFieldInfo;
private PersistentFieldInfo[] pkeyFieldInfo;
private boolean fieldInfoInitialized=false;
private PersistentFieldInfo[] fkeyFields;
private CMRFieldInfo[] cmrFieldInfo;
private transient Field[] pkeyClassPkeyFields; // fields in primary key class
private Hashtable queries = new Hashtable();
private HashSet allQueriedMethods;
public PersistenceDescriptor() {
}
/**
* The copy constructor.
*/
public PersistenceDescriptor(PersistenceDescriptor pers) {
super(pers);
this.getCMPFields().addAll(pers.getCMPFields());
//this.primaryKeyFieldDesc = pers.primaryKeyFieldDesc;
}
public String getCMRFieldReturnType(String field) {
String returnType = "java.util.Collection";
try {
if( !field.trim().equals("") ) {
Class persClass = getPersistentClass();
String methodName = "get" + field.substring(0,1).toUpperCase(Locale.US)
+ field.substring(1);
Method method = TypeUtil.getMethod
(persClass, persClass.getClassLoader(), methodName,
new String[] {});
returnType = method.getReturnType().getName();
}
} catch(Throwable t) {
if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
DOLUtils.getDefaultLogger().log(Level.FINE, t.toString(), t);
}
}
return returnType;
}
/**
* Called from EjbCMPEntityDescriptor
* when some classes in this object are updated.
*/
public boolean classesChanged() {
// XXX Remove any fields marked as persistent that no longer exist
// in bean class as getter/setter.
persistentClass = null;
stateClass = null;
Vector fieldDescriptors = parentDesc.getFieldDescriptors();
// Remove obsolete cmp fields
if( this.cmpFields != null ) {
for(Iterator iter = cmpFields.iterator(); iter.hasNext();) {
FieldDescriptor next = (FieldDescriptor) iter.next();
if( !fieldDescriptors.contains(next) ) {
iter.remove();
}
}
}
// Remove obsolete pkey fields
if( this.pkeyFields != null ) {
for(Iterator iter = pkeyFields.iterator(); iter.hasNext();) {
FieldDescriptor next = (FieldDescriptor) iter.next();
if( !fieldDescriptors.contains(next) ) {
iter.remove();
}
}
}
FieldDescriptor primKeyFieldDesc = parentDesc.getPrimaryKeyFieldDesc();
if( (primKeyFieldDesc != null) &&
!fieldDescriptors.contains(primKeyFieldDesc) ) {
parentDesc.setPrimaryKeyFieldDesc(null);
}
// CMP 2.x
// Remove queries for methods no longer in allQueriedMethods
// First clone old set of queries
Hashtable queriesClone = (Hashtable) queries.clone();
queries = new Hashtable();
initializeAllQueriedMethods();
for (Object o : queriesClone.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Method oldMethod = (Method)entry.getKey();
Method newMethod = findEquivalentMethod(allQueriedMethods,
oldMethod);
if( newMethod != null ) {
QueryDescriptor oldQuery = (QueryDescriptor) entry.getValue();
QueryDescriptor newQuery =
new QueryDescriptor(oldQuery, newMethod);
// Only add to list of methods having queries if
// it's still in the class.
queries.put(newMethod, newQuery);
}
}
// Force the persistence descriptor to regenerate its
// derived info.
invalidate();
return false;
}
/**
* Search for a matching method in a list of methods
* that might have been loaded with a different classloader.
* Because of this possibility, we can't use
* java.lang.reflect.Method.equals().
* @return matched method or NULL
*/
private Method findEquivalentMethod(Collection methods,
Method methodToMatch) {
Method matchedMethod = null;
for(Iterator iter = methods.iterator(); iter.hasNext();) {
Object o = iter.next();
Method next;
if (o instanceof Method) {
next = (Method) o;
} else {
next = ((MethodDescriptor) o).getMethod(parentDesc);
if (next==null) {
return null;
}
}
// Compare methods, ignoring declaring class.
if( methodsEqual(next, methodToMatch, false) ) {
matchedMethod = next;
break;
}
}
return matchedMethod;
}
/**
* Checks whether two methods that might have been loaded by
* different class loaders are equal.
* @param compareDeclaringClass if true, declaring class will
* be considered as part of equality test.
*/
private boolean methodsEqual(Method m1, Method m2,
boolean compareDeclaringClass) {
boolean equal = false;
do {
String m1Name = m1.getName();
String m2Name = m2.getName();
if( !m1Name.equals(m2Name) ) { break; }
String m1DeclaringClass = m1.getDeclaringClass().getName();
String m2DeclaringClass = m2.getDeclaringClass().getName();
if( compareDeclaringClass ) {
if( !m1DeclaringClass.equals(m2DeclaringClass) ) { break; }
}
Class[] m1ParamTypes = m1.getParameterTypes();
Class[] m2ParamTypes = m2.getParameterTypes();
if( m1ParamTypes.length != m2ParamTypes.length ) { break; }
equal = true;
for(int pIndex = 0; pIndex < m1ParamTypes.length; pIndex++) {
String m1ParamClass = m1ParamTypes[pIndex].getName();
String m2ParamClass = m2ParamTypes[pIndex].getName();
if( !m1ParamClass.equals(m2ParamClass) ) {
equal = false;
break;
}
}
} while(false);
return equal;
}
public void setParentDescriptor(EjbCMPEntityDescriptor parentDesc)
{
this.parentDesc = parentDesc;
}
public Descriptor getParentDescriptor()
{
return parentDesc;
}
public EjbBundleDescriptorImpl getEjbBundleDescriptor()
{
return parentDesc.getEjbBundleDescriptor();
}
/**
* Get all CMR fields of this bean. All relationships
* are stored in EjbBundleDescriptor to avoid the complexity of
* keeping the two sets consistent. NOTE : To add or remove
* a relationship use EjbBundleDescriptor.
*/
public Set getRelationships()
{
Set allRelationships = getEjbBundleDescriptor().getRelationships();
Set myRelationships = new HashSet();
for(Iterator iter = allRelationships.iterator(); iter.hasNext();) {
RelationshipDescriptor next = (RelationshipDescriptor) iter.next();
if( next.hasParticipant(parentDesc) ) {
myRelationships.add(next);
}
}
return myRelationships;
}
/**
* Return array of CMRFieldInfo objects for all CMR fields.
*/
public CMRFieldInfo[] getCMRFieldInfo()
{
if ( cmrFieldInfo == null ) {
try {
initCMRFieldStuff();
} catch ( Exception ex ) {
DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
new Object[] {ex.toString()});
throw new DeploymentException(ex);
}
}
return cmrFieldInfo;
}
/**
* Return the CMRFieldInfo object for the given CMR field
*/
public CMRFieldInfo getCMRFieldInfoByName(String fieldName)
{
CMRFieldInfo[] cmrf = this.getCMRFieldInfo();
for ( int i=0; i0; i-- ) {
for ( int j=0; j 0 ) {
CMRFieldInfo tmp = cmrFieldInfo[j];
cmrFieldInfo[j] = cmrFieldInfo[j+1];
cmrFieldInfo[j+1] = tmp;
}
}
}
}
public void clearCMPFields() {
this.cmpFields.clear();
setCMPFields(this.cmpFields);
}
public void addCMPField(String field) {
addCMPField(new FieldDescriptor(field));
}
public void addCMPField(FieldDescriptor fieldDesc) {
this.cmpFields.add(fieldDesc);
setCMPFields(this.cmpFields);
}
public void removeCMPField(String field) {
removeCMPField(new FieldDescriptor(field));
}
public void removeCMPField(FieldDescriptor fieldDesc) {
this.cmpFields.remove(fieldDesc);
setCMPFields(this.cmpFields);
}
/**
* Set the FieldDescriptor objects that the EJB container will persist
* for this bean.
*/
public void setCMPFields(Set cmpFields) {
this.cmpFields = cmpFields;
persFieldInfo = null;
fieldInfoInitialized = false;
}
/**
* Has the supplied field object been deemed persistent.
*/
public boolean isCMPField(String field) {
return this.getCMPFields().contains(new FieldDescriptor(field));
}
/**
* Return the Set of fields deemed persistent.
* The elements of this Set are FieldDescriptor objects.
* This Set should be modified by calling addCMPField, removeCMPField
*/
public Set getCMPFields() {
return this.cmpFields;
}
/**
public void clearPkeyFields() {
this.pkeyFields.clear();
setPkeyFields(this.pkeyFields);
}
public void addPkeyField(String field) {
addPkeyField(new FieldDescriptor(field));
}
public void addPkeyField(FieldDescriptor fieldDesc) {
this.pkeyFields.add(fieldDesc);
setPkeyFields(this.pkeyFields);
}
public void removePkeyField(String field) {
removePkeyField(new FieldDescriptor(field));
}
public void removePkeyField(FieldDescriptor fieldDesc) {
this.pkeyFields.remove(fieldDesc);
setPkeyFields(this.pkeyFields);
}
**/
/**
* Set the FieldDescriptor objects for primary key fields
* for this bean.
*/
public void setPkeyFields(Set pkeyFields) {
this.pkeyFields = pkeyFields;
fieldInfoInitialized = false;
persFieldInfo = null;
pkeyStuffInitialized = false;
}
/**
* Return the Set of primary key fields.
* The elements of this Set are FieldDescriptor objects.
* This Set can be modified by calling addPkeyField, removePkeyField
*/
public Set getPkeyFields() {
if ( !pkeyStuffInitialized )
initPkeyInfo();
return pkeyFields;
}
/**
* Is the supplied field object one of the pkey fields.
*/
public boolean isPkeyField(String field) {
return isPkeyField(new FieldDescriptor(field));
}
public boolean isPkeyField(FieldDescriptor fieldDesc) {
return this.getPkeyFields().contains(fieldDesc);
}
/**
* Initialize pkeyFields, pkeyIsOneField, primaryKeyClassName
* Must be called after this PersistenceDescriptor has been attached
* to the Ejb/JoinDescriptor which has been
* attached to the EjbBundleDescriptor.
*/
private void initPkeyInfo()
{
try {
pkeyIsOneField = false;
pkeyFieldSpecified = true;
primaryKeyClassName = parentDesc.getPrimaryKeyClassName();
FieldDescriptor fd = parentDesc.getPrimaryKeyFieldDesc();
if ( pkeyFields == null || pkeyFields.size() == 0 ) {
pkeyFields = new HashSet();
if ( fd != null ) // primkey-field was set
pkeyFields.add(fd);
else if (!primaryKeyClassName.equals("java.lang.Object")) {
// get fields of primaryKeyClass
primaryKeyClass = getClass(primaryKeyClassName);
Field[] fields = primaryKeyClass.getFields();
pkeyFieldsAllPrimitive = true;
for ( int i=0; i0; i-- ) {
for ( int j=0; j 0 ) {
PersistentFieldInfo tmp = persFieldInfo[j];
persFieldInfo[j] = persFieldInfo[j+1];
persFieldInfo[j+1] = tmp;
}
}
}
// Initialize pkeyFieldInfo[] and persNoPkeyFieldInfo[]
// They contain the same PersistentFieldInfo objects as persFieldInfo.
pkeyFieldInfo = new PersistentFieldInfo[pkeyFields.size()];
if ( pkeyFieldSpecified ) {
// check if PK class has public non-persistent fields
StringBuilder nonPersFieldsInPK = new StringBuilder();
for ( Iterator it=pkeyFields.iterator(); it.hasNext(); ) {
FieldDescriptor fd = (FieldDescriptor)it.next();
boolean isPersistent = false;
for ( int i=0; i