OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

sdo message

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]


Subject: HelperContext material for vF2F


Hello All,

Attached is some material for the HelperContext discussion.

-Blaise

SDO3-HelperContexts.ppt

/**
 * <copyright>
 *
 * Service Data Objects
 * Version 2.1.1
 * Licensed Materials
 *
 * (c) Copyright BEA Systems, Inc., International Business Machines Corporation, 
 * Oracle Corporation, Primeton Technologies Ltd., Rogue Wave Software, SAP AG., 
 * Software AG., Sun Microsystems, Sybase Inc., Xcalia, Zend Technologies, 
 * 2005-2008. All rights reserved.
 *
 * </copyright>
 * 
 */

package commonj.sdo.impl;


import commonj.sdo.helper.CopyHelper;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.DataHelper;
import commonj.sdo.helper.EqualityHelper;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.SDO;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;
import commonj.sdo.impl.ExternalizableDelegator.Resolvable;

/**
 * This class instantiates a HelperProviderImpl that returns concrete helpers.
 * This class may be replaced by another implementation.
 * @deprecated
 */
public class HelperProvider
{
  static final HelperContext DEFAULT_HELPER_CONTEXT = SDO.getHelperContextFactory().getDefaultHelperContext();
  
  HelperProvider()
  {
  }
  
  /**
   * Gets a CopyHelper
   * @return a CopyHelper object
   */
  public static CopyHelper getCopyHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getCopyHelper();
  }

  /**
   * Gets a DataFactory
   * @return a DataFactory object
   */
  public static DataFactory getDataFactory()
  {
    return DEFAULT_HELPER_CONTEXT.getDataFactory();
  }

  /**
   * Gets a DataHelper
   * @return a DataHelper object
   */
  public static DataHelper getDataHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getDataHelper();
  }

  /**
   * Gets an EqualityHelper
   * @return an EqualityHelper object
   */
  public static EqualityHelper getEqualityHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getEqualityHelper();
  }
  
  /**
   * Gets a TypeHelper 
   * @return a TypeHelper object
   */
  public static TypeHelper getTypeHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getTypeHelper();
  }

  /**
   * Gets an XMLHelper
   * @return an XMLHelper object
   */
  public static XMLHelper getXMLHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getXMLHelper();
  }
  
  /**
   * Gets an XSDHelper 
   * @return an XSDHelper object
   */
  public static XSDHelper getXSDHelper()
  {
    return DEFAULT_HELPER_CONTEXT.getXSDHelper();
  }

  /**
   * Gets a Resolvable 
   * @return a Resolvable object
   * @see ExternalizableDelegator
   */
  public static Resolvable createResolvable()
  {
    return SDO.getHelperContextFactory().createResolvable();
  }
  
  /**
   * Gets a Resolvable 
   * @param target the target object for the Resolvable
   * @return a Resolvable object
   * @see ExternalizableDelegator
   */
  public static Resolvable createResolvable(Object target)
  {
    return SDO.getHelperContextFactory().createResolvable(target);
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  // New in SDO 2.1
  ////////////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Gets the default HelperContext
   * @return a HelperContext object
   */
  public static HelperContext getDefaultContext()
  {
    return DEFAULT_HELPER_CONTEXT;
  }
    
}


/**
 * <copyright>
 *
 * Service Data Objects
 * Version 3.0
 * Licensed Materials
 *
 * (c) Copyright BEA Systems, Inc., International Business Machines Corporation,
 * Oracle Corporation, Primeton Technologies Ltd., Rogue Wave Software, SAP AG.,
 * Software AG., Sun Microsystems, Sybase Inc., Xcalia, Zend Technologies,
 * 2005-2008. All rights reserved.
 *
 * </copyright>
 *
 */
package commonj.sdo.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;

public class SDO {

    private static HelperContentFactoryResolver helperContextFactoryResolver;

    public static HelperContentFactoryResolver getHelperContextFactoryResolver() {
        if (null == helperContextFactoryResolver) {
            helperContextFactoryResolver = new DefaultImplementationResolver();
        }
        return helperContextFactoryResolver;
    }

    public static void setHelperContextFactoryResolver(HelperContentFactoryResolver anImplementationResolver) {
        helperContextFactoryResolver = anImplementationResolver;
    }

    public static HelperContextFactory getHelperContextFactory() {
        return getHelperContextFactoryResolver().getHelperContextFactory();
    }

    public static HelperContextFactory getHelperContextFactory(String implName) {
        return getHelperContextFactoryResolver().getHelperContextFactory(implName);
    }

    public static interface HelperContentFactoryResolver {

        /**
         * The name of the system property that will be checked for an implementation name.
         */
        static final String PROPERTY_NAME = "commonj.sdo.helper.HelperContextFactory";

        /**
         * The name of the resource that is used for service location.
         */
        static final String SERVICE_RESOURCE_NAME = "META-INF/services/" + PROPERTY_NAME;

        HelperContextFactory getHelperContextFactory();

        HelperContextFactory getHelperContextFactory(String implName);

    }

    private static class DefaultImplementationResolver implements HelperContentFactoryResolver {

        /**
         * Locate and instantiate the default HelperContextFactory.
         * 
         * @see getInstance(String)
         */
        public HelperContextFactory getHelperContextFactory() {
            return getHelperContextFactory(null);
        }

        /**
         * Locate and instantiate a HelperContextFactory. <p/> If the specified
         * implName is not null, attempt to instantiate an implementation class
         * with that name. If implName is null, then the name of the
         * implementation to use is determined by the value of the
         * "commonj.sdo.helper.HelperContextFactory" system property. If this is not
         * set or this code does not have permission to read it then the name
         * will be retrieved from the
         * META-INF/services/commonj.sdo.helper.HelperContextFactory resource. <p/>
         * Name lookup and class loading is done using first the Thread's
         * current context classloader and then, if that is not set, not
         * readable, or does not provide an implementation, using the
         * classloader used to load the HelperContextFactory class itself. <p/>
         * 
         * @param implName
         *            The name of the implementation class.
         * @return A singleton instance of the selected HelperContextFactory.
         */
        public HelperContextFactory getHelperContextFactory(String implName) {
            if (implName == null) {
                implName = getImplementationName();
            }
            ClassLoader cl = getContextClassLoader();
            if (cl != null) {
                HelperContextFactory impl = getHelperContextFactory(cl, implName);
                if (impl != null) {
                    return impl;
                }
            }
            cl = HelperContextFactory.class.getClassLoader();
            HelperContextFactory impl = getHelperContextFactory(cl, implName);
            if (impl != null) {
                return impl;
            }
            return null;
        }

        /**
         * Map from implementation classes to their corresponding singleton instances.
         */
        protected final HashMap<Class, HelperContextFactory> instanceMap = new HashMap<Class, HelperContextFactory>();

        protected HelperContextFactory getHelperContextFactory(ClassLoader cl, String implName) {
            try {
                if (implName == null) {
                    implName = getImplementationName(cl);
                    if (implName == null)
                        return null;
                }
                Class instanceClass = cl.loadClass(implName);
                HelperContextFactory instance = instanceMap.get(instanceClass);
                if (instance == null) {
                    instance = (HelperContextFactory) instanceClass.newInstance();
                    instanceMap.put(instanceClass, instance);
                }
                return instance;
            } catch (Exception e) {
                return null;
            }
        }

      protected String getImplementationName(ClassLoader cl) throws IOException {
            InputStream is = cl.getResourceAsStream(SERVICE_RESOURCE_NAME);
            if (is == null) return null;
            InputStreamReader in = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(in, 128);
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    int i = line.indexOf('#');
                    if (i != -1) {
                        line = line.substring(0, i);
                    }
                    line = line.trim();
                    if (line.length() > 0) return line;
                }
                return null;
            } finally {
                reader.close();
            }
        }

        protected String getImplementationName() {
            try {
              return (String)AccessController.doPrivileged(new PrivilegedAction() {
                  public Object run() {
                      return System.getProperty(PROPERTY_NAME);
                  }
              });
            } catch (SecurityException e) {
                return null;
            }
        }

        protected ClassLoader getContextClassLoader() {
            try {
              return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
                  public Object run() {
                      return Thread.currentThread().getContextClassLoader();
                  }
              });
            } catch (SecurityException e) {
                return null;
            }
        }

    }

}


/**
 * <copyright>
 *
 * Service Data Objects
 * Version 3.0
 * Licensed Materials
 *
 * (c) Copyright BEA Systems, Inc., International Business Machines Corporation,
 * Oracle Corporation, Primeton Technologies Ltd., Rogue Wave Software, SAP AG.,
 * Software AG., Sun Microsystems, Sybase Inc., Xcalia, Zend Technologies,
 * 2005-2008. All rights reserved.
 *
 * </copyright>
 *
 */
package commonj.sdo.helper;

import java.util.Map;

import commonj.sdo.helper.HelperContext;
import commonj.sdo.impl.ExternalizableDelegator.Resolvable;

public abstract class HelperContextFactory {

    public HelperContextFactory() {
    }

    /**
     * Must return false if the HelperContextFactory provides spec defined behaviour
     * in a way that is not compliant with the spec (such as custom serialization).
     * HelperContextFactory implemenations that return false will not be candidates
     * for the following call:
     * SDO.getHelperContextFactory();
     */
    public abstract boolean isStandard();

    /**
     * Gets the implementation's default HelperContext.
     * @return a HelperContext object
     */
    public abstract HelperContext getDefaultHelperContext();

    /**
     * Create a new HelperContext in this implementation.
     * @return a HelperContext object
     */
    public abstract HelperContext createHelperContext();

    /**
     * Create a new HelperContext in this implementation.
     * @param properties - Properties required to initialize the HelperContext.
     * @return a HelperContext object
     */
    public abstract HelperContext createHelperContext(Map properties);

    /**
     * Create a new HelperContext in this implementation.
    * @param classLoader - The class loader for the generated static classes (if any).
     * @return a HelperContext object
     */
    public abstract HelperContext createHelperContext(ClassLoader classLoader);

    /**
     * Create a new HelperContext in this implementation.
     * @param classLoader - The class loader for the generated static classes (if any).
     * @param properties - Properties required to initialize the HelperContext.
     * @return a HelperContext object
     */
    public abstract HelperContext createHelperContext(ClassLoader classLoader, Map properties);

    public abstract Resolvable createResolvable();

    public abstract Resolvable createResolvable(Object target);

}




[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]