/**
  * Create a list of the entities that will be deployed. This list is built from the information
  * provided in the PersistenceUnitInfo argument. The list contains Classes specified in the
  * PersistenceUnitInfo's class list and also files that are annotated with @Entity and @Embeddable
  * in the jar files provided in the persistence info. This list of classes will used to build a
  * deployment project and to decide what classes to weave.
  */
 public static Collection<MetadataClass> buildEntityList(
     MetadataProcessor processor, ClassLoader loader) {
   ArrayList<MetadataClass> entityList = new ArrayList<MetadataClass>();
   for (String className : processor.getProject().getWeavableClassNames()) {
     entityList.add(processor.getMetadataFactory().getMetadataClass(className));
   }
   return entityList;
 }
  /** Process the Object/relational metadata from XML and annotations */
  public static void processORMetadata(
      MetadataProcessor processor, boolean throwExceptionOnFail, Mode mode) {
    if (mode == Mode.ALL || mode == Mode.COMPOSITE_MEMBER_INITIAL) {
      // DO NOT CHANGE the order of invocation of various methods.

      // 1 - Load the list of mapping files for the persistence unit. Need to
      // do this before we start processing entities as the list of entity
      // classes depend on metadata read from mapping files.
      processor.loadMappingFiles(throwExceptionOnFail);
    }

    // 2 - Process each XML entity mappings file metadata (except for
    // the actual classes themselves). This method is also responsible
    // for handling any XML merging.
    processor.processEntityMappings(mode);

    // 3 - Process the persistence unit classes (from XML and annotations)
    // and their metadata now.
    processor.processORMMetadata(mode);
  }
  @Override
  public void buildSessions() {
    XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
    DatasourceLogin login = new DatabaseLogin();
    login.setUserName(username);
    login.setPassword(password);
    ((DatabaseLogin) login).setConnectionString(url);
    ((DatabaseLogin) login).setDriverClassName(DATABASE_PLATFORM);
    Platform platform = builder.getDatabasePlatform();
    ConversionManager conversionManager = platform.getConversionManager();
    if (conversionManager != null) {
      conversionManager.setLoader(xrdecl);
    }
    login.setDatasourcePlatform(platform);
    ((DatabaseLogin) login).bindAllParameters();
    ((DatabaseLogin) login).setUsesStreamsForBinding(true);

    Project orProject = null;
    if (DBWS_OR_STREAM.size() != 0) {
      MetadataProcessor processor =
          new MetadataProcessor(
              new XRPersistenceUnitInfo(xrdecl),
              new DatabaseSessionImpl(login),
              xrdecl,
              false,
              true,
              false,
              false,
              false,
              null,
              null);
      processor.setMetadataSource(
          new JPAMetadataSource(xrdecl, new StringReader(DBWS_OR_STREAM.toString())));
      PersistenceUnitProcessor.processORMetadata(
          processor, true, PersistenceUnitProcessor.Mode.ALL);
      processor.addNamedQueries();
      orProject = processor.getProject().getProject();
    } else {
      orProject = new Project();
    }
    orProject.setName(builder.getProjectName().concat(OR_PRJ_SUFFIX));
    orProject.setDatasourceLogin(login);
    DatabaseSession databaseSession = orProject.createDatabaseSession();
    if ("off".equalsIgnoreCase(builder.getLogLevel())) {
      databaseSession.dontLogMessages();
    } else {
      databaseSession.setLogLevel(
          AbstractSessionLog.translateStringToLoggingLevel(builder.getLogLevel()));
    }
    xrService.setORSession(databaseSession);
    orProject.convertClassNamesToClasses(xrdecl);

    Project oxProject = null;
    Map<String, OXMMetadataSource> metadataMap = new HashMap<String, OXMMetadataSource>();
    StreamSource xml = new StreamSource(new StringReader(DBWS_OX_STREAM.toString()));
    try {
      JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
      Unmarshaller unmarshaller = jc.createUnmarshaller();

      JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
      XmlBindingsModel model = jaxbElt.getValue();
      for (XmlBindings xmlBindings : model.getBindingsList()) {
        metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
      }
    } catch (JAXBException jaxbex) {
      jaxbex.printStackTrace();
    }

    Map<String, Map<String, OXMMetadataSource>> properties =
        new HashMap<String, Map<String, OXMMetadataSource>>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
    try {
      org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext jCtx =
          org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromOXM(
              parentClassLoader, properties);
      oxProject = jCtx.getXMLContext().getSession(0).getProject();
    } catch (JAXBException e) {
      e.printStackTrace();
    }
    ((XMLLogin) oxProject.getDatasourceLogin()).setPlatformClassName(DOM_PLATFORM_CLASSNAME);
    ((XMLLogin) oxProject.getDatasourceLogin()).setEqualNamespaceResolvers(false);

    prepareDescriptors(oxProject, orProject, xrdecl);
    ProjectHelper.fixOROXAccessors(orProject, oxProject);
    xrService.setORSession(databaseSession);
    xrService.setXMLContext(new XMLContext(oxProject));
    xrService.setOXSession(xrService.getXMLContext().getSession(0));
  }