/** INTERNAL: Transform the object-level value into a database-level value */ public Object getFieldValue(Object objectValue, AbstractSession session) { DatabaseMapping mapping = getMapping(); Object fieldValue = objectValue; if ((mapping != null) && (mapping.isDirectToFieldMapping() || mapping.isDirectCollectionMapping())) { // CR#3623207, check for IN Collection here not in mapping. if (objectValue instanceof Collection) { // This can actually be a collection for IN within expressions... however it would be better // for expressions to handle this. Collection values = (Collection) objectValue; Vector fieldValues = new Vector(values.size()); for (Iterator iterator = values.iterator(); iterator.hasNext(); ) { Object value = iterator.next(); if (!(value instanceof Expression)) { value = getFieldValue(value, session); } fieldValues.add(value); } fieldValue = fieldValues; } else { if (mapping.isDirectToFieldMapping()) { fieldValue = ((AbstractDirectMapping) mapping).getFieldValue(objectValue, session); } else if (mapping.isDirectCollectionMapping()) { fieldValue = ((DirectCollectionMapping) mapping).getFieldValue(objectValue, session); } } } return fieldValue; }
/** * INTERNAL: This methods clones all the fields and ensures that each collection refers to the * same clones. */ @Override public Object clone() { VariableOneToOneMapping clone = (VariableOneToOneMapping) super.clone(); Map setOfKeys = new HashMap(getSourceToTargetQueryKeyNames().size()); Map sourceToTarget = new HashMap(getSourceToTargetQueryKeyNames().size()); Vector foreignKeys = org.eclipse.persistence.internal.helper.NonSynchronizedVector.newInstance( getForeignKeyFields().size()); if (getTypeField() != null) { clone.setTypeField((DatabaseField) this.getTypeField().clone()); } for (Iterator enumtr = getSourceToTargetQueryKeyNames().keySet().iterator(); enumtr.hasNext(); ) { // Clone the SourceKeyFields DatabaseField field = (DatabaseField) enumtr.next(); DatabaseField clonedField = (DatabaseField) field.clone(); setOfKeys.put(field, clonedField); // on the next line I'm cloning the query key names sourceToTarget.put(clonedField, getSourceToTargetQueryKeyNames().get(field)); } for (Enumeration enumtr = getForeignKeyFields().elements(); enumtr.hasMoreElements(); ) { DatabaseField field = (DatabaseField) enumtr.nextElement(); foreignKeys.addElement(setOfKeys.get(field)); } clone.setSourceToTargetQueryKeyFields(sourceToTarget); clone.setForeignKeyFields(foreignKeys); clone.setTypeIndicatorTranslation(new HashMap(this.getTypeIndicatorTranslation())); return clone; }
/** * INTERNAL: Convert all the class-name-based settings in this InheritancePolicy to actual * class-based settings. This method is used when converting a project that has been built with * class names to a project with classes. It will also convert referenced classes to the versions * of the classes from the classLoader. */ public void convertClassNamesToClasses(ClassLoader classLoader) { Vector newParentInterfaces = new Vector(); for (Iterator iterator = getParentInterfaceNames().iterator(); iterator.hasNext(); ) { String interfaceName = (String) iterator.next(); Class interfaceClass = null; try { if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) { try { interfaceClass = (Class) AccessController.doPrivileged( new PrivilegedClassForName(interfaceName, true, classLoader)); } catch (PrivilegedActionException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( interfaceName, exception.getException()); } } else { interfaceClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName( interfaceName, true, classLoader); } } catch (ClassNotFoundException exc) { throw ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, exc); } newParentInterfaces.add(interfaceClass); } this.parentInterfaces = newParentInterfaces; }
/** * PUBLIC: Connect to the database using the predefined login. This connects all of the child * sessions and expects that they are in a valid state to be connected. */ public void login(String userName, String password) throws DatabaseException { // Bug#3440544 Check if logged in already to stop the attempt to login more than once if (isLoggedIn) { throw ValidationException.alreadyLoggedIn(this.getName()); } else { if (this.eventManager != null) { this.eventManager.preLogin(this); } // Bug 3848021 - ensure the external transaction controller is initialized if (!isConnected()) { getServerPlatform().initializeExternalTransactionController(); } // Connection all sessions and initialize for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { DatabaseSessionImpl session = (DatabaseSessionImpl) sessionEnum.next(); if (session.hasEventManager()) { session.getEventManager().preLogin(session); } session.getDatasourceLogin().setUserName(userName); session.getDatasourceLogin().setPassword(password); if (!session.isConnected()) { session.connect(); } } initializeDescriptors(); this.isLoggedIn = true; } }
/** * PUBLIC: Return a session broker that behaves as a client session broker. An acquire session * broker is done under the covers on each session inside the session broker, and a new broker is * returned. * * <p>NOTE: when finished with the client broker, it should be releases. See * releaseClientSessionBroker. */ public SessionBroker acquireClientSessionBroker() { log(SessionLog.FINER, SessionLog.CONNECTION, "acquire_client_session_broker"); SessionBroker clientBroker = copySessionBroker(); clientBroker.parent = this; clientBroker .getIdentityMapAccessorInstance() .setIdentityMapManager(getIdentityMapAccessorInstance().getIdentityMapManager()); clientBroker.commitManager = getCommitManager(); clientBroker.commandManager = getCommandManager(); clientBroker.externalTransactionController = getExternalTransactionController(); clientBroker.setServerPlatform(getServerPlatform()); String sessionName; AbstractSession serverSession; Iterator names = this.getSessionsByName().keySet().iterator(); while (names.hasNext()) { sessionName = (String) names.next(); serverSession = getSessionForName(sessionName); if (serverSession instanceof org.eclipse.persistence.sessions.server.ServerSession) { if (serverSession.getProject().hasIsolatedClasses()) { throw ValidationException.isolatedDataNotSupportedInSessionBroker(sessionName); } clientBroker.internalRegisterSession( sessionName, ((org.eclipse.persistence.sessions.server.ServerSession) serverSession) .acquireClientSession()); } else { throw ValidationException.cannotAcquireClientSessionFromSession(); } } clientBroker.initializeSequencing(); return clientBroker; }
/** * INTERNAL: Return the classification for the field contained in the mapping. This is used to * convert the row value to a consistent java value. */ @Override public Class getFieldClassification(DatabaseField fieldToClassify) { if ((getTypeField() != null) && (fieldToClassify.equals(getTypeField()))) { return getTypeField().getType(); } String queryKey = (String) getSourceToTargetQueryKeyNames().get(fieldToClassify); if (queryKey == null) { return null; } // Search any of the implementor descriptors for a mapping for the query-key. Iterator iterator = getReferenceDescriptor().getInterfacePolicy().getChildDescriptors().iterator(); if (iterator.hasNext()) { ClassDescriptor firstChild = (ClassDescriptor) iterator.next(); DatabaseMapping mapping = firstChild.getObjectBuilder().getMappingForAttributeName(queryKey); if ((mapping != null) && (mapping.isDirectToFieldMapping())) { return ((AbstractDirectMapping) mapping).getAttributeClassification(); } QueryKey targetQueryKey = firstChild.getQueryKeyNamed(queryKey); if ((targetQueryKey != null) && (targetQueryKey.isDirectQueryKey())) { return firstChild .getObjectBuilder() .getFieldClassification(((DirectQueryKey) targetQueryKey).getField()); } } return null; }
/** PUBLIC: Add all of the elements. */ public void putAll(Map map) { Iterator entriesIterator = map.entrySet().iterator(); while (entriesIterator.hasNext()) { Map.Entry entry = (Map.Entry) entriesIterator.next(); put(entry.getKey(), entry.getValue()); } }
/** * INTERNAL: Convert all the class-name-based settings in this mapping to actual class-based * settings. This method is used when converting a project that has been built with class names to * a project with classes. */ @Override public void convertClassNamesToClasses(ClassLoader classLoader) { super.convertClassNamesToClasses(classLoader); Iterator iterator = getTypeIndicatorNameTranslation().entrySet().iterator(); this.typeIndicatorTranslation = new HashMap(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String referenceClassName = (String) entry.getKey(); Object indicator = entry.getValue(); Class referenceClass = null; try { if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) { try { referenceClass = (Class) AccessController.doPrivileged( new PrivilegedClassForName(referenceClassName, true, classLoader)); } catch (PrivilegedActionException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( referenceClassName, exception.getException()); } } else { referenceClass = PrivilegedAccessHelper.getClassForName(referenceClassName, true, classLoader); } } catch (ClassNotFoundException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( referenceClassName, exception); } addClassIndicator(referenceClass, indicator); } }
/** * PUBLIC: Set the message log. * * @see #logMessages() */ public void setLog(Writer log) { super.setLog(log); for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); session.setLog(log); } }
/** * PUBLIC: Set the profiler for the session. This allows for performance operations to be * profiled. */ public void setProfiler(SessionProfiler profiler) { super.setProfiler(profiler); for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); session.setProfiler(profiler); } }
/** * PUBLIC: set the integrityChecker. IntegrityChecker holds all the ClassDescriptor Exceptions. */ public void setIntegrityChecker(IntegrityChecker integrityChecker) { super.setIntegrityChecker(integrityChecker); for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); session.setIntegrityChecker(integrityChecker); } }
/** * INTERNAL: Set isSynchronized flag to indicate that members of session broker are synchronized. * This method should only be called by setSynchronized method of UnitOfWork obtained from either * DatabaseSession Broker or ClientSession Broker. */ public void setSynchronized(boolean synched) { if (!isServerSessionBroker()) { super.setSynchronized(synched); Iterator itSessions = getSessionsByName().values().iterator(); while (itSessions.hasNext()) { ((AbstractSession) itSessions.next()).setSynchronized(synched); } } }
/** PUBLIC: Return if this session is a server session broker. */ public boolean isServerSessionBroker() { for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); if (session.isServerSession()) { return true; } } return false; }
/** * INTERNAL: The foreign key names and their primary keys are converted to DatabaseField and * stored. */ protected void initializeForeignKeys(AbstractSession session) { HashMap newSourceToTargetQueryKeyNames = new HashMap(getSourceToTargetQueryKeyNames().size()); Iterator iterator = getSourceToTargetQueryKeyNames().entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); DatabaseField field = getDescriptor().buildField((DatabaseField) entry.getKey()); newSourceToTargetQueryKeyNames.put(field, entry.getValue()); } this.sourceToTargetQueryKeyNames = newSourceToTargetQueryKeyNames; }
/** * INTERNAL: writes the first field from each of the owned tables, used for fine-grained * pessimistic locking. */ protected void writeForUpdateOfFields( ExpressionSQLPrinter printer, SQLSelectStatement statement) { for (Iterator iterator = getForUpdateOfFields().iterator(); iterator.hasNext(); ) { DatabaseField field = (DatabaseField) iterator.next(); if (printer.getPlatform().shouldPrintAliasForUpdate()) { writeAlias(printer, field, statement); } else { writeField(printer, field, statement); } } }
/** * PUBLIC: Release the session. This does nothing by default, but allows for other sessions such * as the ClientSession to do something. */ public void release() { if (isClientSessionBroker()) { log(SessionLog.FINER, SessionLog.CONNECTION, "releasing_client_session_broker"); } AbstractSession session; for (Iterator enumtr = getSessionsByName().values().iterator(); enumtr.hasNext(); ) { session = (AbstractSession) enumtr.next(); session.release(); } super.release(); }
/** PUBLIC: Return a collection of the field to query key associations. */ public Vector getSourceToTargetQueryKeyFieldAssociations() { Vector associations = new Vector(getSourceToTargetQueryKeyNames().size()); Iterator sourceFieldEnum = getSourceToTargetQueryKeyNames().keySet().iterator(); Iterator targetQueryKeyEnum = getSourceToTargetQueryKeyNames().values().iterator(); while (sourceFieldEnum.hasNext()) { Object fieldValue = ((DatabaseField) sourceFieldEnum.next()).getQualifiedName(); Object attributeValue = targetQueryKeyEnum.next(); associations.addElement(new Association(fieldValue, attributeValue)); } return associations; }
/** * PUBLIC: Disconnect from all databases. * * @exception EclipseLinkException if a transaction is active, you must rollback any active * transaction before logout. * @exception DatabaseException the database will also raise an error if their is an active * transaction, or a general error occurs. */ public void logout() throws DatabaseException { for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { DatabaseSessionImpl session = (DatabaseSessionImpl) sessionEnum.next(); session.logout(); } if (!isClientSessionBroker()) { if (hasExternalTransactionController()) { getExternalTransactionController().clearSequencingListeners(); } } sequencing = null; isLoggedIn = false; }
/** INTERNAL: Rollback the transaction on all child sessions. */ protected void basicRollbackTransaction() throws DatabaseException { DatabaseException globalException = null; for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); try { session.rollbackTransaction(); } catch (DatabaseException exception) { globalException = exception; } } if (globalException != null) { throw globalException; } }
/** * INTERNAL: Returns a number of member sessions that require sequencing callback. Always returns * 0 if sequencing is not connected. */ public int howManySequencingCallbacks() { if (this.isClientSessionBroker()) { return getParent().howManySequencingCallbacks(); } else { int nCallbacks = 0; Iterator itSessions = getSessionsByName().values().iterator(); while (itSessions.hasNext()) { if (((DatabaseSessionImpl) itSessions.next()).isSequencingCallbackRequired()) { nCallbacks++; } } return nCallbacks; } }
/** PUBLIC: Return if all sessions are still connected to the database. */ public boolean isConnected() { if ((getSessionsByName() == null) || (getSessionsByName().isEmpty())) { return false; } for (Iterator sessionEnum = getSessionsByName().values().iterator(); sessionEnum.hasNext(); ) { AbstractSession session = (AbstractSession) sessionEnum.next(); if (!session.isConnected()) { return false; } } return true; }
/** * INTERNAL: Return the typeIndicatorName translation Used by the Mapping Workbench to avoid * classpath dependencies */ public Map getTypeIndicatorNameTranslation() { if (typeIndicatorNameTranslation.isEmpty() && !typeIndicatorTranslation.isEmpty()) { Iterator keysEnum = typeIndicatorTranslation.keySet().iterator(); Iterator valuesEnum = typeIndicatorTranslation.values().iterator(); while (keysEnum.hasNext()) { Object key = keysEnum.next(); Object value = valuesEnum.next(); if (key instanceof Class) { String className = ((Class) key).getName(); typeIndicatorNameTranslation.put(className, value); } } } return typeIndicatorNameTranslation; }
/** * PUBLIC: Register the session under its name. All of the session's descriptors must have already * been registered. The session should not be connected and descriptors should not be initialized. */ public void registerSession(String name, AbstractSession session) { session.setIsInBroker(true); getSessionsByName().put(name, session); session.setBroker(this); session.setName(name); // The keys/classes must also be used as some descriptors may be under multiple classes. Iterator descriptors = session.getDescriptors().values().iterator(); Iterator classes = session.getDescriptors().keySet().iterator(); while (descriptors.hasNext()) { ClassDescriptor descriptor = (ClassDescriptor) descriptors.next(); Class descriptorClass = (Class) classes.next(); getSessionNamesByClass().put(descriptorClass, name); getDescriptors().put(descriptorClass, descriptor); } }
/** INTERNAL: Set the type field classification through searching the indicators hashtable. */ @Override public void preInitialize(AbstractSession session) throws DescriptorException { super.preInitialize(session); if (getTypeIndicatorTranslation().isEmpty()) { return; } Class type = null; for (Iterator typeValuesEnum = getTypeIndicatorTranslation().values().iterator(); typeValuesEnum.hasNext() && (type == null); ) { Object value = typeValuesEnum.next(); if ((value != Helper.getNullWrapper()) && (!(value instanceof Class))) { type = value.getClass(); } } getTypeField().setType(type); }
/** * INTERNAL: Return the class indicator associations for XML. List of class-name/value * associations. */ public Vector getClassIndicatorAssociations() { Vector associations = new Vector(); Iterator classesEnum = getTypeIndicatorNameTranslation().keySet().iterator(); Iterator valuesEnum = getTypeIndicatorNameTranslation().values().iterator(); while (classesEnum.hasNext()) { Object className = classesEnum.next(); // If the project was built in runtime is a class, MW is a string. if (className instanceof Class) { className = ((Class) className).getName(); } Object value = valuesEnum.next(); associations.addElement(new TypedAssociation(className, value)); } return associations; }
/** * INTERNAL: Compare the references of the two objects are the same, not the objects themselves. * Used for independent relationships. This is used for testing and validation purposes. * * <p>Must get separate fields for the objects because we may be adding a different class to the * attribute because of the interface */ @Override protected boolean compareObjectsWithoutPrivateOwned( Object firstObject, Object secondObject, AbstractSession session) { Object firstPrivateObject = getRealAttributeValueFromObject(firstObject, session); Object secondPrivateObject = getRealAttributeValueFromObject(secondObject, session); if ((firstPrivateObject == null) && (secondPrivateObject == null)) { return true; } if ((firstPrivateObject == null) || (secondPrivateObject == null)) { return false; } if (firstPrivateObject.getClass() != secondPrivateObject.getClass()) { return false; } Iterator targetKeys = getSourceToTargetQueryKeyNames().values().iterator(); ClassDescriptor descriptor = session.getDescriptor(firstPrivateObject.getClass()); ClassDescriptor descriptor2 = session.getDescriptor(secondPrivateObject.getClass()); while (targetKeys.hasNext()) { String queryKey = (String) targetKeys.next(); DatabaseField field = descriptor.getObjectBuilder().getFieldForQueryKeyName(queryKey); Object firstObjectField = descriptor .getObjectBuilder() .extractValueFromObjectForField(firstPrivateObject, field, session); DatabaseField field2 = descriptor2.getObjectBuilder().getFieldForQueryKeyName(queryKey); Object secondObjectField = descriptor2 .getObjectBuilder() .extractValueFromObjectForField(secondPrivateObject, field2, session); if (!((firstObjectField == null) && (secondObjectField == null))) { if ((firstObjectField == null) || (secondObjectField == null)) { return false; } if (!firstObjectField.equals(secondObjectField)) { return false; } } } return true; }
/** INTERNAL: Acquires a special historical session for reading objects as of a past time. */ public org.eclipse.persistence.sessions.Session acquireHistoricalSession(AsOfClause clause) throws ValidationException { if (isServerSessionBroker()) { throw ValidationException.cannotAcquireHistoricalSession(); } // ? logMessage("acquire_client_session_broker", (Object[])null); SessionBroker historicalBroker = copySessionBroker(); String sessionName; AbstractSession session; Iterator names = this.getSessionsByName().keySet().iterator(); while (names.hasNext()) { sessionName = (String) names.next(); session = getSessionForName(sessionName); historicalBroker.registerSession(sessionName, session.acquireHistoricalSession(clause)); } return historicalBroker; }
// Bug#3551263 Override getQuery(String name, Vector arguments) in Session search through // the server session broker as well public DatabaseQuery getQuery(String name, Vector arguments) { DatabaseQuery query = super.getQuery(name, arguments); if (isClientSessionBroker() && (query == null)) { String sessionName = null; AbstractSession ssession = null; Iterator names = this.getSessionsByName().keySet().iterator(); while (names.hasNext()) { sessionName = (String) names.next(); ssession = getSessionForName(sessionName); if (ssession instanceof org.eclipse.persistence.sessions.server.ClientSession) { query = ((ClientSession) ssession).getParent().getBroker().getQuery(name, arguments); if (query != null) { return query; } } } } return query; }
/** PUBLIC: Return true if the pre-defined query is defined on the session. */ public boolean containsQuery(String queryName) { boolean containsQuery = getQueries().containsKey(queryName); if (isClientSessionBroker() && (containsQuery == false)) { String sessionName = null; AbstractSession ssession = null; Iterator names = this.getSessionsByName().keySet().iterator(); while (names.hasNext()) { sessionName = (String) names.next(); ssession = getSessionForName(sessionName); if (ssession instanceof org.eclipse.persistence.sessions.server.ClientSession) { if (((ClientSession) ssession).getParent().getBroker().containsQuery(queryName)) { return true; } } } } return containsQuery; }
/** * PUBLIC: Return the query from the session pre-defined queries with the given name. This allows * for common queries to be pre-defined, reused and executed by name. */ public DatabaseQuery getQuery(String name) { // Bug#3473441 Should always call super first to ensure any change in Session will be reflected // here. DatabaseQuery query = super.getQuery(name); if (isClientSessionBroker() && (query == null)) { String sessionName = null; AbstractSession ssession = null; Iterator names = this.getSessionsByName().keySet().iterator(); while (names.hasNext()) { sessionName = (String) names.next(); ssession = getSessionForName(sessionName); if (ssession instanceof org.eclipse.persistence.sessions.server.ClientSession) { query = ((ClientSession) ssession).getParent().getBroker().getQuery(name); if (query != null) { return query; } } } } return query; }