⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vouchergeneratorhelper.java

📁 Sun公司Dream项目
💻 JAVA
字号:
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * http://www.opensource.org/licenses/cddl1.php.  If 
 * applicable, add the following below this CDDL HEADER, 
 * with the fields enclosed by brackets "[]" replaced 
 * with your own identifying information: 
 * Portions Copyright [yyyy]
 * [name of copyright owner]
 */ 

/*
 * $(@)VoucherGeneratorHelper.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
 * 
 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
 */
package com.sun.dream;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;


/**
 * This class creates and caches instances of VoucherGenerator 
 * implementations specified for different DRM types
 * in the OPERA servers configuration file. The class provides a 
 * convinence method that can be used to get an voucher generator 
 * implementation object for a given DRM type.
 *  
 *
 */
public class VoucherGeneratorHelper {
    
    /**
     * Initialize the drmType - voucher generator implementation
     * class name mapping and the drmtype - configuration string mapping.
     * 
     * @param drmTypeClassMap
     * @param drmTypeConfig
     * @throws ServletException
     */
    public void init(Map drmTypeClassMap, Map drmTypeConfig) 
    throws ServletException {
        int mapsize = drmTypeClassMap.size();
        try {
            m_voucherClass = Class.forName("com.sun.dream.VoucherGenerator");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new ServletException("VoucherGenerator interface is not in classpath probably!!");
        }
        Iterator keyValuePairs1 = drmTypeClassMap.entrySet().iterator();
        for (int i = 0; i < mapsize; i++)
        {
          Map.Entry entry = (Map.Entry) keyValuePairs1.next();
          String drmType = (String) entry.getKey();
          String voucherGenClassName = (String) entry.getValue();
          Class  voucherGenClass = createClass(voucherGenClassName);
          m_voucherGeneratorInstances.put(drmType, voucherGenClass);
          String configValue = (String) drmTypeConfig.get(drmType);
          if (configValue == null) {
              configValue = "";
          }
          m_voucherGeneratorConfig.put(drmType, configValue);
        }
        
    }    
    
    Class createClass(String className) 
    throws ServletException
    {
      Class classDefinition = null;
      try {
        classDefinition = Class.forName(className);
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
          throw new ServletException("Not able to find class " + className + " in classpath.");
      }
      return classDefinition;
    }

    /**
     * Returns the Voucher Generator implementation
     * object for a given drm type.
     * 
     * @param drmType
     * @return
     * @throws ServletException
     */
    public VoucherGenerator createObject(String drmType) 
    throws ServletException
    {
      Class classDefinition = (Class) m_voucherGeneratorInstances.get(drmType);
      if (classDefinition == null) {
          throw new ServletException("Voucher generator implementation class for the drm type " + drmType + " is not available.");
      }
      VoucherGenerator object = null;
      try {
        object = (VoucherGenerator) classDefinition.newInstance();
        String config = (String) m_voucherGeneratorConfig.get(drmType);
        if (config == null) {
            config = "";
        }
        object.addConfigParameter(config);
        if (!m_voucherClass.isInstance(object)) {
            throw new ServletException("The VoucherGenerator implementation for drm type " + drmType + " does not inmplements the com.sun.opera.VoucherGenerator interface");
        }
      } catch (InstantiationException e) {
          e.printStackTrace();
          throw new ServletException("Error in creating an instance of the implementation class for the drm type " + drmType);
      } catch (IllegalAccessException e) {
          e.printStackTrace();
          throw new ServletException("Error in creating an instance of the implementation class for the drm type " + drmType); 
      }
      return object;
    }

    
      
    Map      m_voucherGeneratorInstances = new HashMap();
    Map      m_voucherGeneratorConfig    = new HashMap();
    Class    m_voucherClass              = null;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -