package org.ff4j.store; /* * #%L * ff4j-core * %% * Copyright (C) 2013 - 2014 Ff4J * %% * Licensed 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. * #L% */ import java.io.InputStream; import java.util.Collection; import java.util.Map; import java.util.Set; import org.ff4j.conf.XmlConfig; import org.ff4j.conf.XmlParser; import org.ff4j.core.Feature; import org.ff4j.core.FeatureStore; import org.ff4j.exception.FeatureAlreadyExistException; import org.ff4j.exception.FeatureNotFoundException; import org.ff4j.exception.GroupNotFoundException; import org.ff4j.utils.JsonUtils; import org.ff4j.utils.Util; /** * SuperClass for stores. * * @author Cedrick Lunven (@clunven) */ public abstract class AbstractFeatureStore implements FeatureStore { /** * Initialize store from XML Configuration File. * * @param xmlConfFile * xml configuration file */ public Map < String, Feature > importFeaturesFromXmlFile(String xmlConfFile) { // Argument validation if (xmlConfFile == null || xmlConfFile.isEmpty()) { throw new IllegalArgumentException("Configuration filename cannot be null nor empty"); } // Load as Inputstream InputStream xmlIS = getClass().getClassLoader().getResourceAsStream(xmlConfFile); if (xmlIS == null) { throw new IllegalArgumentException("File " + xmlConfFile + " could not be read, please check path and rights"); } // Use the Feature Parser XmlConfig conf = new XmlParser().parseConfigurationFile(xmlIS); Map < String, Feature > features = conf.getFeatures(); importFeatures(features.values()); return features; } /** {@inheritDoc} */ @Override public void createSchema() { /* * In most of cases there is nothing to do. The feature and properties are createdat runtime. * But not always (JDBC, Mongo, Cassandra)... this is the reason why the dedicated store must * override this method. It a default implementation (Pattern Adapter). */ return; } /** * Import features from a set of feature. * * @param features */ public void importFeatures(Collection < Feature > features) { if (features != null) { for (Feature feature : features) { if (exist(feature.getUid())) { delete(feature.getUid()); } create(feature); } } } /** {@inheritDoc} */ public String toJson() { StringBuilder sb = new StringBuilder("{"); sb.append("\"type\":\"" + this.getClass().getCanonicalName() + "\""); sb.append(JsonUtils.cacheJson(this)); Set<String> myFeatures = readAll().keySet(); sb.append(",\"numberOfFeatures\":" + myFeatures.size()); sb.append(",\"features\":["); boolean first = true; for (String myFeature : myFeatures) { if (!first) { sb.append(","); } first = false; sb.append("\"" + myFeature + "\""); } Set<String> myGroups = readAllGroups(); sb.append("],\"numberOfGroups\":" + myGroups.size()); sb.append(",\"groups\":["); first = true; for (String myGroup : myGroups) { if (!first) { sb.append(","); } first = false; sb.append("\"" + myGroup + "\""); } sb.append("]"); sb.append("}"); return sb.toString(); } /** {@inheritDoc} */ @Override public String toString() { return toJson(); } /** * Validate feature uid. * * @param uid * target uid */ protected void assertFeatureExist(String uid) { Util.assertHasLength(uid); if (!exist(uid)) { throw new FeatureNotFoundException(uid); } } /** * Check that current feature does not exist. * * @param uid * current feature identifier.s */ protected void assertFeatureNotExist(String uid) { Util.assertHasLength(uid); if (exist(uid)) { throw new FeatureAlreadyExistException(uid); } } /** * Validate feature uid. * * @param uid * target uid */ protected void assertGroupExist(String groupName) { Util.assertHasLength(groupName); if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } } /** * Validate feature uid. * * @param uid * target uid */ protected void assertFeatureNotNull(Feature feature) { if (feature == null) { throw new IllegalArgumentException("Feature cannot be null nor empty"); } } }