/***************************************
 *                                     *
 *  JBoss: The OpenSource J2EE WebOS   *
 *                                     *
 *  Distributable under LGPL license.  *
 *  See terms of license at gnu.org.   *
 *                                     *
 ***************************************/

package org.jboss.iiop.csiv2;

import org.omg.CORBA.LocalObject;
import org.omg.CORBA.Policy;
import org.omg.CORBA.ORB;

import org.omg.IOP.Codec;
import org.omg.IOP.TaggedComponent;

import org.jboss.iiop.CorbaORBService;
import org.jboss.logging.Logger;
import org.jboss.metadata.IorSecurityConfigMetaData;

/**
 * Implements <code>org.omg.CORBA.Policy</code> objects containing
 * csiv2 ior security config info
 *
 * @author  Dimitris.Andreadis@jboss.org
 * @version $Revision: 1.6.4.2 $
 */
public class CSIv2Policy 
   extends LocalObject
   implements Policy
{
   /** @since 4.0.1 */
   static final long serialVersionUID = -8487980590230439410L;

   // Static  -----------------------------------------------------------------
   private static final Logger log = Logger.getLogger(CSIv2Policy.class);
   
   // TODO: contact request@omg.org to get a policy type
   public static final int TYPE = 0x87654321; 
   
   // Private -----------------------------------------------------------------
   private TaggedComponent sslTaggedComponent;
   private TaggedComponent secTaggedComponent;
   
   // Constructor -------------------------------------------------------------
   public CSIv2Policy(TaggedComponent sslTaggedComponent, 
                      TaggedComponent secTaggedComponent)
   {
      this.sslTaggedComponent = sslTaggedComponent;
      this.secTaggedComponent = secTaggedComponent;
   }
   
   public CSIv2Policy(IorSecurityConfigMetaData metadata, Codec codec)
   {
      if (log.isDebugEnabled())
         log.debug(metadata);
      
      // convert the ior metadata to a cached security tagged component
      
      try {
         // get the singleton orb
         ORB orb = ORB.init();

         this.sslTaggedComponent =
            CSIv2Util.createSSLTaggedComponent(
               metadata,
               codec,
               CorbaORBService.getTheActualSSLPort(),
               orb);

         this.secTaggedComponent =
            CSIv2Util.createSecurityTaggedComponent(
               metadata,
               codec,
               CorbaORBService.getTheActualSSLPort(),
               orb);
      }
      catch (Exception e) {
         throw new RuntimeException("Unexpected exception " + e);
      }
   }

   /**
    * Return a copy of the cached SSL TaggedComponent
   **/
   public TaggedComponent getSSLTaggedComponent()
   {
      return CSIv2Util.createCopy(this.sslTaggedComponent);
   }
   
   /**
    * Return a copy of the cached CSI TaggedComponent
   **/
   public TaggedComponent getSecurityTaggedComponent()
   {
      return CSIv2Util.createCopy(this.secTaggedComponent);
   }
   
   // org.omg.CORBA.Policy operations -----------------------------------------
   /**
    * Returns a copy of the Policy object.
    */
   public Policy copy() 
   {
      return new CSIv2Policy(getSSLTaggedComponent(),
                             getSecurityTaggedComponent());
   }
   
   /**
    * Destroys the Policy object.
    */
   public void destroy() 
   {
      this.sslTaggedComponent = null;
      this.secTaggedComponent = null;
   }

   /**
    * Returns the constant value that corresponds to the type of the policy 
    * object.
    */
   public int policy_type() 
   {
      return TYPE;
   }

    public String toString()
    {
        return "CSIv2Policy[" + this.sslTaggedComponent + ", " 
                              + this.secTaggedComponent + "]";
    }
}