RepoPersistenceManager.java revision f5908f0beda7b53325a52e5ea2266000bce2fe41
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright © 2011 ForgeRock AS. All rights reserved.
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
public class RepoPersistenceManager implements PersistenceManager {
// The ID URI prefix for configuration
// Meta-data when converting from array to list to enable later conversion to original form
private BundleContext ctx;
private RepoBootService repo;
// Fall-back is in-memory store of configurations
bootstrapRepo(); // This happens asynchronously, i.e. repo may not be initialized when this returns
}
void bootstrapRepo() {
try {
Filter filter = ctx.createFilter("(" + Constants.OBJECTCLASS + "=" + RepoBootService.class.getName() + ")");
repoTracker.open();
new Thread() {
public void run() {
try {
}
}
while (configAdmin == null) {
Filter admFilter = ctx.createFilter("(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName() + ")");
adminTracker.open();
}
// Proceed to configuration file handling once repo bootstrapped
// and configuration admin service is available
} catch (InterruptedException ex) {
}
}
}.start();
} catch (InvalidSyntaxException ex) {
}
}
/**
* Returns <code>true</code> if a persisted <code>Dictionary</code> exists
* for the given <code>pid</code>.
*
* @param pid The identifier for the dictionary to test.
*/
boolean exists = false;
try {
} catch (NotFoundException ex) {
exists = false;
} catch (ObjectSetException ex) {
throw new RuntimeException("Failed to check if configuration exists in repository: " + ex.getMessage(), ex);
}
}
if (!exists) {
if (exists) {
}
} else {
}
if (!exists) {
}
return exists;
}
/**
* Returns the <code>Dictionary</code> for the given <code>pid</code>.
*
* @param pid The identifier for the dictionary to load.
*
* @return The dictionary for the identifier. This must not be
* <code>null</code> but may be empty.
*
* @throws IOException If an error occurrs loading the dictionary. An
* <code>IOException</code> must also be thrown if no dictionary
* exists for the given identifier.
*/
try {
}
} catch (NotFoundException ex) {
}
} catch (ObjectSetException ex) {
}
return result;
}
/**
* Returns an enumeration of all <code>Dictionary</code> objects known to
* this persistence manager.Dictionary
* <p>
* Implementations of this method are allowed to return lazy enumerations.
* That is, it is allowable for the enumeration to not return a dictionary
* if loading it results in an error.
*
* @return A possibly empty Enumeration of all dictionaries.
*
* @throws IOException If an error occurrs getting the dictionaries.
*/
} else {
}
return new Enumeration() {
public boolean hasMoreElements() {
boolean hasMore = false;
try {
if (!hasMore) {
List<Map<String, Object>> queryResult = (List<Map<String, Object>>) result.get(QueryConstants.QUERY_RESULT);
}
}
}
} catch (RuntimeException ex) {
logger.warn("Failure getting configuration dictionaries for hasMoreElements " + ex.getMessage(), ex);
throw ex;
logger.warn("Failure getting configuration dictionaries for hasMoreElements " + ex.getMessage(), ex);
throw new RuntimeException(ex);
}
return hasMore;
}
public Object nextElement() {
try {
} else {
return fullEntry;
}
} catch (RuntimeException ex) {
throw ex;
throw new RuntimeException(ex);
}
}
};
}
/**
* Stores the <code>Dictionary</code> under the given <code>pid</code>.
*
* @param pid The identifier of the dictionary.
* @param properties The <code>Dictionary</code> to store.
*
* @throws IOException If an error occurrs storing the dictionary. If this
* exception is thrown, it is expected, that
* {@link #exists(String) exists(pid} returns <code>false</code>.
*/
// Store config handling settings in memory
return;
}
try {
try {
} catch (NotFoundException ex) {
// Just detect that it doesn't exist
}
boolean retry;
do {
retry = false;
try {
} catch (PreconditionFailedException ex) {
retry = true;
}
} while (retry);
} else {
logger.debug("Existing config same as store request, ignoring {} {} {}", new Object[] {pid, rev, obj});
}
} else {
}
} else {
}
} catch (ObjectSetException ex) {
}
}
/**
* Removes the <code>Dictionary</code> for the given <code>pid</code>. If
* such a dictionary does not exist, this method has no effect.
*
* @param pid The identifier of the dictionary to delet.
*
* @throws IOException If an error occurrs deleting the dictionary. This
* exception must not be thrown if no dictionary with the given
* identifier exists.
*/
}
try {
boolean retry;
do {
retry = false;
try {
}
} catch (PreconditionFailedException ex) {
retry = true;
} catch (NotFoundException ex) {
// If it doesn't exists (anymore) that's fine
}
} while (retry);
}
} catch (ObjectSetException ex) {
throw new IOException("Failed to delete configuration + " + pid + " in repository: " + ex.getMessage(), ex);
}
}
/**
* Convert OSGi pid to an ID suitable for addressing the repository
* @param pid the OSGi pid
* @return the qualified objectset id
*/
return CONFIG_CONTEXT_PREFIX + pid;
}
/**
* Convert dictionary and contents to a Map, compatible with the JSON model.
* Arrays are encoded with special meta-data entries marking their origin as array.
*
* @param dict the (OSGi configuration) dictionary
* @return the converted map
*/
while(keysEnum.hasMoreElements()) {
// Deal with outdated collection classes
if (rawValue instanceof Dictionary) {
// Add marker entries to mark this as coming from an array
}
}
return convert;
}
/**
* Convert the JSON model map and contents to types and contents
* compatible with OSGi configuration Dictionary
* e.g. using legacy types (Hashtables, Vectors),
* Encoded property names decoded.
*
* @param properties map to convert
* @return converted to legacy types
* @throws IOException if the conversion failed
*/
if (properties instanceof Dictionary) {
return (Dictionary) properties;
} else {
}
}
}
try {
logger.warn("Failed to convert back to original array type " + origType + " " + ex.getMessage(), ex);
throw new IOException("Failed to convert back to original array type " + origType + " " + ex.getMessage(), ex);
}
} else {
}
}
}
return converted;
}
}
/*
* Convert to a key (property name) compatible with storing in the repository
*
* Note: does not support keys that contain a sequence of "_.", which should not occur in our configuration files
*/
}
/*
* Convert from encoded version for repository storage to the original key
*/
}
}