FileDocCategorySizeDatePackage
SerializedDescriptorHelper.javaAPI DocGlassfish v2 API19289Fri May 04 22:35:00 BST 2007com.sun.enterprise.instance

SerializedDescriptorHelper

public class SerializedDescriptorHelper extends Object
Handles the serialization and deserialization of descriptor object graphs.

The static "store" method is used from the deployment code to create the initial serialized data file for an application. In EE environments this file will be copied to the relevant instances during synchronization.

The static "load" method is used from the various subclasses of BaseManager to attempt to load the serialized object graph and then return an instance of SerializedDescriptorHelper.Loader. This object not only loads the serialized descriptor (if possible) but also allows the manager to get the loaded application object. If that object is null, the manager loads the application from the XML descriptors and should try to rewrite the file using the new application object by invoking the "store" method on the returned Loader object.

author
tjquinn

Fields Summary
private static final String
SERIALIZED_DESCRIPTOR_FILE_NAME
file name used for serializing (and deserializing) the DOL descriptor
private static final String
SERIALIZATION_ENABLED_PROPERTY
property that allows control of serialization
private static final String
SERIALIZATION_ENABLED_DEFAULT
default value for using descriptor serialization
private static final boolean
isSerializedDescriptorIOEnabled
private static final Logger
logger
Constructors Summary
Methods Summary
private static java.lang.StringgetCurrentSoftwareVersion()
Returns an expression containing the major and minor version strings and the build ID of the currently-running software.

        return Version.getMajorVersion() + "." + 
                Version.getMinorVersion() + "-" + 
                Version.getBuildVersion();
    
private static java.io.FilegetSerializedDescriptorFile(java.lang.String moduleID, BaseManager manager)
Returns a File object for the serialized descriptor file for the specified module overseen by the specified manager.

param
moduleID the unique ID of the module
param
manager the BaseManager concrete instance that manages apps of this type
return
a File for the serialized descriptor file for this app

        return new File(
                manager.getGeneratedXMLLocation(moduleID), 
                SERIALIZED_DESCRIPTOR_FILE_NAME);
    
private static booleanisSerializedDescriptorIOEnabled()
Returns whether serialized descriptor processing is turned on or not.

return
true if serialized reading and writing should take place

        return isSerializedDescriptorIOEnabled;
    
public static com.sun.enterprise.instance.SerializedDescriptorHelper$Loaderload(java.lang.String moduleID, BaseManager manager)
Attempts to load the serialized object graph from the appropriate file.

Whether the attempt succeeds or not, the method returns an instance of Loader that the caller can use to retrieve the loaded application and, if appropriate, attempt to write the application object graph it built from the XML descriptors.

Because failures to load the serialized data are not fatal errors, this method throws no exceptions. Instead the loader's internal application object will be null. The caller should check for this and then load the application from the XML descriptors instead.

param
moduleID the module ID of the application to load
param
manager the manager for this type of module
return
the SerializedDescriptorHelper.Loader the caller can use to retrieve the application and, if appropriate, rewrite the file


                                                                                                                                 
       
              
              
        Loader loader = new Loader(moduleID, manager);
        loader.loadSerializedDescriptor();
        return loader;
    
private static voidlog(java.util.logging.Level level, java.lang.String messageKey, java.lang.Throwable t, java.lang.Object args)
Logs a message, looking up the message key and substituting arguments and including a Throwable indicating an error.

param
level the logging level at which to log this message
param
messageKey the key to the message
param
t the Throwable to be recorded with the log message
param
args... the arguments (if any) to be substituted into the looked-up message

        String msg = logger.getResourceBundle().getString(messageKey);
        String formattedMsg = MessageFormat.format(msg, args);
        logger.log(level, formattedMsg, t);
    
private static voidlog(java.util.logging.Level level, java.lang.String messageKey, java.lang.Object args)

        logger.log(level, messageKey, args);
    
public static voidstore(java.lang.String moduleID, BaseManager manager, com.sun.enterprise.deployment.Application application)
Attempts to write the application object graph to the corresponding file.

Because failures to serialized the object graph are not fatal, the method throws no exceptions.

param
moduleID the module being serialized
param
manager the manager in charge of this type of application
param
application the object graph to storeSerializedDescriptor

        File file = getSerializedDescriptorFile(moduleID, manager);
        store(moduleID, manager, application, file);
    
private static voidstore(java.lang.String moduleID, BaseManager manager, com.sun.enterprise.deployment.Application application, java.io.File serializedFile)
Writes the specified application to the correct location for the module.

param
moduleID the unique identifier of the module
param
manager the BaseManager instance for this type of module
param
application the Application object graph to be written
param
serializedFile the File object that refers to the serialized file for this app

        if (isSerializedDescriptorIOEnabled()) {
            /*
             *Try to write the application object graph to the file.
             */
            if ( ! storeSerializedDescriptor(
                    application, 
                    serializedFile, 
                    moduleID)) {

                /*
                 *In case of any error serializing the descriptors, store an
                 *Exception object instead.  This will save the server from
                 *future attempts to write the serialized descriptors that we 
                 *expect will continue to fail.
                 */
                storeSerializedDescriptor(
                        new Exception(getCurrentSoftwareVersion()), 
                        serializedFile, 
                        moduleID);
            }
        }
    
private static booleanstoreSerializedDescriptor(java.lang.Object obj, java.io.File file, java.lang.String moduleID)
Attempts to store the specified object in serialized form in the indicated file. The module ID is used in logging.

param
obj the Object to be serialized
param
file the File into which the object graph should be written
param
moduleID the unique identifier for the module (used in logging)
return
true if the object graph was written to the file successfully; false otherwise

        if ( ! isSerializedDescriptorIOEnabled()) {
            return false;
        }
        /*
         *Predeployed system apps will not have generated/xml directories.
         */
        if ( ! file.getParentFile().exists()) {
            return false;
        }
        ObjectOutputStream oos = null;
        boolean result;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(obj);
            result = true;
            if(logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, 
                        "Serialized application " + moduleID);
            }

        } catch (Throwable t) {
            result = false;
            log(Level.WARNING, "core.error_ser_descr", t, moduleID);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException ioe) {
                    log(Level.WARNING, "core.error_ser_descr", ioe, moduleID);
                }
            }
        }
        return result;