001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2016 ForgeRock AS. 015 */ 016package org.opends.server.schema; 017 018import static org.opends.server.types.Schema.*; 019 020import java.util.List; 021import java.util.Map; 022 023import org.forgerock.opendj.ldap.schema.AttributeType; 024import org.forgerock.opendj.ldap.schema.Schema; 025import org.forgerock.opendj.ldap.schema.SchemaBuilder; 026import org.forgerock.opendj.ldap.schema.SchemaElement; 027import org.opends.server.core.ServerContext; 028import org.opends.server.util.ServerConstants; 029 030/** 031 * Provides common operations for server schema elements. 032 */ 033public class ServerSchemaElement 034{ 035 036 /** The underlying schema element. */ 037 private final SchemaElement element; 038 039 /** 040 * Creates an element. 041 * 042 * @param element 043 * The schema element to wrap. 044 */ 045 public ServerSchemaElement(SchemaElement element) 046 { 047 this.element = element; 048 } 049 050 /** 051 * Retrieves the definition string used to create this schema element 052 * and including the X-SCHEMA-FILE extension. 053 * 054 * @return The definition string used to create this attribute 055 * type including the X-SCHEMA-FILE extension. 056 */ 057 public String getDefinitionWithFileName() 058 { 059 final String definition = element.toString(); 060 return addSchemaFileToElementDefinitionIfAbsent(definition, getSchemaFile()); 061 } 062 063 /** 064 * Returns the description of this schema element. 065 * 066 * @return The description of this schema element, or the empty string if it does not have a description. 067 */ 068 public String getDescription() 069 { 070 return element.getDescription(); 071 } 072 073 /** 074 * Returns a map of extra properties of this schema element. 075 * 076 * @return An unmodifiable map containing all of the extra properties associated with this schema element. 077 */ 078 public Map<String, List<String>> getExtraProperties() 079 { 080 return element.getExtraProperties(); 081 } 082 083 /** 084 * Returns the single value of the provided extra property. 085 * 086 * @param property 087 * The name of property to retrieve. 088 * @return the single value of the property 089 */ 090 public String getExtraPropertyAsSingleValue(String property) 091 { 092 List<String> values = element.getExtraProperties().get(property); 093 return values != null && !values.isEmpty() ? values.get(0) : null; 094 } 095 096 /** 097 * Returns the origin of the provided schema element. 098 * 099 * @return the origin of the provided schema element. 100 */ 101 public String getOrigin() 102 { 103 return getExtraPropertyAsSingleValue(ServerConstants.SCHEMA_PROPERTY_ORIGIN); 104 } 105 106 /** 107 * Returns the schema file of the provided schema element. 108 * 109 * @return the schema file of the provided schema element. 110 */ 111 public String getSchemaFile() 112 { 113 return getExtraPropertyAsSingleValue(ServerConstants.SCHEMA_PROPERTY_FILENAME); 114 } 115 116 /** 117 * Updates the property of the provided attribute type. 118 * 119 * @param serverContext 120 * the server context 121 * @param attributeType 122 * attribute type to update 123 * @param property 124 * the property to set 125 * @param values 126 * the values to set 127 * @return the updated attribute type 128 */ 129 public static AttributeType updateProperty(ServerContext serverContext, AttributeType attributeType, String property, 130 String...values) 131 { 132 SchemaBuilder schemaBuilder = 133 new SchemaBuilder(serverContext != null ? serverContext.getSchemaNG() : Schema.getDefaultSchema()); 134 AttributeType.Builder builder = 135 schemaBuilder.buildAttributeType(attributeType).removeExtraProperty(property, (String) null); 136 if (values != null && values.length > 0) 137 { 138 builder.extraProperties(property, values); 139 return builder.addToSchemaOverwrite().toSchema().getAttributeType(attributeType.getNameOrOID()); 140 } 141 return attributeType; 142 } 143}