private void autoWiredFeature(Object bean, Field field) {
   // Find the required and name parameters
   AutowiredFF4JFeature annFeature = field.getAnnotation(AutowiredFF4JFeature.class);
   String featureName =
       StringUtils.hasLength(annFeature.value()) ? annFeature.value() : field.getName();
   Feature feature = readFeature(field, featureName, annFeature.required());
   if (feature != null) {
     if (Feature.class.isAssignableFrom(field.getType())) {
       injectValue(field, bean, featureName, feature);
     } else if (Boolean.class.isAssignableFrom(field.getType())) {
       injectValue(field, bean, featureName, new Boolean(feature.isEnable()));
     } else if (boolean.class.isAssignableFrom(field.getType())) {
       injectValue(field, bean, featureName, feature.isEnable());
     } else {
       throw new IllegalArgumentException(
           "Field annotated with @AutowiredFF4JFeature"
               + " must inherit from org.ff4j.Feature or be boolean "
               + field.getType()
               + " [class="
               + bean.getClass().getName()
               + ", field="
               + field.getName()
               + "]");
     }
   }
 }
Example #2
0
  /** {@inheritDoc} */
  @Override
  public Feature mapRow(ResultSet rs, int rowNum) throws SQLException {
    String featUid = rs.getString(COL_FEAT_UID);
    Feature f = new Feature(featUid, rs.getInt(COL_FEAT_ENABLE) > 0);
    f.setDescription(rs.getString(COL_FEAT_DESCRIPTION));
    f.setGroup(rs.getString(COL_FEAT_GROUPNAME));

    // Build Flipping Strategy From DataBase
    String strategy = rs.getString(COL_FEAT_STRATEGY);
    if (strategy != null && !"".equals(strategy)) {
      try {
        FlippingStrategy flipStrategy = (FlippingStrategy) Class.forName(strategy).newInstance();
        flipStrategy.init(featUid, ParameterUtils.toMap(rs.getString(COL_FEAT_EXPRESSION)));
        f.setFlippingStrategy(flipStrategy);
      } catch (InstantiationException ie) {
        throw new FeatureAccessException(
            "Cannot instantiate Strategy, no default constructor available", ie);
      } catch (IllegalAccessException iae) {
        throw new FeatureAccessException(
            "Cannot instantiate Strategy, no visible constructor", iae);
      } catch (ClassNotFoundException e) {
        throw new FeatureAccessException("Cannot instantiate Strategy, classNotFound", e);
      }
    }
    return f;
  }
  /**
   * User action to update a target feature's description.
   *
   * @param req http request containing operation parameters
   */
  @SuppressWarnings("unchecked")
  private void opUpdateFeatureDescription(FeatureStore store, HttpServletRequest req) {
    // uid
    final String featureId = req.getParameter(FEATID);
    if (featureId != null && !featureId.isEmpty()) {
      Feature fp = new Feature(featureId, false);

      // Description
      final String featureDesc = req.getParameter(DESCRIPTION);
      if (null != featureDesc && !featureDesc.isEmpty()) {
        fp.setDescription(featureDesc);
      }

      // GroupName
      final String groupName = req.getParameter(GROUPNAME);
      if (null != groupName && !groupName.isEmpty()) {
        fp.setGroup(groupName);
      }

      // Strategy
      final String strategy = req.getParameter(STRATEGY);
      if (null != strategy && !strategy.isEmpty()) {
        try {
          Class<?> strategyClass = Class.forName(strategy);
          FlippingStrategy fstrategy = (FlippingStrategy) strategyClass.newInstance();

          final String strategyParams = req.getParameter(STRATEGY_INIT);
          if (null != strategyParams && !strategyParams.isEmpty()) {
            Map<String, String> initParams = new HashMap<String, String>();
            String[] params = strategyParams.split(";");
            for (String currentP : params) {
              String[] cur = currentP.split("=");
              if (cur.length < 2) {
                throw new IllegalArgumentException(
                    "Invalid Syntax : param1=val1,val2;param2=val3,val4");
              }
              initParams.put(cur[0], cur[1]);
            }
            fstrategy.init(featureId, initParams);
          }
          fp.setFlippingStrategy(fstrategy);

        } catch (ClassNotFoundException e) {
          throw new IllegalArgumentException("Cannot find strategy class", e);
        } catch (InstantiationException e) {
          throw new IllegalArgumentException("Cannot instanciate strategy", e);
        } catch (IllegalAccessException e) {
          throw new IllegalArgumentException("Cannot instanciate : no public constructor", e);
        }
      }

      populateFeatureWithPermissions(fp, req);

      // Creation
      store.update(fp);
      log.info(featureId + " has been updated");
    }
  }
 /** {@inheritDoc} */
 @Override
 public Map<String, Feature> readAll() {
   LinkedHashMap<String, Feature> mapFP = new LinkedHashMap<String, Feature>();
   for (DBObject dbObject : collection.find()) {
     Feature feature = MAPPER.mapFeature(dbObject);
     mapFP.put(feature.getUid(), feature);
   }
   return mapFP;
 }
 /** {@inheritDoc} */
 @Override
 public void create(Feature fp) {
   if (fp == null) {
     throw new IllegalArgumentException("Feature cannot be null nor empty");
   }
   if (exist(fp.getUid())) {
     throw new FeatureAlreadyExistException(fp.getUid());
   }
   collection.save(MAPPER.toDBObject(fp));
 }
 /** {@inheritDoc} */
 @Override
 public Map<String, Feature> readGroup(String groupName) {
   if (groupName == null || groupName.isEmpty()) {
     throw new IllegalArgumentException("Groupname cannot be null nor empty");
   }
   if (!existGroup(groupName)) {
     throw new GroupNotFoundException(groupName);
   }
   LinkedHashMap<String, Feature> mapFP = new LinkedHashMap<String, Feature>();
   for (DBObject dbObject : collection.find(BUILDER.getGroupName(groupName))) {
     Feature feature = MAPPER.mapFeature(dbObject);
     mapFP.put(feature.getUid(), feature);
   }
   return mapFP;
 }
  /** TDD, update by adding in the authorization */
  @Test
  public void testPut_upsertUpdateAddGroup() throws Exception {
    // Given
    assertFF4J.assertThatFeatureExist(AWESOME);
    assertFF4J.assertThatFeatureNotInGroup(AWESOME, "g2");
    // When
    Feature f1 = ff4j.getFeature(AWESOME);
    f1.setGroup("g2");
    WebResource webResFeat = resourceFeatures().path(AWESOME);
    ClientResponse res =
        webResFeat
            . //
            type(MediaType.APPLICATION_JSON)
            . //
            put(ClientResponse.class, toJson(new FeatureApiBean(f1)));

    // Then HTTPResponse
    Assert.assertEquals(Status.NO_CONTENT.getStatusCode(), res.getStatus());
    // Then Object Entity : null
    // Then
    assertFF4J.assertThatFeatureIsInGroup(AWESOME, "g2");
  }
 /** {@inheritDoc} */
 @Override
 public void update(Feature fp) {
   if (fp == null) {
     throw new IllegalArgumentException("Feature cannot be null nor empty");
   }
   Feature fpExist = read(fp.getUid());
   collection.updateOne(
       BUILDER.getFeatUid(fp.getUid()), new Document(MONGO_SET, MAPPER.toDocument(fp)));
   // enable/disable
   if (fp.isEnable() != fpExist.isEnable()) {
     if (fp.isEnable()) {
       enable(fp.getUid());
     } else {
       disable(fp.getUid());
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 public void update(Feature fp) {
   if (fp == null) {
     throw new IllegalArgumentException("Feature cannot be null nor empty");
   }
   Feature fpExist = read(fp.getUid());
   collection.save(MAPPER.toDBObject(fp));
   // enable/disable
   if (fp.isEnable() != fpExist.isEnable()) {
     if (fp.isEnable()) {
       enable(fp.getUid());
     } else {
       disable(fp.getUid());
     }
   }
 }
 private void populateFeatureWithPermissions(Feature fp, HttpServletRequest req) {
   // Permissions
   final String permission = req.getParameter(PERMISSION);
   if (null != permission && PERMISSION_RESTRICTED.equals(permission)) {
     @SuppressWarnings("unchecked")
     Map<String, String[]> parameters = req.getParameterMap();
     Set<String> permissions = new HashSet<>();
     for (String key : parameters.keySet()) {
       if (key.startsWith(PREFIX_CHECKBOX)) {
         if (key.equals(PREFIX_CHECKBOX + "other")) {
           permissions.addAll(
               Arrays.asList(parameters.get(PREFIX_TEXTBOX + "other-value")[0].split(",")));
         } else {
           permissions.add(key.replace(PREFIX_CHECKBOX, ""));
         }
       }
     }
     fp.setPermissions(permissions);
   }
 }