001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2008 Sun Microsystems, Inc. 026 */ 027 028 package org.opends.server.admin; 029 030 031 032 import java.io.BufferedInputStream; 033 import java.io.IOException; 034 import java.io.InputStream; 035 import java.util.HashMap; 036 import java.util.Map; 037 import java.util.MissingResourceException; 038 import java.util.Properties; 039 040 041 042 /** 043 * A class for retrieving non-internationalized resource properties 044 * associated with a managed object definition. 045 * <p> 046 * Resource properties are not available for the {@link TopCfgDefn}. 047 */ 048 public final class ManagedObjectDefinitionResource { 049 050 // Mapping from definition to property tables. 051 private final Map<AbstractManagedObjectDefinition<?, ?>, 052 Properties> properties; 053 054 // The resource name prefix. 055 private final String prefix; 056 057 058 059 /** 060 * Creates a new resource instance for the named profile. 061 * 062 * @param profile 063 * The name of the profile. 064 * @return Returns the resource instance for the named profile. 065 */ 066 public static ManagedObjectDefinitionResource createForProfile( 067 String profile) { 068 return new ManagedObjectDefinitionResource("admin.profiles." 069 + profile); 070 } 071 072 073 074 // Private constructor. 075 private ManagedObjectDefinitionResource(String prefix) { 076 this.properties = 077 new HashMap<AbstractManagedObjectDefinition<?, ?>, Properties>(); 078 this.prefix = prefix; 079 } 080 081 082 083 /** 084 * Get the resource value associated with the specified key. 085 * 086 * @param d 087 * The managed object definition. 088 * @param key 089 * The resource key. 090 * @return Returns the resource value associated with the specified 091 * key. 092 * @throws MissingResourceException 093 * If the key was not found. 094 * @throws UnsupportedOperationException 095 * If the provided managed object definition was the 096 * {@link TopCfgDefn}. 097 */ 098 public String getString(AbstractManagedObjectDefinition<?, ?> d, String key) 099 throws MissingResourceException, UnsupportedOperationException { 100 if (d.isTop()) { 101 throw new UnsupportedOperationException( 102 "Profile resources are not available for the " 103 + "Top configuration definition"); 104 } 105 106 Properties p = getProperties(d); 107 String result = p.getProperty(key); 108 109 if (result == null) { 110 String baseName = prefix + "." + d.getClass().getName(); 111 String path = baseName.replace('.', '/') + ".properties"; 112 113 throw new MissingResourceException("Can't find resource " 114 + path + ", key " + key, baseName, key); 115 } 116 117 return result; 118 } 119 120 121 122 // Retrieve the properties table associated with a managed object, 123 // lazily loading it if necessary. 124 private synchronized Properties getProperties( 125 AbstractManagedObjectDefinition<?, ?> d) 126 throws MissingResourceException { 127 Properties p = properties.get(d); 128 129 if (p == null) { 130 // Load the resource file. 131 String baseName = prefix + "." + d.getClass().getName(); 132 String path = baseName.replace('.', '/') + ".properties"; 133 InputStream stream = ClassLoaderProvider.getInstance() 134 .getClassLoader().getResourceAsStream(path); 135 136 if (stream == null) { 137 throw new MissingResourceException("Can't find resource " 138 + path, baseName, ""); 139 } 140 141 p = new Properties(); 142 try { 143 p.load(new BufferedInputStream(stream)); 144 } catch (IOException e) { 145 throw new MissingResourceException("Can't load resource " 146 + path + " due to IO exception: " + e.getMessage(), 147 baseName, ""); 148 } 149 150 // Cache the resource. 151 properties.put(d, p); 152 } 153 154 return p; 155 } 156 }