8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: SMSFlatFileObject.java,v 1.9 2008/06/25 05:44:09 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted [2011] [ForgeRock AS]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.sm.flatfile;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.FileFilter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Enumeration;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Properties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.regex.Pattern;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.naming.directory.ModificationItem;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.SMSEntry; // for the sunserviceID hack.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.SMSException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.SchemaException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceAlreadyExistsException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceNotFoundException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.CaseInsensitiveHashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.CaseInsensitiveHashSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.CaseInsensitiveProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.CaseInsensitiveTreeSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class represents a configuration object stored in a file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Each file lives in a file system under a directory of configuration
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * objects organized in a hierarchy. Each level in the hierarchy
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is represented by a directory. The naming convention of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a configuration object is hierarchy levels separated by a comma,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for example "ou=serviceName,ou=services,dc=sun,dc=com".
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This object would live in the directory
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <config-dir>/dc=com/dc=sun/ou=services/ou=serviceName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The directory has a file with the object's attributes in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * java.util.Properties format. The file name is Attributes.properties.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Attributes with multi-values are seperated by a comma.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * A comma within a value is encoded as %2C, and a % within a value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is encoded with %25.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class SMSFlatFileObject extends SMSFlatFileObjectBase {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Properties mNameMap = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private File mNameMapHandle = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String DEFAULT_NAMEMAP_FILENAME = "NameMap.properties";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String DEFAULT_ATTRIBUTE_FILENAME = "Attributes.properties";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Simple class that looks for subentries with name matching the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * filter. Only wildcard '*' character is supported in the filter.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private class FilenameFilter implements FileFilter {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Pattern to match
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Pattern pattern;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Default constructor
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public FilenameFilter(String filter) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filter != null && filter.length() != 0 &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster !filter.equals("*")) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Replace "*" with ".*"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int idx = filter.indexOf('*');
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (idx != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filter = filter.substring(0, idx) + ".*" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filter.substring(idx + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idx = filter.indexOf('*', idx + 2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster pattern = Pattern.compile(filter.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean accept(File file) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filename = file.getName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pattern == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check for all files
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (pattern.matcher(filename.toLowerCase()).matches());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Loads the name mapper, create it if it doesn't exist.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster **/
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void loadMapper()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer nameMapFilename = new StringBuffer(mRootDir);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nameMapFilename.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nameMapFilename.append(DEFAULT_NAMEMAP_FILENAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMapHandle = new File(nameMapFilename.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mNameMapHandle.isFile()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!mNameMapHandle.canRead()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.initialize: cannot read file "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMapHandle.getPath();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMap = loadProperties(mNameMapHandle, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMapHandle.createNewFile();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.initialize: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "cannot create file, " + nameMapFilename +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ". Exception "+e.getMessage();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SecurityException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.initialize: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "cannot create file " + nameMapFilename +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ". Exception " + e.getMessage();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMap = new CaseInsensitiveProperties();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // create root dn if this is a new directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster create(null, mRootDN, new HashMap());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.initialize: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "created SMS object for " + mRootDN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // not possible
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ServiceAlreadyExistsException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.initialize: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRootDN + " already exists");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // also create ou=services this is a new directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster create(null, "ou=services,"+mRootDN, new HashMap());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.initialize: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "created SMS object for ou=services,"+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRootDN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // not possible
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ServiceAlreadyExistsException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.initialize: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "ou=services," + mRootDN + " already exists");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a path to object's attributes file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String getAttrFile(String objName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if the name is present in NameMap.properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String attrFile = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((attrFile = mNameMap.getProperty(objKey)) != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (attrFile);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check immidiate parent
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int index = objName.indexOf(',');
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (index != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objKey = objKey.substring(index + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((attrFile = mNameMap.getProperty(objKey)) != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String dirName = objName.substring(0, index).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb = new StringBuilder(attrFile.length() +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dirName.length() + 2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(attrFile.substring(0, attrFile.length() -
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DEFAULT_ATTRIBUTE_FILENAME.length()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(FileNameEncoder.encode(dirName));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(DEFAULT_ATTRIBUTE_FILENAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (sb.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Construct the file name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new StringBuilder(mRootDir.length()+objName.length()+20);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(mRootDir);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // objName is assumed to be a dn so construct the filepath
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // backwards from the top of directory tree.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster char[] objchars = objName.toCharArray();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int i, j;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (i = j = objchars.length-1; i >= 0; i--) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objchars[i] == ',') {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (i == j) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster j--;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String rdn = new String(objchars, i+1, j-i).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // encode file name in case there are characters
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // unsupported in the FS such as '/' or '\' and '*'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // on windows.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String encodedRdn = FileNameEncoder.encode(rdn);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(encodedRdn);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster j = i-1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (i != j) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String lastRdn = new String(objchars, 0, j-i);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String encodedLastRdn = FileNameEncoder.encode(lastRdn);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(encodedLastRdn);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(DEFAULT_ATTRIBUTE_FILENAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrFile = sb.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return attrFile;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates lookup/search files with the given values under the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * given directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void createLookupFiles(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File dirHandle,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String attr,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set sunserviceids
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb = new StringBuilder(dirHandle.getPath());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(File.separatorChar);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(attr);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append('=');
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String fileprefix = sb.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator i = sunserviceids.iterator(); i.hasNext(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String id = ((String)i.next()).toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File idFile = new File(fileprefix + id);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster idFile.createNewFile();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.createLookupIdFiles: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " File, "+ idFile.getPath() + ". Exception: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster e.getMessage();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error("SMSFlatFileObject.createLookupIdFiles",e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Real routine to get sub entries, used by subEntries() and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * schemaSubEntries().
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServiceNotFoundException if the configuration object is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * not found.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SchemaException if a sub directory name is not in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * expected "ou=..." format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected Set getSubEntries(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filter,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String sidFilter,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean isSubConfig,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int numOfEntries,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean sortResults,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean ascendingOrder
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set subentries = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest(); // wait indefinitely for the read lock.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if object exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = mNameMap.getProperty(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObjectBase.getSubEntries: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " : not found in objects map.";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.warning(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceNotFoundException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File filehandle = new File(filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File parentDir = filehandle.getParentFile();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!parentDir.isDirectory()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.getSubEntries: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " : " + filehandle.getPath() +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " does not exist or is not a directory.";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceNotFoundException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Create file filter for filter and sid filter.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FilenameFilter subentFileFilter = new FilenameFilter(filter);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FilenameFilter sidFileFilter = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sidFilter != null && sidFilter.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // filter also needs to be encoded since the file names
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // are encoded.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (isSubConfig) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sidFileFilter = new FilenameFilter(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSEntry.ATTR_SERVICE_ID + "=" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sidFilter.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sidFileFilter = new FilenameFilter(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSEntry.ATTR_XML_KEYVAL + "=" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sidFilter.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Create set for return, use sorted set if sortResults is true.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sortResults) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subentries = new CaseInsensitiveTreeSet(ascendingOrder);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subentries = new CaseInsensitiveHashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Set all entries that match filter, and that match
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // sunserviceid/sunxmlkeyvalye if sidFilter was not null.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File[] subentriesFound = parentDir.listFiles(subentFileFilter);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int numEntriesAdded = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean done = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; (i < subentriesFound.length) && !done; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File[] sunserviceidFiles = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sidFileFilter == null || ((sunserviceidFiles =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subentriesFound[i].listFiles(sidFileFilter)) != null &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sunserviceidFiles.length > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filename = subentriesFound[i].getName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int equalSign = filename.indexOf('=');
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (equalSign < 0 || equalSign == (filename.length()-1)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.getSubEntries: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Invalid sub entry name found: "+filename;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SchemaException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String subentryname =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FileNameDecoder.decode(filename.substring(equalSign+1));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subentries.add(subentryname);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster numEntriesAdded++;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // stop if number of entries requested has been reached.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // if sort results, need to get the whole list first.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster done = !sortResults && (numOfEntries > 0) &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (numEntriesAdded == numOfEntries);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sortResults && (numOfEntries > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // remove extra entries from the bottom.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((numEntriesAdded - numOfEntries) > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object l = ((CaseInsensitiveTreeSet) subentries).last();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subentries.remove(l);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster numEntriesAdded--;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return subentries;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Delete sunxmlkeyvalue files under the given directory.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void deleteSunXmlKeyValFiles(File dirHandle) throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Construct the file filter and get the files
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb = new StringBuilder(SMSEntry.ATTR_XML_KEYVAL);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("=*");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FilenameFilter filter = new FilenameFilter(sb.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File[] deleteFiles = dirHandle.listFiles(filter);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; deleteFiles != null && i < deleteFiles.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File deleteFile = deleteFiles[i];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster deleteFile.delete();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructor for SMSFlatFileObject.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public SMSFlatFileObject()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Reads in attributes of a configuration object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return A Map with the coniguration object's attributes or null if the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * configuration object does not exist or no attributes are found.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Ignored argument. Access check is assumed to have
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * occurred before reaching this method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param objName Name of the configuration object, expected to be a dn.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SMSException if an IO error occurred during the read.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SchemaException if a format error occurred while reading the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * attributes properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws IllegalArgumentException if objName argument is null or empty.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Map read(SSOToken token, String objName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // check args
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objName == null || objName.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.read: object name is null or empty.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map attrMap = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // check if object exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = mNameMap.getProperty(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.read: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " not found.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Read in file as properties.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File filehandle = new File(filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Properties props = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster props = loadProperties(filehandle, objName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ServiceNotFoundException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // props will be null if object does not exist and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // this func subsequently returns null
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // convert each value string to a Set.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (props != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrMap = new CaseInsensitiveHashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Enumeration keys = props.propertyNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (keys.hasMoreElements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)keys.nextElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String vals = props.getProperty(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((vals != null) && (vals.length() > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrMap.put(key, toValSet(key, vals));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return attrMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates the configuration object. Creates the directory for the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * object and the attributes properties file with the given attributes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Ignored argument. Access check is assumed to have
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * occurred before reaching this method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param objName Name of the configuration object to create. Name is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * expected to be a dn.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param attrs Map of attributes for the object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws IllegalArgumentException if the objName or attrs argument is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * null or empty.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServiceAlreadyExistsException if the configuration object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * already exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SMSException if an IO error occurred while creating the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * configuration object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void create(SSOToken token, String objName, Map attrs)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objName == null || objName.length() == 0 || attrs == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.create: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "One or more arguments is null or empty");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if object already exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " + objName +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " already exists in " + filepath;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceAlreadyExistsException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Now Create the object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey); // recheck
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " + objName +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " already exists in " + filepath;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceAlreadyExistsException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = getAttrFile(objName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File filehandle = new File(filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File parentDir = filehandle.getParentFile();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (parentDir.isDirectory()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " + objName +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " directory " + parentDir.getPath() +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " exists before create!";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceAlreadyExistsException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Put attrs into in properties format,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // replacing any percent's with %25 and commas with %2C
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // in the values.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set sunserviceids = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set sunxmlkeyvals = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // there's no need for case insensitive properties here since
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // we are not reading from it.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Properties props = new Properties();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set keys = attrs.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (keys != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator i = keys.iterator(); i.hasNext(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set vals = (Set)attrs.get(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key.equalsIgnoreCase(SMSEntry.ATTR_SERVICE_ID)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sunserviceids = vals;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (key.equalsIgnoreCase(SMSEntry.ATTR_XML_KEYVAL)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sunxmlkeyvals = vals;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster props.put(key, toValString(vals));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Create directory, property file, etc.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // create directory
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!parentDir.mkdirs()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + ": Could not create directory " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster parentDir.getPath();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // create the attributes properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!filehandle.createNewFile()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + ": Could not create file " + filepath;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.create: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " IOException encountered when creating file "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + filehandle.getPath() + ". Exception: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster e.getMessage();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error("SMSFlatFileObject.create", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // write the attributes properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster saveProperties(props, filehandle, objName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // create sunserviceid files for faster return in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // schemaSubEntries method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sunserviceids != null && !sunserviceids.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster createSunServiceIdFiles(parentDir, sunserviceids);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // create sunxmlkeyvalue files for faster search
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sunxmlkeyvals != null && !sunxmlkeyvals.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster createSunXmlKeyValFiles(parentDir, sunxmlkeyvals);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // add the name in the name map and save.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMap.setProperty(objKey, filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster saveProperties(mNameMap, mNameMapHandle, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // If any error occurred, clean up - remove the directory
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // and files created.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster deleteDir(parentDir);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMap.remove(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw e;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Modify the attributes for the given configuration object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Ignored argument. Access check is assumed to have
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * occurred before reaching this method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param objName Name of the configuration object to modify. Name is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * expected to be a dn.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param mods Array of attributes to modify.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws IllegalArgumentException if objName or mods argument is null or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * empty, or if an error was encountered getting attributes from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * mods argument.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServiceNotFoundException if the attributes properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for the configuration object is not found.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SchemaException if a format error occurred while reading in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * existing attributes properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SMSException if an IO error occurred while reading or writing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to the attributes properties file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void modify(SSOToken token, String objName, ModificationItem[] mods)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((objName == null) || (objName.length() == 0) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (mods == null) || (mods.length == 0)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.modify: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "One or more arguments is null or empty");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if object exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.modify: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " not found.";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceNotFoundException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Now do the modification.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey); // recheck
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.modify: object " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName + " not found.";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceNotFoundException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File filehandle = new File(filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!filehandle.isFile()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.modify: Attributes file for object "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + objName + " not found.";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServiceNotFoundException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Read in attributes in existing file first.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Properties props = loadProperties(filehandle, objName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean hasSunXmlKeyValue = (props.getProperty(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSEntry.ATTR_XML_KEYVAL) == null) ? false: true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Replace modification items in attributes properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < mods.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modifyValues(objName, mods[i], props);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * save attributes properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * sunserviceid's are never modified so don't worry about
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * renaming them in modify().
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster **/
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster saveProperties(props, filehandle, objName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check for sunxmlkeyvalues
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!hasSunXmlKeyValue) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hasSunXmlKeyValue = (props.getProperty(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSEntry.ATTR_XML_KEYVAL) == null) ? false: true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (hasSunXmlKeyValue) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Delete the lookup files and recreate them
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster deleteSunXmlKeyValFiles(filehandle.getParentFile());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set xmlKeyVals = toValSet(SMSEntry.ATTR_XML_KEYVAL,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster props.getProperty(SMSEntry.ATTR_XML_KEYVAL));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster createSunXmlKeyValFiles(filehandle.getParentFile(), xmlKeyVals);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Deletes the configuration object and all objects below it.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Ignored argument. Access check is assumed to have
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * occurred before reaching this method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param objName Name of the configuration object to delete. Name is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * expected to be a dn.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws IllegalArgumentException if objName argument is null or empty.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SMSException if any files for or under the configuration object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * could not be removed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void delete(SSOToken token, String objName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((objName == null) || (objName.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.delete: object name is null or empty.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objKey = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if object exists
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((filepath == null) && mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.delete: " + objName +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ": object not found.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filepath = mNameMap.getProperty(objKey); // recheck.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (mDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.message("SMSFlatFileObject.delete: " + objName +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ": object not found.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File filehandle = new File(filepath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File parentDir = filehandle.getParentFile();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // delete everything from the file dir on.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster deleteDir(parentDir);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // remove all names from name map under the objname.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objName = objName.toLowerCase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Enumeration keysEnum = mNameMap.keys();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (keysEnum.hasMoreElements()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)keysEnum.nextElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key.endsWith(objName)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mNameMap.remove(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster saveProperties(mNameMap, mNameMapHandle, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.writeDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>ture</code> if the configuration object exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token Ignored argument. Access check is assumed to have
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * occurred before reaching this method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param objName Name of the configuration object to check.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true>/code> if the configuration object exists.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws IllegalArgumentException if objName is null or empty.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean entryExists(SSOToken token, String objName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean exists = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (objName == null || objName.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "SMSFlatFileObject.entryExists: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "One or more arguments is null or empty.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readRequest();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String filepath = mNameMap.getProperty(objName.toLowerCase());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (filepath != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster exists = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mRWLock.readDone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return exists;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return a String representing the name of this class.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String toString() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ("SMSFlatFileObject");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Recursively deletes the given directory and all
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * files and directories underneath.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void deleteDir(File dirhandle)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File[] files = dirhandle.listFiles();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // remove all files in each sub-dir
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (files != null && files.length > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < files.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (files[i].isDirectory()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster deleteDir(files[i]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (!files[i].delete()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errmsg = "SMSFlatFileObject.delete: File "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster files[i].getPath()+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " could not be removed!";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mDebug.error(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SMSException(errmsg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dirhandle.delete();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates sunxmlkeyvalue files with the given values under the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * given directory. sunxmlkeyvalue files are created so searching
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for realms does not have to read in every attribute properrties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * file and look for the sunxmlkeyvalue attribute. we just need to look
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for sub directories with the sunxmlkeyvalue file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void createSunXmlKeyValFiles(File dirHandle, Set sunxmlkeyvals)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster createLookupFiles(dirHandle, SMSEntry.ATTR_XML_KEYVAL,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sunxmlkeyvals);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates sunserviceid files with the given values under the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * given directory. sunserviceid files are created so getting
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * schemasubentries does not have to read in every attribute properrties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * file and look for the serviceid attribute. we just need to look
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for sub directories with the sunserviceid file.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void createSunServiceIdFiles(File dirHandle, Set sunserviceids)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster createLookupFiles(dirHandle, SMSEntry.ATTR_SERVICE_ID,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sunserviceids);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}