import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.oxm.mappings.XMLDirectMapping; import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType; import org.eclipse.persistence.sessions.*; public class NullPolicySessionEventListener extends SessionEventAdapter { @Override public void preLogin(SessionEvent event) { Project project = event.getSession().getProject(); for(ClassDescriptor descriptor : project.getOrderedDescriptors()) { for(DatabaseMapping mapping : descriptor.getMappings()) { if(mapping.isAbstractDirectMapping()) { XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping; xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE); xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true); } } } } }
/** * Returns a ClassDescriptor from the given project with the matching javaClassName, * or null if not found. */ protected static ClassDescriptor getDescriptorForClassName(Project project, String javaClassName) { for (ClassDescriptor cd : project.getOrderedDescriptors()) { if (cd != null && cd.getJavaClassName().equals(javaClassName)) { return cd; } } return null; }
/** * Returns a ClassDescriptor from the given project with the matching javaClassName, * or null if not found. */ protected static ClassDescriptor getDescriptorForClassName(Project project, String javaClassName) { for (ClassDescriptor cd : project.getOrderedDescriptors()) { if (cd != null && cd.getJavaClassName().equals(javaClassName)) { return cd; } } return null; }
/** * Find a descriptor by name in the given project * used to avoid referring to descriptors by class. * This avoids having to construct a project by class facilitating weaving */ protected ClassDescriptor findDescriptor(Project project, String className){ Iterator iterator = project.getOrderedDescriptors().iterator(); while (iterator.hasNext()){ ClassDescriptor descriptor = (ClassDescriptor)iterator.next(); if (descriptor.getJavaClassName().equals(className)){ return descriptor; } } return null; }
private void storeXMLDescriptorsByQName(DatabaseSession session) { Iterator iterator = session.getProject().getOrderedDescriptors().iterator(); while (iterator.hasNext()) { XMLDescriptor xmlDescriptor = (XMLDescriptor) iterator.next(); storeXMLDescriptorByQName(xmlDescriptor); } }
/** * Returns descriptors from all sessions. * * @return descriptors from all sessions */ public List<Descriptor> getDescriptors() { List<Descriptor> descriptors = new ArrayList<Descriptor>(); List<Session> sessions = getSessions(); for (Session session : sessions) { List<Descriptor> orderedDescriptors = (List<Descriptor>) (List) session.getProject().getOrderedDescriptors(); for (Descriptor xDesc : orderedDescriptors) { descriptors.add(xDesc); } } return descriptors; }
/** * Returns descriptors from all sessions. * * @return descriptors from all sessions */ public List<Descriptor> getDescriptors() { List<Descriptor> descriptors = new ArrayList<Descriptor>(); List<Session> sessions = getSessions(); for (Session session : sessions) { List<Descriptor> orderedDescriptors = (List<Descriptor>) (List) session.getProject().getOrderedDescriptors(); for (Descriptor xDesc : orderedDescriptors) { descriptors.add(xDesc); } } return descriptors; }
/** * INTERNAL: Return the session corresponding to this XMLDescriptor. Since * the class may be mapped by more that one of the projects used to create * the XML Context, this method will return the first match. */ public AbstractSession getSession(XMLDescriptor xmlDescriptor) { if (null == xmlDescriptor) { return null; } int numberOfSessions = sessions.size(); for (int x = 0; x < numberOfSessions; x++) { AbstractSession next = ((AbstractSession) sessions.get(x)); if (next.getProject().getOrderedDescriptors().contains(xmlDescriptor)) { return next; } } throw XMLMarshalException.descriptorNotFoundInProject(xmlDescriptor.getJavaClass().getName()); }
/** * PUBLIC: * Add the descriptor to the project. */ public void addDescriptor(ClassDescriptor descriptor) { getOrderedDescriptors().add(descriptor); String alias = descriptor.getAlias(); if (alias != null) { addAlias(alias, descriptor); } // Avoid loading class definition at this point if we haven't done so yet. if ((descriptors != null) && !descriptors.isEmpty()) { getDescriptors().put(descriptor.getJavaClass(), descriptor); } }
/** * PUBLIC: * Add the descriptor to the project. */ public void addDescriptor(ClassDescriptor descriptor) { getOrderedDescriptors().add(descriptor); String alias = descriptor.getAlias(); if (alias != null) { addAlias(alias, descriptor); } // Avoid loading class definition at this point if we haven't done so yet. if ((descriptors != null) && !descriptors.isEmpty()) { getDescriptors().put(descriptor.getJavaClass(), descriptor); } }
/** * INTERNAL: Return the session corresponding to this XMLDescriptor. Since * the class may be mapped by more that one of the projects used to create * the XML Context, this method will return the first match. */ @Override protected AbstractSession getSession(XMLDescriptor xmlDescriptor) { if (null == xmlDescriptor) { return null; } if(null == sessions) { return super.getSession(xmlDescriptor); } for (int x = 0, numberOfSessions = sessions.size(); x < numberOfSessions; x++) { AbstractSession next = ((AbstractSession) sessions.get(x)); if (next.getProject().getOrderedDescriptors().contains(xmlDescriptor)) { return next; } } throw XMLMarshalException.descriptorNotFoundInProject(xmlDescriptor.getJavaClass().getName()); }
/** * PUBLIC: * Add the descriptor to the project. */ @Override public void addDescriptor(ClassDescriptor descriptor) { getOrderedDescriptors().add(descriptor); String alias = descriptor.getAlias(); if (alias != null) { addAlias(alias, descriptor); } // Avoid loading class definition at this point if we haven't done so yet. if ((descriptors != null) && !descriptors.isEmpty()) { getDescriptors().put(descriptor.getJavaClass(), descriptor); } }
Map<String, Object> properties = new HashMap<String, Object>(1); properties.put(JAXBContextProperties.SESSION_EVENT_LISTENER, new SessionEventAdapter() { @Override public void postLogin(SessionEvent event) { Project project = event.getSession().getProject(); for(ClassDescriptor descriptor : project.getOrderedDescriptors()) { for(DatabaseMapping mapping : descriptor.getMappings()) { mapping.setAttributeAccessor(new MyAttributeAccessor(mapping.getAttributeAccessor())); } } super.preLogin(event); } }); JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);
/** * INTERNAL: Used by the BuilderInterface when reading a Project from INI files. */ public void addDescriptor(ClassDescriptor descriptor, DatabaseSessionImpl session) { getOrderedDescriptors().add(descriptor); String alias = descriptor.getAlias(); if (alias != null) { addAlias(alias, descriptor); } // Avoid loading class definition at this point if we haven't done so yet. if ((descriptors != null) && !descriptors.isEmpty()) { getDescriptors().put(descriptor.getJavaClass(), descriptor); } session.initializeDescriptorIfSessionAlive(descriptor); }
public DynamicXMLMetadataSource(AbstractSession session, String packageName) { ObjectFactory objectFactory = new ObjectFactory(); xmlBindings = new XmlBindings(); xmlBindings.setPackageName(packageName); JavaTypes javaTypes = new JavaTypes(); xmlBindings.setJavaTypes(javaTypes); for (ClassDescriptor ormDescriptor : session.getProject().getOrderedDescriptors()) { String descriptorPackageName = ""; if (ormDescriptor.getJavaClassName().lastIndexOf('.') > 0) { descriptorPackageName = ormDescriptor.getJavaClassName().substring(0, ormDescriptor.getJavaClassName().lastIndexOf('.')); } if (descriptorPackageName.equals(packageName)) { javaTypes.getJavaType().add(createJAXBType(ormDescriptor, objectFactory)); } } }
public DynamicXMLMetadataSource(AbstractSession session, String packageName) { ObjectFactory objectFactory = new ObjectFactory(); xmlBindings = new XmlBindings(); xmlBindings.setPackageName(packageName); JavaTypes javaTypes = new JavaTypes(); xmlBindings.setJavaTypes(javaTypes); for (ClassDescriptor ormDescriptor : session.getProject().getOrderedDescriptors()) { String descriptorPackageName = ""; if (ormDescriptor.getJavaClassName().lastIndexOf('.') > 0) { descriptorPackageName = ormDescriptor.getJavaClassName().substring(0, ormDescriptor.getJavaClassName().lastIndexOf('.')); } if (descriptorPackageName.equals(packageName)) { javaTypes.getJavaType().add(createJAXBType(ormDescriptor, objectFactory)); } } }
private void setupDocumentPreservationPolicy(DatabaseSession session) { XMLLogin login = (XMLLogin) session.getDatasourceLogin(); if (login.getDocumentPreservationPolicy() == null) { Iterator iterator = session.getProject().getOrderedDescriptors().iterator(); while (iterator.hasNext()) { Descriptor xmlDescriptor = (Descriptor) iterator.next(); if (xmlDescriptor.shouldPreserveDocument()) { login.setDocumentPreservationPolicy(new DescriptorLevelDocumentPreservationPolicy()); break; } } } if (login.getDocumentPreservationPolicy() == null) { login.setDocumentPreservationPolicy(new NoDocumentPreservationPolicy()); } login.getDocumentPreservationPolicy().initialize(context); if (login.getDocumentPreservationPolicy().shouldPreserveDocument() && !hasDocumentPreservation) { hasDocumentPreservation = true; } }
/** * Remove a dynamic type from the system. * * This implementation assumes that the dynamic type has no relationships to * it and that it is not involved in an inheritance relationship. If there * are concurrent processes using this type when it is removed some * exceptions may occur. * * @param session * @param typeName */ public void removeType(String typeName) { DynamicType type = getType(typeName); if (type != null) { getSession().getIdentityMapAccessor().initializeIdentityMap(type.getJavaClass()); ClassDescriptor descriptor = type.getDescriptor(); getSession().getProject().getOrderedDescriptors().remove(descriptor); getSession().getProject().getDescriptors().remove(type.getJavaClass()); } }
protected NonreflectiveMethodDefinition buildConstructor() { NonreflectiveMethodDefinition methodDefinition = new NonreflectiveMethodDefinition(); methodDefinition.setName(getClassName()); methodDefinition.setIsConstructor(true); methodDefinition.addLine("setName(\"" + getProject().getName() + "\");"); methodDefinition.addLine("applyLogin();"); if (!getProject().getDefaultReadOnlyClasses().isEmpty()) { methodDefinition.addLine("setDefaultReadOnlyClasses(buildDefaultReadOnlyClasses());"); } methodDefinition.addLine(""); // Sort by name. List<ClassDescriptor> descriptors = buildSortedListOfDescriptors(getProject().getOrderedDescriptors()); for (ClassDescriptor descriptor : descriptors) { // Singleton interface descriptors should not exist. if (!(descriptor.isDescriptorForInterface() && (descriptor.getInterfacePolicy().getImplementorDescriptor() != null))) { methodDefinition.addLine("addDescriptor(build" + getDescriptorMethodNames().get(descriptor) + "ClassDescriptor());"); } } return methodDefinition; }
protected NonreflectiveMethodDefinition buildConstructor() { NonreflectiveMethodDefinition methodDefinition = new NonreflectiveMethodDefinition(); methodDefinition.setName(getClassName()); methodDefinition.setIsConstructor(true); methodDefinition.addLine("setName(\"" + getProject().getName() + "\");"); methodDefinition.addLine("applyLogin();"); if (!getProject().getDefaultReadOnlyClasses().isEmpty()) { methodDefinition.addLine("setDefaultReadOnlyClasses(buildDefaultReadOnlyClasses());"); } methodDefinition.addLine(""); // Sort by name. List<ClassDescriptor> descriptors = buildSortedListOfDescriptors(getProject().getOrderedDescriptors()); for (ClassDescriptor descriptor : descriptors) { // Singleton interface descriptors should not exist. if (!(descriptor.isDescriptorForInterface() && (descriptor.getInterfacePolicy().getImplementorDescriptor() != null))) { methodDefinition.addLine("addDescriptor(build" + getDescriptorMethodNames().get(descriptor) + "ClassDescriptor());"); } } return methodDefinition; }