use of org.eclipse.persistence.logging.SessionLog in project eclipselink by eclipse-ee4j.
the class MOXyJsonProvider method writeTo.
/**
* @see jakarta.ws.rs.ext.MessageBodyWriter#writeTo(java.lang.Object, java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], jakarta.ws.rs.core.MediaType, jakarta.ws.rs.core.MultivaluedMap, java.io.OutputStream)
*/
@Override
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
try {
if (null == genericType) {
genericType = type;
}
Set<Class<?>> domainClasses = getDomainClasses(genericType);
JAXBContext jaxbContext = getJAXBContext(domainClasses, annotations, mediaType, httpHeaders);
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_write_to_moxy_json_provider", new Object[0]);
}
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, attributePrefix);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, includeRoot);
marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, marshalEmptyCollections);
marshaller.setProperty(MarshallerProperties.JSON_NAMESPACE_SEPARATOR, namespaceSeperator);
if (null != valueWrapper) {
marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, valueWrapper);
}
marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, wrapperAsArrayName);
marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
Map<String, String> mediaTypeParameters = null;
if (null != mediaType) {
mediaTypeParameters = mediaType.getParameters();
}
if (null != mediaTypeParameters && mediaTypeParameters.containsKey(CHARSET)) {
String charSet = mediaTypeParameters.get(CHARSET);
marshaller.setProperty(Marshaller.JAXB_ENCODING, charSet);
}
preWriteTo(object, type, genericType, annotations, mediaType, httpHeaders, marshaller);
if (domainClasses.size() == 1) {
Class<?> domainClass = domainClasses.iterator().next();
if (!(List.class.isAssignableFrom(type) || type.isArray()) && domainClass.getPackage().getName().startsWith("java.")) {
object = new JAXBElement(new QName((String) marshaller.getProperty(MarshallerProperties.JSON_VALUE_WRAPPER)), domainClass, object);
}
}
marshaller.marshal(object, entityStream);
} catch (JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
}
}
use of org.eclipse.persistence.logging.SessionLog in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method updateLoggers.
/**
* Update loggers and settings for the singleton logger and the session logger.
* @param persistenceProperties the properties map
* @param serverPlatformChanged the boolean that denotes a serverPlatform change in the session.
*/
protected void updateLoggers(Map persistenceProperties, boolean serverPlatformChanged, ClassLoader loader) {
// Logger(SessionLog type) can be specified by the logger property or ServerPlatform.getServerLog().
// The logger property has a higher priority to ServerPlatform.getServerLog().
String loggerClassName = PropertiesHandler.getPropertyValueLogDebug(PersistenceUnitProperties.LOGGING_LOGGER, persistenceProperties, session);
// The sessionLog instance should be different from the singletonLog because they have
// different state.
SessionLog singletonLog = null, sessionLog = null;
if (loggerClassName != null) {
SessionLog currentLog = session.getSessionLog();
if (loggerClassName.equals(LoggerType.ServerLogger)) {
ServerPlatform serverPlatform = session.getServerPlatform();
singletonLog = serverPlatform.getServerLog();
sessionLog = serverPlatform.getServerLog();
} else if (!currentLog.getClass().getName().equals(loggerClassName)) {
// Logger class was specified and it's not what's already there.
try {
Class<? extends SessionLog> sessionLogClass = findClassForProperty(loggerClassName, PersistenceUnitProperties.LOGGING_LOGGER, loader);
singletonLog = sessionLogClass.getConstructor().newInstance();
sessionLog = sessionLogClass.getConstructor().newInstance();
} catch (Exception ex) {
throw EntityManagerSetupException.failedToInstantiateLogger(loggerClassName, PersistenceUnitProperties.LOGGING_LOGGER, ex);
}
}
} else if (serverPlatformChanged) {
ServerPlatform serverPlatform = session.getServerPlatform();
singletonLog = serverPlatform.getServerLog();
sessionLog = serverPlatform.getServerLog();
}
// Don't change default loggers if the new loggers have not been created.
if (singletonLog != null && sessionLog != null) {
AbstractSessionLog.setLog(singletonLog);
session.setSessionLog(sessionLog);
}
// Bug5389828. Update the logging settings for the singleton logger.
initOrUpdateLogging(persistenceProperties, AbstractSessionLog.getLog());
initOrUpdateLogging(persistenceProperties, session.getSessionLog());
// Set logging file.
String loggingFileString = (String) persistenceProperties.get(PersistenceUnitProperties.LOGGING_FILE);
if (loggingFileString != null) {
if (!loggingFileString.trim().equals("")) {
try {
if (sessionLog != null) {
if (sessionLog instanceof AbstractSessionLog) {
FileOutputStream fos = new FileOutputStream(loggingFileString);
((AbstractSessionLog) sessionLog).setWriter(fos);
} else {
FileWriter fw = new FileWriter(loggingFileString);
sessionLog.setWriter(fw);
}
}
} catch (IOException e) {
session.handleException(ValidationException.invalidLoggingFile(loggingFileString, e));
}
} else {
session.handleException(ValidationException.invalidLoggingFile());
}
}
}
use of org.eclipse.persistence.logging.SessionLog in project eclipselink by eclipse-ee4j.
the class JAXBMarshaller method setProperty.
/**
* Set a property on the JAXBMarshaller. Attempting to set any unsupported
* property will result in a jakarta.xml.bind.PropertyException
* @see org.eclipse.persistence.jaxb.MarshallerProperties
*/
@Override
public void setProperty(String key, Object value) throws PropertyException {
try {
if (key == null) {
throw new IllegalArgumentException();
} else {
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_set_marshaller_property", new Object[] { key, value });
}
if (MOXySystemProperties.moxyLogPayload != null && xmlMarshaller.isLogPayload() == null) {
xmlMarshaller.setLogPayload(MOXySystemProperties.moxyLogPayload);
}
if (Constants.JAXB_FRAGMENT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean fragment = (Boolean) value;
xmlMarshaller.setFragment(fragment);
} else if (JAXB_FORMATTED_OUTPUT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean formattedOutput = (Boolean) value;
xmlMarshaller.setFormattedOutput(formattedOutput);
} else if (JAXB_ENCODING.equals(key)) {
xmlMarshaller.setEncoding((String) value);
} else if (JAXB_SCHEMA_LOCATION.equals(key)) {
xmlMarshaller.setSchemaLocation((String) value);
} else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) {
xmlMarshaller.setNoNamespaceSchemaLocation((String) value);
} else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) {
if (value == null) {
xmlMarshaller.setNamespacePrefixMapper(null);
} else if (value instanceof Map) {
NamespacePrefixMapper namespacePrefixMapper = new MapNamespacePrefixMapper((Map) value);
xmlMarshaller.setNamespacePrefixMapper(namespacePrefixMapper);
} else {
xmlMarshaller.setNamespacePrefixMapper((NamespacePrefixMapper) value);
}
} else if (SUN_NAMESPACE_PREFIX_MAPPER.equals(key) || SUN_JSE_NAMESPACE_PREFIX_MAPPER.equals(key)) {
if (value == null) {
xmlMarshaller.setNamespacePrefixMapper(null);
} else {
xmlMarshaller.setNamespacePrefixMapper(new NamespacePrefixMapperWrapper(value));
}
} else if (MarshallerProperties.INDENT_STRING.equals(key) || SUN_INDENT_STRING.equals(key) || SUN_JSE_INDENT_STRING.equals(key)) {
xmlMarshaller.setIndentString((String) value);
} else if (MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS.equals(key)) {
xmlMarshaller.setMarshalEmptyCollections((Boolean) value);
} else if (MarshallerProperties.JSON_REDUCE_ANY_ARRAYS.equals(key)) {
xmlMarshaller.setReduceAnyArrays((Boolean) value);
} else if (MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
xmlMarshaller.setWrapperAsCollectionName((Boolean) value);
} else if (MarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setUseXsdTypesWithPrefix((Boolean) value);
} else if (MarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonTypeCompatibility((Boolean) value);
} else if (MarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonTypeAttributeName((String) value);
} else if (MarshallerProperties.JSON_DISABLE_NESTED_ARRAY_NAME.equals(key)) {
xmlMarshaller.getJsonTypeConfiguration().setJsonDisableNestedArrayName((Boolean) value);
} else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) {
xmlMarshaller.setCharacterEscapeHandler((CharacterEscapeHandler) value);
} else if (MarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
xmlMarshaller.setLogPayload(((Boolean) value));
} else if (MarshallerProperties.MOXY_LOGGING_LEVEL.equals(key)) {
if (value instanceof String) {
AbstractSessionLog.getLog().setLevel(LogLevel.toValue((String) value).getId(), SessionLog.MOXY);
} else {
AbstractSessionLog.getLog().setLevel(((LogLevel) value).getId(), SessionLog.MOXY);
}
} else if (SUN_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER_MARSHALLER.equals(key)) {
if (value == null) {
xmlMarshaller.setCharacterEscapeHandler(null);
} else {
xmlMarshaller.setCharacterEscapeHandler(new CharacterEscapeHandlerWrapper(value));
}
} else if (XML_DECLARATION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
Boolean fragment = !(Boolean) value;
xmlMarshaller.setFragment(fragment);
} else if (XML_HEADERS.equals(key)) {
xmlMarshaller.setXmlHeader((String) value);
} else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setEqualUsingIdenity((Boolean) value);
} else if (MarshallerProperties.MEDIA_TYPE.equals(key)) {
MediaType mType = null;
if (value instanceof MediaType) {
mType = (MediaType) value;
} else if (value instanceof String) {
mType = MediaType.getMediaType((String) value);
}
if (mType == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setMediaType(mType);
} else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) {
xmlMarshaller.setAttributePrefix((String) value);
} else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setIncludeRoot((Boolean) value);
} else if (MarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
if (value == null || (((String) value).length() == 0)) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setValueWrapper((String) value);
} else if (MarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
xmlMarshaller.setNamespaceSeparator((Character) value);
} else if (MarshallerProperties.OBJECT_GRAPH.equals(key)) {
if (value == null) {
xmlMarshaller.setMarshalAttributeGroup(null);
} else if (value instanceof ObjectGraphImpl) {
xmlMarshaller.setMarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
} else if (value.getClass() == ClassConstants.STRING) {
xmlMarshaller.setMarshalAttributeGroup(value);
} else {
throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
}
} else if (MarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationMode = ((BeanValidationMode) value);
} else if (MarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
// noinspection StatementWithEmptyBody
if (value == null) {
// Allow null value for preferred validation factory.
}
this.prefValidatorFactory = value;
} else if (MarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.beanValidationGroups = ((Class<?>[]) value);
} else if (MarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
if (value == null) {
throw new PropertyException(key, Constants.EMPTY_STRING);
}
this.bvNoOptimisation = ((boolean) value);
} else {
throw new PropertyException(key, value);
}
}
} catch (ClassCastException exception) {
throw new PropertyException(key, exception);
}
}
use of org.eclipse.persistence.logging.SessionLog in project eclipselink by eclipse-ee4j.
the class JUnitTestCase method verifyObjectInCacheAndDatabase.
/**
* Verifies that the object was merged to the cache, and written to the database correctly.
*/
public static void verifyObjectInCacheAndDatabase(Object writtenObject, String persistenceUnit) {
AbstractSession dbs = getDatabaseSession(persistenceUnit);
Object readObject = dbs.readObject(writtenObject);
if (!dbs.compareObjects(readObject, writtenObject)) {
SessionLog oldLog = dbs.getSessionLog();
dbs.setSessionLog(new DefaultSessionLog());
dbs.setLogLevel(SessionLog.FINEST);
StringWriter newLog = new StringWriter();
dbs.setLog(newLog);
dbs.compareObjects(readObject, writtenObject);
dbs.setSessionLog(oldLog);
fail("Object from cache: " + readObject + " does not match object that was written: " + writtenObject + ". " + newLog);
}
dbs.getIdentityMapAccessor().initializeAllIdentityMaps();
readObject = dbs.readObject(writtenObject);
if (!dbs.compareObjects(readObject, writtenObject)) {
SessionLog oldLog = dbs.getSessionLog();
dbs.setSessionLog(new DefaultSessionLog());
dbs.setLogLevel(SessionLog.FINEST);
StringWriter newLog = new StringWriter();
dbs.setLog(newLog);
dbs.compareObjects(readObject, writtenObject);
dbs.setSessionLog(oldLog);
fail("Object from database: " + readObject + " does not match object that was written: " + writtenObject + ". " + newLog);
}
}
Aggregations