use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject method buildDatasourceLoginDescriptor.
public ClassDescriptor buildDatasourceLoginDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DatasourceLogin.class);
descriptor.setDefaultRootElement("login");
descriptor.getInheritancePolicy().setClassIndicatorField(new XMLField("@xsi:type"));
descriptor.getInheritancePolicy().addClassIndicator(DatabaseLogin.class, getPrimaryNamespaceXPath() + "database-login");
descriptor.getInheritancePolicy().addClassIndicator(EISLogin.class, getPrimaryNamespaceXPath() + "eis-login");
descriptor.getInheritancePolicy().addClassIndicator(XMLLogin.class, getPrimaryNamespaceXPath() + "xml-login");
XMLDirectMapping platformMapping = new XMLDirectMapping();
platformMapping.setAttributeName("platform");
platformMapping.setGetMethodName("getDatasourcePlatform");
platformMapping.setSetMethodName("usePlatform");
platformMapping.setConverter(new Converter() {
private Map<String, String> platformList;
private String oldPrefix = "oracle.toplink.";
private String newPrefix = "org.eclipse.persistence.";
private String oldOxmPrefix = oldPrefix + "ox.";
private String newOxmPrefix = newPrefix + "oxm.";
@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
if (objectValue == null) {
return null;
}
return objectValue.getClass().getName();
}
@Override
public Object convertDataValueToObjectValue(Object fieldValue, Session session) {
if (fieldValue == null) {
return null;
}
if (((String) fieldValue).startsWith(oldPrefix)) {
if (((String) fieldValue).startsWith(oldOxmPrefix)) {
fieldValue = ((String) fieldValue).replaceFirst(oldOxmPrefix, newOxmPrefix);
} else {
fieldValue = ((String) fieldValue).replaceFirst(oldPrefix, newPrefix);
}
}
// convert deprecated platforms to new platforms
String result = platformList.get(fieldValue);
if (result != null) {
fieldValue = result;
}
Object attributeValue;
Class<?> attributeClass = session.getDatasourcePlatform().convertObject(fieldValue, ClassConstants.CLASS);
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
attributeValue = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(attributeClass));
} catch (PrivilegedActionException exception) {
throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception.getException());
}
} else {
attributeValue = PrivilegedAccessHelper.newInstanceFromClass(attributeClass);
}
} catch (Exception exception) {
throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception);
}
return attributeValue;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
this.platformList = new HashMap<>();
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.AccessPlatform", "org.eclipse.persistence.platform.database.AccessPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.AttunityPlatform", "org.eclipse.persistence.platform.database.AttunityPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.CloudscapePlatform", "org.eclipse.persistence.platform.database.CloudscapePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DatabasePlatform", "org.eclipse.persistence.platform.database.DatabasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DB2MainframePlatform", "org.eclipse.persistence.platform.database.DB2MainframePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DB2Platform", "org.eclipse.persistence.platform.database.DB2Platform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.DBasePlatform", "org.eclipse.persistence.platform.database.DBasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.HSQLPlatform", "org.eclipse.persistence.platform.database.HSQLPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.InformixPlatform", "org.eclipse.persistence.platform.database.InformixPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.OraclePlatform", "org.eclipse.persistence.platform.database.OraclePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.PointBasePlatform", "org.eclipse.persistence.platform.database.PointBasePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SQLAnyWherePlatform", "org.eclipse.persistence.platform.database.SQLAnywherePlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SQLServerPlatform", "org.eclipse.persistence.platform.database.SQLServerPlatform");
this.platformList.put("org.eclipse.persistence.internal.databaseaccess.SybasePlatform", "org.eclipse.persistence.platform.database.SybasePlatform");
this.platformList.put("org.eclipse.persistence.oraclespecific.Oracle8Platform", "org.eclipse.persistence.platform.database.oracle.Oracle8Platform");
this.platformList.put("org.eclipse.persistence.oraclespecific.Oracle9Platform", "org.eclipse.persistence.platform.database.oracle.Oracle9Platform");
this.platformList.put("org.eclipse.persistence.platform.database.SQLAnyWherePlatform", "org.eclipse.persistence.platform.database.SQLAnywherePlatform");
// CR#... Mapping must also have the field classification.
if (mapping.isDirectToFieldMapping()) {
AbstractDirectMapping directMapping = (AbstractDirectMapping) mapping;
// Allow user to specify field type to override computed value. (i.e. blob, nchar)
if (directMapping.getFieldClassification() == null) {
directMapping.setFieldClassification(ClassConstants.STRING);
}
}
}
});
platformMapping.setXPath(getPrimaryNamespaceXPath() + "platform-class/text()");
descriptor.addMapping(platformMapping);
XMLDirectMapping userNameMapping = new XMLDirectMapping();
userNameMapping.setAttributeName("userName");
userNameMapping.setGetMethodName("getUserName");
userNameMapping.setSetMethodName("setUserName");
userNameMapping.setXPath(getPrimaryNamespaceXPath() + "user-name/text()");
descriptor.addMapping(userNameMapping);
XMLDirectMapping passwordMapping = new XMLDirectMapping();
passwordMapping.setAttributeName("password");
passwordMapping.setGetMethodName("getPassword");
passwordMapping.setSetMethodName("setEncryptedPassword");
passwordMapping.setXPath(getPrimaryNamespaceXPath() + "password/text()");
descriptor.addMapping(passwordMapping);
XMLDirectMapping usesExternalConnectionPoolingMapping = new XMLDirectMapping();
usesExternalConnectionPoolingMapping.setAttributeName("usesExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setGetMethodName("shouldUseExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setSetMethodName("setUsesExternalConnectionPooling");
usesExternalConnectionPoolingMapping.setXPath(getPrimaryNamespaceXPath() + "external-connection-pooling/text()");
usesExternalConnectionPoolingMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(usesExternalConnectionPoolingMapping);
XMLDirectMapping usesExternalTransactionControllerMapping = new XMLDirectMapping();
usesExternalTransactionControllerMapping.setAttributeName("usesExternalTransactionController");
usesExternalTransactionControllerMapping.setGetMethodName("shouldUseExternalTransactionController");
usesExternalTransactionControllerMapping.setSetMethodName("setUsesExternalTransactionController");
usesExternalTransactionControllerMapping.setXPath(getPrimaryNamespaceXPath() + "external-transaction-controller/text()");
usesExternalTransactionControllerMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(usesExternalTransactionControllerMapping);
XMLCompositeObjectMapping defaultSequenceMapping = new XMLCompositeObjectMapping();
defaultSequenceMapping.setAttributeName("defaultSequence");
defaultSequenceMapping.setSetMethodName("setDefaultSequence");
defaultSequenceMapping.setGetMethodName("getDefaultSequenceToWrite");
defaultSequenceMapping.setReferenceClass(Sequence.class);
defaultSequenceMapping.setXPath(getPrimaryNamespaceXPath() + "sequencing/" + getPrimaryNamespaceXPath() + "default-sequence");
descriptor.addMapping(defaultSequenceMapping);
XMLCompositeCollectionMapping sequencesMapping = new XMLCompositeCollectionMapping();
MapContainerPolicy containerPolicy = new MapContainerPolicy(HashMap.class);
containerPolicy.setKeyName("name", Sequence.class.getName());
sequencesMapping.setContainerPolicy(containerPolicy);
sequencesMapping.setAttributeName("sequences");
sequencesMapping.setSetMethodName("setSequences");
sequencesMapping.setGetMethodName("getSequencesToWrite");
sequencesMapping.setReferenceClass(Sequence.class);
sequencesMapping.setXPath(getPrimaryNamespaceXPath() + "sequencing/" + getPrimaryNamespaceXPath() + "sequences/" + getPrimaryNamespaceXPath() + "sequence");
descriptor.addMapping(sequencesMapping);
return descriptor;
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class ServerPlatformBase method initializeExternalTransactionController.
/**
* INTERNAL: initializeExternalTransactionController(): Populate the DatabaseSession's
* external transaction controller with an instance of my transaction controller class.
*
* To change the external transaction controller class, we recommend creating a subclass of
* ServerPlatformBase, and overriding getExternalTransactionControllerClass().
*/
@Override
public void initializeExternalTransactionController() {
this.ensureNotLoggedIn();
// JTA must never be disable during CMP (WLS/Oc4j)
if (!isJTAEnabled() && !isCMP()) {
return;
}
// BUG 3975114: display a warning if JTA is disabled and we're in CMP
if (!isJTAEnabled() && isCMP()) {
getDatabaseSession().getSessionLog().log(SessionLog.WARNING, SessionLog.EJB, "jta_cannot_be_disabled_in_cmp", null, true);
}
// a subclass. Show a warning
try {
if (getDatabaseSession().getExternalTransactionController() != null) {
this.externalTransactionControllerNotNullWarning();
return;
}
ExternalTransactionController controller = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
controller = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(this.getExternalTransactionControllerClass()));
} catch (PrivilegedActionException exception) {
Exception throwableException = exception.getException();
if (throwableException instanceof InstantiationException) {
throw ValidationException.cannotCreateExternalTransactionController(getExternalTransactionControllerClass().getName());
} else {
throw ValidationException.cannotCreateExternalTransactionController(getExternalTransactionControllerClass().getName());
}
}
} else {
controller = PrivilegedAccessHelper.newInstanceFromClass(this.getExternalTransactionControllerClass());
}
getDatabaseSession().setExternalTransactionController(controller);
} catch (InstantiationException instantiationException) {
throw ValidationException.cannotCreateExternalTransactionController(getExternalTransactionControllerClass().getName());
} catch (IllegalAccessException illegalAccessException) {
throw ValidationException.cannotCreateExternalTransactionController(getExternalTransactionControllerClass().getName());
}
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class AnyMappingContentHandler method processComplexElement.
@Override
protected void processComplexElement() throws SAXException {
getParent().unmappedContent();
Class<?> unmappedContentHandlerClass = getParent().getUnmarshaller().getUnmappedContentHandlerClass();
UnmappedContentHandler unmappedContentHandler;
if (null == unmappedContentHandlerClass) {
unmappedContentHandler = UnmarshalRecord.DEFAULT_UNMAPPED_CONTENT_HANDLER;
} else {
try {
PrivilegedNewInstanceFromClass privilegedNewInstanceFromClass = new PrivilegedNewInstanceFromClass(unmappedContentHandlerClass);
unmappedContentHandler = (UnmappedContentHandler) privilegedNewInstanceFromClass.run();
} catch (ClassCastException e) {
throw XMLMarshalException.unmappedContentHandlerDoesntImplement(e, unmappedContentHandlerClass.getName());
} catch (IllegalAccessException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
} catch (InstantiationException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
}
}
UnmappedContentHandlerWrapper unmappedContentHandlerWrapper = new UnmappedContentHandlerWrapper(getParent(), unmappedContentHandler);
executeEvents(unmappedContentHandlerWrapper);
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class SessionsFactory method processLoginConfig.
/**
* INTERNAL:
* Process the common elements of a Login.
*/
protected void processLoginConfig(LoginConfig loginConfig, DatasourceLogin login) {
// Platform class
String platformClassName = loginConfig.getPlatformClass();
if (platformClassName != null) {
try {
@SuppressWarnings({ "unchecked" }) Class<DatasourcePlatform> platformClass = (Class<DatasourcePlatform>) m_classLoader.loadClass(platformClassName);
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
login.usePlatform(AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(platformClass)));
} else {
login.usePlatform(PrivilegedAccessHelper.newInstanceFromClass(platformClass));
}
} catch (Exception exception) {
throw SessionLoaderException.failedToLoadTag("platform-class", platformClassName, exception);
}
}
// Table qualifier
String tableQualifier = loginConfig.getTableQualifier();
if (tableQualifier != null) {
login.setTableQualifier(tableQualifier);
}
// Username - setUserName checks for null
login.setUserName(loginConfig.getUsername());
// Encryption class (must be set before the password)
// XML Schema default is org.eclipse.persistence.internal.security.JCEEncryptor
login.setEncryptionClassName(loginConfig.getEncryptionClass());
// Password is encrypted on the model - setEncryptedPassword checks for null
login.setEncryptedPassword(loginConfig.getEncryptedPassword());
// External connection pool - XML Schema default is false
login.setUsesExternalConnectionPooling(loginConfig.getExternalConnectionPooling());
// External transaction controller - XML Schema default is false
login.setUsesExternalTransactionController(loginConfig.getExternalTransactionController());
// Sequencing - XML Schema default is null
if (loginConfig.getSequencingConfig() != null) {
if (loginConfig.getSequencingConfig().getDefaultSequenceConfig() != null) {
Sequence sequence = buildSequence(loginConfig.getSequencingConfig().getDefaultSequenceConfig());
login.setDefaultSequence(sequence);
}
if ((loginConfig.getSequencingConfig().getSequenceConfigs() != null) && !loginConfig.getSequencingConfig().getSequenceConfigs().isEmpty()) {
Enumeration<SequenceConfig> eSequenceConfigs = loginConfig.getSequencingConfig().getSequenceConfigs().elements();
while (eSequenceConfigs.hasMoreElements()) {
Sequence sequence = buildSequence(eSequenceConfigs.nextElement());
login.addSequence(sequence);
}
}
}
// Properties (assumes they are all valid)
if (loginConfig.getPropertyConfigs() != null) {
Enumeration<PropertyConfig> e = loginConfig.getPropertyConfigs().elements();
while (e.hasMoreElements()) {
PropertyConfig propertyConfig = e.nextElement();
login.getProperties().put(propertyConfig.getName(), propertyConfig.getValue());
}
}
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class SessionsFactory method buildEISLoginConfig.
/**
* INTERNAL:
* Wrapped by the getLogin() call, therefore, config can't be null at this
* point.
*/
protected Login buildEISLoginConfig(EISLoginConfig eisLoginConfig) {
EISLogin eisLogin = new EISLogin();
// Connection Spec
String specClassName = eisLoginConfig.getConnectionSpecClass();
if (specClassName != null) {
try {
@SuppressWarnings({ "unchecked" }) Class<EISConnectionSpec> specClass = (Class<EISConnectionSpec>) m_classLoader.loadClass(specClassName);
EISConnectionSpec spec = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
spec = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(specClass));
} else {
spec = PrivilegedAccessHelper.newInstanceFromClass(specClass);
}
eisLogin.setConnectionSpec(spec);
} catch (Exception exception) {
throw SessionLoaderException.failedToLoadTag("connection-spec-class", specClassName, exception);
}
}
// Connection Factory URL, setConnectionFactoryURL checks for null
eisLogin.setConnectionFactoryURL(eisLoginConfig.getConnectionFactoryURL());
// Process the common elements in LoginConfig
processLoginConfig(eisLoginConfig, eisLogin);
// Finally, return the newly created EISLogin
return eisLogin;
}
Aggregations