org.apache.sling.feature.Configuration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.sling.feature Show documentation
Show all versions of org.apache.sling.feature Show documentation
A feature describes an OSGi system
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.sling.feature;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* A configuration has either
*
* - a pid
*
- or a factory pid and a name
*
* and properties.
*
* This class is not thread-safe.
*/
public class Configuration
implements Comparable {
/**
* Prefix for instructions for the configurator.
*/
public static final String CONFIGURATOR_PREFIX = ":configurator:";
/**
* Prefix for special properties which are not configuration properties.
*/
public static final String PROP_PREFIX = CONFIGURATOR_PREFIX + "feature:";
/**
* This optional configuration property stores the artifact id (mvn id) of the
* bundle this configuration belongs to.
*/
public static final String PROP_ARTIFACT_ID = PROP_PREFIX + "service.bundleLocation";
/** The pid or name for factory pids. */
private final String pid;
/** The properties. */
private final Dictionary properties = new OrderedDictionary();
/**
* Create a new configuration
* @param pid The pid
* @throws IllegalArgumentException If pid is {@code null}
*/
public Configuration(final String pid) {
if ( pid == null ) {
throw new IllegalArgumentException("pid must not be null");
}
this.pid = pid;
}
@Override
public int compareTo(final Configuration o) {
return this.pid.compareTo(o.pid);
}
/**
* Get the pid.
*
* @return The pid
*/
public String getPid() {
return this.pid;
}
/**
* Check whether the pid is a factory pid
*
* @param pid The pid
* @return {@code true} if it's a factory pid
*/
public static boolean isFactoryConfiguration(final String pid) {
return pid.contains("~");
}
/**
* Return the factory pid of a pid if it's a factory configuration
*
* @param pid The pid
* @return The factory pid or {@code null}.
*/
public static String getFactoryPid(final String pid) {
final int pos = pid.indexOf('~');
if (pos != -1) {
return pid.substring(0, pos);
}
return null;
}
/**
* Return the name for a factory configuration.
*
* @param pid The pid
* @return The name or {@code null}.
*/
public static String getName(final String pid) {
final int pos = pid.indexOf('~');
if (pos != -1) {
return pid.substring(pos + 1);
}
return null;
}
/**
* Get all properties of the configuration.
*
* @return The properties
*/
public Dictionary getProperties() {
return this.properties;
}
/**
* Get the configuration properties of the configuration.
*
* @return The properties
*/
public Dictionary getConfigurationProperties() {
final Dictionary p = new Hashtable<>();
final Enumeration keys = this.properties.keys();
while (keys.hasMoreElements()) {
final String key = keys.nextElement();
if (!key.startsWith(CONFIGURATOR_PREFIX)) {
p.put(key, this.properties.get(key));
}
}
return p;
}
/**
* Create a copy of the configuration with a provided PID.
*
* @param aPid The pid of the configuration
* @return A copy of this configuration with the given PID
*/
public Configuration copy(final String aPid) {
final Configuration result = new Configuration(aPid);
final Enumeration keyEnum = this.getProperties().keys();
while (keyEnum.hasMoreElements()) {
final String key = keyEnum.nextElement();
result.getProperties().put(key, this.getProperties().get(key));
}
return result;
}
@Override
public String toString() {
return "Configuration [pid=" + pid
+ ", properties=" + properties
+ "]";
}
static class OrderedDictionary extends Dictionary implements Map {
private static class EnumarationImpl implements Enumeration {
private final Iterator iterator;
public EnumarationImpl(Iterator iterator) {
this.iterator = iterator;
}
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public E nextElement() {
return iterator.next();
}
}
private final Map map = new LinkedHashMap();
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public Enumeration keys() {
return new EnumarationImpl<>(map.keySet().iterator());
}
@Override
public Enumeration