Search in sources :

Example 1 with EclipseLinkObjectPersistenceRuntimeXMLProject

use of org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject in project eclipselink by eclipse-ee4j.

the class XMLProjectReader method read.

/**
 * PUBLIC:
 * Read the EclipseLink project deployment XML from the reader on the file.
 * Note the class loader must be able to resolve the domain classes.
 * Note the file must be the deployment XML, not the Mapping Workbench project file.
 * This API supports 10g (10.0.3), 11g (11.1.1) formats.
 */
public static Project read(Reader reader, ClassLoader classLoader) {
    // Since a reader is pass and it can only be streamed once (mark does not work)
    // It must be first read into a buffer so multiple readers can be used to
    // determine the format.  This does not effect performance severely.
    StringWriter writer;
    Document document;
    try {
        writer = new StringWriter(4096);
        char[] c = new char[4096];
        int r = 0;
        while ((r = reader.read(c)) != -1) {
            writer.write(c, 0, r);
        }
        String schema = null;
        if (shouldUseSchemaValidation()) {
            schema = SCHEMA_DIR + ECLIPSELINK_SCHEMA;
        }
        // Assume the format is OPM parse the document with OPM validation on.
        XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
        XMLParser parser = createXMLParser(xmlPlatform, true, false, schema);
        try {
            document = parser.parse(new StringReader(writer.toString()));
        } catch (Exception parseException) {
            // If the parse fails, it may be because the format was EclipseLink 1.0
            try {
                if (shouldUseSchemaValidation()) {
                    schema = SCHEMA_DIR + ECLIPSELINK_1_0_SCHEMA;
                }
                parser = createXMLParser(xmlPlatform, true, false, schema);
                document = parser.parse(new StringReader(writer.toString()));
            } catch (Exception parseException2) {
                // If the parse fails, it may be because the format was 11.1.1
                try {
                    if (shouldUseSchemaValidation()) {
                        schema = SCHEMA_DIR + TOPLINK_11_SCHEMA;
                    }
                    parser = createXMLParser(xmlPlatform, true, false, schema);
                    document = parser.parse(new StringReader(writer.toString()));
                } catch (Exception parseException3) {
                    // not support in eclipselink, just not valid, through original exception.
                    throw parseException;
                }
                String version = document.getDocumentElement().getAttribute("version");
                // If 10.1.3 format use old format read.
                if ((version == null) || (version.indexOf("1.0") == -1)) {
                    throw parseException;
                }
            }
        }
    } catch (Exception exception) {
        throw XMLMarshalException.unmarshalException(exception);
    }
    String version = document.getDocumentElement().getAttribute("version");
    // If 10.1.3 format use old format read.
    if (version != null) {
        if (version.indexOf("10.1.3") != -1) {
            return read1013Format(document, classLoader);
        } else if (version.indexOf("11.1.1") != -1) {
            return read1111Format(document, classLoader);
        }
        if (version.indexOf("TopLink") != -1) {
            // default to read 11.1.1
            return read1111Format(document, classLoader);
        }
    }
    if (project == null) {
        project = new EclipseLinkObjectPersistenceRuntimeXMLProject();
    }
    // bug261072: clone the project since readObjectPersistenceRuntimeFormat will change its datasourceLogin and Classloader
    return readObjectPersistenceRuntimeFormat(document, classLoader, project.clone());
}
Also used : StringWriter(java.io.StringWriter) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) StringReader(java.io.StringReader) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) Document(org.w3c.dom.Document) XMLParser(org.eclipse.persistence.platform.xml.XMLParser) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with EclipseLinkObjectPersistenceRuntimeXMLProject

use of org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject in project eclipselink by eclipse-ee4j.

the class XRSessionsFactory method loadProjectConfig.

@Override
protected Project loadProjectConfig(ProjectConfig projectConfig) {
    Project project = null;
    String projectString = projectConfig.getProjectString();
    if (projectConfig.isProjectClassConfig()) {
        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                project = (Project) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(m_classLoader.loadClass(projectString)));
            } else {
                project = (Project) PrivilegedAccessHelper.newInstanceFromClass(m_classLoader.loadClass(projectString));
            }
        } catch (Throwable exception) {
            throw SessionLoaderException.failedToLoadProjectClass(projectString, exception);
        }
    } else {
        try {
            URL url = null;
            Matcher matcher = matchDBWSOrProject.matcher(projectString);
            if (matcher.find()) {
                // need to try a variety of URLs to find DBWS OR/OX Project
                String orXml = matcher.group();
                for (String prefix : META_INF_PATHS) {
                    String searchPath = prefix + orXml;
                    url = m_classLoader.getResource(searchPath);
                    if (url != null) {
                        break;
                    }
                }
            } else {
                matcher = matchDBWSOxProject.matcher(projectString);
                if (matcher.find()) {
                    String oxXml = matcher.group();
                    for (String prefix : META_INF_PATHS) {
                        String searchPath = prefix + oxXml;
                        url = m_classLoader.getResource(searchPath);
                        if (url != null) {
                            break;
                        }
                    }
                }
            }
            if (url != null) {
                Project p = new EclipseLinkObjectPersistenceRuntimeXMLProject();
                XMLLogin xmlLogin = new XMLLogin();
                xmlLogin.setDatasourcePlatform(new DOMPlatform());
                p.setDatasourceLogin(xmlLogin);
                if (m_classLoader != null) {
                    p.getDatasourceLogin().getDatasourcePlatform().getConversionManager().setLoader(m_classLoader);
                }
                XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
                XMLParser parser = xmlPlatform.newXMLParser();
                parser.setNamespaceAware(true);
                parser.setWhitespacePreserving(false);
                XMLContext context = new XMLContext(p);
                context.getSession(Project.class).getEventManager().addListener(new MissingDescriptorListener());
                XMLUnmarshaller unmarshaller = context.createUnmarshaller();
                project = (Project) unmarshaller.unmarshal(url);
            }
        } catch (ValidationException validationException) {
            if (validationException.getErrorCode() == ValidationException.PROJECT_XML_NOT_FOUND) {
                throw SessionLoaderException.failedToLoadProjectXml(projectString, validationException);
            } else {
                throw SessionLoaderException.failedToParseXML(projectString, validationException);
            }
        }
    }
    return project;
}
Also used : DOMPlatform(org.eclipse.persistence.oxm.platform.DOMPlatform) ValidationException(org.eclipse.persistence.exceptions.ValidationException) Matcher(java.util.regex.Matcher) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) MissingDescriptorListener(org.eclipse.persistence.internal.sessions.factories.MissingDescriptorListener) URL(java.net.URL) Project(org.eclipse.persistence.sessions.Project) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLParser(org.eclipse.persistence.platform.xml.XMLParser)

Example 3 with EclipseLinkObjectPersistenceRuntimeXMLProject

use of org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject in project eclipselink by eclipse-ee4j.

the class KeyMappingsTestSuite method setUp.

@BeforeClass
public static void setUp() {
    final String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE);
    if ("true".equalsIgnoreCase(ddlCreateProp)) {
        ddlCreate = true;
    }
    final String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP);
    if ("true".equalsIgnoreCase(ddlDropProp)) {
        ddlDrop = true;
    }
    final String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG);
    if ("true".equalsIgnoreCase(ddlDebugProp)) {
        ddlDebug = true;
    }
    final String username = System.getProperty(DATABASE_USERNAME_KEY);
    if (username == null) {
        fail("error retrieving database username");
    }
    final String password = System.getProperty(DATABASE_PASSWORD_KEY);
    if (password == null) {
        fail("error retrieving database password");
    }
    final String url = System.getProperty(DATABASE_URL_KEY);
    if (url == null) {
        fail("error retrieving database url");
    }
    final String driver = System.getProperty(DATABASE_DRIVER_KEY);
    if (driver == null) {
        fail("error retrieving database driver");
    }
    XRServiceFactory factory = new XRServiceFactory() {

        @Override
        public XRServiceAdapter buildService(XRServiceModel xrServiceModel) {
            parentClassLoader = this.getClass().getClassLoader();
            xrSchemaStream = new ByteArrayInputStream(KEYMAPPINGS_SCHEMA.getBytes());
            return super.buildService(xrServiceModel);
        }

        @Override
        public void buildSessions() {
            XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
            XMLContext context = new XMLContext(new EclipseLinkObjectPersistenceRuntimeXMLProject(), xrdecl);
            XMLUnmarshaller unmarshaller = context.createUnmarshaller();
            Project orProject = (Project) unmarshaller.unmarshal(new StringReader(KEYMAPPINGS_OR_PROJECT));
            DatasourceLogin login = new DatabaseLogin();
            login.setUserName(username);
            login.setPassword(password);
            ((DatabaseLogin) login).setConnectionString(url);
            ((DatabaseLogin) login).setDriverClassName(driver);
            Platform platform = new MySQLPlatform();
            ConversionManager conversionManager = platform.getConversionManager();
            if (conversionManager != null) {
                conversionManager.setLoader(xrdecl);
            }
            login.setDatasourcePlatform(platform);
            ((DatabaseLogin) login).bindAllParameters();
            orProject.setDatasourceLogin(login);
            Project oxProject = (Project) unmarshaller.unmarshal(new StringReader(KEYMAPPINGS_OX_PROJECT));
            login = (DatasourceLogin) oxProject.getDatasourceLogin();
            if (login != null) {
                platform = login.getDatasourcePlatform();
                if (platform != null) {
                    conversionManager = platform.getConversionManager();
                    if (conversionManager != null) {
                        conversionManager.setLoader(xrdecl);
                    }
                }
            }
            ProjectHelper.fixOROXAccessors(orProject, oxProject);
            xrService.setORSession(orProject.createDatabaseSession());
            xrService.getORSession().dontLogMessages();
            xrService.setXMLContext(new XMLContext(oxProject));
            xrService.setOXSession(xrService.getXMLContext().getSession(0));
        }
    };
    XMLContext context = new XMLContext(new DBWSModelProject());
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    DBWSModel model = (DBWSModel) unmarshaller.unmarshal(new StringReader(KEYMAPPINGS_DBWS));
    xrService = factory.buildService(model);
    if (ddlCreate) {
        try {
            AllTests.runDdl(CREATE_DDL, ddlDebug);
        } catch (Exception e) {
        // e.printStackTrace();
        }
    }
}
Also used : DatasourceLogin(org.eclipse.persistence.sessions.DatasourceLogin) DBWSModel(org.eclipse.persistence.dbws.DBWSModel) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) MySQLPlatform(org.eclipse.persistence.platform.database.MySQLPlatform) XMLContext(org.eclipse.persistence.oxm.XMLContext) XRDynamicClassLoader(org.eclipse.persistence.internal.xr.XRDynamicClassLoader) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) Project(org.eclipse.persistence.sessions.Project) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XRServiceFactory(org.eclipse.persistence.internal.xr.XRServiceFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) XRServiceModel(org.eclipse.persistence.internal.xr.XRServiceModel) StringReader(java.io.StringReader) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) MySQLPlatform(org.eclipse.persistence.platform.database.MySQLPlatform) BeforeClass(org.junit.BeforeClass)

Example 4 with EclipseLinkObjectPersistenceRuntimeXMLProject

use of org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject in project eclipselink by eclipse-ee4j.

the class RelationshipsTestSuite method setUp.

@BeforeClass
public static void setUp() {
    final String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE);
    if ("true".equalsIgnoreCase(ddlCreateProp)) {
        ddlCreate = true;
    }
    final String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP);
    if ("true".equalsIgnoreCase(ddlDropProp)) {
        ddlDrop = true;
    }
    final String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG);
    if ("true".equalsIgnoreCase(ddlDebugProp)) {
        ddlDebug = true;
    }
    final String username = System.getProperty(DATABASE_USERNAME_KEY);
    if (username == null) {
        fail("error retrieving database username");
    }
    final String password = System.getProperty(DATABASE_PASSWORD_KEY);
    if (password == null) {
        fail("error retrieving database password");
    }
    final String url = System.getProperty(DATABASE_URL_KEY);
    if (url == null) {
        fail("error retrieving database url");
    }
    final String driver = System.getProperty(DATABASE_DRIVER_KEY);
    if (driver == null) {
        fail("error retrieving database driver");
    }
    XRServiceFactory factory = new XRServiceFactory() {

        @Override
        public XRServiceAdapter buildService(XRServiceModel xrServiceModel) {
            parentClassLoader = this.getClass().getClassLoader();
            xrSchemaStream = new ByteArrayInputStream(RELATIONSHIPS_SCHEMA.getBytes());
            return super.buildService(xrServiceModel);
        }

        @Override
        public void buildSessions() {
            XMLContext context = new XMLContext(new EclipseLinkObjectPersistenceRuntimeXMLProject());
            XMLUnmarshaller unmarshaller = context.createUnmarshaller();
            Project orProject = (Project) unmarshaller.unmarshal(new StringReader(RELATIONSHIPS_OR_PROJECT));
            DatabaseLogin login = new DatabaseLogin();
            login.setUserName(username);
            login.setPassword(password);
            login.setConnectionString(url);
            login.setDriverClassName(driver);
            Platform platform = new MySQLPlatform();
            login.setDatasourcePlatform(platform);
            login.bindAllParameters();
            orProject.setDatasourceLogin(login);
            Project oxProject = (Project) unmarshaller.unmarshal(new StringReader(RELATIONSHIPS_OX_PROJECT));
            ProjectHelper.fixOROXAccessors(orProject, oxProject);
            xrService.setORSession(orProject.createDatabaseSession());
            xrService.getORSession().dontLogMessages();
            xrService.setXMLContext(new XMLContext(oxProject));
            xrService.setOXSession(xrService.getXMLContext().getSession(0));
        }
    };
    XMLContext context = new XMLContext(new DBWSModelProject());
    XMLUnmarshaller unmarshaller = context.createUnmarshaller();
    DBWSModel model = (DBWSModel) unmarshaller.unmarshal(new StringReader(RELATIONSHIPS_DBWS));
    xrService = factory.buildService(model);
    if (ddlCreate) {
        try {
            AllTests.runDdl(CREATE_DDL, ddlDebug);
        } catch (Exception e) {
        // e.printStackTrace();
        }
    }
}
Also used : DBWSModel(org.eclipse.persistence.dbws.DBWSModel) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) Platform(org.eclipse.persistence.internal.databaseaccess.Platform) MySQLPlatform(org.eclipse.persistence.platform.database.MySQLPlatform) XMLContext(org.eclipse.persistence.oxm.XMLContext) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) Project(org.eclipse.persistence.sessions.Project) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XRServiceFactory(org.eclipse.persistence.internal.xr.XRServiceFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) XRServiceModel(org.eclipse.persistence.internal.xr.XRServiceModel) StringReader(java.io.StringReader) EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) MySQLPlatform(org.eclipse.persistence.platform.database.MySQLPlatform) BeforeClass(org.junit.BeforeClass)

Aggregations

EclipseLinkObjectPersistenceRuntimeXMLProject (org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject)4 XMLPlatform (org.eclipse.persistence.platform.xml.XMLPlatform)4 StringReader (java.io.StringReader)3 XMLContext (org.eclipse.persistence.oxm.XMLContext)3 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)3 Project (org.eclipse.persistence.sessions.Project)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DBWSModel (org.eclipse.persistence.dbws.DBWSModel)2 DBWSModelProject (org.eclipse.persistence.dbws.DBWSModelProject)2 ValidationException (org.eclipse.persistence.exceptions.ValidationException)2 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)2 XRServiceFactory (org.eclipse.persistence.internal.xr.XRServiceFactory)2 XRServiceModel (org.eclipse.persistence.internal.xr.XRServiceModel)2 MySQLPlatform (org.eclipse.persistence.platform.database.MySQLPlatform)2 XMLParser (org.eclipse.persistence.platform.xml.XMLParser)2 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)2 BeforeClass (org.junit.BeforeClass)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1