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

📄 mmirightsmanager.java

📁 Sun公司Dream项目
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            rex.printStackTrace();
            logger.severe("message data object is probably not of type MMIResponse ");
        }
    
        // rights granted?
        ActionStat stat = null;
        MMIRightsResponseElement [] mre = response.getMMIRightsResponseElement();
        // Assume that the number of elements is 1
        if( mre[0].getNotification().getNotification().equalsIgnoreCase("granted") ) {
            stat = new ActionStat( true );
        } else {
            stat = new ActionStat( false );
        }
      
        if( mre[0].getNotification().getNotification().equalsIgnoreCase("error") ) {
            logger.warning("MMI notification: ERROR");
        }
        
        if( stat.getPermission() ) { 
            // get keys if any available 
            // logger.severe("Uncomment in production FROM HERE--------------------------");
            ///* UNCOMMENT in production XXX XXX
                byte [] keys;
                try {
                    keys = mre[0].getKeys().getKeys();
                } catch(Exception ex) {
                    ex.printStackTrace();
                    keys = null;
                }

                stat.keys = keys;
            //*/
            // logger.severe("Uncomment in production TO HERE--------------------------");
          
            // for now just copy all values in the instat into the outstat. 
            stat.num = inStat.num;
            stat.start = inStat.start;
            stat.end = inStat.end;
            stat.scale = inStat.scale;
            stat.target = inStat.target;    
        }

        return stat;
    }

    /**
     * Dispatches MMI request, parses the result and logs the result into the
     * repo. 
     */
    private ActionStat dispatchMMI( String contentId, ActionId actionId, String
    mediaUrl, int mesgType, ActionStat inStat ) {
        // XXX UNCOMMENT IN PRODUCTION XXX
        logger.severe("Uncomment in production FROM HERE--------------------------");
        
        String licenseServer = _props.getProperty( LICENSE_SERVER ); 
        String portNumber = _props.getProperty( PORT_NUMBER );
        String action = _props.getProperty( ACTION );
        logger.info("From props: licenseServer " + licenseServer + " portNumber " + portNumber);
        
        // create the url from where the mmiclient will fetch info
        URL url = null;
        try {
            url = new URL( "http", licenseServer, Integer.parseInt(portNumber), action );
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            logger.severe("Trouble forming url");
        }
        logger.info("url set on MMI: " + url );

        _mmiClient.setURL(url);

        // make an mmiPlainTextMessage
        MMIPlainTextMessage mesg = prepareMMIPlainTextMessage( mesgType, contentId, actionId, inStat );
        logger.info("MMIPlainTextMessage " + mesg.print("\n"));
        MMIMessage mmimesg = null;
        try { // request by GET
             mmimesg = _mmiClient.requestRightsByGET( mesg );
        } catch( Exception ex ) {
            ex.printStackTrace();
            logger.severe("requestRightsByGet failed!");
        }

        // from the mmimesg extract relevant information for our use.
        ActionStat outStat;
        if( _props.getProperty( DEBUG ) != null && _props.getProperty( DEBUG ).equals("true") )
            outStat = new ActionStat(true);
        else
            outStat = extractActionStat( mmimesg, inStat );
         
        return outStat;

        //return outStat;
        /* 
        logger.severe("Uncomment in production TO HERE--------------------------");
        byte [] keys = {
(byte)0x02,(byte)0x10,(byte)0x10,(byte)0x97,(byte)0x54,(byte)0xE3,(byte)0x34,(byte)0xC3,(byte)0x27,(byte)0xE8,(byte)0x36,(byte)0xEA,(byte)0x00,(byte)0xDA,(byte)0x83,(byte)0x14,(byte)0x4E,(byte)0xC6,(byte)0x21,(byte)0x9D,(byte)0x89,(byte)0x24,(byte)0xCD,(byte)0x6F,(byte)0x62,(byte)0x8D,(byte)0xD7,(byte)0x34,(byte)0x70,(byte)0x2F,(byte)0xEC,(byte)0x3B,(byte)0xFE,(byte)0x52,(byte)0x0E };
        ActionStat outStat = new ActionStat(true);
        outStat.keys = keys;
        return outStat;
        */
     }

    // ------------------------------------------------------------------
    // FUNCTIONS BELOW THIS CALLED FROM DRIVER CODE 
    /**
     * Called from driver code (possibly native code) to check if a particular
     * action is permitted under conditions stated in ActionStat. It first
     * checks local repository to see if rights already requested for. If not
     * then prepares MMI request, gets results and logs them. 
     */
    public boolean checkAction( int id, String url, 
                                 ActionStatNative natStat  ) {
        
        ActionId actionId = ActionId.valueOf( id );
        String contentId = getContentId( url );
        
        logger.info("ActionId " + actionId + " url " + url + " natStat " + natStat );
        // Convert it to something we can use.
        ActionStat inStat = new ActionStat( natStat ); 

        logger.finest("inStat " + inStat);
        
        // check in repo
        ActionStat outStat = _repo.getStat( contentId, actionId );
        if ( outStat == null || ( outStat.num == 0 && outStat.getPermission() == true ) ) {
            // not in repo? get from mmi
            if( outStat == null ) {
                logger.finest( actionId + " " + url + " stat not in repo" );
            } else {
                logger.finest( "Fetching using MMI since Outstat permission:" +
                outStat.getPermission() + " and outstat.num " + outStat.num );
            }
            outStat = dispatchMMI( contentId, actionId, url, RIGHTS_REQUEST, inStat );    
            logger.info(outStat.toString());
        }
        
        if ( outStat == null ) {
            logger.severe("stat after dispatchMMI is null");
            return false;      
        } else { // insert it back into the repository
            _repo.putStat( contentId, actionId, outStat );
        }
    
        // compare inStat and outStat and decided if request is permitted.
        boolean isPermitted = outStat.matchStat( inStat );
        logger.finest("Ispermitted? " + isPermitted);
         
        // doesn't do anything. forces the instat to update values in the native
        // object. These will become visible to the native code on return from
        // this function. 
        inStat.getNativeObject();

        return isPermitted;
    }

    /**
     * Release all rights that may have been acquired previously 
     */
    public void releaseRights( String url ) {
        String contentId = getContentId( url );
        
        // cycle through all actions and release rights if action was previously
        // granted.
        for( ActionId id : ActionId.values() ) {
            ActionStat inStat = _repo.getStat( contentId, id );    
            if( inStat != null && inStat.getPermission() && inStat.num > 0 ) {
                ActionStat outStat = dispatchMMI( contentId, id, url, RIGHTS_RELEASE, inStat );
                if( outStat.getPermission() == true ) {
                    logger.info( id + " rights released for " + url );
                } else {
                    logger.warning("Cannot release " + id + " rights for " + url);
                }
            }
            
            logger.info("Clearing cache for " + contentId + " " + id);
            _repo.rmStat( contentId, id );
        }
    }
 
    /**
     * Readies a media for consumption be getting keys. Keys are stored in a
     * license file which is configured from the configure.txt 
     */ 
    public void prepareKeys( int id, String url )
    {
        ActionId actionId = ActionId.valueOf( id );
        String contentId = getContentId( url );
        logger.info("PrepareKeys: actionId " + actionId + " contentId " + contentId );        

        /* Get stat from the repo */
        ActionStat stat = _repo.getStat( contentId, actionId );
        
        /* get keys from the stat and print to */ 
        String licenseName = _props.getProperty( LICENSE_NAME );
        logger.info("LicenseName value is " + licenseName);

        /* We dont have enough rights to prepare keys */
        if( stat.num <= 0 ) {
            logger.warning("insufficient rights to prepare keys");         

            if( _props.getProperty( DEBUG ) != null && _props.getProperty( DEBUG ).equals("true") ) 
                ;
            else {
                File f = new File( licenseName );
                f.delete();
                logger.info("No keys. Deleting license file");
                return;
            }
        }

        
        stat.num--;
      
        // replace the stat with the num decremented
        _repo.putStat( contentId, actionId, stat );

        FileOutputStream outStream; 
        if( _props.getProperty( DEBUG ) != null && _props.getProperty( DEBUG ).equals("true") )
            ;
        else {
            try {
                if( stat.keys != null  ) {
                    outStream = new FileOutputStream( licenseName );
                    outStream.write( stat.keys );
                    outStream.close();
                    logger.info("Wrote to license file");
                } else {
                    File f = new File( licenseName );
                    f.delete();
                    logger.info("No keys. Deleting license file");
                }
            } catch( Exception ex ) {
                ex.printStackTrace();
                logger.warning("Cannot open licenseName ");
            }
        }

        return;
    }
   
    /**
     * Called from driver code (possibly native code) to report the usage of
     * rights for a particular action and media url. This information is updated
     * in the local repository and may be used in determining the result of
     * future checkActions. 
     */ 
    public void reportUsage( int id, String url ) {
        ActionId actionId = ActionId.valueOf( id );
        String contentId = getContentId( url );
        logger.info("Reporting action " + actionId + " url " + url );
        ActionStat stat = _repo.getStat( contentId, actionId );
       
        if( stat != null ) {
            logger.finest("Decrementing stat.num");
            stat.num--;
        }

        _repo.putStat( contentId, actionId, stat );
    }
}

⌨️ 快捷键说明

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