You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/09/20 11:58:37 UTC

cvs commit: jakarta-avalon-excalibur/loader/src/java/org/apache/excalibur/policy/reader PolicyReader.java policy.dtd

donaldp     2002/09/20 02:58:37

  Modified:    loader/src/java/org/apache/excalibur/policy/reader
                        policy.dtd
  Added:       loader/src/java/org/apache/excalibur/policy/reader
                        PolicyReader.java
  Log:
  Add in reader for Policy
  
  Revision  Changes    Path
  1.2       +2 -1      jakarta-avalon-excalibur/loader/src/java/org/apache/excalibur/policy/reader/policy.dtd
  
  Index: policy.dtd
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/loader/src/java/org/apache/excalibur/policy/reader/policy.dtd,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- policy.dtd	20 Sep 2002 09:14:18 -0000	1.1
  +++ policy.dtd	20 Sep 2002 09:58:37 -0000	1.2
  @@ -20,7 +20,8 @@
   The policy is the document root and contians the other elements:
   -->
   <!ELEMENT policy (keystore*,grant*)>
  -  <!ATTLIST policy id ID #IMPLIED
  +  <!ATTLIST policy
  +          version CDATA #REQUIRED
             xmlns CDATA #FIXED "http://jakarta.apache.org/avalon/dtds/phoenix/policy_1_0.dtd" >
   
   <!--
  
  
  
  1.1                  jakarta-avalon-excalibur/loader/src/java/org/apache/excalibur/policy/reader/PolicyReader.java
  
  Index: PolicyReader.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.policy.reader;
  
  import java.util.ArrayList;
  import org.apache.excalibur.policy.metadata.GrantMetaData;
  import org.apache.excalibur.policy.metadata.KeyStoreMetaData;
  import org.apache.excalibur.policy.metadata.PolicyMetaData;
  import org.apache.excalibur.policy.metadata.PermissionMetaData;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  
  /**
   * This class builds a {@link PolicyMetaData} object from
   * specified XML document.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/20 09:58:37 $
   */
  public class PolicyReader
  {
      /**
       * Build ClassLoader MetaData from a DOM tree.
       *
       * @param element the root element
       * @return the meta data
       * @throws Exception if malformed DOM
       */
      public PolicyMetaData build( final Element element )
          throws Exception
      {
          final String version = element.getAttribute( "version" );
          if( !"1.0".equals( version ) )
          {
              final String message = "Bad version:" + version;
              throw new Exception( message );
          }
  
          final NodeList keyStoreConfigs = element.getElementsByTagName( "keystore" );
          final KeyStoreMetaData[] keyStores = buildKeyStores( keyStoreConfigs );
  
          final NodeList grantConfigs =
              element.getElementsByTagName( "grant" );
          final GrantMetaData[] grants = buildGrants( grantConfigs );
  
          return new PolicyMetaData( keyStores, grants );
      }
  
      /**
       * Build an array of GrantMetaDatas from node list.
       *
       * @param elements the nodes to process
       * @return the GrantMetaData
       */
      private GrantMetaData[] buildGrants( final NodeList elements )
          throws Exception
      {
          final ArrayList grants = new ArrayList();
          final int length = elements.getLength();
  
          for( int i = 0; i < length; i++ )
          {
              final Element element = (Element) elements.item( i );
              final GrantMetaData grant = buildGrant( element );
              grants.add( grant );
          }
  
          return (GrantMetaData[]) grants.toArray( new GrantMetaData[ grants.size() ] );
      }
  
      /**
       * Build a GrantMetaData from an element.
       *
       * @param element the nodes to process
       * @return the GrantMetaData
       */
      private GrantMetaData buildGrant( final Element element )
          throws Exception
      {
          final String codeBase = element.getAttribute( "code-base" );
          final String signedBy = element.getAttribute( "signed-by" );
          final String keyStore = element.getAttribute( "key-store" );
          final NodeList permissionElements =
              element.getElementsByTagName( "permission" );
          final PermissionMetaData[] permissions = buildPermissions( permissionElements );
          return new GrantMetaData( codeBase, signedBy, keyStore, permissions );
      }
  
      /**
       * Build an array of PermissionMetaDatas from node list.
       *
       * @param elements the nodes to process
       * @return the PermissionMetaDatas
       */
      private PermissionMetaData[] buildPermissions( final NodeList elements )
          throws Exception
      {
          final ArrayList grants = new ArrayList();
          final int length = elements.getLength();
  
          for( int i = 0; i < length; i++ )
          {
              final Element element = (Element) elements.item( i );
              final PermissionMetaData permission = buildPermission( element );
              grants.add( permission );
          }
  
          return (PermissionMetaData[]) grants.toArray( new PermissionMetaData[ grants.size() ] );
      }
  
      /**
       * Build a PermissionMetaData from an element.
       *
       * @param element the node to process
       * @return the PermissionMetaData
       */
      private PermissionMetaData buildPermission( final Element element )
          throws Exception
      {
          final String classname = element.getAttribute( "class" );
          final String target = element.getAttribute( "target" );
          final String action = element.getAttribute( "action" );
          return new PermissionMetaData( classname, target, action );
      }
  
      /**
       * Build an array of KeyStore meta datas from node list.
       *
       * @param elements the nodes to process
       * @return the keyStores
       */
      private KeyStoreMetaData[] buildKeyStores( final NodeList elements )
          throws Exception
      {
          final ArrayList keyStores = new ArrayList();
          final int length = elements.getLength();
  
          for( int i = 0; i < length; i++ )
          {
              final Element element = (Element) elements.item( i );
              final KeyStoreMetaData keyStore = buildKeyStore( element );
              keyStores.add( keyStore );
          }
  
          return (KeyStoreMetaData[]) keyStores.toArray( new KeyStoreMetaData[ keyStores.size() ] );
      }
  
      /**
       * Build a KeyStoreMetaData from an element.
       *
       * @param element the nodes to process
       * @return the keyStore
       */
      private KeyStoreMetaData buildKeyStore( final Element element )
          throws Exception
      {
          final String name = element.getAttribute( "name" );
          final String location = element.getAttribute( "location" );
          final String type = element.getAttribute( "type" );
          return new KeyStoreMetaData( name, location, type );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>