Skip to content

Commit

Permalink
JNDI: persistence modules vs. SimpleJndiName
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Nov 14, 2022
1 parent 07f6cb5 commit f3aad33
Show file tree
Hide file tree
Showing 21 changed files with 1,131 additions and 1,044 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,26 +15,20 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

/*
* PersistenceManagerFactory.java
*
* Created on February 25, 2000
*/

package com.sun.jdo.api.persistence.support;

import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Properties;

import org.glassfish.api.naming.SimpleJndiName;

/**
*
* @author Craig Russell
* @version 0.1
* @author Craig Russell 2000
*/
public interface PersistenceManagerFactory extends Serializable {

public interface PersistenceManagerFactory extends java.io.Serializable
{
/**
/**
* Sets JDBC driver name
* @param driverName JDBC driver name
*/
Expand Down Expand Up @@ -214,13 +209,13 @@ public interface PersistenceManagerFactory extends java.io.Serializable
* Sets ConnectionFactory name
* @param connectionFactoryName ConnectionFactory name
*/
void setConnectionFactoryName (String connectionFactoryName);
void setConnectionFactoryName(SimpleJndiName connectionFactoryName);

/**
* Returns ConnectionFactory name
* @return ConnectionFactoryName
*/
String getConnectionFactoryName ();
SimpleJndiName getConnectionFactoryName ();

/**
* Sets Identifier. An Identifier is a string that user can use to identify
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -19,7 +20,9 @@
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper;
import com.sun.jdo.spi.persistence.utility.logging.Logger;

import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl;
import org.glassfish.persistence.common.DatabaseConstants;
import org.glassfish.persistence.common.Java2DBProcessorHelper;
Expand All @@ -34,9 +37,9 @@ public class CMPProcessor {

private static Logger logger = LogHelperEJBCompiler.getLogger();

private Java2DBProcessorHelper helper = null;
private Java2DBProcessorHelper helper;

private DeploymentContext ctx;
private final DeploymentContext ctx;

/**
* Creates a new instance of CMPProcessor
Expand All @@ -55,15 +58,14 @@ public void process() {
ResourceReferenceDescriptor cmpResource = bundle.getCMPResourceReference();

// If this bundle's beans are not created by Java2DB, there is nothing to do.
if (!DeploymentHelper.isJavaToDatabase(
cmpResource.getSchemaGeneratorProperties())) {
if (!DeploymentHelper.isJavaToDatabase(cmpResource.getSchemaGeneratorProperties())) {
return;
}

helper = new Java2DBProcessorHelper(ctx);
helper.init();

String resourceName = cmpResource.getJndiName();
SimpleJndiName resourceName = cmpResource.getJndiName();
helper.setProcessorType("CMP", bundle.getName()); // NOI18N
helper.setJndiName(resourceName, bundle.getName());

Expand All @@ -74,10 +76,7 @@ public void process() {

boolean userDropTables = cmpResource.isDropTablesAtUndeploy();

if (logger.isLoggable(logger.FINE)) {
logger.fine("ejb.CMPProcessor.createanddroptables", //NOI18N
new Object[] {new Boolean(createTables), new Boolean(userDropTables)});
}
logger.fine("ejb.CMPProcessor.createanddroptables", new Object[] {createTables, userDropTables});

if (!createTables && !userDropTables) {
// Nothing to do.
Expand All @@ -88,7 +87,7 @@ public void process() {
helper.setDropTablesValue(userDropTables, bundle.getName());

constructJdbcFileNames(bundle);
if (logger.isLoggable(logger.FINE)) {
if (logger.isLoggable(Logger.FINE)) {
logger.fine("ejb.CMPProcessor.createanddropfilenames",
helper.getCreateJdbcFileName(bundle.getName()),
helper.getDropJdbcFileName(bundle.getName()));
Expand Down Expand Up @@ -120,11 +119,8 @@ public void clean() {
*/
private void constructJdbcFileNames(EjbBundleDescriptorImpl ejbBundle) {
String filePrefix = DeploymentHelper.getDDLNamePrefix(ejbBundle);

helper.setCreateJdbcFileName(filePrefix + DatabaseConstants.CREATE_DDL_JDBC_FILE_SUFFIX,
ejbBundle.getName());
helper.setDropJdbcFileName(filePrefix + DatabaseConstants.DROP_DDL_JDBC_FILE_SUFFIX,
ejbBundle.getName());
helper.setCreateJdbcFileName(filePrefix + DatabaseConstants.CREATE_DDL_JDBC_FILE_SUFFIX, ejbBundle.getName());
helper.setDropJdbcFileName(filePrefix + DatabaseConstants.DROP_DDL_JDBC_FILE_SUFFIX, ejbBundle.getName());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,14 +15,27 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

/*
* MappingGenerator.java
*
* Created on Aug 18, 2003
*/

package com.sun.jdo.spi.persistence.support.ejb.ejbc;

import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper;
import com.sun.jdo.api.persistence.mapping.ejb.ConversionException;
import com.sun.jdo.api.persistence.mapping.ejb.MappingFile;
import com.sun.jdo.api.persistence.mapping.ejb.beans.CmpFieldMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.CmrFieldMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.ColumnPair;
import com.sun.jdo.api.persistence.mapping.ejb.beans.EntityMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings;
import com.sun.jdo.api.persistence.model.Model;
import com.sun.jdo.api.persistence.model.ModelException;
import com.sun.jdo.api.persistence.model.mapping.MappingClassElement;
import com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator;
import com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException;
import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper;
import com.sun.jdo.spi.persistence.utility.StringHelper;
import com.sun.jdo.spi.persistence.utility.logging.Logger;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -41,27 +55,10 @@
import java.util.ResourceBundle;
import java.util.Set;

import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper;
import com.sun.jdo.api.persistence.mapping.ejb.ConversionException;
import com.sun.jdo.api.persistence.mapping.ejb.MappingFile;
import com.sun.jdo.api.persistence.mapping.ejb.beans.CmpFieldMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.CmrFieldMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.ColumnPair;
import com.sun.jdo.api.persistence.mapping.ejb.beans.EntityMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping;
import com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings;
import com.sun.jdo.api.persistence.model.Model;
import com.sun.jdo.api.persistence.model.ModelException;
import com.sun.jdo.api.persistence.model.mapping.MappingClassElement;
import com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator;
import com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException;
import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper;
import com.sun.jdo.spi.persistence.utility.StringHelper;
import com.sun.jdo.spi.persistence.utility.logging.Logger;
import org.glassfish.api.ActionReport;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl;
import org.glassfish.persistence.common.DatabaseConstants;
import org.glassfish.persistence.common.I18NHelper;
Expand Down Expand Up @@ -152,9 +149,10 @@ public SchemaElement generateMapping(
Schema2BeansException, SQLException, GeneratorException,
ConversionException {

SchemaElement schema = null;
if (ctx == null)
if (ctx == null) {
isVerifyFlag = true;
return null;
}
File cmpMappingFile = getSunCmpMappingFile(inputFilesPath);
boolean mappedBeans = !ignoreSunDeploymentDescriptors
&& cmpMappingFile.exists();
Expand All @@ -175,6 +173,7 @@ public SchemaElement generateMapping(
Results deploymentArguments = getDeploymentArguments(
ctx, cmpResource, mustHaveDBVendorName);
dbVendorName = deploymentArguments.getDatabaseVendorName();
SchemaElement schema = null;
if (mappedBeans) {
// If sun-cmp-mappings.xml exists and we are doing a deployment,
// validate some arguments and make sure we have dbschema.
Expand Down Expand Up @@ -249,8 +248,7 @@ public SchemaElement generateMapping(
+ cmpResource.getJndiName()
+ ", isJavaToDatabase=" + isJavaToDatabaseFlag); // NOI18N
}
}
else {
} else {
// Generate mapping file and dbschema, since either
// sun-cmp-mappings.xml does not exist (e.g. user didn't yet map)
// or DeploymentContext is null (e.g. running under auspices of AVK).
Expand Down Expand Up @@ -343,16 +341,18 @@ private SunCmpMappings getSunCmpMappings(File cmpMappingFile)
try {
is.close();
} catch(Exception ex) {
if (logger.isLoggable(Logger.FINE))
if (logger.isLoggable(Logger.FINE)) {
logger.fine(ex.toString());
}
}
}
if (iasMapping != null) {
try {
iasMapping.close();
} catch(Exception ex) {
if (logger.isLoggable(Logger.FINE))
if (logger.isLoggable(Logger.FINE)) {
logger.fine(ex.toString());
}
}
}
}
Expand Down Expand Up @@ -383,8 +383,9 @@ private static File getSunCmpMappingFile(String filesPath) {
String dirs = cmpMappingFile.substring(
0, cmpMappingFile.lastIndexOf(File.separatorChar));
File fileDirs = new File(dirs);
if (!fileDirs.exists())
if (!fileDirs.exists()) {
fileDirs.mkdirs();
}
}

return new File(cmpMappingFile);
Expand Down Expand Up @@ -429,8 +430,9 @@ private void writeSunCmpMappingFile(Set mappingClasses, File cmpMappingFile)
sunCmpMapping.close();
}
} catch (IOException ex) {
if (logger.isLoggable(Logger.FINE))
if (logger.isLoggable(Logger.FINE)) {
logger.fine(ex.toString());
}
}
}
}
Expand All @@ -457,8 +459,9 @@ private static void writeSchemaFile(SchemaElement schema, File filePath)
schemaStream.close();
}
} catch (IOException ex) {
if (logger.isLoggable(Logger.FINE))
logger.fine(ex.toString());
if (logger.isLoggable(Logger.FINE)) {
logger.fine(ex.toString());
}
}
}
}
Expand Down Expand Up @@ -570,8 +573,7 @@ private Results getDeploymentArguments(
// cmp-resource, try to get the dbvendorname from the database.
if (null == dbVendorName && connectToDatabase) {
try {
Connection conn = DeploymentHelper.getConnection(
cmpResource.getJndiName());
Connection conn = DeploymentHelper.getConnection(cmpResource.getJndiName());
dbVendorName = conn.getMetaData().getDatabaseProductName();
} catch (Exception ex) {
// Ignore exceptions and use default.
Expand Down Expand Up @@ -624,7 +626,7 @@ private ResourceReferenceDescriptor checkOrCreateCMPResource(

// In JavaToDB case we can deploy to the default jdbc-resource.
cmpResource = new ResourceReferenceDescriptor();
cmpResource.setJndiName("jdbc/__default");
cmpResource.setJndiName(new SimpleJndiName("jdbc/__default"));
cmpResource.setDatabaseVendorName(DBVendorTypeHelper.DERBY);
cmpResource.setCreateTablesAtDeploy(true);
cmpResource.setDropTablesAtUndeploy(true);
Expand Down Expand Up @@ -717,10 +719,11 @@ private void ensureDBSchemaExistence(
SchemaElement schemaElement = new SchemaElement(outSchemaImpl);
schemaElement.setName(DBIdentifier.create(generatedSchemaName));

if(dmd.getDatabaseProductName().compareToIgnoreCase("MYSQL") == 0)
if(dmd.getDatabaseProductName().compareToIgnoreCase("MYSQL") == 0) {
outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), true);
else
} else {
outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), false);
}
outstream = new FileOutputStream(
new File(classout,
new StringBuffer(generatedSchemaName)
Expand All @@ -742,8 +745,9 @@ private void ensureDBSchemaExistence(
outstream.close();
}
} catch (IOException ex) {
if (logger.isLoggable(Logger.FINE))
if (logger.isLoggable(Logger.FINE)) {
logger.fine(ex.toString());
}
}
}
}
Expand All @@ -758,30 +762,30 @@ private void ensureDBSchemaExistence(
*/
private void addAllTables(SunCmpMapping sunCmpMapping, Set tables) {
EntityMapping[] beans = sunCmpMapping.getEntityMapping();
for (int i = 0; i < beans.length; i++) {
for (EntityMapping bean : beans) {
// Always add the table name.
addTableName(beans[i].getTableName(), tables);
addTableName(bean.getTableName(), tables);

// Check if there are table names specified in the
// cmp-field-mapping.
CmpFieldMapping[] cmpfields = beans[i].getCmpFieldMapping();
for (int j = 0; j < cmpfields.length; j++) {
CmpFieldMapping[] cmpfields = bean.getCmpFieldMapping();
for (CmpFieldMapping cmpfield : cmpfields) {
// There might be more than one column-name for each cmp field.
String[] names = cmpfields[j].getColumnName();
for (int jj = 0; jj < names.length; jj++) {
addRelatedTableName(names[jj], tables);
String[] names = cmpfield.getColumnName();
for (String name : names) {
addRelatedTableName(name, tables);
}
}

// Check the table names specified in the cmr-field-mapping.
CmrFieldMapping[] cmrfields = beans[i].getCmrFieldMapping();
for (int j = 0; j < cmrfields.length; j++) {
CmrFieldMapping[] cmrfields = bean.getCmrFieldMapping();
for (CmrFieldMapping cmrfield : cmrfields) {
// There might be more than one column-pair for each cmr field.
ColumnPair[] pairs = cmrfields[j].getColumnPair();
for (int jj = 0; jj < pairs.length; jj++) {
String[] names = pairs[jj].getColumnName();
for (int jjj = 0; jjj < names.length; jjj++) {
addRelatedTableName(names[jjj], tables);
ColumnPair[] pairs = cmrfield.getColumnPair();
for (ColumnPair pair : pairs) {
String[] names = pair.getColumnName();
for (String name : names) {
addRelatedTableName(name, tables);
}
}
}
Expand Down
Loading

0 comments on commit f3aad33

Please sign in to comment.