/** * $Id: XmlBusinessHoursAcrPm_java.txt,v 1.7 2009/08/14 23:51:33 norb Exp $ * * Copyright (c) 1996-2009 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package examples.acrs; import examples.acrs.BusinessHoursAcr; import com.cafesoft.cams.access.AccessControlRule; import com.cafesoft.cams.access.AbstractAccessControlRulePersistenceManager; import com.cafesoft.cams.access.SyntaxException; import com.cafesoft.cams.Config; import com.cafesoft.cams.ConfigException; import com.cafesoft.cams.PersistenceException; import com.cafesoft.core.xml.dom.XmlDomUtils; import com.cafesoft.core.log.Logger; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import java.io.PrintWriter; import java.io.IOException; /** * XmlBusinessHoursAcrPm implements methods to create, load, store, and * remove instances of BusinessHoursAcr. *

* XML representation of BusinessHoursAcr in "access-control-policy.xml": *

 * 
 *         
 * 
 * 
* * @version $Revision: 1.7 $ $Date: 2009/08/14 23:51:33 $ * @author Norbert K. Kuhnert */ public class XmlBusinessHoursAcrPm extends AbstractAccessControlRulePersistenceManager { /** * Create a new XmlBusinessHoursAcrPm instance. */ public XmlBusinessHoursAcrPm() { } // // extending AbstractAccessControlRulePersistenceManager // /** * Create a new/empty BusinessHoursAcr. * * @return a new/empty BusinessHoursAcr instance. */ public AccessControlRule acrCreate() { return new BusinessHoursAcr(); } /** * Load an BusinessHoursAcr from persistent storage. * * @param object the XML DOM element associated with the * BusinessHoursAcr to be created. * * @return the BusinessHoursAcr instance. * @exception PersistenceException if an error creating the new * AccessControlRule. * @exception IllegalArgumentException if the parameter is null or * it's type is not supported by the factory implementation. */ public AccessControlRule acrLoad(Object object) throws PersistenceException { if (object == null) throw new IllegalArgumentException( "null XML DOM Element"); if (! (object instanceof Element)) throw new IllegalArgumentException( "parameter is not an XML DOM Element"); Element element = (Element)object; // The Element must have local name "business-hours-acr" if (!element.getLocalName().equalsIgnoreCase("business-hours-acr")) throw new PersistenceException( "unexpected top-level tag name: " + element.getLocalName() + ", expected: business-hours-acr"); // Create the AccessControlRule name. BusinessHoursAcr acr = new BusinessHoursAcr( element.getAttribute("id")); // Traverse all Child Nodes to populate the rule NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); // Branch to handle different Child node types. switch (node.getNodeType()) { case Node.ELEMENT_NODE: Element child = (Element)node; // Make sure the child Element is in the // same namespace as the business-hours-acr if (XmlDomUtils.nameSpaceURIMatches(element, child)) { // Set the start/end hours try { acr.setStartHour(Integer.parseInt( child.getAttribute("start-hour"))); } catch (SyntaxException e) { logger.error(this, "invalid start-hour", e); throw new PersistenceException(e); } try { acr.setEndHour(Integer.parseInt( child.getAttribute("end-hour"))); } catch (SyntaxException e) { logger.error(this, "invalid end-hour", e); throw new PersistenceException(e); } } break; case Node.TEXT_NODE: // Ignore white space text break; default: // Ignore all other node types. } } // Done populating the AccessControlRule return acr; } /** * Store an AccessControlRule to persistent storage. * * @param acr the AccessControlRule to be stored. * @param dest an object that may be needed for some AccessControlRule * storage destination types. For example, if the AccessControlRule * is a file, then a PrintStream might be provided. Some storage * destinations (e.g. a relational database), may not use this * parameter. * * @exception PersistenceException if an error storing the * AccessControlRule. * @exception IllegalArgumentException if a required parameter is null, * or it's type is not supported by the implementation. */ public void acrStore(AccessControlRule acr, Object dest) throws PersistenceException { // Make sure the AccessControl rule was specified and is the // expected type. if (acr == null) throw new PersistenceException("null access control rule"); if (!(acr instanceof BusinessHoursAcr)) throw new PersistenceException("unsupported AccessControlRule type: " + acr.getClass().getName() + ", expected: " + BusinessHoursAcr.class.getName()); // Since storage is XML, the destination Object is expected // to be a PrintWriter. if (dest == null) throw new PersistenceException("null destination. Expected PrintWriter"); if (!(dest instanceof PrintWriter)) throw new PersistenceException("unsupported destination type: " + dest.getClass().getName() + ", expected PrintWriter"); BusinessHoursAcr rule = (BusinessHoursAcr)acr; PrintWriter out = (PrintWriter)dest; // Write the BusinessHoursAcr to the PrintWriter as XML out.print("\t\t"); out.print("\t\t\t"); out.println("\t\t"); } /** * Remove an AccessControlRule from persistent storage. * * @param acr the AccessControlRule to be removed from persistent * storage. * * @return the AccessControlRule instance. * @exception PersistenceException if an error creating the new * AccessControlRule. * @exception IllegalArgumentException if the parameter is null or * it's type is not supported by the factory implementation. */ public void acrRemove(AccessControlRule acr) throws PersistenceException { // Do nothing. The AccessControlRule instance is simply not // written to the XML file. (Note: Whatever class is editing // the AccessControlPolicy containing the referenced rule, // will simply remove it from it's AccessControlRuleLibrary. // When that is persisted, this rule will not be written). } } // End of class: XmlBusinessHoursAcrPm